CustomizedPushService.py 7.6 KB

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