From d322c9488a7fe57cc9265301e562905346462235 Mon Sep 17 00:00:00 2001 From: BryantHe Date: Tue, 15 Aug 2023 17:49:05 +0800 Subject: [PATCH] feat: add apis of user info --- app/api/v1/__init__.py | 2 ++ app/api/v1/auth.py | 4 +-- app/api/v1/exception/user.py | 11 ++++++++ app/api/v1/schema/user.py | 14 ++++++++++ app/api/v1/user.py | 54 ++++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 app/api/v1/exception/user.py create mode 100644 app/api/v1/schema/user.py create mode 100644 app/api/v1/user.py diff --git a/app/api/v1/__init__.py b/app/api/v1/__init__.py index 277f4d8..bcd220e 100644 --- a/app/api/v1/__init__.py +++ b/app/api/v1/__init__.py @@ -3,6 +3,7 @@ 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 +from app.api.v1.user import user def create_v1(): @@ -10,6 +11,7 @@ def create_v1(): bp_v1.register_blueprint(api, url_prefix='/api') bp_v1.register_blueprint(auth, url_prefix='/auth') bp_v1.register_blueprint(record, url_prefix='/record') + bp_v1.register_blueprint(user, url_prefix='/user') return bp_v1 diff --git a/app/api/v1/auth.py b/app/api/v1/auth.py index b7d92b6..cf37b20 100644 --- a/app/api/v1/auth.py +++ b/app/api/v1/auth.py @@ -15,7 +15,7 @@ def wechat_login(json_data): try: result = rpc.admin.wechat_login(json_data['code']) except Exception as e: - raise WechatLoginError() + raise WechatLoginError(extra_data={'error_docs': str(e)}) return result @@ -27,5 +27,5 @@ def refresh_token(query_data): try: result = rpc.admin.refresh_token(query_data['refresh_token']) except Exception as e: - raise AuthError() + raise AuthError(extra_data={'error_docs': str(e)}) return result \ No newline at end of file diff --git a/app/api/v1/exception/user.py b/app/api/v1/exception/user.py new file mode 100644 index 0000000..a836587 --- /dev/null +++ b/app/api/v1/exception/user.py @@ -0,0 +1,11 @@ +from apiflask import HTTPError + + +class UserInfoNotFound(HTTPError): + status_code = 404 + message = '查询用户信息失败' + + +class UserInfoError(HTTPError): + status_code = 500 + message = '设置用户信息失败' \ No newline at end of file diff --git a/app/api/v1/schema/user.py b/app/api/v1/schema/user.py new file mode 100644 index 0000000..6db386d --- /dev/null +++ b/app/api/v1/schema/user.py @@ -0,0 +1,14 @@ +from apiflask import Schema +from apiflask.fields import String, URL, Float + + +class UserInfoIn(Schema): + nickname = String() + avatar_url = URL() + height = Float() + + +class UserInfoOut(Schema): + nickname = String() + avatar_url = URL() + height = Float() \ No newline at end of file diff --git a/app/api/v1/user.py b/app/api/v1/user.py new file mode 100644 index 0000000..a507af6 --- /dev/null +++ b/app/api/v1/user.py @@ -0,0 +1,54 @@ +from apiflask import APIBlueprint +from flask import session + +from app import rpc +from app.api.v1.exception.user import UserInfoNotFound, UserInfoError +from app.api.v1.schema.user import UserInfoIn, UserInfoOut +from app.util.auth import login_required + +user = APIBlueprint('user', __name__) + + +@user.post('/info') +@user.doc(summary='设置用户信息', description='设置用户信息') +@user.input(UserInfoIn, location='json') +@login_required +def wechat_login(json_data): + try: + rpc.admin.set_user_info(session['user_id'], + json_data.get('nickname'), + json_data.get('avatar_url')) + except Exception as e: + raise UserInfoError(extra_data={'error_docs': str(e)}) + + try: + rpc.body_record.set_body_info(session['user_id'], + height=json_data.get('height')) + except Exception as e: + raise UserInfoError(extra_data={'error_docs': str(e)}) + + return {'msg': 'setting user info success'} + + +@user.get('/info') +@user.doc(summary='查询用户信息', description='查询用户信息') +@user.output(UserInfoOut) +@login_required +def refresh_token(): + result = {} + try: + user_dict = rpc.admin.get_user_info(session['user_id']) + except Exception as e: + raise UserInfoNotFound(extra_data={'error_docs': str(e)}) + + try: + body_info_dict = rpc.body_record.get_body_info(session['user_id']) + except Exception as e: + raise UserInfoNotFound(extra_data={'error_docs': str(e)}) + + if user_dict: + result.update(user_dict) + if body_info_dict: + user_dict['height'] = body_info_dict['height'] + + return result