'''作業題目:
模拟部落格園登入
作業需求:
1),啟動程式,首頁面應該顯示成如下格式:
歡迎來到部落格園首頁
1:請登入
2:請注冊
3:文章頁面
4:日記頁面
5:評論頁面
6:收藏頁面
7:登出
8:退出程式
2),使用者輸入選項,3~6選項必須在使用者登入成功之後,才能通路成功。
3),使用者選擇登入,使用者名密碼從register檔案中讀取驗證,三次機會,
沒成功則結束整個程式運作,成功之後,可以選擇通路3~6項,通路頁面之前,
必須要在log檔案中列印日志,日志格式為-->使用者:xx 在xx年xx月xx日 執行了 %s函數,
通路頁面時,頁面内容為:歡迎xx使用者通路評論(文章,日記,收藏)頁面
4),如果使用者沒有注冊,則可以選擇注冊,注冊成功之後,可以自動完成登入,然後進入首頁選擇。
5),登出使用者是指登出使用者的登入狀态,使其在通路任何頁面時,必須重新登入。'''
def mk_file(): #判斷檔案是否存在,不存在則建立空檔案
if notos.path.exists(register_file):
f= open(register_file, mode="w", encoding="utf-8")
f.close()def get_file_content(): #讀取檔案内容,傳回一個字典
mk_file()
with open(register_file, mode="r", encoding="utf-8") as f:
content=f.read().strip()if content != "":
content=eval(content)else:
content={}returncontentdef edit_file_content(usr_dic): #修改檔案内容
with open(register_file, mode="w", encoding="utf-8") as f:
f.write(str(usr_dic))def login(): #使用者登入
globalusername
usr_dic=get_file_content()if username != "": #檢查使用者是否已登入
if usr_dic.get(username, {}) !={}:if usr_dic[username]["status"] == "logged":return
for i in range(3, 0, -1): #三次機會
while 1:print("\033[31;0m請先登入\033[0m".center(50, "*"))
name= input("Login Username:").strip()if name == "":print("\033[31;0m使用者名不能為空.\033[0m")continuepwd= input("Login Password:").strip()if pwd == "":print("\033[31;0m密碼不能為空.\033[0m")continue
if name inusr_dic:if usr_dic[name]["pwd"] == pwd: #驗證通過
usr_dic[name]["status"] = "logged"username=name
edit_file_content(usr_dic)print("\033[32;0m恭喜您,使用者名%s登入成功.\033[0m" %name)return
if i == 1: #驗證失敗
exit("\033[31;0m使用者名或密碼錯誤,您的賬号已鎖住.\033[0m")else:print("\033[31;0m使用者名或密碼錯誤,您還有%d次機會.\033[0m" % (i-1))break
def register(): #使用者注冊
globalusernamewhile 1:
name= input("Register Username:").strip()
pwd= input("Register Password:").strip()if name != "" and pwd != "": #使用者名和密碼的合法性
if len(name) < 3 and len(name) > 30:print("\033[31;0m使用者名長度為3~30.\033[0m")continue
if len(pwd) < 6 and len(pwd) > 20:print("\033[31;0m使用者密碼長度為6~20.\033[0m")continueusr_dic=get_file_content()if name in usr_dic: #檢查使用者名是否存在
print("\033[31;0m注冊失敗,使用者名%s已存在.\033[0m" %name)continue
else: #不存在,則添加使用者名和密碼
usr_dic.setdefault(name, {"pwd": pwd, "status": "logged"})
edit_file_content(usr_dic)
username=name
login()print("\033[32;0m恭喜您,賬号%s注冊成功.\033[0m" %name)return
else:print("\033[31;0m使用者名和密碼不能為空.\033[0m")def write_log(auth_flag): #裝飾器函數,也是一個閉包函數,記錄日志
defauth(func):defrecord_log():if auth_flag: #需要登入
login()
time_lst= time.strftime("%Y %m %d", time.localtime()).split()
with open(log_file, mode="a", encoding="utf-8") as f: #追加日志
f.write("使用者:%s 在%s年%s月%s日 執行了 %s函數\n" % (username, time_lst[0], time_lst[1], time_lst[2], func.__name__))return func() #調用被裝飾函數
returnrecord_logreturnauth
@write_log(1) #這裡相當于 xx = write_log(1) 和 article_page = xx(artical_page)
def article_page(): #文章頁面
print("\033[32;0m歡迎%s使用者通路文章頁面.\033[0m".center(50, "*") %username)
@write_log(1)def diary_page(): #日記頁面
print("\033[32;0m歡迎%s使用者通路日記頁面.\033[0m".center(50, "*") %username)
@write_log(1)def comment_page(): #評論頁面
print("\033[32;0m歡迎%s使用者通路評論頁面.\033[0m".center(50, "*") %username)
@write_log(1)def collect_page(): #收藏頁面
print("\033[32;0m歡迎%s使用者通路收藏頁面.\033[0m".center(50, "*") %username)
@write_log(0)#這裡相當于 xx = write_log(0) 和 article_page = xx(artical_page)
def logout(): #登出
globalusername
usr_dic=get_file_content()
usr_dic[username]["status"] = "logout"edit_file_content(usr_dic)print("\033[32;0m使用者%s已成功登出.\033[0m" %username)
username= ""
importosimporttime
menu= ["請登入", "請注冊", "文章頁面", "日記頁面", "評論頁面", "收藏頁面", "登出", "退出程式"] #菜單清單
register_file = "register" #使用者注冊檔案
log_file = "log" #日志檔案
username = "" #使用者名,預設為空
print("\033[32;0m歡迎%s來到部落格園首頁\033[0m".center(50, "*") %username)while 1:for k, item in enumerate(menu, 1): #顯示菜單
if username == "":print("%d:%s" %(k, item))else:if k <= 2:continue
print("%d:%s" %(k, item))
user_choice= 1 #定義使用者選擇的菜單序号預設為1
while 1: #使用者選擇菜單序号
user_choice = input("請輸入菜單序号:").strip()ifuser_choice.isdigit():
user_choice=int(user_choice)if user_choice >=1 and user_choice <=len(menu):break
else:print("\033[31;0m輸入有誤,請重新輸入菜單序号!\033[0m")else:print("\033[31;0m輸入有誤,請重新輸入菜單序号!\033[0m")if user_choice == 1: #登入
login()elif user_choice == 2: #注冊
print("\033[31;0m請先登出.\033[0m") if username != "" elseregister()elif user_choice == 3: #文章頁面
article_page()elif user_choice == 4: #日記頁面
diary_page()elif user_choice == 5: #評論頁面
comment_page()elif user_choice == 6: #收藏頁面
collect_page()elif user_choice == 7: #登出
logout() if username != "" else print("\033[31;0m您還沒有登入,不能登出.\033[0m")else: #退出程式
exit("\033[32;0m退出程式.\033[0m")