CustomizedPushService.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # @Author : Rocky
  2. # @File : CustomizedPushService.py
  3. # @Time : 2023/10/19 15:49
  4. import logging
  5. import time
  6. from Model.models import DeviceTypeModel, Device_Info, GatewayPush, CountryModel, SysMsgModel
  7. from Service.CommonService import CommonService
  8. from Service.HuaweiPushService.HuaweiPushService import HuaweiPushObject
  9. from Service.PushService import PushObject
  10. CUSTOMIZED_PUSH_LOGGER = logging.getLogger('customized_push')
  11. class CustomizedPushObject:
  12. @staticmethod
  13. def query_push_user(device_name, country, register_period):
  14. """
  15. 查询需要推送的用户id列表
  16. @param device_name: 设备型号
  17. @param country: 国家
  18. @param register_period: 用户注册年限
  19. @return: uid_id_list
  20. """
  21. # 设备型号和国家
  22. device_name_list = device_name.split(',')
  23. device_type_list = DeviceTypeModel.objects.filter(name__in=device_name_list).values_list('type', flat=True)
  24. country_qs = CountryModel.objects.filter(country_name=country).values('id')
  25. country_id = country_qs[0]['id']
  26. device_info_qs = Device_Info.objects.filter(Type__in=device_type_list, userID__region_country=country_id)
  27. # 获取时间范围
  28. now_time = int(time.time())
  29. n, m = register_period[:1], register_period[-1:]
  30. if m == '-':
  31. # 0-,所有时间
  32. if n == '0':
  33. user_id_list = device_info_qs.distinct('userID_id').values_list('userID_id', flat=True)
  34. # n-,n年以上
  35. else:
  36. # n年前时间戳转时间字符串
  37. n_years_seconds = int(n) * 365 * 24 * 60 * 60
  38. n_year_ago_timestamp = now_time - n_years_seconds
  39. n_year_ago = CommonService.timestamp_to_str(n_year_ago_timestamp)
  40. # 注册时间越小越早
  41. user_id_list = device_info_qs.filter(userID__data_joined__lte=n_year_ago).\
  42. distinct('userID_id').values_list('userID_id', flat=True)
  43. else:
  44. # n-m年,(如2-3年)
  45. n_years_seconds, m_years_seconds = int(n) * 365 * 24 * 60 * 60, int(m) * 365 * 24 * 60 * 60
  46. n_year_ago_timestamp = now_time - n_years_seconds
  47. m_year_ago_timestamp = now_time - m_years_seconds
  48. # 时间戳转时间字符串
  49. n_year_ago = CommonService.timestamp_to_str(n_year_ago_timestamp) # 2021
  50. m_year_ago = CommonService.timestamp_to_str(m_year_ago_timestamp) # 2020
  51. # 2020 <= 注册时间 <= 2021
  52. user_id_list = device_info_qs.\
  53. filter(userID__data_joined__gte=m_year_ago, userID__data_joined__lte=n_year_ago).\
  54. distinct('userID_id').values_list('userID_id', flat=True)
  55. return user_id_list
  56. @classmethod
  57. def push_and_save_sys_msg(cls, **kwargs):
  58. """
  59. 推送和保存系统消息
  60. @param kwargs: 参数
  61. @return:
  62. """
  63. user_id_list = kwargs['user_id_list']
  64. title = kwargs['title']
  65. msg = kwargs['msg']
  66. icon_link = kwargs['icon_link'] if kwargs['icon_link'] != '' else None
  67. # 推送
  68. n_time = int(time.time())
  69. if kwargs['push_app'] == 'ZosiSmart':
  70. app_bundle_id_list = ['com.ansjer.zccloud_a', 'com.ansjer.zccloud']
  71. else:
  72. app_bundle_id_list = ['com.ansjer.zccloud_ab', 'com.ansjer.customizede']
  73. gateway_push_qs = GatewayPush.objects.filter(user_id__in=user_id_list, app_bundle_id__in=app_bundle_id_list).\
  74. values('user_id', 'app_bundle_id', 'push_type', 'token_val')
  75. for gateway_push in gateway_push_qs:
  76. push_succeed = False
  77. # ios
  78. if gateway_push['push_type'] == 0:
  79. push_succeed = PushObject.ios_apns_push(nickname='', app_bundle_id=gateway_push['app_bundle_id'],
  80. token_val=gateway_push['token_val'], n_time=n_time,
  81. event_type=0, msg_title=title, msg_text=msg,
  82. launch_image=icon_link)
  83. # gcm
  84. elif gateway_push['push_type'] == 1:
  85. if icon_link is None:
  86. icon_link = ''
  87. push_succeed = PushObject.android_fcm_push(nickname='', app_bundle_id=gateway_push['app_bundle_id'],
  88. token_val=gateway_push['token_val'], n_time=n_time,
  89. event_type=0, msg_title=title, msg_text=msg, image=icon_link)
  90. # 华为
  91. elif gateway_push['push_type'] == 3:
  92. huawei_push_object = HuaweiPushObject()
  93. push_succeed = huawei_push_object.\
  94. send_push_notify_message(token_val=gateway_push['token_val'], msg_title=title,
  95. msg_text=msg, n_time=n_time, image_url=icon_link)
  96. # 推送成功,写入系统消息
  97. if push_succeed:
  98. SysMsgModel.objects.create(userID_id=gateway_push_qs['user_id'], title=title, msg=msg, addTime=n_time,
  99. updTime=n_time)
  100. CUSTOMIZED_PUSH_LOGGER.info('customized_push_id:{}推送完成'.format(kwargs['id']))