from apiflask import APIBlueprint from flask import session from app import rpc from app.api.v1.exception.api import ImageNotFound from app.api.v1.exception.user import UserInfoNotFound, UserInfoError from app.api.v1.schema.user import UserInfoIn, UserInfoOut, UserIdIn 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 set_user_info(json_data): try: rpc.admin.set_user_info(session['user_id'], json_data.get('nickname', None)) 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', None), is_hidden_weight=json_data.get('is_hidden_weight', None),) 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.input(UserIdIn, location='query') @user.output(UserInfoOut) @login_required def get_user_info(query_data): user_id = query_data['user_id'] if query_data.get('user_id') else session['user_id'] result = {} try: user_dict = rpc.admin.get_user_info(user_id) except Exception as e: raise UserInfoNotFound(extra_data={'error_docs': str(e)}) try: presign_url = rpc.storage.get_presign_url(user_dict['avatar_id'], 'bodyrecord', bucket='bodyrecord') user_dict['avatar_url'] = presign_url except Exception as e: raise ImageNotFound(extra_data={'error_docs': str(e)}) try: body_info_dict = rpc.body_record.get_body_info(user_id) except Exception as e: raise UserInfoNotFound(extra_data={'error_docs': str(e)}) result.update(user_dict) if body_info_dict: result['height'] = body_info_dict.get('height', None) if body_info_dict.get('is_hidden_weight', None): result['is_hidden_weight'] = True else: result['is_hidden_weight'] = False else: result['height'] = None result['is_hidden_weight'] = False return result