#!/user/bin/python
# -*- coding:utf-8 -*-
#1、先定義後端函數功能2、定義裝飾器基本實作3、加上參數4、加上傳回值5、
user_list = [ #這是使用者資訊
{"name":"alex","passwd":"123"},
{"name":"linhaifeng","passwd":"123"},
{"name":"wupeiqi","passwd":"123"},
{"name":"yuanhao","passwd":"123"},
]
current_dic = {"username":None,"login":False} #這是記錄目前使用者的狀态
def auth_func(func):
def wrapper(*args,**kwargs):
if current_dic["username"] and current_dic["login"]: #這裡定義預設是TRUE
res = func(*args,**kwargs)
return res
username = input("使用者名:").strip() #這下面的一大片預設就是else了來進行判斷
passwd = input("密碼:").strip()
for user_dic in user_list:
if username == user_dic["name"] and passwd == user_dic["passwd"]:
current_dic["username"] = username
current_dic["login"] = True
res = func(*args,**kwargs)
return res
else:
print("使用者名或密碼錯誤")
return wrapper
#1、定義京東首頁
@auth_func
def index():
print("歡迎來到京東首頁")
#2、定義家目錄
@auth_func
def home(name):
print("歡迎回家%s" %name)
#3、定義購物車功能
@auth_func
def shopping_car(name):
print("%s的購物車裡有[%s,%s,%s]" %(name,"奶茶","妹妹","娃娃"))
#5、
print("before",current_dic)
index()
print("after",current_dic)
home("産品經理")
shopping_car("産品經理")