feat: add apis of user info
continuous-integration/drone/push Build is passing Details

This commit is contained in:
BryantHe 2023-08-15 17:49:05 +08:00
parent 72bee4d3e1
commit d322c9488a
5 changed files with 83 additions and 2 deletions

View File

@ -3,6 +3,7 @@ from apiflask import APIBlueprint
from app.api.v1.api import api from app.api.v1.api import api
from app.api.v1.auth import auth from app.api.v1.auth import auth
from app.api.v1.record import record from app.api.v1.record import record
from app.api.v1.user import user
def create_v1(): def create_v1():
@ -10,6 +11,7 @@ def create_v1():
bp_v1.register_blueprint(api, url_prefix='/api') bp_v1.register_blueprint(api, url_prefix='/api')
bp_v1.register_blueprint(auth, url_prefix='/auth') bp_v1.register_blueprint(auth, url_prefix='/auth')
bp_v1.register_blueprint(record, url_prefix='/record') bp_v1.register_blueprint(record, url_prefix='/record')
bp_v1.register_blueprint(user, url_prefix='/user')
return bp_v1 return bp_v1

View File

@ -15,7 +15,7 @@ def wechat_login(json_data):
try: try:
result = rpc.admin.wechat_login(json_data['code']) result = rpc.admin.wechat_login(json_data['code'])
except Exception as e: except Exception as e:
raise WechatLoginError() raise WechatLoginError(extra_data={'error_docs': str(e)})
return result return result
@ -27,5 +27,5 @@ def refresh_token(query_data):
try: try:
result = rpc.admin.refresh_token(query_data['refresh_token']) result = rpc.admin.refresh_token(query_data['refresh_token'])
except Exception as e: except Exception as e:
raise AuthError() raise AuthError(extra_data={'error_docs': str(e)})
return result return result

View File

@ -0,0 +1,11 @@
from apiflask import HTTPError
class UserInfoNotFound(HTTPError):
status_code = 404
message = '查询用户信息失败'
class UserInfoError(HTTPError):
status_code = 500
message = '设置用户信息失败'

14
app/api/v1/schema/user.py Normal file
View File

@ -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()

54
app/api/v1/user.py Normal file
View File

@ -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