peng 2 лет назад
Родитель
Сommit
f8b61934e7

+ 10 - 6
Controller/DetectController.py

@@ -43,16 +43,19 @@ class NotificationView(View):
         n_time = request_dict.get('n_time', None)
         event_type = request_dict.get('event_type', None)
         is_st = request_dict.get('is_st', None)
+        uid = request_dict.get('uid', None)
         if not all([channel, n_time]):
             return JsonResponse(status=200, data={'code': 444, 'msg': 'error channel or n_time'})
         try:
-            uid = DevicePushService.decode_uid(etk, uidToken)  # 解密uid
+            if not uid:
+                uid = DevicePushService.decode_uid(etk, uidToken)  # 解密uid
             if len(uid) != 20 and len(uid) != 14:
                 return JsonResponse(status=200, data={'code': 404, 'msg': 'wrong uid'})
             logger.info("旧移动侦测接口的uid:{}".format(uid))
+            event_type = int(event_type)
             pkey = '{uid}_{channel}_{event_type}_ptl'.format(uid=uid, event_type=event_type, channel=channel)
             ykey = '{uid}_redis_qs'.format(uid=uid)
-            is_sys_msg = self.is_sys_msg(int(event_type))
+            is_sys_msg = self.is_sys_msg(event_type)
             if is_sys_msg is True:
                 dkey = '{uid}_{channel}_{event_type}_flag'.format(uid=uid, event_type=event_type, channel=channel)
             else:
@@ -66,16 +69,17 @@ class NotificationView(View):
             # 一分钟外,推送开启状态
             detect_med_type = 0  # 0推送旧机制 1存库不推送,2推送存库
             # 暂时注销
-            if have_pkey:
-                res_data = {'code': 0, 'msg': 'Push it once a minute'}
-                return JsonResponse(status=200, data=res_data)
+            if event_type != 606:
+                if have_pkey:
+                    res_data = {'code': 0, 'msg': 'Push it once a minute'}
+                    return JsonResponse(status=200, data=res_data)
 
             # 数据库读取数据
             if have_ykey:
                 uid_push_list = eval(redisObj.get_data(key=ykey))
             else:
                 # 从数据库查询出来
-                uid_push_qs = DevicePushService.query_uid_push(uid)
+                uid_push_qs = DevicePushService.query_uid_push(uid, event_type)
                 if not uid_push_qs.exists():
                     logger.info('消息推送-uid_push 数据不存在')
                     return JsonResponse(status=200, data={'code': 176, 'msg': 'no uid_push data'})

+ 65 - 57
Controller/DetectControllerV2.py

@@ -44,7 +44,7 @@ class NotificationV2View(View):
         @param request_dict:electricity 电量值
         """
         logger = logging.getLogger('info')
-        logger.info("移动侦测V2接口参数:{}".format(request_dict))
+        logger.info('移动侦测V2接口参数:{}'.format(request_dict))
         uidToken = request_dict.get('uidToken', None)
         etk = request_dict.get('etk', None)
         channel = request_dict.get('channel', '1')
@@ -53,6 +53,8 @@ class NotificationV2View(View):
         is_st = request_dict.get('is_st', None)
         region = request_dict.get('region', None)
         electricity = request_dict.get('electricity', '')
+        time_token = request_dict.get('time_token', None)
+        uid = request_dict.get('uid', None)
 
         if not all([channel, n_time]):
             return JsonResponse(status=200, data={
@@ -60,71 +62,77 @@ class NotificationV2View(View):
                 'msg': 'param is wrong'})
         if not region or not is_st:
             return JsonResponse(status=200, data={'code': 404, 'msg': 'no region or is_st'})
-
+        # 时间戳token校验
+        if time_token:
+            if not CommonService.check_time_stamp_token(time_token, n_time):
+                return JsonResponse(status=200, data={'code': 13, 'msg': 'Timestamp token verification failed'})
         try:
-            with transaction.atomic():
-                is_st = int(is_st)
-                region = int(region)
+            is_st = int(is_st)
+            region = int(region)
+            event_type = int(event_type)
+            if not uid:
                 uid = DevicePushService.decode_uid(etk, uidToken)  # 解密uid
-                if len(uid) != 20 and len(uid) != 14:
-                    return JsonResponse(status=200, data={'code': 404, 'msg': 'wrong uid'})
-                req_limiting = '{uid}_{channel}_{event_type}_ptl' \
+            if len(uid) != 20 and len(uid) != 14:
+                return JsonResponse(status=200, data={'code': 404, 'msg': 'wrong uid'})
+            req_limiting = '{uid}_{channel}_{event_type}_ptl' \
+                .format(uid=uid, channel=channel, event_type=event_type)
+            is_sys_msg = self.is_sys_msg(int(event_type))  # 判断事件类型是否是系统消息
+            if is_sys_msg:
+                push_interval = '{uid}_{channel}_{event_type}_flag' \
                     .format(uid=uid, channel=channel, event_type=event_type)
-                is_sys_msg = self.is_sys_msg(int(event_type))  # 判断事件类型是否是系统消息
-                if is_sys_msg:
-                    push_interval = '{uid}_{channel}_{event_type}_flag' \
-                        .format(uid=uid, channel=channel, event_type=event_type)
-                else:
-                    push_interval = '{uid}_{channel}_flag'.format(uid=uid, channel=channel)
-                redisObj = RedisObject(db=6)
-                cache_req_limiting = redisObj.get_data(key=req_limiting)  # 获取请求限流缓存数据
-                cache_app_push = redisObj.get_data(key=push_interval)  # 获取APP推送消息时间间隔缓存数据
-                logger.info('消息推送- 限流key: {}, 推送间隔key: {}'.
-                            format(cache_req_limiting, cache_app_push))
+            else:
+                push_interval = '{uid}_{channel}_flag'.format(uid=uid, channel=channel)
+            redisObj = RedisObject(db=6)
+            cache_req_limiting = redisObj.get_data(key=req_limiting)  # 获取请求限流缓存数据
+            cache_app_push = redisObj.get_data(key=push_interval)  # 获取APP推送消息时间间隔缓存数据
+            logger.info('消息推送- 限流key: {}, 推送间隔key: {}'.
+                        format(cache_req_limiting, cache_app_push))
+            if event_type != 606:
                 if cache_req_limiting:  # 限流存在则直接返回
                     return JsonResponse(status=200, data={'code': 0, 'msg': 'Push again in one minute'})
-                redisObj.set_data(key=req_limiting, val=1, expire=60)  # 当缓存不存在限流数据 重新设置一分钟请求一次
-                uid_push_qs = DevicePushService.query_uid_push(uid)  # 查询uid_set与push数据列表
-                if not uid_push_qs.exists():
-                    logger.info('消息推送-uid_push 数据不存在')
-                    return JsonResponse(status=200, data={'code': 176, 'msg': 'no uid_push data'})
-                ai_type = uid_push_qs.first()['uid_set__ai_type']
-                event_type = self.get_combo_msg_type(ai_type, int(event_type))  # 解析消息事件类型看是否多类型组合
-                # 将uid_set以及uid_push 转数组列表
-                uid_set_push_list = DevicePushService.cache_uid_push(uid_push_qs)
-                nickname = uid_set_push_list[0]['uid_set__nickname']
-                nickname = uid if not nickname else nickname
-                # APP消息提醒推送间隔
-                detect_interval = uid_set_push_list[0]['uid_set__detect_interval']
+            redisObj.set_data(key=req_limiting, val=1, expire=60)  # 当缓存不存在限流数据 重新设置一分钟请求一次
+            uid_push_qs = DevicePushService.query_uid_push(uid, event_type)  # 查询uid_set与push数据列表
+            if not uid_push_qs.exists():
+                logger.info('消息推送-uid_push 数据不存在')
+                return JsonResponse(status=200, data={'code': 176, 'msg': 'no uid_push data'})
+            ai_type = uid_push_qs.first()['uid_set__ai_type']
+            event_type = self.get_combo_msg_type(ai_type, event_type)  # 解析消息事件类型看是否多类型组合
+            # 将uid_set以及uid_push 转数组列表
+            uid_set_push_list = DevicePushService.cache_uid_push(uid_push_qs)
+            nickname = uid_set_push_list[0]['uid_set__nickname']
+            nickname = uid if not nickname else nickname
+            # APP消息提醒推送间隔
+            detect_interval = uid_set_push_list[0]['uid_set__detect_interval']
+            if event_type != 606:
                 if not cache_app_push:
                     # 缓存APP提醒推送间隔 默认1分钟提醒一次
                     DevicePushService.cache_push_detect_interval(redisObj, push_interval, detect_interval,
                                                                  uid_set_push_list[0]['uid_set__new_detect_interval'])
-                bucket = ''
-                aws_s3_client = ''
-                if is_st == 1 or is_st == 3:  # 使用aws s3
-                    aws_s3_client = s3_client(region=region)
-                    bucket = 'foreignpush' if region == 1 else 'push'
-                kwag_args = {
-                    'uid': uid,
-                    'channel': channel,
-                    'event_type': event_type,
-                    'n_time': n_time,
-                }
-                params = {'nickname': nickname, 'uid': uid, 'kwag_args': kwag_args, 'is_st': is_st, 'region': region,
-                          'is_sys_msg': is_sys_msg, 'channel': channel, 'event_type': event_type, 'n_time': n_time,
-                          'electricity': electricity, 'bucket': bucket, 'aws_s3_client': aws_s3_client,
-                          'app_push': cache_app_push, 'storage_location': 2}
-                # APP消息推送与获取报警消息数据列表
-                result = DevicePushService.save_msg_push(uid_set_push_list, **params)
-                # 批量系统消息&报警消息数据存库
-                DevicePushService.save_sys_msg(is_sys_msg, result['local_date_time'],
-                                               result['sys_msg_list'], result['new_device_info_list'])
-                params['aws_s3_client'] = aws_s3_client
-                params['uid_set_push_list'] = uid_set_push_list
-                params['code_dict'] = result
-                result_dict = DevicePushService.get_push_url(**params)  # 获取S3对象上传链接
-                return JsonResponse(status=200, data=result_dict)
+            bucket = ''
+            aws_s3_client = ''
+            if is_st == 1 or is_st == 3:  # 使用aws s3
+                aws_s3_client = s3_client(region=region)
+                bucket = 'foreignpush' if region == 1 else 'push'
+            kwag_args = {
+                'uid': uid,
+                'channel': channel,
+                'event_type': event_type,
+                'n_time': n_time,
+            }
+            params = {'nickname': nickname, 'uid': uid, 'kwag_args': kwag_args, 'is_st': is_st, 'region': region,
+                      'is_sys_msg': is_sys_msg, 'channel': channel, 'event_type': event_type, 'n_time': n_time,
+                      'electricity': electricity, 'bucket': bucket, 'aws_s3_client': aws_s3_client,
+                      'app_push': cache_app_push, 'storage_location': 2}
+            # APP消息推送与获取报警消息数据列表
+            result = DevicePushService.save_msg_push(uid_set_push_list, **params)
+            # 批量系统消息&报警消息数据存库
+            DevicePushService.save_sys_msg(is_sys_msg, result['local_date_time'],
+                                           result['sys_msg_list'], result['new_device_info_list'])
+            params['aws_s3_client'] = aws_s3_client
+            params['uid_set_push_list'] = uid_set_push_list
+            params['code_dict'] = result
+            result_dict = DevicePushService.get_push_url(**params)  # 获取S3对象上传链接
+            return JsonResponse(status=200, data=result_dict)
         except Exception as e:
             logger.info('消息推送-异常详情,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
             data = {

+ 36 - 1
Service/CommonService.py

@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*-
+import base64
 import datetime
 import os
 import time
@@ -281,4 +282,38 @@ class CommonService:
         data = (''.join(dataList))
         data = data + key
         sign = hashlib.md5(data.encode(encoding="utf-8")).hexdigest()
-        return sign
+        return sign
+
+    @staticmethod
+    def check_time_stamp_token(token, time_stamp):
+        # 时间戳token校验
+        if not all([token, time_stamp]):
+            return False
+        try:
+            token = int(CommonService.decode_data(token))
+            time_stamp = int(time_stamp)
+            now_time = int(time.time())
+            distance = now_time - time_stamp
+            if token != time_stamp or distance > 60000 or distance < -60000:  # 为了全球化时间控制在一天内
+                return False
+            return True
+        except Exception as e:
+            print(e)
+            return False
+
+    @staticmethod
+    def decode_data(content, start=1, end=4):
+        """
+        数据解密
+        @param content: 数据内容
+        @param start: 起始长度
+        @param end: 结束长度
+        @return content: 解密的数据
+        """
+        if not content:
+            return ''
+        for i in range(start, end):
+            content = base64.b64decode(content)
+            content = content.decode('utf-8')
+            content = content[i:-i]
+        return content

+ 15 - 6
Service/DevicePushService.py

@@ -51,14 +51,20 @@ class DevicePushService:
         return uid
 
     @classmethod
-    def query_uid_push(cls, uid):
+    def query_uid_push(cls, uid, event_type):
         """
         查询uid_set与push数据列表
         """
-        uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
-            values('token_val', 'app_type', 'appBundleId', 'm_code', 'push_type', 'userID_id', 'userID__NickName',
-                   'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group',
-                   'uid_set__channel', 'uid_set__ai_type', 'uid_set__new_detect_interval')
+        if event_type != 606:
+            uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
+                values('token_val', 'app_type', 'appBundleId', 'm_code', 'push_type', 'userID_id', 'userID__NickName',
+                       'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group',
+                       'uid_set__channel', 'uid_set__ai_type', 'uid_set__new_detect_interval')
+        else:
+            uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
+                values('token_val', 'app_type', 'appBundleId', 'm_code', 'push_type', 'userID_id', 'userID__NickName',
+                       'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group',
+                       'uid_set__channel', 'uid_set__ai_type', 'uid_set__new_detect_interval')
         return uid_push_qs
 
     @staticmethod
@@ -451,7 +457,10 @@ class DevicePushService:
             "notification": {
                 "title": msg_title,
                 "content": msg_text,
-                'channel_id': channel_id
+                'channel_id': channel_id,
+                'action_parameters': extra_data,
+                'click_action_type': 1,
+                'click_action_activity': 'com.ansjer.zccloud_a.AJ_MainView.AJ_Home.AJMainActivity'
             }
         }
         push_data = {

+ 4 - 1
Service/PushService.py

@@ -375,7 +375,10 @@ class PushObject:
                 "notification": {
                     "title": msg_title,
                     "content": msg_text,
-                    'channel_id': channel_id
+                    'channel_id': channel_id,
+                    'action_parameters': extra_data,
+                    'click_action_type': 1,
+                    'click_action_activity': 'com.ansjer.zccloud_a.AJ_MainView.AJ_Home.AJMainActivity'
                 }
             }
             push_data = {