feat: add logic of avatar image uploading
continuous-integration/drone/push Build is passing Details

This commit is contained in:
BryantHe 2023-08-16 14:15:15 +08:00
parent 0cdee3b226
commit be587a216e
5 changed files with 54 additions and 50 deletions

View File

@ -4,10 +4,43 @@ import platform
import psutil import psutil
from apiflask import APIBlueprint from apiflask import APIBlueprint
from flask import session
from app import rpc
from app.api.v1.exception.api import ImageUploadError, UserInfoError
from app.api.v1.schema.api import ImageIn
from app.util.auth import login_required
api = APIBlueprint('api', __name__) api = APIBlueprint('api', __name__)
@api.post('/images')
@api.doc(summary='上传图片', description='上传图片')
@api.input(ImageIn, location='files')
@login_required
def wechat_login(files):
f = files['image']
try:
result = rpc.storage.upload(file_name='avatar_' + str(session['user_id']),
file_binary=f.stream,
bucket='bodyrecord',
folder='avatar',
app='bodyrecord')
except Exception as e:
raise ImageUploadError(extra_data={'error_docs': str(e)})
if result.get('status') and result['status'] == 'UPLOADED':
try:
rpc.admin.set_user_info(session['user_id'],
avatar_id=result['_id'])
except Exception as e:
raise UserInfoError(extra_data={'error_docs': str(e)})
return {'msg': 'uploading image success'}
else:
return {'msg': 'uploading image fail'}
@api.get('/hello') @api.get('/hello')
def hello(): def hello():
result_one = f'我是您的专属接口提供服务器:' result_one = f'我是您的专属接口提供服务器:'
@ -80,4 +113,3 @@ def hello():
</div> </div>
""" """
return text return text

View File

@ -0,0 +1,11 @@
from apiflask import HTTPError
class ImageUploadError(HTTPError):
status_code = 500
message = '上传图片失败'
class UserInfoError(HTTPError):
status_code = 500
message = '设置用户信息失败'

View File

@ -1,51 +1,12 @@
from apiflask import Schema from apiflask import Schema
from apiflask.fields import Integer, String, Boolean, Nested, List from apiflask.fields import File
class LoginIn(Schema): class ImageIn(Schema):
username = String(required=True) image = File()
password = String(required=True)
tenantId = String(required=True)
uuid = String()
code = String()
class LoginNestedOut(Schema): class FileOut(Schema):
token = String() file = File()
class LoginOut(Schema):
code = Integer()
msg = String()
data = Nested(LoginNestedOut)
class CaptchaImageNestedOut(Schema):
captchaEnabled = Boolean()
img = String()
uuid = String()
class CaptchaImageOut(Schema):
code = Integer()
msg = String()
data = Nested(CaptchaImageNestedOut)
class TenantOut(Schema):
companyName = String()
domain = String()
tenantId = String()
class TenantsNestedOut(Schema):
tenantEnabled = Boolean()
voList = List(Nested(TenantOut))
class TenantsOut(Schema):
code = Integer()
msg = String()
data = Nested(TenantsNestedOut)

View File

@ -4,11 +4,11 @@ from apiflask.fields import String, URL, Float
class UserInfoIn(Schema): class UserInfoIn(Schema):
nickname = String() nickname = String()
avatar_url = URL() avatar_id = String()
height = Float() height = Float()
class UserInfoOut(Schema): class UserInfoOut(Schema):
nickname = String() nickname = String()
avatar_url = URL() avatar_id = String()
height = Float() height = Float()

View File

@ -16,14 +16,14 @@ user = APIBlueprint('user', __name__)
def wechat_login(json_data): def wechat_login(json_data):
try: try:
rpc.admin.set_user_info(session['user_id'], rpc.admin.set_user_info(session['user_id'],
json_data.get('nickname'), json_data.get('nickname', None),
json_data.get('avatar_url')) json_data.get('avatar_id', None))
except Exception as e: except Exception as e:
raise UserInfoError(extra_data={'error_docs': str(e)}) raise UserInfoError(extra_data={'error_docs': str(e)})
try: try:
rpc.body_record.set_body_info(session['user_id'], rpc.body_record.set_body_info(session['user_id'],
height=json_data.get('height')) height=json_data.get('height', None))
except Exception as e: except Exception as e:
raise UserInfoError(extra_data={'error_docs': str(e)}) raise UserInfoError(extra_data={'error_docs': str(e)})