ResponseObject.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. 5: 'Please try again one minute later!',
  11. 10: res,
  12. 12: 'You are not the primary user of the device!',
  13. 14: 'Device is not belong to you',
  14. 44: 'System error! Can not send email',
  15. 48: 'System object error!',
  16. 89: 'Already send the code, please check it or get it again after 10m',
  17. 90: 'please check code or get it again after 5m',
  18. 100: 'Phone format error!',
  19. 101: 'Phone already existed!',
  20. 102: 'Phone doesn\'t exist!',
  21. 103: 'Mail already existed!',
  22. 104: 'Account doesn\'t exist!',
  23. 105: 'Email format error!',
  24. 107: 'The username not conform to the rules!',
  25. 108: 'Email format error!',
  26. 109: 'The password not conform to the rules!',
  27. 110: 'user doesn\'t activated',
  28. 111: 'Error password',
  29. 120: 'The code has expired',
  30. 121: 'The verification code is wrong!',
  31. 173: 'Data does not exists!',
  32. 174: 'Data already exists!',
  33. 176: 'Delete error',
  34. 177: 'Update error',
  35. 178: 'ADD error',
  36. 179: 'Nickname repeated',
  37. 306: 'The link has expired!',
  38. 309: 'Please ReLogin! errmsg token',
  39. 404: 'You don not have permission to access this!',
  40. 414: 'Please confirm the request url!',
  41. 424: 'Database Error !',
  42. 444: 'Wrong parameters!',
  43. 474: 'System Maintaining!',
  44. 500: 'Query Database Error:',
  45. 700: 'Upload file error',
  46. 701: 'The file does not exist!',
  47. 900: 'There is no information about this version!',
  48. 901: 'Getting URL failure!',
  49. 902: 'No update!',
  50. 903: 'Error filename',
  51. 906: 'Cause of file operation error',
  52. 907: 'The download file does not exist!',
  53. }
  54. data_cn = {
  55. 0: '成功',
  56. 5: '请一分钟后再尝试',
  57. 10: res,
  58. 12: '您不是设备主用户',
  59. 14: '设备不属于您',
  60. 44: '系统错误!无法发送电子邮件',
  61. 48: '系统对象错误',
  62. 89: '已发验证码,请检测或10分钟后重新获取。',
  63. 90: '请检测或5分钟后重新获取。',
  64. 100: '手机格式错误!',
  65. 101: '手机已存在!',
  66. 102: '手机不存在!',
  67. 103: '邮箱已存在!',
  68. 104: '账户不存在!',
  69. 105: '邮箱格式错误!',
  70. 107: '用户名格式不符合!',
  71. 108: '邮箱格式错误!',
  72. 109: '密码格式不符合!',
  73. 110: '用户未激活!',
  74. 111: '密码不正确!',
  75. 120: '验证码过期',
  76. 121: '验证码错了!',
  77. 173: '数据不存在!',
  78. 174: '数据已存在!',
  79. 176: '删除错误',
  80. 177: '更新错误',
  81. 178: '增加错误',
  82. 179: '名称不能重复',
  83. 306: '链接已超过有效期!',
  84. 309: '请重新登录!',
  85. 404: '您没有访问的权限!',
  86. 414: '请确认请求url!',
  87. 424: '数据库错误!',
  88. 444: '参数错误!',
  89. 474: '系统维护中!',
  90. 500: '查询数据库错误!',
  91. 700: '上传文件错误',
  92. 701: '文件不存在',
  93. 900: '版本信息不存在',
  94. 901: '获取链接失败',
  95. 902: '无更新!',
  96. 903: '文件名不符合!',
  97. 906: '文件操作错误',
  98. 907: '文件不存在!',
  99. }
  100. if self.lang == 'cn':
  101. msg = data_cn
  102. else:
  103. msg = data_en
  104. try:
  105. message = msg[code]
  106. except Exception as e:
  107. message = '系统错误,code不存在'
  108. return {'result_code': code, 'reason': message, 'result': res, 'error_code': code}
  109. def formal(self, code, res={}):
  110. formal_data = self.data(code, res)
  111. return json.dumps(formal_data, ensure_ascii=False)
  112. def json(self, code, res={}):
  113. result = self.formal(code, res)
  114. return HttpResponse(result)