天天看点

验证功能

#!/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("产品经理")