Pārlūkot izejas kodu

完善定制化推送接口

locky 1 gadu atpakaļ
vecāks
revīzija
3c81ecda33

+ 8 - 9
Controller/CustomizedPushController.py

@@ -4,11 +4,10 @@
 import logging
 import threading
 
-from django.http import HttpResponse, JsonResponse
+from django.http import HttpResponse
 from django.views import View
 
-from Model.models import Device_Info, SceneLog, CustomizedPush, Device_User
-from Object.RedisObject import RedisObject
+from Model.models import CustomizedPush
 from Object.ResponseObject import ResponseObject
 from Service.CustomizedPushService import CustomizedPushObject
 
@@ -38,7 +37,6 @@ class CustomizedPushView(View):
         if not customized_push_id:
             return response.json(444)
         try:
-            redis_obj = RedisObject()
             customized_push_qs = CustomizedPush.objects.filter(id=customized_push_id, push_satus=False).values(
                 'title', 'msg', 'link', 'icon_link', 'country', 'device_type', 'register_period', 'push_app')
             if not customized_push_qs.exists():
@@ -54,14 +52,15 @@ class CustomizedPushView(View):
                 'register_period': customized_push_qs[0]['register_period'],
                 'push_app': customized_push_qs[0]['push_app']
             }
+            # customized_push(**kwargs)
             # 异步推送消息和保存数据
-            push_thread = threading.Thread(
-                target=customized_push,
-                kwargs=kwargs)
-            push_thread.start()
+            # push_thread = threading.Thread(
+            #     target=customized_push,
+            #     kwargs=kwargs)
+            # push_thread.start()
 
             # 更新推送状态
-            customized_push_qs.update(push_satus=True)
+            # customized_push_qs.update(push_satus=True)
             return response.json(0)
         except Exception as e:
             return HttpResponse(repr(e), status=500)

+ 37 - 21
Service/CustomizedPushService.py

@@ -30,11 +30,13 @@ class CustomizedPushObject:
         device_info_qs = Device_Info.objects.filter(Type__in=device_type_list, userID__region_country=country_id)
         # 获取时间范围
         now_time = int(time.time())
-        n, m = register_period[:1], register_period[-1:]
-        if m == '-':
+
+        index = register_period.find('-')
+        n, m = register_period[:index], register_period[index+1:]
+        if m == '':
             # 0-,所有时间
             if n == '0':
-                user_id_list = device_info_qs.distinct('userID_id').values_list('userID_id', flat=True)
+                device_info_qs = device_info_qs.values_list('userID_id', flat=True)
             # n-,n年以上
             else:
                 # n年前时间戳转时间字符串
@@ -42,8 +44,8 @@ class CustomizedPushObject:
                 n_year_ago_timestamp = now_time - n_years_seconds
                 n_year_ago = CommonService.timestamp_to_str(n_year_ago_timestamp)
                 # 注册时间越小越早
-                user_id_list = device_info_qs.filter(userID__data_joined__lte=n_year_ago).\
-                    distinct('userID_id').values_list('userID_id', flat=True)
+                device_info_qs = device_info_qs.filter(userID__data_joined__lte=n_year_ago).\
+                    values_list('userID_id', flat=True)
         else:
             # n-m年,(如2-3年)
             n_years_seconds, m_years_seconds = int(n) * 365 * 24 * 60 * 60, int(m) * 365 * 24 * 60 * 60
@@ -53,9 +55,11 @@ class CustomizedPushObject:
             n_year_ago = CommonService.timestamp_to_str(n_year_ago_timestamp)   # 2021
             m_year_ago = CommonService.timestamp_to_str(m_year_ago_timestamp)   # 2020
             # 2020 <= 注册时间 <= 2021
-            user_id_list = device_info_qs.\
+            device_info_qs = device_info_qs.\
                 filter(userID__data_joined__gte=m_year_ago, userID__data_joined__lte=n_year_ago).\
-                distinct('userID_id').values_list('userID_id', flat=True)
+                values_list('userID_id', flat=True)
+
+        user_id_list = list(device_info_qs)
 
         return user_id_list
 
@@ -69,6 +73,7 @@ class CustomizedPushObject:
         user_id_list = kwargs['user_id_list']
         title = kwargs['title']
         msg = kwargs['msg']
+        link = kwargs['link']
         icon_link = kwargs['icon_link'] if kwargs['icon_link'] != '' else None
 
         # 推送
@@ -77,32 +82,43 @@ class CustomizedPushObject:
             app_bundle_id_list = ['com.ansjer.zccloud_a', 'com.ansjer.zccloud']
         else:
             app_bundle_id_list = ['com.ansjer.zccloud_ab', 'com.ansjer.customizede']
+
         gateway_push_qs = GatewayPush.objects.filter(user_id__in=user_id_list, app_bundle_id__in=app_bundle_id_list).\
             values('user_id', 'app_bundle_id', 'push_type', 'token_val')
         for gateway_push in gateway_push_qs:
+            push_type = gateway_push['push_type']
+            user_id = gateway_push['user_id']
+            app_bundle_id = gateway_push['app_bundle_id']
+            token_val = gateway_push['token_val']
             push_succeed = False
             # ios
-            if gateway_push['push_type'] == 0:
-                push_succeed = PushObject.ios_apns_push(nickname='', app_bundle_id=gateway_push['app_bundle_id'],
-                                                        token_val=gateway_push['token_val'], n_time=n_time,
-                                                        event_type=0, msg_title=title, msg_text=msg,
+            if push_type == 0:
+                push_succeed = PushObject.ios_apns_push(nickname='', app_bundle_id=app_bundle_id, token_val=token_val,
+                                                        n_time=n_time, event_type=0, msg_title=title, msg_text=msg,
                                                         launch_image=icon_link)
             # gcm
-            elif gateway_push['push_type'] == 1:
+            elif push_type == 1:
                 if icon_link is None:
                     icon_link = ''
-                push_succeed = PushObject.android_fcm_push(nickname='', app_bundle_id=gateway_push['app_bundle_id'],
-                                                           token_val=gateway_push['token_val'], n_time=n_time,
-                                                           event_type=0, msg_title=title, msg_text=msg, image=icon_link)
+                push_succeed = PushObject.android_fcm_push(
+                    nickname='', app_bundle_id=app_bundle_id, token_val=token_val, n_time=n_time, event_type=0,
+                    msg_title=title, msg_text=msg, image=icon_link)
             # 华为
-            elif gateway_push['push_type'] == 3:
+            elif push_type == 3:
                 huawei_push_object = HuaweiPushObject()
-                push_succeed = huawei_push_object.\
-                    send_push_notify_message(token_val=gateway_push['token_val'], msg_title=title,
-                                             msg_text=msg, n_time=n_time, image_url=icon_link)
+                push_succeed = huawei_push_object.send_push_notify_message(
+                    token_val=token_val, msg_title=title, msg_text=msg, n_time=n_time, event_type=0, image_url=icon_link
+                )
 
             # 推送成功,写入系统消息
             if push_succeed:
-                SysMsgModel.objects.create(userID_id=gateway_push_qs['user_id'], title=title, msg=msg, addTime=n_time,
-                                           updTime=n_time)
+                sys_msg = msg
+                if link:
+                    sys_msg += '\n' + link
+
+                SysMsgModel.objects.create(userID_id=user_id, title=title, msg=sys_msg, addTime=n_time, updTime=n_time)
+                CUSTOMIZED_PUSH_LOGGER.info('用户{}推送成功'.format(user_id))
+            else:
+                CUSTOMIZED_PUSH_LOGGER.info('用户{}推送失败,push_type:{}'.format(user_id, push_type))
+
         CUSTOMIZED_PUSH_LOGGER.info('customized_push_id:{}推送完成'.format(kwargs['id']))