ResponseObject.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from django.shortcuts import HttpResponse
  2. import simplejson as json
  3. class ResponseObject(object):
  4. def __init__(self, lang='cn'):
  5. self.lang = lang
  6. def data(self, code, res=None):
  7. if res is None:
  8. res = {}
  9. data_cn = {
  10. 0: '成功',
  11. 5: '请一分钟后再尝试',
  12. 8: '用户账号已存在',
  13. 9: '用户账号不存在',
  14. 10: res,
  15. 13: '时间戳token校验失败',
  16. 14: '序列号不足',
  17. 42: '两次输入的新密码错误',
  18. 43: '客户端服务器已关闭,请下载新版本使用',
  19. 44: '系统错误,发送邮件失败',
  20. 45: '系统错误,生成令牌出错!',
  21. 46: '系统错误,发送短信失败!',
  22. 47: '旧密码不正确',
  23. 74: '关联旧用户失败!',
  24. 79: '您已经申请过重置密码,请到邮箱进行确认!',
  25. 89: '您已经获得了验证码,请在10分钟后检查或再次确认。',
  26. 99: '账户或密码错误',
  27. 101: '手机的用户账号已经存在!',
  28. 102: '手机的用户账号不存在!',
  29. 103: '邮箱用户帐户已经存在!',
  30. 104: '邮箱用户帐户不存在!',
  31. 107: '用户名格式不符合规则!',
  32. 108: '邮箱格式不符合规则!',
  33. 109: '该账号无法分配!',
  34. 110: '因为用户未激活,用户是无效用户!',
  35. 111: '您输入的密码不正确!',
  36. 120: '验证码已过期或不存在、请重新获得验证码!',
  37. 121: '验证码错了!',
  38. 138: '手机格式不符合规则!',
  39. 173: '数据不存在!',
  40. 174: '数据已存在',
  41. 175: 'mac地址已用完',
  42. 305: '令牌格式是错误的,相关参数是不存在的!',
  43. 307: '令牌已过期!',
  44. 308: '此次下载已失效',
  45. 309: '你没有权限访问',
  46. 404: 'You don not have permission to access this!',
  47. 444: '请确认参数的正确性!',
  48. 500: '内部错误!',
  49. 1112: '您输入的两次密码不一致!',
  50. 208: '只能预定当天的或者以后的!',
  51. }
  52. data_en = {
  53. 0: 'Success',
  54. 5: 'Please try again one minute later!',
  55. 8: 'User accounts already exist',
  56. 9: 'User accounts is not exist',
  57. 10: res,
  58. 13: 'Timestamp token verification failed',
  59. 14: 'Inadequate serial number',
  60. 42: 'The new password entered twice is incorrect',
  61. 43: 'The client server is closed. Please download the new version for use',
  62. 44: 'System error,send email fail!',
  63. 45: 'System error,generate token fail!',
  64. 46: 'System error, sending SMS failed!',
  65. 47: 'Old password is incorrect',
  66. 74: 'Failed to connect old users!',
  67. 79: 'You have applied for reset password, please go to email for confirmation!',
  68. 89: 'You have already obtained the verification code, please check it or get it again after 10 minutes.',
  69. 99: ' ERROR Incorrect account or password',
  70. 101: 'The user account of the mobile phone has already existed!',
  71. 102: 'The user account of the mobile phone does not exist!',
  72. 103: 'The mailbox user account has already existed!',
  73. 104: 'The mailbox user account does not exist!',
  74. 107: 'The username format does not conform to the rules!',
  75. 108: 'The mailbox format does not conform to the rules! ',
  76. 109: 'The account cannot be allocated!',
  77. 110: 'Because the user is not activated, the user is an invalid user!',
  78. 111: 'The password you entered is incorrect!',
  79. 120: 'The captcha has expired or does not exist, please obtain the captcha again!',
  80. 121: 'The verification code is wrong!',
  81. 138: 'The phone format does not conform to the rules! ',
  82. 173: 'Data does not exists!',
  83. 174: 'Data already exists',
  84. 175: 'MAC address has been used up',
  85. 305: 'The Token format is wrong and the related parameter is None!',
  86. 307: 'The Token has expired!',
  87. 308: 'This download has expired!',
  88. 309: 'You have no access',
  89. 404: 'You don not have permission to access this!',
  90. 444: 'Please confirm the correctness of the parameters!',
  91. 500: 'Internal error!',
  92. 1112: 'The two passwords you entered do not match!',
  93. }
  94. if self.lang == 'cn':
  95. msg = data_cn
  96. else:
  97. msg = data_en
  98. reason = msg.get(code)
  99. if reason is None:
  100. reason = 'code不存在'
  101. return {'code': code, 'msg': reason, 'res': res}
  102. def formal(self, code, res=None):
  103. if res is None:
  104. res = {}
  105. formal_data = self.data(code, res)
  106. return json.dumps(formal_data, ensure_ascii=False)
  107. def json(self, code, res=None):
  108. if res is None:
  109. res = {}
  110. result = self.formal(code, res)
  111. return HttpResponse(result)