From 95621cf3fec07c00f107bd9b8f35369df3c250b8 Mon Sep 17 00:00:00 2001 From: BryantHe Date: Sat, 12 Aug 2023 20:15:48 +0800 Subject: [PATCH] feat: add api of body record 2 --- app/api/v1/auth.py | 2 ++ app/api/v1/record.py | 7 +++++-- app/util/auth.py | 24 ++++++++++++++++++++++++ app/util/common.py | 21 --------------------- 4 files changed, 31 insertions(+), 23 deletions(-) create mode 100644 app/util/auth.py diff --git a/app/api/v1/auth.py b/app/api/v1/auth.py index 33a8301..890f573 100644 --- a/app/api/v1/auth.py +++ b/app/api/v1/auth.py @@ -10,6 +10,7 @@ auth = APIBlueprint('auth', __name__) @auth.post('/wechat_login') @auth.input(WechatLoginIn) @auth.output(WechatLoginOut) +@auth.doc(summary='微信登录', description='微信登录', tags=['认证']) def wechat_login(json_data): try: result = rpc.admin.wechat_login(json_data['code']) @@ -21,6 +22,7 @@ def wechat_login(json_data): @auth.get('/refresh_token') @auth.input(RefreshTokenIn, location='query') @auth.output(RefreshTokenOut) +@auth.doc(summary='刷新 Token', description='刷新 Token', tags=['认证']) def refresh_token(query_data): try: result = rpc.admin.refresh_token(query_data['refresh_token']) diff --git a/app/api/v1/record.py b/app/api/v1/record.py index 5df9232..b354ccd 100644 --- a/app/api/v1/record.py +++ b/app/api/v1/record.py @@ -4,13 +4,14 @@ from flask import session from app import rpc from app.api.v1.exception.record import AddBodyRecordError, BodyRecordNotFound from app.api.v1.schema.record import BodyRecordIn, BodyRecordOut, BodyRecordsIn, BodyRecordsOut -from app.util.common import login_required +from app.util.auth import login_required record = APIBlueprint('record', __name__) @record.post('/') @record.input(BodyRecordIn) +@record.doc(summary='添加身体记录', description='添加身体记录', tags=['身体记录']) @login_required def add_record(json_data): try: @@ -25,6 +26,7 @@ def add_record(json_data): @record.get('/') @record.output(BodyRecordOut) +@record.doc(summary='查询单条身体记录', description='查询单条身体记录', tags=['身体记录']) @login_required def get_record(record_id): try: @@ -37,8 +39,9 @@ def get_record(record_id): @record.get('/list') @record.input(BodyRecordsIn, location='query') @record.output(BodyRecordsOut) +@record.doc(summary='查询身体记录列表', description='查询身体记录列表', tags=['身体记录']) @login_required -def get_record(query_data): +def get_records(query_data): try: result = rpc.body_record.get_all_by_user(query_data['user_id']) except Exception as e: diff --git a/app/util/auth.py b/app/util/auth.py new file mode 100644 index 0000000..d024a4b --- /dev/null +++ b/app/util/auth.py @@ -0,0 +1,24 @@ +from functools import wraps + +from flask import request, session + +from app.api.v1.exception.auth import AuthError + + +def login_required(f): + """ + 登陆保护,验证用户是否登陆 + """ + from app import rpc + + @wraps(f) + def wrapper(*args, **kwargs): + token = request.headers.get("Authorization", default=None) + if not token: + raise AuthError() + user_id = rpc.admin.identify(token) + if not user_id: + raise AuthError() + session['user_id'] = user_id + return f(*args, **kwargs) + return wrapper \ No newline at end of file diff --git a/app/util/common.py b/app/util/common.py index 0aacb05..f22f746 100644 --- a/app/util/common.py +++ b/app/util/common.py @@ -1,11 +1,6 @@ import os from itertools import groupby from operator import itemgetter -from functools import wraps - -from flask import request, session - -from app.api.v1.exception.auth import AuthError def split_group(dict_list, key): @@ -20,20 +15,4 @@ def split_group(dict_list, key): basedir = os.getcwd() -def login_required(f): - """ - 登陆保护,验证用户是否登陆 - """ - from app import rpc - @wraps(f) - def wrapper(*args, **kwargs): - token = request.headers.get("Authorization", default=None) - if not token: - raise AuthError() - user_id = rpc.admin.identify(token) - if not user_id: - raise AuthError() - session['user_id'] = user_id - return f(*args, **kwargs) - return wrapper