def verify_token(token): s = Serializer(SECRET_KEY, expires_in=EXPIRATION_TIME) try: data = s.loads(token) g.user_id = data['user_id'] return True except SignatureExpired: abort(make_result(code=Code.EXPIRED_TOKEN)) # valid token, but expired except BadSignature: abort(make_result(code=Code.INVALID_TOKEN)) # invalid token
def post(self): rep = LoginApi().rep.parse_args(strict=True) user_name = rep.get('user_name') password = rep.get('password') user = User.query.filter_by(user_name=user_name).first() if user is None: abort(make_result(code=Code.ACCOUNT_NOFOUND)) elif user.check_password(password) is False: abort(make_result(code=Code.LOGIN_FAILED)) else: token = generate_auth_token(user.id) return make_result(data={"token": token.decode()})
def custom_abort(http_status_code, *args, **kwargs): """ 改写参数传递错误的返回格式 :param http_status_code: 状态码 :param args: :param kwargs: :return: 定义返回data """ if http_status_code == 400: abort(make_result(code=Code.NO_PARAM)) return abort(http_status_code)
def page_not_found(e): return (make_result(code=Code.SYSTEM_ERRCODE))
def my_abort(http_status_code, *args, **kwargs): if http_status_code == 400: abort(make_result(code=Code.PARAMS_ERRCODE))
def verify_error(): abort(make_result(code=Code.AUTH_ERROR))
def post(self): user_id = g.user_id name = User.query.filter_by(id=user_id).first().user_name return make_result(data={"name": name + "--真实玩家"})
def get(self): req = IdTestApi().rep.parse_args(strict=True) id = req.get('id') return make_result(data={"id": id * 123})
def get(self): req = parser.parse_args(strict=True) a = req.get("a") b = req.get("b") return make_result(data=str(a) + b)