浏览代码

一键通话只推送主用户

locky 2 年之前
父节点
当前提交
89b71d0915
共有 3 个文件被更改,包括 57 次插入47 次删除
  1. 1 1
      Controller/DetectController.py
  2. 39 34
      Controller/DetectControllerV2.py
  3. 17 12
      Service/DevicePushService.py

+ 1 - 1
Controller/DetectController.py

@@ -84,7 +84,7 @@ class NotificationView(View):
                     logger.info('消息推送-uid_push 数据不存在')
                     return JsonResponse(status=200, data={'code': 176, 'msg': 'no uid_push data'})
                 # 修改redis数据,并设置过期时间为10分钟
-                uid_push_list = DevicePushService.cache_uid_push(uid_push_qs)
+                uid_push_list = DevicePushService.qs_to_list(uid_push_qs)
                 redisObj.set_data(key=ykey, val=str(uid_push_list), expire=600)
                 if not uid_push_list:
                     res_data = {'code': 404, 'msg': 'error !'}

+ 39 - 34
Controller/DetectControllerV2.py

@@ -41,6 +41,10 @@ class NotificationV2View(View):
         @param request_dict:is_st 文件类型(0:无,1:图片,2:视频)
         @param request_dict:region 文件存储区域(1:国外,2国内)
         @param request_dict:electricity 电量值
+        @param request_dict:time_token 时间戳token
+        @param request_dict:uid uid
+        @param request_dict:dealings_type 往来检测 1:来,2:离开
+        @param request_dict:detection 检测类型 0:普通,1:算法
         """
         logger = logging.getLogger('info')
         logger.info('移动侦测V2接口参数:{}'.format(request_dict))
@@ -52,73 +56,72 @@ 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)
-        # 往来检测 1:来,2:离开
-        dealings_type = request_dict.get('dealingsType', 0)
-        # 检测类型 0:普通,1:算法
+        dealings_type = int(request_dict.get('dealingsType', 0))
         detection = int(request_dict.get('detection', 0))
 
+        # 参数校验
         if not all([channel, n_time]):
-            return JsonResponse(status=200, data={
-                'code': 444,
-                'msg': 'param is wrong'})
+            return JsonResponse(status=200, data={'code': 444, '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'})
+
+        is_st = int(is_st)
+        region = int(region)
+        event_type = int(event_type)
+
         try:
-            is_st = int(is_st)
-            region = int(region)
-            event_type = int(event_type)
-            if not uid:
-                uid = DevicePushService.decode_uid(etk, uidToken)  # 解密uid
+            uid = DevicePushService.decode_uid(etk, uidToken)
             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))  # 判断事件类型是否是系统消息
+
+            # 判断是否为系统消息
+            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)
+                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))
+                
+            redis_obj = RedisObject()
+            req_limiting = '{uid}_{channel}_{event_type}_ptl'.format(uid=uid, channel=channel, event_type=event_type)
+            cache_req_limiting = redis_obj.get_data(key=req_limiting)  # 获取请求限流缓存数据
+            cache_app_push = redis_obj.get_data(key=push_interval)  # 获取APP推送消息时间间隔缓存数据
+            logger.info('V2推送 - 限流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, event_type)  # 查询uid_set与push数据列表
+            redis_obj.set_data(key=req_limiting, val=1, expire=60)  # 当缓存不存在限流数据 重新设置一分钟请求一次
+
+            # 查询uid_push和uid_set数据
+            uid_push_qs = DevicePushService.query_uid_push(uid, event_type)
             if not uid_push_qs.exists():
                 logger.info('消息推送-{}uid_push数据不存在'.format(uid))
                 return JsonResponse(status=200, data={'code': 176, 'msg': 'no uid_push data'})
+
             ai_type = uid_push_qs.first()['uid_set__ai_type']
             device_type = uid_push_qs.first()['uid_set__device_type']
-            logger.info('ai_type: {}, device_type: {}'.format(ai_type, device_type))
-            # 将uid_set以及uid_push 转数组列表
-            uid_set_push_list = DevicePushService.cache_uid_push(uid_push_qs)
+
+            # uid_push_qs转存列表
+            uid_set_push_list = DevicePushService.qs_to_list(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,
+                    DevicePushService.cache_push_detect_interval(redis_obj, push_interval, detect_interval,
                                                                  uid_set_push_list[0]['uid_set__new_detect_interval'])
             else:
                 cache_app_push = ''
+
             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,
@@ -129,13 +132,15 @@ class NotificationV2View(View):
                       '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, 'ai_type': ai_type, 'device_type': device_type,
-                      'dealings_type': int(dealings_type), 'detection': detection}
+                      'dealings_type': dealings_type, 'detection': detection}
             logger.info('推送数据参数:{}'.format(params))
+
             # 推送消息,生成推送数据列表
             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

+ 17 - 12
Service/DevicePushService.py

@@ -22,7 +22,7 @@ from AnsjerPush.Config.aiConfig import DEVICE_EVENT_TYPE
 from AnsjerPush.config import CONFIG_INFO, CONFIG_CN, MULTI_CHANNEL_TYPE_LIST
 from AnsjerPush.config import JPUSH_CONFIG, FCM_CONFIG, APNS_CONFIG, BASE_DIR, APNS_MODE, XMPUSH_CONFIG, OPPOPUSH_CONFIG
 from Model.models import UidPushModel, SysMsgModel, DeviceSharePermission, DeviceChannelUserSet, \
-    DeviceChannelUserPermission, UidSetModel
+    DeviceChannelUserPermission, UidSetModel, Device_Info
 from Object.ETkObject import ETkObject
 from Object.UidTokenObject import UidTokenObject
 from Object.utils import LocalDateTimeUtil
@@ -54,7 +54,10 @@ class DevicePushService:
     @classmethod
     def query_uid_push(cls, uid, event_type):
         """
-        查询uid_set与push数据列表
+        查询uid_push和uid_set数据
+        @param uid: uid
+        @param event_type: 事件类型
+        @return: uid_push_qs
         """
         if event_type != 606:
             uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, uid_set__detect_status=1). \
@@ -62,24 +65,26 @@ class DevicePushService:
                        'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group',
                        'uid_set__channel', 'uid_set__ai_type', 'uid_set__device_type', 'uid_set__new_detect_interval')
         else:
-            uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
+            # 一键通话只推主用户
+            device_info_qs = Device_Info.objects.filter(UID=uid).values('vodPrimaryUserID')
+            primary_user_id = device_info_qs[0]['vodPrimaryUserID']
+            uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid, userID_id=primary_user_id). \
                 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__device_type', 'uid_set__new_detect_interval')
         return uid_push_qs
 
     @staticmethod
-    def cache_uid_push(uid_push_qs):
+    def qs_to_list(qs):
         """
-        将uid_push 信息进行缓存
-        @param uid_push_qs: uid_set & uid_push 列表对象
-        @return: uid_set_list
+        qs对象转存列表
+        @param qs: query set对象
+        @return: qs_list
         """
-        uid_set_list = []
-        for qs in uid_push_qs:
-            uid_set_list.append(qs)
-        # redis_obj.set_data(key=name, val=str(redis_list), expire=expire)
-        return uid_set_list
+        qs_list = []
+        for i in qs:
+            qs_list.append(i)
+        return qs_list
 
     @staticmethod
     def cache_push_detect_interval(redis_obj, name, detect_interval, new_detect_interval):