mirror of
https://github.com/Lost-MSth/Arcaea-server.git
synced 2026-02-05 06:37:28 +08:00
Code refactoring
- Code refactoring mainly for API - Delete a useless option in `setting.py` - Change some constants in Link Play mode
This commit is contained in:
@@ -1,34 +1,32 @@
|
||||
from core.error import ArcError
|
||||
from flask import jsonify
|
||||
|
||||
|
||||
def code_get_msg(code):
|
||||
# api接口code获取msg,返回字符串
|
||||
msg = {
|
||||
0: '',
|
||||
-1: 'See status code',
|
||||
-2: 'No data',
|
||||
-3: 'No data or user',
|
||||
-4: 'No user_id',
|
||||
-100: 'Wrong post data',
|
||||
-101: 'Wrong data type',
|
||||
-102: 'Wrong query parameter',
|
||||
-103: 'Wrong sort parameter',
|
||||
-104: 'Wrong sort order parameter',
|
||||
-201: 'Wrong username or password',
|
||||
-202: 'User is banned',
|
||||
-203: 'Username exists',
|
||||
-204: 'Email address exists',
|
||||
-999: 'Unknown error'
|
||||
}
|
||||
|
||||
return msg[code]
|
||||
default_error = ArcError('Unknown Error')
|
||||
|
||||
|
||||
def return_encode(code: int = 0, data: dict = {}, status: int = 200, msg: str = ''):
|
||||
# 构造返回,返回jsonify处理过后的response_class
|
||||
if msg == '':
|
||||
msg = code_get_msg(code)
|
||||
if code < 0:
|
||||
return jsonify({'status': status, 'code': code, 'data': {}, 'msg': msg})
|
||||
else:
|
||||
return jsonify({'status': status, 'code': code, 'data': data, 'msg': msg})
|
||||
CODE_MSG = {
|
||||
0: '',
|
||||
-1: 'See status code',
|
||||
-2: 'No data',
|
||||
-3: 'No data or user',
|
||||
-4: 'No user_id',
|
||||
-100: 'Wrong post data',
|
||||
-101: 'Wrong data type',
|
||||
-102: 'Wrong query parameter',
|
||||
-103: 'Wrong sort parameter',
|
||||
-104: 'Wrong sort order parameter',
|
||||
-200: 'No permission',
|
||||
-201: 'Wrong username or password',
|
||||
-202: 'User is banned',
|
||||
-203: 'Username exists',
|
||||
-204: 'Email address exists',
|
||||
-999: 'Unknown error'
|
||||
}
|
||||
|
||||
|
||||
def success_return(data: dict = {}, status: int = 200, msg: str = ''):
|
||||
return jsonify({'code': 0, 'data': data, 'msg': msg}), status
|
||||
|
||||
|
||||
def error_return(e: 'ArcError' = default_error, status: int = 200):
|
||||
return jsonify({'code': e.api_error_code, 'data': {} if e.extra_data is None else e.extra_data, 'msg': CODE_MSG[e.api_error_code] if e.message is None else e.message}), status
|
||||
|
||||
Reference in New Issue
Block a user