from enum import Enum class SysCodes(Enum): SUCCESS = ("0000", "操作成功") PARAM_ERROR = ("1006", "参数错误") URI_NOT_FOUND = ("240", "URI不存在") URI_EXISTS = ("241", "URI已经存在") SERVICE_ERROR = ("320", "服务的路由异常,请联系管理员") TIMEOUT = ("998", "处理超时,服务配置出错,请联系管理员") LOGIN_TIMEOUT = ("9998", "登录超时,请重新登录") LOGIN_FAIL = ("401", "登录超时,请重新登录") OPERATE_FAIL = ("1005", "操作失败") USER_EXISTS = ("1007", "用户已经存在") USER_ID_ERROR = ("1009", "用户ID参数错误") USER_NOT_EXISTS = ("1010", "用户不存在或密码不匹配") USER_EMAIL_ERROR = ("1011", "用户邮箱地址错误") USER_EMAIL_EXISTS = ("1012", "邮箱地址已经存在") USER_PHONE_ERROR = ("1013", "手机号码格式不正确") USER_PHONE_EXISTS = ("1014", "手机号码已经存在") LOGIN_ERROR = ("1020", "登录验证出错") USER_NOT_LOGIN = ("1021", "用户没有登录") USER_STATUS_ERROR = ("1022", "用户状态不正常") SEND_CODE_FAIL = ("1023", "发送验证码失败") CHECK_CODE_FAIL = ("1024", "验证码验证失败") USER_NO_AUTHORITY = ("1025", "用户没有权限") USER_GROUP_EXISTS = ("1030", "用户组已经存在") USER_GROUP_NOT_EXISTS = ("1031", "用户组不经存在") OPERATE_ERROR = ("1999", "操作出错") FRONT_ERROR = ("886000", "前端处理验证数据出错") DELETE_CONTENT = ("2012", "请先删除内容") FILE_NOT_FOUND = ("3000", "文件不存在") FILE_SIZE_EXCEEDED = ("3001", "文件大小超出限制") FILE_UPLOAD_FAILED = ("3002", "文件上传失败") REQUEST_FAILED = ("3003", "请求失败") NONE = ("9999", "异常错误") def __init__(self, code, msg): self._value_ = code self._msg = msg @property def code(self): return self._value_ @property def msg(self): return self._msg class Result: code:str = "" msg:str = "" data:dict = None success:bool = False def __init__(self, code:str, msg:str, data:object, success:bool, sys_codes: SysCodes = None) -> None: if sys_codes: self.code = sys_codes.code self.msg = sys_codes.msg else: self.code = code self.msg = msg self.data = data self.success = success def to_dict(self): return { "code": self.code, "msg": self.msg, "data": self.data, "success": self.success } @staticmethod def ok(data=None): return Result("0000", "操作成功", data, True) @staticmethod def error(sys_codes: SysCodes = None): if sys_codes: return Result(sys_codes.code, sys_codes.msg, None, False) return Result.error(SysCodes.NONE)