DeviceVersionInfoController.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : DeviceVersionInfoController.py
  4. @Time : 2024/11/20 14:20
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import json
  10. from django.http import QueryDict
  11. from django.views import View
  12. from Ansjer.config import LOGGER
  13. from Model.models import DeviceVersionInfo, Device_Info
  14. from Object.Enums.RedisKeyConstant import RedisKeyConstant
  15. from Object.RedisObject import RedisObject
  16. from Object.ResponseObject import ResponseObject
  17. from Object.TokenObject import TokenObject
  18. from Service.CommonService import CommonService
  19. class DeviceVersionInfoView(View):
  20. def get(self, request, *args, **kwargs):
  21. request.encoding = 'utf-8'
  22. operation = kwargs.get('operation')
  23. return self.validation(request.GET, request, operation)
  24. def post(self, request, *args, **kwargs):
  25. request.encoding = 'utf-8'
  26. operation = kwargs.get('operation')
  27. return self.validation(request.POST, request, operation)
  28. def delete(self, request, *args, **kwargs):
  29. request.encoding = 'utf-8'
  30. operation = kwargs.get('operation')
  31. delete = QueryDict(request.body)
  32. if not delete:
  33. delete = request.GET
  34. return self.validation(delete, request, operation)
  35. def put(self, request, *args, **kwargs):
  36. request.encoding = 'utf-8'
  37. operation = kwargs.get('operation')
  38. put = QueryDict(request.body)
  39. return self.validation(put, request, operation)
  40. def validation(self, request_dict, request, operation):
  41. response = ResponseObject('cn')
  42. tko = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
  43. if tko.code != 0:
  44. return response.json(tko.code)
  45. response.lang = tko.lang
  46. userID = tko.userID
  47. if operation == 'getInfo':
  48. return self.get_device_version_info(userID, request, request_dict, response)
  49. elif operation == 'validateUserDevice':
  50. return self.validateUserDevice(userID, request, request_dict, response)
  51. else:
  52. return response.json(414)
  53. @classmethod
  54. def get_device_version_info(cls, user_id, request, request_dict, response):
  55. # 从请求字典中获取uid和version
  56. uid = request_dict.get('uid')
  57. version = request_dict.get('version')
  58. # 检查version是否存在
  59. if not version:
  60. return response.json(444) # 错误代码:没有提供version
  61. try:
  62. # 示例输入字符串
  63. ip = CommonService.get_ip_address(request)
  64. LOGGER.info(f'获取设备版本配置信息 user: {user_id}, uid: {uid}, version: {version}, ip: {ip} ')
  65. # 从右侧分割字符串,获取版本和设备代码
  66. ver, d_code = version.rsplit('.', 1) # 使用从右到左分割
  67. ver = ver.replace('V', '') # 移除版本前的'V'
  68. # 创建Redis对象
  69. redis = RedisObject()
  70. # 构建Redis键
  71. version_key = RedisKeyConstant.ZOSI_DEVICE_VERSION_INFO.value + ver + d_code
  72. # 尝试从Redis中获取数据
  73. version_info = redis.get_data(version_key)
  74. if version_info:
  75. # 如果在Redis中找到了数据,直接返回
  76. return response.json(0, json.loads(version_info))
  77. # 从数据库查询设备版本信息
  78. device_version_qs = DeviceVersionInfo.objects.filter(d_code=d_code, software_ver=ver).values()
  79. if not device_version_qs.exists():
  80. return response.json(173) # 错误代码:未找到设备版本信息
  81. # 从QuerySet中获取设备信息
  82. device_info = device_version_qs
  83. # 将设备信息序列化为JSON
  84. device_json = json.dumps(device_info[0])
  85. # 将数据写入Redis,以便后续使用
  86. redis.set_data(version_key, device_json, 60 * 60 * 24) # 设置TTL为24小时
  87. # 返回设备信息
  88. return response.json(0, device_info[0])
  89. except Exception as e:
  90. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  91. @classmethod
  92. def validateUserDevice(cls, user_id, request, request_dict, response):
  93. """
  94. 验证用户设备
  95. @param user_id: 用户id
  96. @param request: 请求
  97. @param request_dict: 请求参数
  98. @param response: 响应参数
  99. @return: 成功 0 失败 173
  100. """
  101. uid = request_dict.get('uid')
  102. serial_number = request_dict.get('serialNumber')
  103. app_bundle_id = request_dict.get('appBundleId')
  104. m_code = request_dict.get('mCode')
  105. if not uid:
  106. return response.json(444) # 错误代码:没有提供uid
  107. try:
  108. LOGGER.info(f'直播验证用户uid:{uid},user:{user_id},m_code:{m_code}')
  109. redis = RedisObject(3)
  110. # 构建Redis键
  111. device_key = f"{RedisKeyConstant.BASIC_USER.value}{user_id}:UID:{uid}"
  112. # 尝试从Redis中获取数据
  113. device_info = redis.get_data(device_key)
  114. # 检查缓存命中和UID匹配
  115. if device_info == uid:
  116. return response.json(0, uid) # 缓存命中返回 0
  117. ip = CommonService.get_ip_address(request)
  118. LOGGER.info(f'直播验证用户uid:{uid},ip:{ip},serial:{serial_number},app{app_bundle_id}')
  119. # 从数据库查询设备版本信息
  120. try:
  121. device_qs = Device_Info.objects.filter(userID_id=user_id, UID=uid).values('UID')
  122. if not device_qs.exists() or uid != device_qs.first().get('UID'):
  123. LOGGER.error(f'验证用户uid错误,未找到设备信息 uid:{uid}')
  124. return response.json(173) # 错误代码:未找到设备信息
  125. # 将数据写入Redis,以便后续使用,设置TTL为48小时
  126. redis.set_data(device_key, uid, 60 * 60 * 24 * 2)
  127. return response.json(0, uid) # 返回设备信息
  128. except Exception as db_exception:
  129. LOGGER.error(f'数据库查询失败 user:{user_id}, uid:{uid}, error: {repr(db_exception)}')
  130. return response.json(500, '数据库查询错误')
  131. except Exception as e:
  132. error_line = e.__traceback__.tb_lineno
  133. LOGGER.error(f'验证用户uid异常 user:{user_id}, uid:{uid}, error_line:{error_line}, error_msg:{repr(e)}')
  134. return response.json(500, f'error_line:{error_line}, error_msg:{repr(e)}')