# -*- coding:utf-8 -*-
import time
login_state = False
user_dict = {'username': None}
def register(): # 注冊函數
while True:
username = input("請輸入新增賬號:").strip()
password = input("請輸入注冊密碼:").strip()
with open("register", encoding="UTF-8")as reg:
for i in reg:
list_reg = i.strip().split(',')
if username == list_reg[0]:
print("使用者名已經存在,請重新輸入")
break
else:
with open("register", encoding="UTF-8", mode="a")as ligo:
ligo.write('\n{},{}'.format(username, password))
print("注冊成功")
return True
def login(): # 登陸函數
global login_state
global user_dict
i = 0 # 計數器
while i
username = input("請輸入您的賬号:").strip()
password = input("請輸入您的密碼:").strip() # 去除空格及換号符
with open('register', encoding='UTF-8')as f1:
for line in f1: # 循環讀取注冊檔案中的内容
line_list = line.strip().split(',')
if username == line_list[0] and password == line_list[1]:
print("*******登陸成功*******")
login_state = True
user_dict = line_list[0]
return True
else:
print("賬戶或密碼輸入錯誤")
i += 1
def log(e):
def loge():
log_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
with open("log", encoding="UTF-8", mode="a") as logg:
logg.write('\n{},{}登陸了,執行了{}函數'.format(log_time, user_dict, e.__name__))
e()
return loge
def jian_ce(f): # 認證函數,檢測使用者是否登陸
def inner():
global login_state
if login_state == False:
print("您尚未登陸,請先登陸後在執行程式")
login()
else:
f()
return inner
def logoff(): # 登出函數
global login_state
if login_state == True:
login_state = False
print("登出成功")
return login_state
else:
print("您尚未登陸,不需要登出")
@jian_ce # 文法糖,裝飾器
@log
def wen_zhang():
print("這是文章頁面")
@jian_ce # 文法糖,裝飾器
@log
def ri_ji():
print("這是日記頁面")
@jian_ce # 文法糖,裝飾器
@log
def ping_lun():
print("這是評論頁面")
@jian_ce # 文法糖,裝飾器
@log
def shou_cang():
print("這是收藏頁面")
xu_dict = { # 定義了一個字典,存放序列号對應函數
1: login,
2: register,
3: wen_zhang,
4: ri_ji,
5: ping_lun,
6: shou_cang,
7: logoff
}
while True:
print('''-------歡迎來到部落格園-------
1:請登陸
2:請注冊
3:文章頁面
4:日記頁面
5:評論頁面
6:收藏頁面
7:登出
8:退出程式
''')
xu_hao = input("請輸入序列号:").strip()
if xu_hao.isdigit():
xu_hao = int(xu_hao)
if xu_hao > 0 and xu_hao <= len(xu_dict):
xu_dict[xu_hao]()
elif xu_hao == 8:
break
print("再見!退出程式成功")
else:
print("你輸入的序号不存在")
else:
print("您輸入的序列号存在非法字元")