ComboCronPushController.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : ComboCronPushController.py
  4. @Time : 2022/7/8 19:52
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import logging
  10. import time
  11. import traceback
  12. from django.db.models import Q
  13. from django.views import View
  14. from Model.models import UnicomComboOrderInfo, UnicomDeviceInfo, GatewayPush, SysMsgModel
  15. from Object.ResponseObject import ResponseObject
  16. from Service.GatewayService import GatewayPushService
  17. class ComboCronPushView(View):
  18. def get(self, request, *args, **kwargs):
  19. request.encoding = 'utf-8'
  20. operation = kwargs.get('operation')
  21. return self.validation(request.GET, request, operation)
  22. def post(self, request, *args, **kwargs):
  23. request.encoding = 'utf-8'
  24. operation = kwargs.get('operation')
  25. return self.validation(request.POST, request, operation)
  26. def validation(self, request_dict, request, operation):
  27. print(request_dict)
  28. print(request)
  29. response = ResponseObject()
  30. if operation == 'expire-push':
  31. return self.combo_expire_push(response)
  32. else:
  33. return response.json(404)
  34. @classmethod
  35. def combo_expire_push(cls, response):
  36. """
  37. 套餐到期预警通知,分别7前3天前消息推送
  38. """
  39. logger = logging.getLogger('info')
  40. logger.info('进入流量包过期消息推送')
  41. try:
  42. now_time = int(time.time())
  43. combo_order_qs = UnicomComboOrderInfo.objects.filter(
  44. ~Q(status=2) & Q(expire_time__gt=now_time + 3600 * 144) & Q(
  45. expire_time__lte=(now_time + 3600 * 168)), is_del=False).values()
  46. if combo_order_qs.exists():
  47. cls.phone_msg_push(combo_order_qs)
  48. combo_order_qs = UnicomComboOrderInfo.objects.filter(
  49. ~Q(status=2) & Q(expire_time__gt=now_time + 3600 * 48) & Q(
  50. expire_time__lte=(now_time + 3600 * 72)), is_del=False).values()
  51. if combo_order_qs.exists():
  52. cls.phone_msg_push(combo_order_qs)
  53. return response.json(0)
  54. except Exception as e:
  55. print(e.args)
  56. ex = traceback.format_exc()
  57. print(ex)
  58. return response.json(177, ex)
  59. @classmethod
  60. def phone_msg_push(cls, combo_order_qs):
  61. """
  62. 消息推送
  63. """
  64. now_time = int(time.time())
  65. logger = logging.getLogger('info')
  66. for item in combo_order_qs:
  67. iccid = item['iccid']
  68. device_info = UnicomDeviceInfo.objects.filter(iccid=iccid).values()
  69. if not device_info.exists():
  70. continue
  71. nickname = device_info.first()['serial_no']
  72. user_id = device_info.first()['user_id']
  73. if not user_id:
  74. continue
  75. # 查询推送配置数据
  76. push_qs = GatewayPush.objects.filter(user_id=user_id, logout=False). \
  77. values('user_id', 'app_bundle_id', 'app_type', 'push_type', 'token_val', 'm_code', 'lang', 'm_code',
  78. 'tz')
  79. if not push_qs.exists():
  80. continue
  81. for push_vo in push_qs:
  82. kwargs = {
  83. 'n_time': now_time,
  84. 'event_type': 1,
  85. 'nickname': nickname,
  86. }
  87. push_type = push_vo['push_type']
  88. token_val = push_vo['token_val']
  89. lang = push_vo['lang']
  90. app_bundle_id = push_vo['app_bundle_id']
  91. # 获取推送所需数据
  92. msg_title = GatewayPushService.get_msg_title(app_bundle_id, nickname)
  93. if lang == 'cn':
  94. sys_msg_text = "温馨提示:尊敬的客户,您" + nickname + "设备4G流量套餐将在" + time.strftime("%Y-%m-%d", time.localtime(
  95. item['expire_time'])) + "到期"
  96. else:
  97. sys_msg_text = 'Dear customer,the flow package for your device ' + nickname + ' will expire on ' + \
  98. time.strftime('%m-%d-%y', time.localtime(item['expire_time']))
  99. kwargs['app_bundle_id'] = app_bundle_id
  100. kwargs['token_val'] = token_val
  101. kwargs['msg_title'] = msg_title
  102. kwargs['msg_text'] = sys_msg_text
  103. cls.sys_msg_save(user_id, nickname, now_time, sys_msg_text)
  104. try:
  105. # ios apns
  106. if push_type == 0:
  107. GatewayPushService.ios_apns_push(**kwargs)
  108. # android gcm
  109. elif push_type == 1:
  110. GatewayPushService.android_fcm_push(**kwargs)
  111. # android 极光推送
  112. elif push_type == 2:
  113. GatewayPushService.android_jpush(**kwargs)
  114. except Exception as e:
  115. logger.info('4G流量预警推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  116. continue
  117. return True
  118. @classmethod
  119. def sys_msg_save(cls, user_id, serial_no, n_time, text_msg):
  120. """
  121. 系统消息存库
  122. """
  123. logger = logging.getLogger('info')
  124. try:
  125. data = {'addTime': n_time, 'updTime': n_time, 'userID_id': user_id, 'eventType': 0, 'msg': text_msg,
  126. 'uid': serial_no}
  127. SysMsgModel.objects.create(**data)
  128. except Exception as e:
  129. logger.info('---4G流量存库异常--- {}'.format(repr(e)))