编写员⼯管理系统进阶版

2023-03-05  本文已影响0人  晨颜

功能全部封装成函数 数据全部来源于⽂件
⼤致功能:注册 登录 添加员⼯ 查看指定员⼯ 查看全体员⼯姓 名等
普通要求 :
添加 查看等功能每次都必须先登录才可执⾏(认证装饰器) 进阶要求 :
⽤户登录⼀次之后⽆⽆需校验身份(全局校验)
升华超越(选做题):(目前在修改版中大致实现)(在最后面的改进版中完善)(如果运行报错,就去两个.txt文件看看最后有没有换行,输入完最后一个字符后,需要换行!!!!!)
每个⽤户配备⻆⾊信息
⽤户认证过程中只有管理员才可执⾏被装饰函数

is_login={'is_login':False}
# 装饰器
def login_auth(func):
    def auth(*args,**kwargs):
        if is_login.get('is_login'):
            res = func(*args,**kwargs)
            return res
        else:
            print('未登录,请先登录')
            login()
    return auth

# 注册登录
def login():
    status = False
    while status!=True:
        print("        1. 登录    2. 注册")

        cmd = input('请输入你的选择:').strip()

        # cmd的验证
        if cmd == '2':
            while True:
                # 1. 接收用户名和密码
                username = input('username>>>:')
                password = input('password>>>:')

                # 3. 判断用户是否已经注册过
                # 3.1 先取出文件中得数据
                with open('userinfo.txt', 'r', encoding='utf-8') as f1:
                    # 3.2 把文件数据一行一行的读出来
                    for line in f1:
                        # print(line)  # kevin|123\n
                        real_username, *_ = line.split('|')
                        # res = line.split('|')
                        if real_username == username:
                            print('该用户已经存在,请从新输入')
                            break
                    else:
                        # 2. 组织用户名和密码成固定格式 kevin|123
                        data = '%s|%s\n' % (username, password)
                        # data = username + '|' + password
                        # 3. 把用户数据保存到文件中
                        with open('userinfo.txt', 'a', encoding='utf-8') as f:
                            f.write(data)

                        print('%s: 注册成功' % username)
        elif cmd == '1':
            username = input('username>>>:')
            password = input('password>>>:')

            # 2. 登录,读取文件数据,得到用户的真实用户名和密码
            with open('userinfo.txt', 'r', encoding='utf-8') as f:
                # 一行一行的读取用户名和密码
                for line in f:
                    real_username, real_pwd = line.split('|')  # kevin|123\n  ['kevin', '123\n']
                    real = real_pwd.strip('\n')
                    # 判断用户名和密码是否正确
                    if real_username == username and real == password:
                        print('登录成功')
                        status = True
                        is_login['is_login']=True
                        return status
                        break
                else:
                    print('登录失败')
'''
def menu_choose():
    menu()
    cmd = "w"
    while cmd != '0':
        detaile_dicct = {}
        menu()
        cmd = input().strip()
        if cmd == '1':
            add()
        elif cmd == '2':
            # print(employee_dict)
            get_employee_dict()
        elif cmd == '3':
            delete()
        elif cmd == '0':
            break
        else:
            print("错误,重来")
'''
def get_employee_dict():
    global employee_dict
    employee_dict = [{}]
    with open('employee_dict.txt','r', encoding='utf-8') as f:
        f.seek(0,0)
        for line in f:
            Id,name = line.strip().split(':')
            detaile_dicct_tmp={}
            detaile_dicct_tmp.update({'Id': Id, 'name': name})  # 将值给临时存储空间
            # global employee_dict
            employee_dict.append(detaile_dicct_tmp)
            #
        employee_dict.pop(0)

        print('显示全部信息',employee_dict,"get_employee_dict")
    return employee_dict
@login_auth
def add():
    get_employee_dict()
    print("请输入编号")
    Id= input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):
            print("已经存在")
            break
    else:
        detaile_dicct_tmp = {}  # 添加临时存储空间
        print("请输入姓名")
        name = input().strip()
        detaile_dicct_tmp.update({'Id':Id, 'name': name})  # 将值给临时存储空间
        employee_dict.append(detaile_dicct_tmp)  # 将临时存储空间的内容给employee_dict
        # print(employee_dict)
        with open('employee_dict.txt', 'a', encoding='utf-8') as f:
            f.write("%s:%s\n"%(Id,name))
@login_auth
def delete():
    count=0
    get_employee_dict()
    print("请输入要删除的编号")
    Id=input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):

            print("存在,执行删除")
            employee_dict.remove(detaile_dicct)  # 移除employee_dict列表中当前字典
            print('显示删除后的内容:',employee_dict)
            # print(employee_dict)
            import os
            with open('employee_dict.txt', 'rt', encoding='utf-8') as read_f,open('b.txt','wt',encoding='utf-8') as write_f:
                k=0
                for line in read_f:
                    if k == count:
                        # print("删除行的下标k=", k,end="\t")
                        # print('\n理论上的删除内容',line)
                        pass

                    else:
                        # print("未删除行下标k=", k,end="\t")

                        write_f.writelines(line)
                    k+=1
                    # print("k=", k)
            os.remove('employee_dict.txt')  # 删除源文件
            os.rename('b.txt', 'employee_dict.txt')

            break
        count += 1  # 记录要删除的数据的行数下标
        print("count=", count)
    else:
        print("不存在")
@login_auth
def find():
    print("请输入编号")
    Id = input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):
            print("找到了", end='')
            print("Id:%s\tname:%s" % (Id, detaile_dicct.get('name')))
            break
    else:
        print("查无此人")
@login_auth
def view_all_name():
    get_employee_dict()
    for detaile_dicct in employee_dict:
        print(detaile_dicct.get('name'),end=' ')
# 菜单
def menu():
    print("\n欢迎来到员工工资管理系统,输入指令:(1增加 2查看 3删除 4查看所有名字 0退出)")
# menu_choose()
employee_dict=[{}]
cmd = "w"
while cmd != '0':
    detaile_dicct = {}
    menu()
    cmd = input().strip()
    if cmd == '1':
        add()
    elif cmd == '2':
        find()
    elif cmd == '3':
        delete()
    elif cmd=='4':
        view_all_name()
    elif cmd == '0':
        break
    else:
        print("错误,重来")

employee_dict.txt
8:张三
6:李四
1:王五
2:赵六
3:王大锤
4:老王
5:吴无
7:章七

userinfo.txt
kevin|123
|
1|1|
2|2
3|3
4|4
5|4

修改版(还有些细节问题需要调整)

is_login={'is_login':False}
super_purview={'super_purview':False}
# 超级管理员装饰器
def super_purview_login_auth(func):
    def auth(*args,**kwargs):
        if is_login.get('is_login'):
            if super_purview.get('super_purview'):
                res = func(*args,**kwargs)
                return res
        else:
            print('未登录,请先登录')
            login()
    return auth

def login_auth(func):
    def auth(*args,**kwargs):
        if is_login.get('is_login'):
            if super_purview.get('super_purview'):
                res = func(*args,**kwargs)
                return res
            else:
                print('权限不足')
        else:
            print('未登录,请先登录')
            login()
    return auth

# 注册登录
def login():
    status = False
    while status!=True:
        print("        1. 登录    2. 注册")

        cmd = input('请输入你的选择:').strip()

        # 注册
        if cmd == '2':
            while True:
                # 1. 接收用户名和密码
                username = input('username>>>:')
                password = input('password>>>:')

                # 3. 判断用户是否已经注册过
                # 3.1 先取出文件中得数据
                with open('userinfo.txt', 'r', encoding='utf-8') as f1:
                    # 3.2 把文件数据一行一行的读出来
                    for line in f1:
                        # print(line)  # kevin|123\n
                        real_username, *_ ,purview= line.split('|')
                        # res = line.split('|')
                        if real_username == username:
                            print('该用户已经存在,请从新输入')
                            break
                    else:
                        # 2. 组织用户名和密码成固定格式 kevin|123
                        data = '%s|%s|普通用户\n' % (username, password)
                        # data = username + '|' + password
                        # 3. 把用户数据保存到文件中
                        with open('userinfo.txt', 'a', encoding='utf-8') as f:
                            f.write(data)

                        print('%s: 注册成功' % username)
        # 登录
        elif cmd == '1':
            username = input('username>>>:')
            password = input('password>>>:')

            # 2. 登录,读取文件数据,得到用户的真实用户名和密码
            with open('userinfo.txt', 'r', encoding='utf-8') as f:
                # 一行一行的读取用户名和密码
                for line in f:
                    real_username, real_pwd,purview= line.split('|')  # kevin|123\n  ['kevin', '123\n']
                    real = real_pwd.strip('\n')
                    purview=purview.strip('\n')
                    # 判断用户名和密码是否正确
                    if real_username == username and real == password:
                        print('登录成功')
                        status = True
                        is_login['is_login']=True
                        if purview=='管理员':
                            global super_purview
                            super_purview['super_purview'] = True
                            print(super_purview['super_purview'])
                        return status
                        print(super_purview['super_purview'])
                        break
                else:
                    print('登录失败')
'''
def menu_choose():
    menu()
    cmd = "w"
    while cmd != '0':
        detaile_dicct = {}
        menu()
        cmd = input().strip()
        if cmd == '1':
            add()
        elif cmd == '2':
            # print(employee_dict)
            get_employee_dict()
        elif cmd == '3':
            delete()
        elif cmd == '0':
            break
        else:
            print("错误,重来")
'''
def get_employee_dict():
    global employee_dict
    employee_dict = [{}]
    with open('employee_dict.txt','r', encoding='utf-8') as f:
        f.seek(0,0)
        for line in f:
            Id,name = line.strip().split(':')
            detaile_dicct_tmp={}
            detaile_dicct_tmp.update({'Id': Id, 'name': name})  # 将值给临时存储空间
            # global employee_dict
            employee_dict.append(detaile_dicct_tmp)
            #
        employee_dict.pop(0)

        print('显示全部信息',employee_dict,"get_employee_dict")
    return employee_dict
@super_purview_login_auth
# @login_auth
def add():

    get_employee_dict()
    print("请输入编号")
    Id= input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):
            print("已经存在")
            break
    else:
        detaile_dicct_tmp = {}  # 添加临时存储空间
        print("请输入姓名")
        name = input().strip()
        detaile_dicct_tmp.update({'Id':Id, 'name': name})  # 将值给临时存储空间
        employee_dict.append(detaile_dicct_tmp)  # 将临时存储空间的内容给employee_dict
        # print(employee_dict)
        with open('employee_dict.txt', 'a', encoding='utf-8') as f:
            f.write("%s:%s\n"%(Id,name))
@super_purview_login_auth
# @login_auth
def delete():
    count=0
    get_employee_dict()
    print("请输入要删除的编号")
    Id=input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):

            print("存在,执行删除")
            employee_dict.remove(detaile_dicct)  # 移除employee_dict列表中当前字典
            print('显示删除后的内容:',employee_dict)
            # print(employee_dict)
            import os
            with open('employee_dict.txt', 'rt', encoding='utf-8') as read_f,open('b.txt','wt',encoding='utf-8') as write_f:
                k=0
                for line in read_f:
                    if k == count:
                        # print("删除行的下标k=", k,end="\t")
                        # print('\n理论上的删除内容',line)
                        pass

                    else:
                        # print("未删除行下标k=", k,end="\t")

                        write_f.writelines(line)
                    k+=1
                    # print("k=", k)
            os.remove('employee_dict.txt')  # 删除源文件
            os.rename('b.txt', 'employee_dict.txt')

            break
        count += 1  # 记录要删除的数据的行数下标
        print("count=", count)
    else:
        print("不存在")
@super_purview_login_auth
# @login_auth
def find():
    print("请输入编号")
    Id = input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):
            print("找到了", end='')
            print("Id:%s\tname:%s" % (Id, detaile_dicct.get('name')))
            break
    else:
        print("查无此人")
# @super_purview_login_auth

#4
def view_all_name():
    get_employee_dict()
    for detaile_dicct in employee_dict:
        print(detaile_dicct.get('name'),end=' ')
# 菜单
def menu():
    print("\n欢迎来到员工工资管理系统,输入指令:(1增加 2查看 3删除 4查看所有名字 0退出)")
# menu_choose()
employee_dict=[{}]
cmd = "w"
while cmd != '0':
    detaile_dicct = {}
    menu()
    cmd = input().strip()
    if cmd == '1':
        add=login_auth(add)
        add()
    elif cmd == '2':
        find = login_auth(find)
        find()
    elif cmd == '3':
        delete = login_auth(delete)
        delete()
    elif cmd=='4':
        view_all_name()
    elif cmd == '0':
        break
    else:
        print("错误,重来")

改进版

is_login={'is_login':False}
super_purview={'super_purview':False}
# 装饰器登录检测
def login_auth(func):
    def auth(*args,**kwargs):
        if is_login.get('is_login'):
            res = func(*args,**kwargs)
            return res
        else:
            print('未登录,请先登录')
            login()
    return auth
# 装饰器权限检测
def login_purview(func):
    def auth(*args,**kwargs):
        if super_purview.get('super_purview'):
            res = func(*args,**kwargs)
            return res
        else:
            print('权限不足')
    return auth
# 注册登录
def login():
    status = False
    while status!=True:
        print("        1. 登录    2. 注册")
        cmd = input('请输入你的选择:').strip()
        # 注册
        if cmd == '2':
            while True:
                # 1. 接收用户名和密码
                username = input('username>>>:')
                password = input('password>>>:')

                # 3. 判断用户是否已经注册过
                # 3.1 先取出文件中得数据
                with open('userinfo.txt', 'r', encoding='utf-8') as f1:
                    # 3.2 把文件数据一行一行的读出来
                    for line in f1:
                        # print(line)  # kevin|123\n
                        real_username, *_ ,purview= line.split('|')
                        # res = line.split('|')
                        if real_username == username:
                            print('该用户已经存在,请从新输入')
                            break
                    else:
                        # 2. 组织用户名和密码成固定格式 kevin|123
                        data = '%s|%s|普通用户\n' % (username, password)
                        # data = username + '|' + password
                        # 3. 把用户数据保存到文件中
                        with open('userinfo.txt', 'a', encoding='utf-8') as f:
                            f.write(data)

                        print('%s: 注册成功' % username)
        # 登录
        elif cmd == '1':
            username = input('username>>>:')
            password = input('password>>>:')

            # 2. 登录,读取文件数据,得到用户的真实用户名和密码
            with open('userinfo.txt', 'r', encoding='utf-8') as f:
                # 一行一行的读取用户名和密码
                for line in f:
                    real_username, real_pwd,purview= line.split('|')  # kevin|123\n  ['kevin', '123\n']
                    real = real_pwd.strip('\n')
                    purview=purview.strip('\n')
                    # 判断用户名和密码是否正确
                    if real_username == username and real == password:
                        print('登录成功',end=" ")
                        status = True
                        is_login['is_login']=True
                        if purview=='管理员':
                            global super_purview
                            super_purview['super_purview'] = True
                            print(super_purview['super_purview'],"欢迎超级管理员")
                        print("欢迎用户登录")
                        return status
                        # print(super_purview['super_purview'])

                        break
                else:
                    print('登录失败')
# 获取员工信息
def get_employee_dict():
    global employee_dict
    employee_dict = [{}]
    with open('employee_dict.txt','r', encoding='utf-8') as f:
        f.seek(0,0)
        for line in f:
            Id,name = line.strip().split(':')
            detaile_dicct_tmp={}
            detaile_dicct_tmp.update({'Id': Id, 'name': name})  # 将值给临时存储空间
            employee_dict.append(detaile_dicct_tmp)
        employee_dict.pop(0)

        # print('显示全部信息',employee_dict,"get_employee_dict")
    return employee_dict
#1增加
@login_auth
@login_purview
def add():
    get_employee_dict()
    print("请输入编号")
    Id= input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):
            print("已经存在")
            break
    else:
        detaile_dicct_tmp = {}  # 添加临时存储空间
        print("请输入姓名")
        name = input().strip()
        detaile_dicct_tmp.update({'Id':Id, 'name': name})  # 将值给临时存储空间
        employee_dict.append(detaile_dicct_tmp)  # 将临时存储空间的内容给employee_dict
        # print(employee_dict)
        with open('employee_dict.txt', 'a', encoding='utf-8') as f:
            f.write("%s:%s\n"%(Id,name))
# 2查看
@login_auth
@login_purview
def find():
    print("请输入编号")
    Id = input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):
            print("找到了", end='')
            print("Id:%s\tname:%s" % (Id, detaile_dicct.get('name')))
            break
    else:
        print("查无此人")
# 3删除
@login_auth
@login_purview
def delete():
    count=0
    get_employee_dict()
    print("请输入要删除的编号")
    Id=input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):

            print("存在,执行删除")
            employee_dict.remove(detaile_dicct)  # 移除employee_dict列表中当前字典
            # print('显示删除后的内容:',employee_dict)
            # print(employee_dict)
            import os
            with open('employee_dict.txt', 'rt', encoding='utf-8') as read_f,open('b.txt','wt',encoding='utf-8') as write_f:
                k=0
                for line in read_f:
                    if k == count:
                        # print("删除行的下标k=", k,end="\t")
                        # print('\n理论上的删除内容',line)
                        pass

                    else:
                        # print("未删除行下标k=", k,end="\t")

                        write_f.writelines(line)
                    k+=1
                    # print("k=", k)
            os.remove('employee_dict.txt')  # 删除源文件
            os.rename('b.txt', 'employee_dict.txt')

            break
        count += 1  # 记录要删除的数据的行数下标
        # print("count=", count)
    else:
        print("不存在")
#4
@login_auth
def view_all_name():
    get_employee_dict()
    for detaile_dicct in employee_dict:
        print(detaile_dicct.get('name'),end=' ')
# 菜单
def menu():
    print("\n欢迎来到员工工资管理系统,输入指令:(1增加 2查看 3删除 4查看所有名字 0退出)")
# menu_choose()
employee_dict=[{}]
cmd = "w"
while cmd != '0':
    detaile_dicct = {}
    menu()
    cmd = input().strip()
    if cmd == '1':
        # add=login_auth(add)
        add()
    elif cmd == '2':
        # find = login_auth(find)
        find()
    elif cmd == '3':
        # delete = login_auth(delete)
        delete()
    elif cmd=='4':
        view_all_name()
    elif cmd == '0':
        break
    else:
        print("错误,重来")

第五个功能(修改权限)未实现,勿用第五个功能!!!!!!!!

is_login={'is_login':False}
super_purview={'super_purview':'普通'}
# 装饰器登录检测
def login_auth(func):
    def auth(*args,**kwargs):
        if is_login.get('is_login'):
            res = func(*args,**kwargs)
            return res
        else:
            print('未登录,请先登录')
            login()
    return auth
# 装饰器权限检测
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 login():
    status = False
    while status!=True:
        print("        1. 登录    2. 注册")
        cmd = input('请输入你的选择:').strip()
        # 注册
        if cmd == '2':
            while True:
                # 1. 接收用户名和密码
                username = input('username>>>:')
                password = input('password>>>:')

                # 3. 判断用户是否已经注册过
                # 3.1 先取出文件中得数据
                with open('userinfo.txt', 'r', encoding='utf-8') as f1:
                    # 3.2 把文件数据一行一行的读出来
                    for line in f1:
                        # print(line)  # kevin|123\n
                        real_username, *_ ,purview= line.split('|')
                        # res = line.split('|')
                        if real_username == username:
                            print('该用户已经存在,请从新输入')
                            break
                    else:
                        # 2. 组织用户名和密码成固定格式 kevin|123
                        data = '%s|%s|普通者\n' % (username, password)
                        # data = username + '|' + password
                        # 3. 把用户数据保存到文件中
                        with open('userinfo.txt', 'a', encoding='utf-8') as f:
                            f.write(data)

                        print('用户%s: 注册成功' % username)
                        break
        # 登录
        elif cmd == '1':
            username = input('username>>>:')
            password = input('password>>>:')

            # 2. 登录,读取文件数据,得到用户的真实用户名和密码
            with open('userinfo.txt', 'r', encoding='utf-8') as f:
                # 一行一行的读取用户名和密码
                for line in f:
                    real_username, real_pwd,purview= line.split('|')  # kevin|123\n  ['kevin', '123\n']
                    real = real_pwd.strip('\n')
                    purview=purview.strip('\n')
                    # 判断用户名和密码是否正确
                    if real_username == username and real == password:
                        print('登录成功',end=" ")
                        status = True
                        is_login['is_login']=True
                        if purview=='超级管理员':
                            global super_purview
                            super_purview['super_purview'] = '超级管理员'
                            print(super_purview['super_purview'],"欢迎超级管理员")
                        elif purview=='管理员':
                            # global super_purview
                            super_purview['super_purview'] = '管理员'
                            print(super_purview['super_purview'],"欢迎管理员")
                        else:
                            print("欢迎用户登录")
                        return status
                        # print(super_purview['super_purview'])

                        break
                else:
                    print('登录失败')
# 获取员工信息
def get_employee_dict():
    global employee_dict
    employee_dict = [{}]
    with open('employee_dict.txt','r', encoding='utf-8') as f:
        f.seek(0,0)
        for line in f:
            Id,name = line.strip().split(':')
            detaile_dicct_tmp={}
            detaile_dicct_tmp.update({'Id': Id, 'name': name})  # 将值给临时存储空间
            employee_dict.append(detaile_dicct_tmp)
        employee_dict.pop(0)

        # print('显示全部信息',employee_dict,"get_employee_dict")
    return employee_dict
# 修改用户权限
def change_purview():
    user_dict = [{}]
    username = input('请输入要修改的用户权限的用户名>>>:').strip()

    with open('userinfo.txt', 'r', encoding='utf-8') as f1:
        # 3.2 把文件数据一行一行的读出来
        count =0
        for line in f1:
            real_username, *_, purview = line.split('|')
            if real_username == username:
                print('当前权限', purview)
                #
                while True:
                    # detaile_dicct = {}
                    new_purview = input('请输入新权限  1.管理员 2.普通者>>>:').strip()

                    if new_purview == '1':
                        whatisneed=f1.writelines('%s|%s|管理员\n'%(real_username,*_))
                        modify_file('userinfo.txt', count, whatisneed)
                        pass
                    elif new_purview == '2':
                        pass


                    else:


                        print("错误,重来")
                break
            count+=1
        else:
            print('不存在,请从新输入')

def modify_file(file_name,count,whatisneed):
    import os
    with open(file_name, 'rt', encoding='utf-8') as read_f, open('b.txt', 'wt', encoding='utf-8') as write_f:
        k = 0
        for line in read_f:
            if k == count:
                # write_f.writelines("?????")
                whatisneed
            else:
                # print("未修改行下标k=", k,end="\t")

                write_f.writelines(line)
            k += 1
            # print("k=", k)
    os.remove(file_name)  # 删除源文件
    os.rename('b.txt', file_name)
#1增加
@login_auth
@login_super_purview
def add():
    get_employee_dict()
    print("请输入编号")
    Id= input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):
            print("已经存在")
            break
    else:
        detaile_dicct_tmp = {}  # 添加临时存储空间
        print("请输入姓名")
        name = input().strip()
        detaile_dicct_tmp.update({'Id':Id, 'name': name})  # 将值给临时存储空间
        employee_dict.append(detaile_dicct_tmp)  # 将临时存储空间的内容给employee_dict
        # print(employee_dict)
        with open('employee_dict.txt', 'a', encoding='utf-8') as f:
            f.write("%s:%s\n"%(Id,name))
# 2查看
@login_auth
@login_super_purview
def find():
    print("请输入编号")
    Id = input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):
            print("找到了", end='')
            print("Id:%s\tname:%s" % (Id, detaile_dicct.get('name')))
            break
    else:
        print("查无此人")
# 3删除
@login_auth
@login_super_purview
def delete():
    count=0
    get_employee_dict()
    print("请输入要删除的编号")
    Id=input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):

            print("存在,执行删除")
            employee_dict.remove(detaile_dicct)  # 移除employee_dict列表中当前字典
            # print('显示删除后的内容:',employee_dict)
            # print(employee_dict)
            import os
            with open('employee_dict.txt', 'rt', encoding='utf-8') as read_f,open('b.txt','wt',encoding='utf-8') as write_f:
                k=0
                for line in read_f:
                    if k == count:
                        # print("删除行的下标k=", k,end="\t")
                        # print('\n理论上的删除内容',line)
                        pass

                    else:
                        # print("未删除行下标k=", k,end="\t")

                        write_f.writelines(line)
                    k+=1
                    # print("k=", k)
            os.remove('employee_dict.txt')  # 删除源文件
            os.rename('b.txt', 'employee_dict.txt')

            break
        count += 1  # 记录要删除的数据的行数下标
        # print("count=", count)
    else:
        print("不存在")
#4
@login_auth
@login_purview
def view_all_name():
    get_employee_dict()
    for detaile_dicct in employee_dict:
        print(detaile_dicct.get('name'),end=' ')
# 菜单
def menu():
    print("\n欢迎来到员工工资管理系统,输入指令:(1增加 2查看 3删除 4查看所有名字 5修改权限(仅超级管理员) 0退出)")
# menu_choose()
employee_dict=[{}]
cmd = "w"
while cmd != '0':
    detaile_dicct = {}
    menu()
    cmd = input().strip()
    if cmd == '1':
        # add=login_auth(add)
        add()
    elif cmd == '2':
        # find = login_auth(find)
        find()
    elif cmd == '3':
        # delete = login_auth(delete)
        delete()
    elif cmd=='4':
        view_all_name()
    elif cmd=='5':
        change_purview()
    elif cmd == '0':
        break
    else:
        print("错误,重来")

改进版:可能功能有点乱,但是执行上已经没有问题了

is_login={'is_login':False}
super_purview={'super_purview':'普通'}
# 装饰器登录检测
def login_auth(func):
    def auth(*args,**kwargs):
        if is_login.get('is_login'):
            res = func(*args,**kwargs)
            return res
        else:
            print('未登录,请先登录')
            login()
    return auth
# 装饰器权限检测
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 login():
    status = False
    while status!=True:
        print("        1. 登录    2. 注册")
        cmd = input('请输入你的选择:').strip()
        # 注册
        if cmd == '2':
            while True:
                # 1. 接收用户名和密码
                username = input('username>>>:')
                password = input('password>>>:')

                # 3. 判断用户是否已经注册过
                # 3.1 先取出文件中得数据
                with open('userinfo.txt', 'r', encoding='utf-8') as f1:
                    # 3.2 把文件数据一行一行的读出来
                    for line in f1:
                        # print(line)  # kevin|123\n
                        real_username, *_ ,purview= line.split('|')
                        # res = line.split('|')
                        if real_username == username:
                            print('该用户已经存在,请从新输入')
                            break
                    else:
                        # 2. 组织用户名和密码成固定格式 kevin|123
                        data = '%s|%s|普通者\n' % (username, password)
                        # data = username + '|' + password
                        # 3. 把用户数据保存到文件中
                        with open('userinfo.txt', 'a', encoding='utf-8') as f:
                            f.write(data)

                        print('用户%s: 注册成功' % username)
                        break
        # 登录
        elif cmd == '1':
            username = input('username>>>:')
            password = input('password>>>:')

            # 2. 登录,读取文件数据,得到用户的真实用户名和密码
            with open('userinfo.txt', 'r', encoding='utf-8') as f:
                # 一行一行的读取用户名和密码
                for line in f:
                    real_username, real_pwd,purview= line.split('|')  # kevin|123\n  ['kevin', '123\n']
                    real = real_pwd.strip('\n')
                    purview=purview.strip('\n')
                    # 判断用户名和密码是否正确
                    if real_username == username and real == password:
                        print('登录成功',end=" ")
                        status = True
                        is_login['is_login']=True
                        if purview=='超级管理员':
                            global super_purview
                            super_purview['super_purview'] = '超级管理员'
                            print(super_purview['super_purview'],"欢迎超级管理员")
                        elif purview=='管理员':
                            # global super_purview
                            super_purview['super_purview'] = '管理员'
                            print(super_purview['super_purview'],"欢迎管理员")
                        else:
                            print("欢迎用户登录")
                        return status
                        # print(super_purview['super_purview'])

                        break
                else:
                    print('登录失败')
# 获取员工信息
def get_employee_dict():
    global employee_dict
    employee_dict = [{}]
    with open('employee_dict.txt','r', encoding='utf-8') as f:
        f.seek(0,0)
        for line in f:
            Id,name = line.strip().split(':')
            detaile_dicct_tmp={}
            detaile_dicct_tmp.update({'Id': Id, 'name': name})  # 将值给临时存储空间
            employee_dict.append(detaile_dicct_tmp)
        employee_dict.pop(0)

        # print('显示全部信息',employee_dict,"get_employee_dict")
    return employee_dict
# 修改用户权限
@login_auth
@login_super_purview
def change_purview():
    user_dict = [{}]
    # 获取用户信息
    with open('userinfo.txt', 'r', encoding='utf-8') as f:
        f.seek(0, 0)
        for line in f:
            real_username, pwd ,purview = line.strip().split('|')
            detaile_dicct_tmp = {}
            detaile_dicct_tmp.update({'real_username': real_username, 'pwd': pwd ,'purview':purview })  # 将值给临时存储空间
            user_dict .append(detaile_dicct_tmp)
        user_dict .pop(0)
    username = input('请输入要修改的用户权限的用户名>>>:').strip()

    # with open('userinfo.txt', 'r', encoding='utf-8') as f1:
    # 3.2 把字典数据一行一行的读出来
    count =0
    for detaile_dicct_tmp in user_dict:
        # real_username, pwd, purview = line.split('|')
        if detaile_dicct_tmp.get('real_username') == username:
            print('当前权限',detaile_dicct_tmp.get('purview'))
            while True:
                # detaile_dicct = {}
                new_purview = input('请输入新权限  1.管理员 2.普通者>>>:').strip()
                if new_purview == '1':
                    res='%s|%s|管理员\n'%(detaile_dicct_tmp.get('real_username'),detaile_dicct_tmp.get('pwd'))
                    modify_file('userinfo.txt', count,res)
                    print("已经修改为管理员")
                    return
                elif new_purview == '2':
                    res='%s|%s|普通者\n'%(detaile_dicct_tmp.get('real_username'),detaile_dicct_tmp.get('pwd'))
                    modify_file('userinfo.txt', count, res)
                    print("已经修改为普通者")
                    return
                else:
                    print("错误,重来")
            break
        count+=1
    else:
        print('用户名不存在,请从新输入')
#修改文件
def modify_file(file_name,count,*res):
    import os
    with open(file_name, 'rt', encoding='utf-8') as read_f, open('b.txt', 'wt', encoding='utf-8') as write_f:
        k = 0
        for line in read_f:
            if k == count:
                if res!=None:
                    write_f.writelines(res)
                else:
                    pass
            else:
                # print("未修改行下标k=", k,end="\t")

                write_f.writelines(line)
            k += 1
            # print("k=", k)
    os.remove(file_name)  # 删除源文件
    os.rename('b.txt', file_name)
#1增加
@login_auth
@login_super_purview
def add():
    get_employee_dict()
    print("请输入编号")
    Id= input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):
            print("已经存在")
            break
    else:
        detaile_dicct_tmp = {}  # 添加临时存储空间
        print("请输入姓名")
        name = input().strip()
        detaile_dicct_tmp.update({'Id':Id, 'name': name})  # 将值给临时存储空间
        employee_dict.append(detaile_dicct_tmp)  # 将临时存储空间的内容给employee_dict
        # print(employee_dict)
        with open('employee_dict.txt', 'a', encoding='utf-8') as f:
            f.write("%s:%s\n"%(Id,name))
# 2查看
@login_auth
@login_super_purview
def find():
    print("请输入编号")
    Id = input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):
            print("找到了", end='')
            print("Id:%s\tname:%s" % (Id, detaile_dicct.get('name')))
            break
    else:
        print("查无此人")
# 3删除
@login_auth
@login_super_purview
def delete():
    count=0
    get_employee_dict()
    print("请输入要删除的编号")
    Id=input().strip()
    for detaile_dicct in employee_dict:
        if Id == detaile_dicct.get('Id'):

            print("存在,执行删除")
            employee_dict.remove(detaile_dicct)  # 移除employee_dict列表中当前字典
            modify_file('employee_dict.txt', count)
            break
        count += 1  # 记录要删除的数据的行数下标
        # print("count=", count)
    else:
        print("不存在")
#4
@login_auth
@login_purview
def view_all_name():
    get_employee_dict()
    for detaile_dicct in employee_dict:
        print(detaile_dicct.get('name'),end=' ')
# 菜单
def menu():
    print("\n欢迎来到员工工资管理系统,输入指令:(1增加员工 2查看员工 3删除员工 4查看所有员工名字 5修改用户权限(仅超级管理员) 0退出)")
# menu_choose()
employee_dict=[{}]
cmd = "w"
while cmd != '0':
    detaile_dicct = {}
    menu()
    cmd = input().strip()
    if cmd == '1':
        # add=login_auth(add)
        add()
    elif cmd == '2':
        # find = login_auth(find)
        find()
    elif cmd == '3':
        # delete = login_auth(delete)
        delete()
    elif cmd=='4':
        view_all_name()
    elif cmd=='5':
        change_purview()
    elif cmd == '0':
        break
    else:
        print("错误,重来")
image.png
上一篇 下一篇

猜你喜欢

热点阅读