ResponseObject.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. from django.shortcuts import HttpResponse
  2. import simplejson as json
  3. # 后面尽量尽量使用这个JSON响应类,模板未Controller/OauthManage.py
  4. class ResponseObject(object):
  5. def __init__(self, lang='en'):
  6. self.lang = lang
  7. def data(self, code, res={}):
  8. data_en = {
  9. 0: 'Success',
  10. 4: 'Operation failed',
  11. 5: 'Please try again one minute later!',
  12. 9: 'User accounts is not exist',
  13. 10: res,
  14. 13: 'Device is not exist !',
  15. 14: 'Device is not belong to you',
  16. 15: 'Not own service',
  17. 44: 'System error! Can not send email',
  18. 48: 'System object error!',
  19. 60: 'The app info has already exist',
  20. 89: 'Already send the code, please check it or get it again after 5m',
  21. 101: 'Phone already existed!',
  22. 102: 'Phone doesn\'t exist!',
  23. 103: 'Mail already existed!',
  24. 104: 'Mail doesn\'t exist!',
  25. 106: 'Create User Error',
  26. 107: 'The username not conform to the rules!',
  27. 109: 'The password not conform to the rules!',
  28. 110: 'user is not activated',
  29. 111: 'Error password',
  30. 112: 'update password error!',
  31. 113: 'This userID does not exist!',
  32. 114: 'Delete user failure error',
  33. 115: 'The userID of the user to delete does not exist!',
  34. 118: 'You do not enter the password two times!',
  35. 120: 'The code has expired',
  36. 121: 'The verification code is wrong!',
  37. 150: 'The superID is None or superID is equal to userID',
  38. 160: 'The All Equipment has been shared by the User!',
  39. 171: 'Delete Equipment sharing failed',
  40. 172: 'There is no Equipment for this User!',
  41. 173: 'The UID does not exist!',
  42. 174: 'The user equipment has already existed!',
  43. 175: 'The user equipment has already binding!',
  44. 176: 'Delete equipment error',
  45. 177: 'Update equipment error',
  46. 178: 'ADD equipment error',
  47. 179: 'Device nickname cannot be repeated',
  48. 180: 'UID specification error',
  49. 190: 'The rid does not exist!',
  50. 191: 'The roleName does not exist!',
  51. 192: 'Delete role error',
  52. 193: 'role can not be deleted!',
  53. 194: 'Update role error',
  54. 195: 'role can not be updated!',
  55. 302: 'Please ReLogin! Update Token to database error',
  56. 307: 'Please ReLogin! The Token has expired!',
  57. 310: 'Please ReLogin! The userID in the token is None!',
  58. 311: 'Please ReLogin! The token is None!',
  59. 403: 'API ERROR!',
  60. 404: 'You don not have permission to access this!',
  61. 414: 'Client error ! Please confirm the request path!',
  62. 424: 'Database Error !',
  63. 444: 'Wrong parameters!',
  64. 500: 'Query Database Error:',
  65. 501: 'Update database error!',
  66. 502: 'The fields do not exist!',
  67. 600: 'Query permission denied!',
  68. 601: 'Delete permission denied!',
  69. 602: 'Modify permission denied!',
  70. 603: 'Permission denied!',
  71. 604: 'Query permission denied!',
  72. 605: 'Upload permission denied! ',
  73. 606: 'User has not assigned role!',
  74. 607: 'Add permission denied!',
  75. 611: 'Add permission denied!',
  76. 612: 'Query permission denied!',
  77. 615: 'SuperAdmin does not exist!',
  78. 616: 'User does not exist!',
  79. 800: 'Request KEY is None!',
  80. 801: 'Wrong request KEY!',
  81. 802: 'Parameter error!',
  82. 803: 'Wrong parameter',
  83. 804: 'Share Error! content or sharedAll must be provided!',
  84. 805: 'Un share Error! content or unsharedAll must be provided!',
  85. 806: 'The content parameter is incomplete!',
  86. 807: 'The role has already existed!',
  87. 808: 'Add a new role to the wrong',
  88. 816: 'Add a new permissions error',
  89. 817: 'The permissions do not exist!',
  90. 818: 'The deletion of the permissions failed',
  91. 700: 'Upload file error',
  92. 701: 'The file does not exist!',
  93. 900: 'There is no information about this version!',
  94. 901: 'Getting URL failure!',
  95. 902: 'The code does not exist!',
  96. 903: 'The name of the file does not meet the conditions!',
  97. 906: 'Cause of file operation error',
  98. 907: 'The download file does not exist!',
  99. 910: 'There are too many downloads. Please wait a moment...',
  100. }
  101. data_cn = {
  102. 0: '成功',
  103. 4: '操作失败',
  104. 5: '请一分钟后再尝试',
  105. 9: '用户账号不存在',
  106. 10: res,
  107. 13: '该设备不存在',
  108. 14: '该设备不属于您',
  109. 15: '您尚未打开此服务',
  110. 44: '系统错误!无法发送电子邮件',
  111. 48: '系统对象错误',
  112. 60: 'APP信息已经存在',
  113. 89: '您已经获得了验证码,请在5分钟后检查或再次确认。',
  114. 101: '手机的用户账号已经存在!',
  115. 102: '手机的用户账号不存在!',
  116. 103: '邮箱用户帐户已经存在!',
  117. 104: '邮箱用户帐户不存在!',
  118. 106: '创建用户错误',
  119. 107: '用户名格式不符合规则!',
  120. 109: '密码格式不符合规则!',
  121. 110: '因为用户未激活,用户是无效用户!',
  122. 111: '您输入的密码不正确!',
  123. 112: '未知错误不能更新密码!',
  124. 113: '此用户标识不存在!',
  125. 114: '删除用户失败错误',
  126. 115: '删除用户的用户名不存在!',
  127. 118: '你不输入密码两次!',
  128. 120: '验证码过期',
  129. 121: '验证码错了!',
  130. 150: '超级用户不存在或者用户id错误',
  131. 160: '所有设备已被用户共享!',
  132. 171: '删除设备共享失败',
  133. 172: '这个用户没有设备!',
  134. 173: '设备UID不存在!',
  135. 174: '用户设备已经存在!',
  136. 175: '用户设备已经绑定了!',
  137. 176: '删除设备错误',
  138. 177: '更新设备错误',
  139. 178: '添加设备错误',
  140. 179: '设备名称不能重复',
  141. 180: 'UID规格错误',
  142. 190: '角色rid不存在!',
  143. 191: '角色名不存在!',
  144. 192: '删除角色错误',
  145. 193: '未知的错误导致角色不能被删除!',
  146. 194: '更新角色错误',
  147. 195: '未知的错误导致角色无法更新!',
  148. 302: '更新令牌到数据库错误',
  149. 307: '令牌已经过期!',
  150. 310: '令牌中的用户名不存在!',
  151. 311: '令牌为空!',
  152. 403: 'API错误!',
  153. 404: '您没有访问的权限!',
  154. 414: '客户端错误!请确认请求路径!',
  155. 424: '数据库错误!',
  156. 444: '请确认参数的正确性!',
  157. 500: '查询数据库错误!',
  158. 501: '更新数据库错误!',
  159. 502: '在数据库中,参数字段不存在!',
  160. 600: '您没有权限查询所有用户信息!',
  161. 601: '您没有权限删除用户!',
  162. 602: '您没有修改用户信息的权限!',
  163. 603: '用户帐户没有权限执行此操作!',
  164. 604: '您没有权限查询所有用户设备信息!',
  165. 605: '你没有上传OTA文件的权限!',
  166. 606: '用户没有分配角色!',
  167. 607: '用户没有添加新角色的权限!',
  168. 611: '您没有添加新权限的权限!',
  169. 612: '您没有查询所有用户的权限的权限!',
  170. 615: '超级管理员(USEID)帐户不存在!',
  171. 616: '用户(USERID)帐户不存在!',
  172. 800: '请求参数不存在!',
  173. 801: '错误的请求参数!',
  174. 802: 'content参数错误!',
  175. 803: 'content参数格式错误',
  176. 804: 'content或sharedAll参数没有传递',
  177. 805: 'content或unsharedAll参数没有传递',
  178. 806: 'content参数不完整!',
  179. 807: '角色已经存在!',
  180. 808: '添加新角色错误',
  181. 816: '新增权限错误',
  182. 817: '权限不存在',
  183. 818: '删除权限错误',
  184. 700: '上传文件错误',
  185. 701: '文件不存在',
  186. 900: '版本信息不存在',
  187. 901: '获取链接失败',
  188. 902: 'code不存在!',
  189. 903: '文件名不符合条件!',
  190. 906: '文件操作错误',
  191. 907: '下载文件不存在!',
  192. 910: '下载太多了。请稍等…',
  193. }
  194. if self.lang == 'cn':
  195. msg = data_cn
  196. else:
  197. msg = data_en
  198. try:
  199. message = msg[code]
  200. except Exception as e:
  201. message = '系统错误,code不存在'
  202. return {'result_code': code, 'reason': message, 'result': res, 'error_code': code}
  203. def formal(self, code, res={}):
  204. formal_data = self.data(code, res)
  205. return json.dumps(formal_data,ensure_ascii=False)
  206. def json(self, code, res={}):
  207. result = self.formal(code, res)
  208. return HttpResponse(result)