diff --git a/app/api/v1/api.py b/app/api/v1/api.py index 0b1191d..aedddb5 100644 --- a/app/api/v1/api.py +++ b/app/api/v1/api.py @@ -4,10 +4,43 @@ import platform import psutil 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.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') def hello(): result_one = f'我是您的专属接口提供服务器:' @@ -80,4 +113,3 @@ def hello(): """ return text - diff --git a/app/api/v1/exception/api.py b/app/api/v1/exception/api.py new file mode 100644 index 0000000..53ab0f7 --- /dev/null +++ b/app/api/v1/exception/api.py @@ -0,0 +1,11 @@ +from apiflask import HTTPError + + +class ImageUploadError(HTTPError): + status_code = 500 + message = '上传图片失败' + + +class UserInfoError(HTTPError): + status_code = 500 + message = '设置用户信息失败' diff --git a/app/api/v1/schema/api.py b/app/api/v1/schema/api.py index f9cc113..c4a7970 100644 --- a/app/api/v1/schema/api.py +++ b/app/api/v1/schema/api.py @@ -1,51 +1,12 @@ from apiflask import Schema -from apiflask.fields import Integer, String, Boolean, Nested, List +from apiflask.fields import File -class LoginIn(Schema): - username = String(required=True) - password = String(required=True) - tenantId = String(required=True) - uuid = String() - code = String() +class ImageIn(Schema): + image = File() -class LoginNestedOut(Schema): - token = String() - - -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) +class FileOut(Schema): + file = File() diff --git a/app/api/v1/schema/user.py b/app/api/v1/schema/user.py index 6db386d..42dc4ca 100644 --- a/app/api/v1/schema/user.py +++ b/app/api/v1/schema/user.py @@ -4,11 +4,11 @@ from apiflask.fields import String, URL, Float class UserInfoIn(Schema): nickname = String() - avatar_url = URL() + avatar_id = String() height = Float() class UserInfoOut(Schema): nickname = String() - avatar_url = URL() + avatar_id = String() height = Float() \ No newline at end of file diff --git a/app/api/v1/user.py b/app/api/v1/user.py index c43e671..19ea349 100644 --- a/app/api/v1/user.py +++ b/app/api/v1/user.py @@ -16,14 +16,14 @@ user = APIBlueprint('user', __name__) def wechat_login(json_data): try: rpc.admin.set_user_info(session['user_id'], - json_data.get('nickname'), - json_data.get('avatar_url')) + json_data.get('nickname', None), + json_data.get('avatar_id', 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')) + height=json_data.get('height', None)) except Exception as e: raise UserInfoError(extra_data={'error_docs': str(e)})