2023-08-15 17:49:05 +08:00
|
|
|
|
from apiflask import APIBlueprint
|
|
|
|
|
from flask import session
|
|
|
|
|
|
|
|
|
|
from app import rpc
|
2023-08-19 12:31:22 +08:00
|
|
|
|
from app.api.v1.exception.api import ImageNotFound
|
2023-08-15 17:49:05 +08:00
|
|
|
|
from app.api.v1.exception.user import UserInfoNotFound, UserInfoError
|
2023-08-23 11:11:15 +08:00
|
|
|
|
from app.api.v1.schema.user import UserInfoIn, UserInfoOut, UserIdIn
|
2023-08-15 17:49:05 +08:00
|
|
|
|
from app.util.auth import login_required
|
|
|
|
|
|
|
|
|
|
user = APIBlueprint('user', __name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@user.post('/info')
|
2023-08-26 12:40:23 +08:00
|
|
|
|
@user.doc(summary='设置用户信息', description='设置用户信息。注:gender 1 代表男性,2 代表女性')
|
2023-08-15 17:49:05 +08:00
|
|
|
|
@user.input(UserInfoIn, location='json')
|
|
|
|
|
@login_required
|
2023-08-16 16:07:36 +08:00
|
|
|
|
def set_user_info(json_data):
|
2023-08-15 17:49:05 +08:00
|
|
|
|
try:
|
|
|
|
|
rpc.admin.set_user_info(session['user_id'],
|
2023-08-16 16:07:36 +08:00
|
|
|
|
json_data.get('nickname', None))
|
2023-08-15 17:49:05 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
raise UserInfoError(extra_data={'error_docs': str(e)})
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
rpc.body_record.set_body_info(session['user_id'],
|
2023-08-23 10:57:14 +08:00
|
|
|
|
height=json_data.get('height', None),
|
2023-08-26 12:40:23 +08:00
|
|
|
|
is_hidden_weight=json_data.get('is_hidden_weight', None),
|
2023-09-01 15:59:19 +08:00
|
|
|
|
gender=json_data.get('gender', None),
|
|
|
|
|
age=json_data.get('age', None))
|
2023-08-15 17:49:05 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
raise UserInfoError(extra_data={'error_docs': str(e)})
|
|
|
|
|
|
|
|
|
|
return {'msg': 'setting user info success'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@user.get('/info')
|
2023-09-01 15:59:19 +08:00
|
|
|
|
@user.doc(summary='查询用户信息', description='查询用户信息。注:gender 类型:str, 1 代表男性,2 代表女性')
|
2023-08-23 11:11:15 +08:00
|
|
|
|
@user.input(UserIdIn, location='query')
|
2023-08-15 17:49:05 +08:00
|
|
|
|
@user.output(UserInfoOut)
|
|
|
|
|
@login_required
|
2023-08-23 11:11:15 +08:00
|
|
|
|
def get_user_info(query_data):
|
|
|
|
|
user_id = query_data['user_id'] if query_data.get('user_id') else session['user_id']
|
2023-08-15 17:49:05 +08:00
|
|
|
|
result = {}
|
|
|
|
|
try:
|
2023-08-23 11:11:15 +08:00
|
|
|
|
user_dict = rpc.admin.get_user_info(user_id)
|
2023-08-15 17:49:05 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
raise UserInfoNotFound(extra_data={'error_docs': str(e)})
|
|
|
|
|
|
2023-08-19 12:31:22 +08:00
|
|
|
|
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)})
|
|
|
|
|
|
2023-08-15 17:49:05 +08:00
|
|
|
|
try:
|
2023-08-23 11:11:15 +08:00
|
|
|
|
body_info_dict = rpc.body_record.get_body_info(user_id)
|
2023-08-15 17:49:05 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
raise UserInfoNotFound(extra_data={'error_docs': str(e)})
|
|
|
|
|
|
2023-08-19 12:31:22 +08:00
|
|
|
|
result.update(user_dict)
|
|
|
|
|
if body_info_dict:
|
|
|
|
|
result['height'] = body_info_dict.get('height', None)
|
2023-08-26 12:40:23 +08:00
|
|
|
|
result['gender'] = body_info_dict.get('gender', None)
|
2023-09-01 15:59:19 +08:00
|
|
|
|
result['age'] = body_info_dict.get('age', None)
|
2023-08-23 10:57:14 +08:00
|
|
|
|
if body_info_dict.get('is_hidden_weight', None):
|
|
|
|
|
result['is_hidden_weight'] = True
|
|
|
|
|
else:
|
|
|
|
|
result['is_hidden_weight'] = False
|
2023-08-19 12:31:22 +08:00
|
|
|
|
else:
|
|
|
|
|
result['height'] = None
|
2023-08-23 10:57:14 +08:00
|
|
|
|
result['is_hidden_weight'] = False
|
2023-08-26 12:40:23 +08:00
|
|
|
|
result['gender'] = None
|
2023-09-01 15:59:19 +08:00
|
|
|
|
result['age'] = None
|
2023-08-15 17:49:05 +08:00
|
|
|
|
|
|
|
|
|
return result
|