数据库的格式化如下
分别为姓名|密码|电话号码|邮箱|用户类型
[email protected]|1
[email protected]|1
[email protected]|0
[email protected]|1
# 1、普通用户
# 登陆,注册,修改密码,查看本用户信息
# 2、管理员用户
# 登陆,注册,修改密码,查看本用户信息
# 删除普通虚拟机,添加普通用户信息
# 查看所有的普通用户,按照指定的关键字搜索用户信息
# 提高普通用户的权限
# 3、要求
# 权限用装饰器
# 用户信息,用文件存储
# 4、文件内容
# 用户名|密码|手机号码|邮箱地址|用户类型
代码如下
#Auther Bob #--*--coding:utf-8--*-- # 1、普通用户 # 登陆,注册,修改密码,查看本用户信息 # 2、管理员用户 # 登陆,注册,修改密码,查看本用户信息 # 删除普通虚拟机,添加普通用户信息 # 查看所有的普通用户,按照指定的关键字搜索用户信息 # 提高普通用户的权限 # 3、要求 # 权限用装饰器 # 用户信息,用文件存储 # 4、文件内容 # 用户名|密码|手机号码|邮箱地址|用户类型 login_dict = {"is_login": None, "login_type": None} login_list = [] user_list = [] tmpe_user_list = [] with open("db", "r", encoding="utf-8") as f: for line in f: tmpe_user_list = line.strip().split("|") user_list.append(tmpe_user_list[0]) tmpe_user_list = [] def manage_user(func): def inner(*args, **kwargs): if login_dict["is_login"] == "1" and login_dict["login_type"] == "1": ret = func(*args, **kwargs) return ret elif login_dict["is_login"] == None: print("请先登陆") login() elif login_dict["is_login"] == "1" and login_dict["login_type"] == None: print("请使用管理员账号登陆") login() else: pass return inner def common_user(func): def inner(*args, **kwargs): if login_dict["is_login"] == None: print("请先登陆") login() elif login_dict["is_login"] == "1": ret = func() return ret return inner def login(): user_name = input("请输入用户名:") user_passwd = input("请输入%s的密码:" %(user_name)) if user_name in user_list: with open("db", "r", encoding="utf-8") as f: for line in f: login_list = line.strip().split("|") # print(login_list[0]) # print(login_list[1]) if user_name == login_list[0] and user_passwd == login_list[1]: print('欢迎%s登陆管理平台' %(user_name)) if login_list[4] == "1": login_dict["is_login"] = "1" login_dict["login_type"] = "1" print(login_dict) else: login_dict["is_login"] = "1" login_dict["login_type"] = None print(login_dict) break else: print("用户名或者密码错误,请重新登陆:") login_dict["is_login"] = None login_dict["login_type"] = None login() else: print("用户名或者密码错误,请重新登陆") login_dict["is_login"] = None login_dict["login_type"] = None login() @common_user def regi_common(): break_flag = False while True: if break_flag == True: break else: login_name = input("请输入需要注册的用户名:") if login_name in user_list: print("该用户名已经存在,请重新输入注册的用户名") regi_common() else: login_paswd = input("请输入%s的用户密码:" % (login_name)) login_number = input("请输入%s的电话号码:" % (login_name)) login_email = input("请输入%s的邮箱地址:" % (login_name)) while True: if login_dict["is_login"] == "1" and login_dict["login_type"] == "1": option = input("请输入%s的用户类型/n1:管理员/n2:普通用户/n" % (login_name)) if option == "1": login_type = "1" with open("db","a",encoding="utf-8") as f1: user_info = "/n" + login_name + "|" + login_paswd + "|" + login_number + "|" + login_email + "|" + login_type f1.write(user_info) break_flag = True user_list.append(login_name) break elif option == "2": login_type = "0" with open("db", "a", encoding="utf-8") as f1: user_info = "/n" + login_name + "|" + login_paswd + "|" + login_number + "|" + login_email + "|" + login_type f1.write(user_info) break_flag = True user_list.append(login_name) break else: print("无效的输入,请重新输入") continue elif login_dict["is_login"] == "1" and login_dict["login_type"] == None: option = input("请输入%s的用户类型/n1:普通用户/n" % (login_name)) if option == "1": login_type = "0" with open("db", "a", encoding="utf-8") as f1: user_info = "/n" + login_name + "|" + login_paswd + "|" + login_number + "|" + login_email + "|" + login_type f1.write(user_info) break_flag = True user_list.append(login_name) break else: print("无效的输入,请重新输入") else: pass @manage_user def change_password(): while True: change_name = input("请输入需要修改密码的欧用户名") if change_name in user_list: # old_passwd = input("请输入%s的旧密码" %(change_name)) change_passwd = input("请输入%s的新密码" %(change_name)) with open("db","r",encoding="utf-8") as f1,open("temp","w",encoding="utf-8") as f2: for line in f1: change_list = line.strip().split("|") if change_name == change_list[0]: new = line.replace(line.strip().split("|")[1],change_passwd) f2.write(new) else: f2.write(line) change_list = [] with open("temp","r",encoding="utf-8") as f1,open("db","w",encoding="utf-8") as f2: for line in f1: f2.write(line) print("用户%s密码已经修改成功" %(change_name)) break # break else: print("%s不在用户列表中,请重新输入要修改的用户名:" %(change_name)) option = input("是否要继续1:退出;2:继续") if option == "1": exit() elif option == "2": break else: continue @manage_user def view_user(): while True: print(user_list) view_name_db = str(input("请输入要查询的用户名:")) if view_name_db in user_list: # print(view_name_db) with open("db","r",encoding="utf-8") as f1: for line in f1: view_list = line.strip().split("|") if view_name_db == view_list[0]: if view_list[4] == "1": view_type = "管理员" elif view_list[4] == "0": view_type = "普通用户" s = "%s的信息如下" %(view_name_db) print(s.center(100,"-")) view_info = "用户名:" + view_list[0] + "/n" + "密码:" + view_list[1] + "/n" + "电话号码:" + view_list[2] + "/n" + "邮箱地址:" + view_list[3] + "/n" + "用户类型:" + view_type print(view_info) option = input("是否需要继续查询用户名:/n1:是/n2:否") if option == "1": continue elif option == "2": return True else: continue # view_list = [] else: print("用户名%s不存在" %(view_name_db)) option = input("是否需要重新查询用户名:/n1:是/n2:否") if option == 1: continue elif option == 2: break else: continue @manage_user def del_user(): while True: del_name = input("请输入需要删除的用户") if del_name not in user_list: option = input("用户名%s不存在,是否需要重新删除用户名:/n1:是/n2:否" %(del_name)) if option == "1": continue elif option == "2": break else: continue else: with open("db","r",encoding="utf-8") as f1,open("temp","w",encoding="utf-8") as f2: for line in f1: if line.strip().startswith(del_name): continue else: f2.write(line) with open("temp","r",encoding="utf-8") as f1,open("db","w",encoding="utf-8") as f2: for line in f1: f2.write(line) print("用户名%s已经删除成功" %(del_name)) user_list.remove(del_name) while True: option = input("是否需要继续删除用户:/n1:是/n2:否") if option == "1": break elif option == "2": return True else: print("无效的输入,请重新输入") def main(): while True: s = "请输入你要做什么" print(s.center(100,"-")) option = input("1、登陆/n2、查看用户/n3、删除用户/n4、修改密码/n5、注册用户") if option == "1": login() elif option == "2": view_user() elif option == "3": del_user() elif option == "4": change_password() elif option == "5": regi_common() else: while True: option = input("无效的输入,是否需要继续:/n1、退出/n2、继续/n") if option == "1": exit() elif option == "2": break else: continue main()
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/20902.html