python 登录装饰器
程序员文章站
2022-03-08 14:39:03
...
account = {
'username':'test',
'password':'123'
}
# 如果要在局部函数里改这个全局变量的值,要用global
log_status = {
'username':None,
'isLogin':False
}
# 登录装饰器
def login(auth_type):
"""
:param auth_type: 1.密码登录 2.wechat登录
"""
def wrapper(f):
def inner(*args, **kwargs):
# 如果未登录
if not log_status['isLogin']:
if auth_type == 'password':
username = input('请输入用户名:')
password = input('请输入密码:')
if username == account['username'] and password == account['password']:
print('登录成功,欢迎 %s' %username)
log_status['isLogin'] = True
return f(*args, **kwargs)
else:
print('登录失败')
else:
username = input('输入微信账号:')
password = input('输入微信密码:')
if username == account['username'] and password == account['password']:
print('登录成功,欢迎 %s' %username)
log_status['isLogin'] = True
return f(*args, **kwargs)
else:
print('登录失败')
else:
return f(*args, **kwargs)
return inner
return wrapper
@login('password')
def get_orders():
print('查询订单列表')
@login('wechat')
def get_balance():
print('查询余额')
#
get_orders()
get_balance()
下一篇: 使用matplotlib绘制三维图形