diff --git a/app/api/v1/__init__.py b/app/api/v1/__init__.py index 90acde0..277f4d8 100644 --- a/app/api/v1/__init__.py +++ b/app/api/v1/__init__.py @@ -2,12 +2,14 @@ from apiflask import APIBlueprint from app.api.v1.api import api from app.api.v1.auth import auth +from app.api.v1.record import record def create_v1(): bp_v1 = APIBlueprint('v1', __name__) bp_v1.register_blueprint(api, url_prefix='/api') bp_v1.register_blueprint(auth, url_prefix='/auth') + bp_v1.register_blueprint(record, url_prefix='/record') return bp_v1 diff --git a/app/api/v1/auth.py b/app/api/v1/auth.py index 24d99dc..33a8301 100644 --- a/app/api/v1/auth.py +++ b/app/api/v1/auth.py @@ -1,6 +1,7 @@ from apiflask import APIBlueprint from app import rpc +from app.api.v1.exception.auth import WechatLoginError, AuthError from app.api.v1.schema.auth import WechatLoginIn, WechatLoginOut, RefreshTokenIn, RefreshTokenOut auth = APIBlueprint('auth', __name__) @@ -13,8 +14,7 @@ def wechat_login(json_data): try: result = rpc.admin.wechat_login(json_data['code']) except Exception as e: - # TODO 返回 apiflask 的 json 格式的 error - raise e + raise WechatLoginError() return result @@ -25,6 +25,5 @@ def refresh_token(query_data): try: result = rpc.admin.refresh_token(query_data['refresh_token']) except Exception as e: - # TODO 返回 apiflask 的 json 格式的 error - raise e + raise AuthError() return result \ No newline at end of file diff --git a/app/api/v1/exception/auth.py b/app/api/v1/exception/auth.py new file mode 100644 index 0000000..4751434 --- /dev/null +++ b/app/api/v1/exception/auth.py @@ -0,0 +1,12 @@ +from apiflask import HTTPError + + +class AuthError(HTTPError): + status_code = 403 + message = '请登陆' + + +class WechatLoginError(HTTPError): + status_code = 500 + message = '微信登录失败' + diff --git a/app/api/v1/exception/record.py b/app/api/v1/exception/record.py new file mode 100644 index 0000000..2ebc554 --- /dev/null +++ b/app/api/v1/exception/record.py @@ -0,0 +1,11 @@ +from apiflask import HTTPError + + +class AddBodyRecordError(HTTPError): + status_code = 500 + message = 'add body record failed.' + + +class BodyRecordNotFound(HTTPError): + status_code = 404 + message = '找不到该记录' \ No newline at end of file diff --git a/app/api/v1/record.py b/app/api/v1/record.py new file mode 100644 index 0000000..5df9232 --- /dev/null +++ b/app/api/v1/record.py @@ -0,0 +1,46 @@ +from apiflask import APIBlueprint +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 + +record = APIBlueprint('record', __name__) + + +@record.post('/') +@record.input(BodyRecordIn) +@login_required +def add_record(json_data): + try: + rpc.body_record.add(user_obj_id=session['user_id'], + height=json_data['height'], + weight=json_data['weight'], + bmi=json_data['bmi']) + except Exception as e: + raise AddBodyRecordError() + return {'msg': 'add body record success'} + + +@record.get('/') +@record.output(BodyRecordOut) +@login_required +def get_record(record_id): + try: + result = rpc.body_record.get_one(record_id) + except Exception as e: + raise BodyRecordNotFound() + return result + + +@record.get('/list') +@record.input(BodyRecordsIn, location='query') +@record.output(BodyRecordsOut) +@login_required +def get_record(query_data): + try: + result = rpc.body_record.get_all_by_user(query_data['user_id']) + except Exception as e: + raise BodyRecordNotFound() + return result diff --git a/app/api/v1/schema/record.py b/app/api/v1/schema/record.py new file mode 100644 index 0000000..f87af68 --- /dev/null +++ b/app/api/v1/schema/record.py @@ -0,0 +1,25 @@ +from apiflask import Schema +from apiflask import fields + + +class BodyRecordIn(Schema): + height = fields.Float() + weight = fields.Float() + bmi = fields.Float() + + +class BodyRecordOut(Schema): + record_id = fields.String(attribute='_id') + height = fields.Float() + weight = fields.Float() + bmi = fields.Float() + create_time = fields.String() + update_time = fields.String(allow_none=True) + + +class BodyRecordsIn(Schema): + user_id = fields.String() + + +class BodyRecordsOut(Schema): + records = fields.Nested(BodyRecordOut) \ No newline at end of file diff --git a/app/util/common.py b/app/util/common.py index eb5303c..0aacb05 100644 --- a/app/util/common.py +++ b/app/util/common.py @@ -5,6 +5,8 @@ from functools import wraps from flask import request, session +from app.api.v1.exception.auth import AuthError + def split_group(dict_list, key): dict_list.sort(key=itemgetter(key)) @@ -28,12 +30,10 @@ def login_required(f): def wrapper(*args, **kwargs): token = request.headers.get("Authorization", default=None) if not token: - # TODO raise apiflask 的 json 格式的 error - return '请登陆' + raise AuthError() user_id = rpc.admin.identify(token) if not user_id: - # TODO raise apiflask 的 json 格式的 error - return '请登陆' + raise AuthError() session['user_id'] = user_id return f(*args, **kwargs) return wrapper