AppAccountManagement.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : UserDeviceShareController.py
  4. @Time : 2023/2/3 13:25
  5. @Author : guanhailong
  6. @Email : guanhailong@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import datetime
  10. import time
  11. import logging
  12. from Model.models import Device_User, DeviceSuperPassword
  13. from Object.RedisObject import RedisObject
  14. from Object.ResponseObject import ResponseObject
  15. from Object.TokenObject import TokenObject
  16. from django.views import View
  17. from Service.CommonService import CommonService
  18. class AppAccoutView(View):
  19. def get(self, request, *args, **kwargs):
  20. request.encoding = 'utf-8'
  21. operation = kwargs.get('operation')
  22. request_dict = request.GET
  23. return self.validation(request_dict, request, operation)
  24. def post(self, request, *args, **kwargs):
  25. request.encoding = 'utf-8'
  26. operation = kwargs.get('operation')
  27. request_dict = request.POST
  28. return self.validation(request_dict, request, operation)
  29. def validation(self, request_dict, request, operation):
  30. tko = TokenObject(
  31. request.META.get('HTTP_AUTHORIZATION'))
  32. lang = request_dict.get('lang', tko.lang)
  33. logger = logging.getLogger('info')
  34. logger.info("传参语言{}".format(lang))
  35. response = ResponseObject(lang)
  36. if tko.code != 0:
  37. return response.json(tko.code)
  38. userID = tko.userID
  39. if operation == 'getAuthorizationCode': # 用户提交请求
  40. return self.getAuthorizationCode(request_dict, response, userID)
  41. if operation == 'verifyTheVerificationCode': # 检验验证码
  42. return self.verifyTheVerificationCode(request_dict, response, userID)
  43. else:
  44. return response.json(404)
  45. @staticmethod
  46. def getAuthorizationCode(request_dict, response, userID):
  47. """
  48. 用户提交请求
  49. @param request_dict:请求参数
  50. @param response:响应对象
  51. @param userID:用户ID
  52. @return:
  53. """
  54. uid = request_dict.get('uid', None)
  55. # 需求描述
  56. describe = request_dict.get('describe', None)
  57. purchase_channel = request_dict.get('purchase_channel', None)
  58. orderID = request_dict.get('orderID', None)
  59. buyTime = request_dict.get('buyTime', None)
  60. lang = request_dict.get('lang', 'en')
  61. if not all([uid, describe]):
  62. return response.json(444, 'uid, describe')
  63. try:
  64. nowTime = int(time.time())
  65. device_user_qs = Device_User.objects.filter(userID=userID)
  66. if not device_user_qs.exists():
  67. return response.json(173)
  68. if buyTime:
  69. buyTime = datetime.datetime.strptime(buyTime, '%Y-%m-%d')
  70. buyTime = CommonService.str_to_timestamp(str_time=str(buyTime))
  71. DeviceSuperPassword.objects.create(uid=uid, orderID=orderID, describe=describe,
  72. purchase_channel=purchase_channel, addTime=nowTime, userID_id=userID,
  73. buyTime=buyTime, status=0, lang=lang)
  74. return response.json(0)
  75. except Exception as e:
  76. print('生成验证码异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  77. return response.json(500, repr(e))
  78. @staticmethod
  79. def verifyTheVerificationCode(request_dict, response, userID):
  80. """
  81. 检验验证码
  82. @param request_dict:请求参数
  83. @param response:响应对象
  84. @param userID:用户ID
  85. @return:
  86. """
  87. authcode = request_dict.get('authcode', None)
  88. if authcode:
  89. authcode = CommonService.decode_data(authcode)
  90. if not len(authcode) == 6:
  91. return response.json(121)
  92. super_password_id = 'super_password_' + userID
  93. redisObj = RedisObject()
  94. # redis里面的验证码
  95. redis_image_code = redisObj.get_data(key=super_password_id)
  96. # 验证用户输入的验证码和redis中的验证码
  97. if redis_image_code is False or authcode.lower() != redis_image_code.lower():
  98. return response.json(121)
  99. else:
  100. return response.json(0)
  101. else:
  102. return response.json(444)