Browse Source

设备绑定新增时间戳校验

zhangdongming 3 years ago
parent
commit
2273e34118
1 changed files with 60 additions and 35 deletions
  1. 60 35
      Controller/UnicomCombo/UnicomComboController.py

+ 60 - 35
Controller/UnicomCombo/UnicomComboController.py

@@ -102,8 +102,13 @@ class UnicomComboView(View):
         """
         iccid = request_dict.get('iccid', None)
         serial_no = request_dict.get('serialNo', None)
-        if not all([iccid, serial_no]):
+        time_stamp = request_dict.get('timeStamp', None)
+        sign = request_dict.get('sign', None)
+        if not all([iccid, serial_no, sign, time_stamp]):
             return response.json(444)
+            # 时间戳token校验
+        if not CommonService.check_time_stamp_token(sign, time_stamp):
+            return response.json(13)
         n_time = int(time.time())
         try:
             with transaction.atomic():
@@ -142,40 +147,40 @@ class UnicomComboView(View):
         """
         logger = logging.getLogger('info')
         try:
-            now_time = int(time.time())
-            unicom_device_info_qs = UnicomDeviceInfo.objects.filter(iccid=iccid)
-            if not unicom_device_info_qs.exists():
-                return False
-            unicom_device_info_qs = unicom_device_info_qs.first()
-            if unicom_device_info_qs.status != 1:
-                logger.info('用户激活iccid={},业务系统状态为{}'.format(iccid, unicom_device_info_qs.status))
-                return False
-            # 联通业务逻辑
-            unicom_api = UnicomObjeect()
-            re_data = {'iccid': iccid}
-            result = unicom_api.query_device_status(**re_data)
-            res_dict = unicom_api.get_text_dict(result)
-            # 状态不等于1(激活)时进行激活 1:激活;2:停用
-            if res_dict['data']['status'] != 1:
-                re_data = {"iccid": iccid, "status": 1}
-                unicom_api.update_device_state(**re_data)
-            # 查看是否体验过免费套餐
-            experience_history_qs = UnicomComboExperienceHistory.objects.filter(iccid=iccid)
-            if not experience_history_qs.exists():
-                logger.info('用户首次激活iccid={}'.format(iccid))
-                combo_qs = UnicomCombo.objects.filter(combo_type=1, status=0, is_del=False) \
-                    .values('id', 'expiration_type', 'expiration_days', 'combo_type')
-                if combo_qs.exists():
-                    combo_qs = combo_qs.first()
-                    # 保存体验记录
-                    experience_history_vo = {'iccid': iccid, 'experience_type': 0, 'do_time': now_time}
-                    UnicomComboExperienceHistory.objects.create(**experience_history_vo)
-                    # 保存套餐激活信息
-                    cls.create_combo_order_info('', 0, iccid, combo_qs['id'])
-            # 修改业务联通卡设备激活信息
-            UnicomDeviceInfo.objects.filter(iccid=iccid).update(status=2, updated_time=now_time)
-
-            return True
+            while transaction.atomic():
+                now_time = int(time.time())
+                unicom_device_info_qs = UnicomDeviceInfo.objects.filter(iccid=iccid)
+                if not unicom_device_info_qs.exists():
+                    return False
+                unicom_device_info_qs = unicom_device_info_qs.first()
+                if unicom_device_info_qs.status != 1:
+                    logger.info('用户激活iccid={},业务系统状态为{}'.format(iccid, unicom_device_info_qs.status))
+                    return False
+                # 联通业务逻辑
+                unicom_api = UnicomObjeect()
+                re_data = {'iccid': iccid}
+                result = unicom_api.query_device_status(**re_data)
+                res_dict = unicom_api.get_text_dict(result)
+                # 状态不等于1(激活)时进行激活 1:激活;2:停用
+                if res_dict['data']['status'] != 1:
+                    re_data = {"iccid": iccid, "status": 1}
+                    unicom_api.update_device_state(**re_data)
+                # 查看是否体验过免费套餐
+                experience_history_qs = UnicomComboExperienceHistory.objects.filter(iccid=iccid)
+                if not experience_history_qs.exists():
+                    logger.info('用户首次激活iccid={}'.format(iccid))
+                    combo_qs = UnicomCombo.objects.filter(combo_type=1, status=0, is_del=False) \
+                        .values('id', 'expiration_type', 'expiration_days', 'combo_type')
+                    if combo_qs.exists():
+                        combo_qs = combo_qs.first()
+                        # 保存体验记录
+                        experience_history_vo = {'iccid': iccid, 'experience_type': 0, 'do_time': now_time}
+                        UnicomComboExperienceHistory.objects.create(**experience_history_vo)
+                        # 保存套餐激活信息
+                        cls.create_combo_order_info('', 0, iccid, combo_qs['id'])
+                # 修改业务联通卡设备激活信息
+                UnicomDeviceInfo.objects.filter(iccid=iccid).update(status=2, updated_time=now_time)
+                return True
         except Exception as e:
             print(e)
             return False
@@ -536,3 +541,23 @@ class UnicomComboView(View):
         month_end = LocalDateTimeUtil.get_cur_month_end()
         start_time, month_end_time = LocalDateTimeUtil.get_start_and_end_time(month_end, '%Y-%m-%d')
         return zero_today, month_end_time
+
+    @classmethod
+    def get_test_sign(cls, request_dict, response):
+        """
+        测试盐加密解密
+        @param request_dict:
+        @param response:
+        @return:
+        """
+        verify = request_dict.get('verify', False)
+        if verify:
+            sign = request_dict.get('sign')
+            time_stamp = request_dict.get('timeStamp')
+            sign = CommonService.check_time_stamp_token(sign, time_stamp)
+            if not sign:
+                return response.json(13)
+            return response.json(0)
+        now_time = int(time.time())
+        sign = CommonService.encode_data(str(now_time))
+        return response.json(0, {'sign': sign, 'timeStamp': now_time})