模擬一個(gè)后臺(tái)登陸頁(yè)面
驗(yàn)證用戶登陸
定義用戶列表,然后和用戶輸入賬號(hào)密碼進(jìn)行對(duì)比,如果正確,則讓用戶名登陸并且執(zhí)行函數(shù)
反之則提醒用戶賬號(hào)和密碼錯(cuò)誤
''' 給裝飾器加上驗(yàn)證功能 '''
#定義一個(gè)用戶列表
user_list = [
????{'user':'wentian', 'passwd':'shufeng1121'},
????{'user':'time', 'passwd':'shufeng'}
]
#定義一個(gè)狀態(tài)信息
cruuent_dic = {'username':None, 'login':False}
#裝飾器
def ?text(func):
????def ?wp(*args, **kwargs):#接收傳遞過(guò)來(lái)的所有參數(shù)
????#判斷當(dāng)前是否是登陸狀態(tài),如果是登陸狀態(tài)則直接執(zhí)行函數(shù)乖坠,不是則讓用戶輸入賬號(hào)和密碼
????????if cruuent_dic['username'] and cruuent_dic['login']:
????????????res = func(*args, **kwargs)#當(dāng)前是登陸狀態(tài)執(zhí)行此代碼
????????????return res
????????username = input('請(qǐng)輸入用戶名:')
????????password = input('請(qǐng)輸入密碼:')
????????#循環(huán)取出用戶名列表中的信息
????????for user_dic in user_list:
????????????#判斷賬號(hào)密碼是否正確,正確則登陸成功刀闷,代碼向下執(zhí)行
????????????if username == user_dic['user'] and password == user_dic['passwd']:
????????????#登陸成功熊泵,記錄當(dāng)前的登陸信息
????????????cruuent_dic['username'] = username
????????????cruuent_dic['login'] = True
????????????#執(zhí)行函數(shù)
????????????res = func(*args, **kwargs)
????????????return res
????else: print('用戶名或密碼錯(cuò)誤!')
????return wp
@text#調(diào)用裝飾器
def ?index(name):
????print('主頁(yè)%s' %name)
@text
def gouwu(dq, yp):
????print('購(gòu)物車有%s,%s' %(dq, yp))
index('你好')#調(diào)用函數(shù),并且傳參
gouwu('單人', '三件套') #調(diào)用函數(shù)甸昏,并且傳參