CustomizedPushService.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # @Author : Rocky
  2. # @File : CustomizedPushService.py
  3. # @Time : 2023/10/19 15:49
  4. import logging
  5. import threading
  6. import time
  7. from concurrent.futures import ThreadPoolExecutor
  8. from Model.models import DeviceTypeModel, Device_Info, GatewayPush, CountryModel, SysMsgModel, CustomizedPush, \
  9. UserEmailSubscriptions
  10. from Service.CommonService import CommonService
  11. from Service.HuaweiPushService.HuaweiPushService import HuaweiPushObject
  12. from Service.PushService import PushObject
  13. from AnsjerPush.config import XM_PUSH_CHANNEL_ID
  14. CUSTOMIZED_PUSH_LOGGER = logging.getLogger('customized_push')
  15. class CustomizedPushObject:
  16. @staticmethod
  17. def query_push_user(device_name, country, register_period):
  18. """
  19. 查询需要推送的用户id列表
  20. @param device_name: 设备型号
  21. @param country: 国家
  22. @param register_period: 用户注册年限
  23. @return: uid_id_list
  24. """
  25. # 设备型号和国家
  26. device_name_list = device_name.split(',')
  27. device_type_list = DeviceTypeModel.objects.filter(name__in=device_name_list).values_list('type', flat=True)
  28. country_name_list = country.split(',')
  29. country_id_list = CountryModel.objects.filter(country_name__in=country_name_list).values_list('id', flat=True)
  30. device_info_qs = Device_Info.objects.filter(Type__in=device_type_list,
  31. userID__region_country__in=country_id_list)
  32. # 获取时间范围
  33. now_time = int(time.time())
  34. index = register_period.find('-')
  35. n, m = register_period[:index], register_period[index + 1:]
  36. if m == '':
  37. # 0-,所有时间
  38. if n == '0':
  39. device_info_qs = device_info_qs.values_list('userID_id', flat=True)
  40. # n-,n年以上
  41. else:
  42. # n年前时间戳转时间字符串
  43. n_years_seconds = int(n) * 365 * 24 * 60 * 60
  44. n_year_ago_timestamp = now_time - n_years_seconds
  45. n_year_ago = CommonService.timestamp_to_str(n_year_ago_timestamp)
  46. # 注册时间越小越早
  47. device_info_qs = device_info_qs.filter(userID__data_joined__lte=n_year_ago). \
  48. values_list('userID_id', flat=True)
  49. else:
  50. # n-m年,(如2-3年)
  51. n_years_seconds, m_years_seconds = int(n) * 365 * 24 * 60 * 60, int(m) * 365 * 24 * 60 * 60
  52. n_year_ago_timestamp = now_time - n_years_seconds
  53. m_year_ago_timestamp = now_time - m_years_seconds
  54. # 时间戳转时间字符串
  55. n_year_ago = CommonService.timestamp_to_str(n_year_ago_timestamp) # 2021
  56. m_year_ago = CommonService.timestamp_to_str(m_year_ago_timestamp) # 2020
  57. # 2020 <= 注册时间 <= 2021
  58. device_info_qs = device_info_qs. \
  59. filter(userID__data_joined__gte=m_year_ago, userID__data_joined__lte=n_year_ago). \
  60. values_list('userID_id', flat=True)
  61. user_id_list = list(device_info_qs)
  62. return user_id_list
  63. @classmethod
  64. def push_and_save_sys_msg(cls, **kwargs):
  65. """
  66. 推送和保存系统消息
  67. @param kwargs: 参数
  68. @return:
  69. """
  70. customized_push_id = kwargs['id']
  71. user_id_list = kwargs['user_id_list']
  72. title = kwargs['title']
  73. msg = kwargs['msg']
  74. link = kwargs['link']
  75. icon_link = kwargs['icon_link'] if kwargs['icon_link'] != '' else None
  76. n_time = int(time.time())
  77. push_kwargs = {
  78. 'n_time': n_time,
  79. 'title': title,
  80. 'msg': msg,
  81. 'icon_link': icon_link
  82. }
  83. # 推送
  84. if kwargs['push_app'] == 'ZosiSmart':
  85. app_bundle_id_list = ['com.ansjer.zccloud_a', 'com.ansjer.zccloud']
  86. else:
  87. app_bundle_id_list = ['com.ansjer.zccloud_ab', 'com.ansjer.customizede']
  88. try:
  89. gateway_push_qs = GatewayPush.objects.filter(
  90. user_id__in=user_id_list, app_bundle_id__in=app_bundle_id_list). \
  91. values('user_id', 'app_bundle_id', 'push_type', 'token_val')
  92. if gateway_push_qs.exists():
  93. sys_msg_list = []
  94. saved_user_id_list = []
  95. gateway_push_list = []
  96. for gateway_push in gateway_push_qs:
  97. # user_id保存列表,避免重复写入数据
  98. user_id = gateway_push['user_id']
  99. user_email_sub_qs = UserEmailSubscriptions.objects.filter(
  100. user_id=user_id, push_sub_status=0)
  101. if user_id not in saved_user_id_list:
  102. saved_user_id_list.append(user_id)
  103. sys_msg_list.append(SysMsgModel(
  104. userID_id=user_id, title=title, msg=msg, jumpLink=link, addTime=n_time, updTime=n_time))
  105. if not user_email_sub_qs.exists():
  106. gateway_push_list.append(gateway_push)
  107. # 保存系统消息和异步推送消息
  108. SysMsgModel.objects.bulk_create(sys_msg_list)
  109. pre_push_kwargs = {
  110. 'push_kwargs': push_kwargs,
  111. 'gateway_push_list': gateway_push_list
  112. }
  113. pre_push_thread = threading.Thread(
  114. target=cls.thr_pool_push,
  115. kwargs=pre_push_kwargs)
  116. pre_push_thread.start()
  117. CUSTOMIZED_PUSH_LOGGER.info('customized_push_id:{}推送完成'.format(customized_push_id))
  118. except Exception as e:
  119. CUSTOMIZED_PUSH_LOGGER.info('定制化推送或保存数据异常,'
  120. 'error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  121. @classmethod
  122. def thr_pool_push(cls, **kwargs):
  123. CUSTOMIZED_PUSH_LOGGER.info('线程池推送开始')
  124. push_kwargs = kwargs['push_kwargs']
  125. gateway_push_list = kwargs['gateway_push_list']
  126. with ThreadPoolExecutor() as executor:
  127. executor.map(
  128. lambda gateway_push_kwargs: cls.start_push(push_kwargs, gateway_push_kwargs), gateway_push_list)
  129. @classmethod
  130. def start_push(cls, push_kwargs, gateway_push_kwargs):
  131. title = push_kwargs['title']
  132. n_time = push_kwargs['n_time']
  133. msg = push_kwargs['msg']
  134. icon_link = push_kwargs['icon_link']
  135. push_type = gateway_push_kwargs['push_type']
  136. user_id = gateway_push_kwargs['user_id']
  137. app_bundle_id = gateway_push_kwargs['app_bundle_id']
  138. token_val = gateway_push_kwargs['token_val']
  139. push_succeed = cls.push_msg(push_type, app_bundle_id, token_val, n_time, title, msg, icon_link)
  140. push_status = '成功' if push_succeed else '失败'
  141. CUSTOMIZED_PUSH_LOGGER.info('{}推送{},push_type:{}'.format(user_id, push_status, push_type))
  142. @staticmethod
  143. def push_msg(push_type, app_bundle_id, token_val, n_time, title, msg, icon_link):
  144. push_kwargs = {
  145. 'nickname': '',
  146. 'event_type': 0,
  147. 'app_bundle_id': app_bundle_id,
  148. 'token_val': token_val,
  149. 'msg_title': title,
  150. 'msg_text': msg,
  151. 'n_time': n_time,
  152. }
  153. try:
  154. # ios
  155. if push_type == 0:
  156. push_kwargs['launch_image'] = icon_link
  157. return PushObject.ios_apns_push(**push_kwargs)
  158. # gcm
  159. elif push_type == 1:
  160. if icon_link is None:
  161. icon_link = ''
  162. push_kwargs['image'] = icon_link
  163. return PushObject.android_fcm_push_v1(**push_kwargs)
  164. # 极光
  165. elif push_type == 2:
  166. return PushObject.android_jpush(**push_kwargs)
  167. # 华为
  168. elif push_type == 3:
  169. push_kwargs['image_url'] = icon_link
  170. huawei_push_object = HuaweiPushObject()
  171. return huawei_push_object.send_push_notify_message(**push_kwargs)
  172. # 小米
  173. elif push_type == 4:
  174. push_kwargs['channel_id'] = XM_PUSH_CHANNEL_ID['service_reminder']
  175. return PushObject.android_xmpush(**push_kwargs)
  176. # vivo
  177. elif push_type == 5:
  178. return PushObject.android_vivopush(**push_kwargs)
  179. # oppo
  180. elif push_type == 6:
  181. push_kwargs['channel_id'] = 'VALUE_ADDED'
  182. return PushObject.android_oppopush(**push_kwargs)
  183. # 魅族
  184. elif push_type == 7:
  185. return PushObject.android_meizupush(**push_kwargs)
  186. else:
  187. return False
  188. except Exception as e:
  189. CUSTOMIZED_PUSH_LOGGER.info('定制化推送异常,'
  190. 'error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  191. return False