AppAccountManagement.py 10 KB

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