Przeglądaj źródła

优化序列号绑定uid接口3

locky 9 miesięcy temu
rodzic
commit
d3802c6847
1 zmienionych plików z 25 dodań i 20 usunięć
  1. 25 20
      Controller/SerialNumberController.py

+ 25 - 20
Controller/SerialNumberController.py

@@ -130,8 +130,19 @@ class SerialNumberView(View):
             company_secret = 'ZsKWcxdD'
 
         try:
-            # 判断序列号是否刚解绑,刚解绑1分钟内不能进行绑定
             redisObj = RedisObject()
+            # 查询app是否已扫码,未扫码不能进行绑定
+            # 如果没扫码设置24小时的缓存,扫码后删除缓存
+            scanned_serial_key = serial + 'scanned_serial'
+            if redisObj.get_data(scanned_serial_key):
+                return response.json(5)
+            app_scanned_serial_qs = AppScannedSerial.objects.filter(serial=serial).values('region_country')
+            if not app_scanned_serial_qs.exists():
+                redisObj.set_data(scanned_serial_key, 1, 24*60*60)
+                LOGGER.info(f'{serial}当前序列号未进行APP扫码')
+                return response.json(5)
+
+            # 判断序列号是否刚解绑,刚解绑1分钟内不能进行绑定
             unused_serial_redis_list = redisObj.lrange(UNUSED_SERIAL_REDIS_LIST, 0, -1)
             unused_serial_redis_list = [str(i, 'utf-8') for i in unused_serial_redis_list]
             if serial in unused_serial_redis_list:
@@ -142,6 +153,13 @@ class SerialNumberView(View):
             if serial_unbind_uid_qs.exists():
                 return response.json(5)
 
+            # redis加锁,防止同一个序列号重复绑定
+            key = serial + 'do_attach_uid'
+            is_lock = redisObj.CONN.setnx(key, 1)
+            if not is_lock:
+                return response.json(5)
+            redisObj.CONN.expire(key, 60)
+
             # 判断序列号是否已和企业关联
             company_serial_qs = CompanySerialModel.objects.filter(company__secret=company_secret, serial_number=serial)
             if not company_serial_qs.exists():
@@ -155,20 +173,8 @@ class SerialNumberView(View):
                 if is_verify == '1':
                     region_country = 0
                 else:
-                    # 查询app是否已扫码,未扫码不能进行绑定
-                    app_scanned_serial_qs = AppScannedSerial.objects.filter(serial=serial).values('region_country')
-                    if not app_scanned_serial_qs.exists():
-                        LOGGER.info(f'{serial}当前序列号未进行APP扫码')
-                        return response.json(5)
                     region_country = app_scanned_serial_qs[0]['region_country']
 
-                # redis加锁,防止同一个序列号重复绑定
-                key = serial + 'do_attach_uid'
-                is_lock = redisObj.CONN.setnx(key, 1)
-                if not is_lock:
-                    return response.json(5)
-                redisObj.CONN.expire(key, 60)
-
                 # 获取并判断region_id
                 region_id = CommonService.confirm_region_id(region_country)
                 if region_id not in REGION_ID_LIST:
@@ -216,10 +222,6 @@ class SerialNumberView(View):
                         company_serial.update_time = now_time
                         company_serial.save()
 
-                        dev = Device_Info.objects.filter(UID=uid.uid)
-                        if dev.exists():
-                            dev.update(serial_number=full_serial)
-
                         full_uid_code = uid.full_uid_code
                         if uid.platform in CRCKey.keys():
                             full_uid_code += ':' + CRCKey[uid.platform]
@@ -277,9 +279,7 @@ class SerialNumberView(View):
                     return response.json(10042)
                 return response.json(0, self.get_uid_info_by_serial(company_serial.id))
         except Exception as e:
-            djangoLogger = logging.getLogger('django')
-            djangoLogger.exception(repr(e))
-            return response.json(176, str(e))
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     @classmethod
     def get_uid_info_by_serial(cls, company_serial_id):
@@ -660,6 +660,11 @@ class SerialNumberView(View):
                 data['add_time'] = now_time
                 AppScannedSerial.objects.create(**data)
 
+            # 扫码后删除缓存
+            redis_obj = RedisObject()
+            scanned_serial_key = serial + 'scanned_serial'
+            redis_obj.del_data(scanned_serial_key)
+
             if company_serial.status == 0 or company_serial.status == 1:  # 未使用
                 UnicomComboView().is_4g_device(serial_number, request_dict, request)
                 return response.json(173)