CustomizedPushService.py 8.6 KB

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