ResponseObject.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from django.shortcuts import HttpResponse
  2. import simplejson as json
  3. class ResponseObject(object):
  4. def __init__(self, lang='en'):
  5. self.lang = lang
  6. def data(self, code, res=None):
  7. if res is None:
  8. res = {}
  9. data_en = {
  10. 0: 'Success',
  11. 5: 'Please try again one minute later!',
  12. 10: res,
  13. 12: 'You are not the primary user of the device!',
  14. 14: 'Device is not belong to you',
  15. 15: 'Device has been bound',
  16. 44: 'System error! Can not send email',
  17. 48: 'System object error!',
  18. 89: 'Already send the code, please check it or get it again after 10m',
  19. 90: 'please check code or get it again after 1m',
  20. 99: 'Mail does not exist!',
  21. 100: 'Phone format error!',
  22. 101: 'Phone already existed!',
  23. 102: 'Phone does not exist!',
  24. 103: 'Mail already existed!',
  25. 104: 'Account does not exist!',
  26. 105: 'Email format error!',
  27. 107: 'The username not conform to the rules!',
  28. 109: 'The password not conform to the rules!',
  29. 110: 'user does not activated',
  30. 111: 'Error password',
  31. 119: 'The qr code has expired',
  32. 120: 'The code has expired',
  33. 121: 'The verification code is wrong!',
  34. 173: 'Data does not exists!',
  35. 174: 'Data already exists!',
  36. 176: 'Delete error',
  37. 177: 'Update error',
  38. 178: 'ADD error',
  39. 179: 'Nickname repeated',
  40. 306: 'The link has expired!',
  41. 309: 'Please ReLogin! errmsg token',
  42. 404: 'You don not have permission to access this!',
  43. 414: 'Please confirm the request url!',
  44. 424: 'Database Error !',
  45. 444: 'Wrong parameters!',
  46. 474: 'System Maintaining!',
  47. 475: 'App Version too low, please upgrade!',
  48. 500: 'Query Database Error:',
  49. 700: 'Upload file error',
  50. 701: 'The file does not exist!',
  51. 711: 'Do not downgrade',
  52. 712: 'Area needs to be consistent',
  53. 713: 'Storage rules cannot be changed during the validity period',
  54. 900: 'There is no information about this version!',
  55. 901: 'Getting URL failure!',
  56. 902: 'No update!',
  57. 903: 'Error filename',
  58. 906: 'Cause of file operation error',
  59. 907: 'The download file does not exist!',
  60. }
  61. data_cn = {
  62. 0: '成功',
  63. 5: '请一分钟后再尝试',
  64. 10: res,
  65. 12: '非设备主用户',
  66. 14: '设备不属于您',
  67. 15: '设备已被绑定',
  68. 44: '系统错误!无法发送电子邮件',
  69. 48: '系统对象错误',
  70. 89: '已发验证码,请检测或10分钟后重新获取。',
  71. 90: '请检测或1分钟后重新获取。',
  72. 99: '邮箱不存在!',
  73. 100: '手机格式错误!',
  74. 101: '手机已存在!',
  75. 102: '手机不存在!',
  76. 103: '邮箱已存在!',
  77. 104: '账户不存在!',
  78. 105: '邮箱格式错误!',
  79. 107: '用户名格式不符合!',
  80. 109: '密码格式不符合!',
  81. 110: '用户未激活!',
  82. 111: '密码不正确!',
  83. 119: '二维码过期',
  84. 120: '验证码过期',
  85. 121: '验证码错了!',
  86. 173: '数据不存在!',
  87. 174: '数据已存在!',
  88. 176: '删除错误',
  89. 177: '更新错误',
  90. 178: '增加错误',
  91. 179: '名称不能重复',
  92. 306: '链接已超过有效期!',
  93. 309: '请重新登录!',
  94. 404: '您没有访问的权限!',
  95. 414: '请确认请求url!',
  96. 424: '数据库错误!',
  97. 444: '参数错误!',
  98. 474: '系统维护中!',
  99. 475: '版本过低,请升级程序!',
  100. 500: '查询数据库错误!',
  101. 700: '上传文件错误',
  102. 701: '文件不存在',
  103. 711: '不可降级',
  104. 712: '区域不一致',
  105. 713: '有效期内不可更改存储规则',
  106. 900: '版本信息不存在',
  107. 901: '获取链接失败',
  108. 902: '无更新!',
  109. 903: '文件名不符合!',
  110. 906: '文件操作错误',
  111. 907: '文件不存在!',
  112. }
  113. if self.lang == 'cn':
  114. msg = data_cn
  115. elif self.lang == 'zh-Hans':
  116. msg = data_cn
  117. elif self.lang == 'zh-Hant':
  118. msg = data_cn
  119. else:
  120. msg = data_en
  121. reason = msg.get(code)
  122. if reason is None:
  123. reason = 'code不存在'
  124. return {'result_code': code, 'reason': reason, 'result': res, 'error_code': code}
  125. def formal(self, code, res=None):
  126. if res is None:
  127. res = {}
  128. formal_data = self.data(code, res)
  129. return json.dumps(formal_data, ensure_ascii=False)
  130. def json(self, code, res=None):
  131. if res is None:
  132. res = {}
  133. result = self.formal(code, res)
  134. return HttpResponse(result)