mport time
import inspect
from functools import wraps
user_status = {'username': None,'status': False}
dic1 = {
1: '登录',
2: '注册',
3: '文章',
4: '日记',
5: '评论',
6: '收藏',
7: '注销',
8: '退出程序'
}
dic2 = {
3: 'artecle',
4: 'diary',
5: 'comment',
6: 'collection',
7: 'logout',
8: 'exited',
}
Flag = True
def wrapper(f1): #装饰器 选择页面前判断是否登录,如果未登录 跳转至登录页面
def inner(*args, **kwargs):
if user_status.get('status'):
ret = f1(*args, **kwargs)
time.sleep(1)
print('...
请选择其他操作:
'.center(20))
choice2()
# return inner
else:
print('您还未登录,请登录您的账号'.center(20))
login()
return inner
log=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
def time1(fn):
def inner(*args,**kwargs):
global log
with open("log.txt",mode="a+",encoding="utf-8") as f:
f.write(f"{log}的时候调用了{fn}")
f.write("
")
ret=fn(*args,**kwargs)
return ret
return inner
def login():
print("welcome登录界面".center(20,"*"))
global Flag
count=1
while count < 4:
user_name=input("请输入登录的用户名>>").strip()
user_password=input("请输入登录密码>>").strip()
with open("register", mode="r", encoding="utf-8") as f:
for line in f:
if user_name==line.split()[0] and user_password == line.split()[1]:
print("welcome %s".center(20) %user_name)
global user_status
user_status = {'username': user_name, 'status': True}
return choice2()
else:
count = count + 1
print("用户名或者密码错误")
print("错误次数过多")
def register1():
print("注册界面".center(20,"*"))
new_user_name = input("请输入用户名>>")
with open("register", mode="r+", encoding="utf-8") as f1:
for line in f1:
if new_user_name == line.split()[0]:
print("用户已存在,请直接登录")
return login()
else:
new_user_password = input("请输入密码>>").strip()
with open("register", mode="a", encoding="utf-8") as f2:
f2.write("
")
f2.write(new_user_name)
f2.write(" ")
f2.write(new_user_password)
print("注册完成请登录")
return login()
@wrapper
@time1
def article():
print("这是昊哥博客园首页".center(20,"*"))
print("没学前段呢")
time.sleep(3)
@wrapper
@time1
def diary():
print("这是昊哥博客园日记界面".center(30,"*"))
print("没学前段呢")
time.sleep(3)
@wrapper
@time1
def comment():
print("这是昊哥博客评论界面".center(30,"*"))
print("没学前段呢")
time.sleep(3)
@wrapper
@time1
def collection():
print("这是昊哥博博客园收藏界面".center(30,"*"))
print("没学前段呢")
time.sleep(3)
@wrapper
@time1
def logout():
print("这是昊哥博客园注销界面".center(30,"*"))
print('退出登录')
time.sleep(3)
global user_status
user_status = {'username': None, 'status': False, }
def exited():
print("下次见")
time.sleep(3)
exit()
def choice2(): ## 登录后操作选项
global user_status
if user_status.get('status'):
print('欢迎登陆,请按序号选择登录页面:'.center(20))
for k, v in dic2.items():
print(k, v)
choice = input('请按序号选择页面: ').strip()
if choice.isdigit():
choice = int(choice)
if choice == 3:
article()
elif choice == 4:
diary()
elif choice == 5:
comment()
elif choice == 6:
collection()
elif choice == 7:
logout()
elif choice == 8:
exited()
else:
print('请输入正确序号')
def main():
while Flag: # 主函数
print('请输入序号选择操作:')
for k, v in dic1.items():
print(k, v.center(20))
choice = input('请选择操作项目:').strip()
if choice.isdigit():
choice = int(choice)
if choice == 1:
login()
elif choice == 2:
register1()
elif choice == 3:
article()
elif choice == 4:
diary()
elif choice == 5:
comment()
elif choice == 6:
collection()
elif choice == 7:
logout()
elif choice == 8:
exited()
else:
print('请按提示输入:')
else:
print('请按提示输入')
return main()
main()