AppAccountManagement.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. from django.db import transaction
  12. from Controller.CheckUserData import RandomStr
  13. from Model.models import Device_User, Device_Info, DeviceSuperPassword, SysMsgModel
  14. from Object.RedisObject import RedisObject
  15. from Object.ResponseObject import ResponseObject
  16. from Object.TokenObject import TokenObject
  17. from django.views import View
  18. from Service.CommonService import CommonService
  19. class AppAccoutView(View):
  20. def get(self, request, *args, **kwargs):
  21. request.encoding = 'utf-8'
  22. operation = kwargs.get('operation')
  23. request_dict = request.GET
  24. return self.validation(request_dict, request, operation)
  25. def post(self, request, *args, **kwargs):
  26. request.encoding = 'utf-8'
  27. operation = kwargs.get('operation')
  28. request_dict = request.GET
  29. return self.validation(request_dict, request, operation)
  30. def validation(self, request_dict, request, operation):
  31. token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
  32. lang = request_dict.get('lang', token.lang)
  33. response = ResponseObject(lang)
  34. userID = token.userID
  35. if operation == 'getAuthorizationCode': # 获取用户请求/生成授权码
  36. return self.getAuthorizationCode(request_dict, response, userID)
  37. if operation == 'customerServiceManagement': # 编辑超级密码请求表
  38. return self.customerServiceManagement(request_dict, response)
  39. if operation == 'getDeviceSuperPassword': # 查询超级密码请求表
  40. return self.getDeviceSuperPassword(request_dict, response)
  41. if operation == 'verifyTheVerificationCode': # 检验验证码
  42. return self.verifyTheVerificationCode(request_dict, response, userID)
  43. if operation == 'deleteInformation': # 删除信息
  44. return self.deleteInformation(request_dict, response)
  45. else:
  46. return response.json(404)
  47. def getAuthorizationCode(self, request_dict, response, userID):
  48. """
  49. @param uid:设备id
  50. @param request_dict:请求参数
  51. @param response:响应对象
  52. @param describe:需求描述
  53. @param Purchase_channel:购买渠道描述
  54. @param orderID:订单id
  55. @param buyTime:购买时间
  56. @return:
  57. """
  58. uid = request_dict.get('uid', None)
  59. describe = request_dict.get('describe', None)
  60. if not all([uid, describe]):
  61. return response.json(444)
  62. purchase_channel = request_dict.get('purchase_channel', None)
  63. orderID = request_dict.get('orderID', None)
  64. buyTime = request_dict.get('buyTime', None)
  65. lang = request_dict.get('lang', 'en')
  66. try:
  67. now = int(time.time())
  68. addTime = now
  69. device_info_qs = Device_Info.objects.filter(UID=uid, userID_id=userID)
  70. if not device_info_qs.exists():
  71. return response.json(173)
  72. if buyTime:
  73. buyTime = datetime.datetime.strptime(buyTime, '%Y-%m-%d')
  74. buyTime = CommonService.str_to_timestamp(str_time=str(buyTime))
  75. DeviceSuperPassword.objects.create(uid=uid, orderID=orderID, describe=describe,
  76. purchase_channel=purchase_channel, addTime=addTime, userID_id=userID,
  77. buyTime=buyTime, status=0, lang=lang)
  78. # 验证码生成
  79. super_code = RandomStr(6, True)
  80. super_password_id = "super_password_%s" % userID
  81. redisObj = RedisObject()
  82. redisObj.set_data(key=super_password_id, val=super_code, expire=86400)
  83. return response.json(0)
  84. except Exception as e:
  85. print('生成验证码异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  86. return response.json(500, repr(e))
  87. def customerServiceManagement(self, request_dict, response):
  88. """
  89. @param request_dict:请求参数
  90. @param response:响应对象
  91. @param hint:提示内容
  92. @return:
  93. """
  94. userID = request_dict.get('userID', None)
  95. uid = request_dict.get('uid', None)
  96. status = request_dict.get('status', None)
  97. hint = request_dict.get('hint', None)
  98. lang = request_dict.get('lang', 'en')
  99. if not all({uid, userID}):
  100. return response.json(444)
  101. now = int(time.time())
  102. try:
  103. with transaction.atomic():
  104. device_super_password_qs = DeviceSuperPassword.objects.filter(uid=uid, userID=userID)
  105. if not device_super_password_qs.exists():
  106. return response.json(173)
  107. status = int(status)
  108. authcode = ''
  109. if status == 1:
  110. device_super_password_qs.update(status=status)
  111. super_password_id = 'super_password_' + userID
  112. redisObj = RedisObject()
  113. # redis里面的验证码
  114. redis_super_code = redisObj.get_data(key=super_password_id)
  115. # 验证用户输入的验证码和redis中的验证码
  116. if redis_super_code is False:
  117. return response.json(120)
  118. authcode = CommonService.encode_data(redis_super_code)
  119. if status == 0 and len(hint) > 1:
  120. device_super_password_qs.update(status=status, hint=hint)
  121. msg = hint
  122. SysMsgModel.objects.create(userID_id=userID, msg=msg, addTime=now, updTime=now, uid=uid,
  123. eventType=2)
  124. return response.json(0)
  125. authcode = CommonService.decode_data(authcode)
  126. if status == 1:
  127. if lang == 'en':
  128. msg = "Your authorization code is " + authcode + ",valid within 24 hours"
  129. else:
  130. msg = "您的授权代码:" + authcode + ",24小时内有效"
  131. SysMsgModel.objects.create(userID_id=userID, msg=msg, addTime=now, updTime=now, uid=uid,
  132. eventType=2)
  133. return response.json(0)
  134. return response.json(177)
  135. except Exception as e:
  136. print('修改状态异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  137. return response.json(500, repr(e))
  138. def getDeviceSuperPassword(self, request_dict, response):
  139. """
  140. @param request_dict:请求参数
  141. @param response:响应对象
  142. @return:
  143. """
  144. pageNo = request_dict.get('pageNo', None)
  145. pageSize = request_dict.get('pageSize', None)
  146. status = request_dict.get('status', None)
  147. userID = request_dict.get('userID', None)
  148. uid = request_dict.get('uid', None)
  149. if not all([pageNo, pageSize]):
  150. return response.json(444)
  151. page = int(pageNo)
  152. line = int(pageSize)
  153. try:
  154. device_super_password_qs = DeviceSuperPassword.objects.all()
  155. if status:
  156. device_super_password_qs = device_super_password_qs.filter(status=status)
  157. if userID:
  158. device_super_password_qs = device_super_password_qs.filter(userID=userID)
  159. if uid:
  160. device_super_password_qs = device_super_password_qs.filter(uid=uid)
  161. if not device_super_password_qs.exists():
  162. return response.json(0, [])
  163. count = device_super_password_qs.count()
  164. device_super_password_qs = device_super_password_qs.values('id',
  165. 'uid',
  166. 'userID',
  167. 'orderID',
  168. 'describe',
  169. 'purchase_channel',
  170. 'addTime',
  171. 'status',
  172. 'buyTime',
  173. 'hint',
  174. 'lang')
  175. device_super_password_qs = device_super_password_qs.order_by('-addTime')[
  176. (page - 1) * line:page * line]
  177. return response.json(0, {'list': list(device_super_password_qs), 'count': count})
  178. except Exception as e:
  179. print('查询异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  180. return response.json(500, repr(e))
  181. def verifyTheVerificationCode(self, request_dict, response, userID):
  182. """
  183. @param request_dict:请求参数
  184. @param response:响应对象
  185. @param userID:用户ID
  186. @param authcode:验证码
  187. @return:
  188. """
  189. authcode = request_dict.get('authcode', None)
  190. if authcode:
  191. authcode = CommonService.decode_data(authcode)
  192. super_password_id = 'super_password_' + userID
  193. redisObj = RedisObject()
  194. # redis里面的验证码
  195. redis_image_code = redisObj.get_data(key=super_password_id)
  196. # 验证用户输入的验证码和redis中的验证码
  197. if redis_image_code is False or authcode.lower() != redis_image_code.lower():
  198. return response.json(121)
  199. else:
  200. return response.json(0)
  201. else:
  202. return response.json(444)
  203. def deleteInformation(self, request_dict, response):
  204. """
  205. @param request_dict:请求参数
  206. @param response:响应对象
  207. @param id:主键
  208. """
  209. id = request_dict.get('id', None)
  210. if not id:
  211. return response.json(444)
  212. device_super_password_qs = DeviceSuperPassword.objects.filter(id=id)
  213. if not device_super_password_qs.exists():
  214. return response.json(173)
  215. device_super_password_qs.delete()
  216. return response.json(0)