import os
import re
import json
import time
import sys
import hashlib
import random
is_login={'is_login':False}
super_purview={'super_purview':'普通用户'}
id_employee_dict={}
path1=os.path.abspath(os.path.join(os.path.abspath('core'), "..\\.."))
print('test:psth1=',path1)
sys.path.append(path1)
sys.path.append(path1+'\\lib')
# 装饰器权限检测
def login_purview(func):
def auth(*args,**kwargs):
if super_purview.get('super_purview')=="管理员" or super_purview.get('super_purview')=="超级管理员":
res = func(*args,**kwargs)
return res
else:
print('权限不足')
return auth
# 装饰器超级权限检测
def login_super_purview(func):
def auth(*args,**kwargs):
if super_purview.get('super_purview')=='超级管理员':
res = func(*args,**kwargs)
return res
else:
print('权限不足')
return auth
def statstics_time(func_name):
# func_name = index
def get_time(*args, **kwargs):
# 1. 在函数执行之前,要记录一下此时的时间
start_time = time.time()
# 2. 开始执行函数
func_name(*args, **kwargs)
# 3. 函数执行完毕之后,再次记录此时的时间
end_time = time.time()
# 4. 统计函数的执行时间
print('执行了%s秒' % (end_time - start_time))
return get_time
@statstics_time
def login():
'''
# 转注册
while True:
input_chhoose=input("无账号?选择1进入注册,选择其他则继续登录")
if input_chhoose!='1':
break
else:
register()'''
# 输入并校验员工编号
while True:
input_employee_id=input("请输入员工编号:")
if is_exist(input_employee_id):
break
else:
print('错误,重来')
# 输入并校验密码
while True:
input_employee_pwd=input("请输入密码:")
read_file(input_employee_id)
res=id_employee_dict.get('校验码')
pwd = input_employee_pwd + res
md5 = hashlib.md5()
md5.update(pwd.encode('utf-8'))
cryptograph_pwd = md5.hexdigest()
# print("测试,密文=", cryptograph_pwd)
if id_employee_dict.get('密码')==cryptograph_pwd:
print('成功登录')
break
else:
print('错误,重来')
# 修改登录状态
global is_login
is_login['is_login']=True
# 检测并获得相应权限
super_purview['super_purview']=id_employee_dict.get('权限')
def login_auth(func):
def auth(*args,**kwargs):
if is_login.get('is_login'):
res = func(*args,**kwargs)
return res
else:
print('未登录,请先登录')
while 1:
cmd=input('1.登录 2.注册')
if cmd=='1':
login()
break
elif cmd=='2':
register()
print('注册成功,请登录')
login()
break
else:
print('好好输入')
return auth
def is_id(input_id):
res=re.findall('^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$',input_id)
if res==[]:
print("test notid")
return 0
else:
print("test id")
return 1
def is_phone(input_phone):
res=re.findall('^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$',input_phone)
if res==[]:
return 0
else:
return 1
def register():
# 员工编号
while True:
input_employee_id = input("请输入员工编号:")
if is_exist(input_employee_id):
print('已经存在,重来')
else:
break
# 姓名
input_name = input("请输入姓名:")
# 密码
input_employee_pwd = input("请输入密码:")
res=get_code(5)
pwd=input_employee_pwd +res
md5 = hashlib.md5()
md5.update(pwd.encode('utf-8'))
cryptograph_pwd=md5.hexdigest()
print("测试,密文=",cryptograph_pwd)
# 身份证号码
while True:
input_id = input("请输入身份证号码:")
if is_id(input_id):
break
else:
print('错误,重来')
# 手机号码
while True:
input_phone = input("请输入手机号码:")
if is_phone(input_phone):
break
else:
print('错误,重来')
# 部门
department_dict={
}
# 职位
job_dict={
"1":"总经理(总裁)",
"2":"副总经理",
"3":"人力资源总监",
"4":"财务总监(CFO)",
"5":"营销总监",
"6":"市场总监(CMO)",
"7":"销售总监",
"8":"生产总监",
"9":"运营总监",
"10":"技术总监(CTO)",
"11":"总经理助理"
}
print(job_dict)
while True:
input_job = input("请选择职位:")
if input_job in job_dict:
job=job_dict[input_job]
break
else:
print('错误,重来')
# 合成字典
id_employee_dict={"员工编号":input_employee_id,"姓名":input_name,"密码":cryptograph_pwd,"校验码":res,"职位":job,"手机号码":input_phone,"身份证号码":input_id,"权限":"普通用户"}
# print("测试id_employee_dict=",id_employee_dict)
# file_path=os.path.join(path1,"db",id_employee_dict.get("员工编号")+'.txt')
write_file(add,id_employee_dict.get("员工编号"),id_employee_dict)
run()
def read_file(employee_id):
# path1=os.path.abspath(os.path.join(os.path.abspath('core'), "..\\.."))
file_path=path1+'\\db\\'+employee_id+'.txt'
# print(file_path)
with open(file_path,'r',encoding='utf-8') as f:
global id_employee_dict
id_employee_dict=json.load(f)
return id_employee_dict
def write_file(doing,employee_id,*args):
if doing==add:
# arg接收了员工字典
with open(os.path.join(path1, "db", employee_id + '.txt'), 'w', encoding='utf-8') as f:
json.dump(args[0],f,ensure_ascii=False)# ensure_ascii=False,不然,默认是True,会转为万国码
print('成功')
elif doing==delete:
os.remove(os.path.join(path1, "db", employee_id + '.txt'))
# with open(os.path.join(path1, "db", employee_id + '.txt'), 'w', encoding='utf-8') as f:
elif doing==modify:
pass
def change_purview():
pass
@login_auth
def add():
register()
@login_auth
def delete():
while True:
input_employee_id = input("请输入员工编号:")
if is_exist(input_employee_id):
write_file(delete,input_employee_id )
break
else:
print('查无此人,重来')
@login_auth
def find():
while True:
input_employee_id = input("请输入员工编号:")
if is_exist(input_employee_id):
with open(os.path.join(path1,'db',input_employee_id+'.txt'),'r',encoding='utf-8') as f:
id_employee_dict=json.load(f)
print(id_employee_dict)
break
else:
print('查无此人,重来')
# def is_exist(input_employee_id):
def employee_id_list():
file_list=os.listdir(path1+'\\db')
employee_id=[]
for file_name in file_list:
id=re.findall(('(.+).txt'),file_name)
if id!=[]:
# print('id=',id)
employee_id.append(id[0])
print('测试employee_id=',employee_id)
return employee_id
def is_exist(input_employee_id):
# path1=os.path.abspath(os.path.join(os.path.abspath('core'), "..\\.."))
# print(path1)
# employee_id_list()
for employee_id in employee_id_list():
if input_employee_id==employee_id:
return 1
else:
return 0
@login_auth
@login_purview
@login_super_purview
def view_all():
employee_file_name_list=[]
file_list=os.listdir(os.path.join(path1,'db'))
# print('测试file_list=',file_list)
for file_name in file_list:
employee_file_name=re.findall(('.+.txt'),file_name)
if employee_file_name!=[]:
# print('测试employee_file_name=',employee_file_name)
employee_file_name_list.append(employee_file_name[0])
res=os.path.join(path1,'db',employee_file_name[0])
# print(res)
with open(res,'r',encoding='utf-8')as f:
# json.load(f)
print(f.read())
# json.load(open(os.path.join(path1,'db',employee_file_name),'r',encoding='utf-8'))
# print('测试mployee_file_name_list=',employee_file_name_list)
#产生随机数
def get_code(n):
code = '' # 存储生成的随机验证码
# 6位的验证码
for i in range(n):
# 1. 产生0-9之间的任意一个数据
random_int = str(random.randint(0, 9))
# 2. 产生大写字母
random_upper = chr(random.randint(65, 90)) # A-Z
# 3. 产生小写字母
random_lower = chr(random.randint(97, 122)) # a-z
# 4. 要从以上三种情况中随机选出一个字符来
temp = random.choice([random_int, random_lower, random_upper])
# print(temp,end='')
code += temp
return code
# res=get_code(6)
def menu():
print("""
欢迎来到员工管理系统,请输入选择:
1.增加,2查找,3删除 4.查看全体⽤户数据功能 0退出
请输入选项
""")
fun_dict={
'1':add,
'2':find,
'3':delete,
'4':view_all,
# '4':add,
}
def run():
while True:
menu()
cmd=input()
if cmd in fun_dict:
res=fun_dict[cmd]
res()
else:
print('重来')
# run()
run()
# register()
网友评论