def f(x,y):
pass
print(f.__code__.co_argcount)
print(f.__code__.co_varnames)
#请求合法性验证,如签名
def verify_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
params = request.json or request.args.to_dict()
ret = check_sign(params)
if ret['code'] != 1000:
return json.dumps(d_convert(ret), ensure_ascii=False)
# 判断方法是否有名为current_user的参数,有的话就尝试通过token获取用户
arg_count = f.__code__.co_argcount
arg_names = f.__code__.co_varnames
if arg_count > 0 and arg_names[0] == 'current_user':
ret = check_login_token(params.get('token'))
# 传入current_user
return f(current_user=ret.get('user'), *args, **kwargs)
else:
return f(*args, **kwargs)
return decorated_function
#可以自由选择是否需要获取current_user
@bp_user.route('/home',methods=['GET','POST'])
@verify_required
def home(current_user):
print(f'home:{current_user.id=}')
#可以自由选择是否需要获取current_user
@bp_user.route('/test',methods=['GET','POST'])
@verify_required
def test():
pass
网友评论