AppAccountManagement.py 10 KB

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