# -*- encoding: utf-8 -*- """ @File : ComboCronPushController.py @Time : 2022/7/8 19:52 @Author : stephen @Email : zhangdongming@asj6.wecom.work @Software: PyCharm """ import logging import time import traceback from django.db.models import Q from django.views import View from Model.models import UnicomComboOrderInfo, UnicomDeviceInfo, GatewayPush from Object.ResponseObject import ResponseObject from Service.GatewayService import GatewayPushService class ComboCronPushView(View): def get(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') return self.validation(request.GET, request, operation) def post(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') return self.validation(request.POST, request, operation) def validation(self, request_dict, request, operation): print(request_dict) print(request) response = ResponseObject() if operation == 'expire-push': return self.combo_expire_push(response) else: return response.json(404) @classmethod def combo_expire_push(cls, response): logger = logging.getLogger('info') logger.info('进入流量包过期消息推送') try: now_time = int(time.time()) combo_order_qs = UnicomComboOrderInfo.objects.filter( ~Q(status=2) & Q(expire_time__gt=now_time + 3600 * 144) & Q( expire_time__lte=(now_time + 3600 * 168)), is_del=False).values() if combo_order_qs.exists(): cls.phone_msg_push(combo_order_qs) combo_order_qs = UnicomComboOrderInfo.objects.filter( ~Q(status=2) & Q(expire_time__gt=now_time + 3600 * 48) & Q( expire_time__lte=(now_time + 3600 * 72)), is_del=False).values() if combo_order_qs.exists(): cls.phone_msg_push(combo_order_qs) return response.json(0) except Exception as e: print(e.args) ex = traceback.format_exc() print(ex) return response.json(177, ex) @classmethod def phone_msg_push(cls, combo_order_qs): """ 消息推送 """ mow_time = int(time.time()) logger = logging.getLogger('info') for item in combo_order_qs: iccid = item['iccid'] device_info = UnicomDeviceInfo.objects.filter(iccid=iccid).values() if not device_info.exists(): continue nickname = device_info.first()['serial_no'] user_id = device_info.first()['user_id'] if not user_id: continue # 查询推送配置数据 push_qs = GatewayPush.objects.filter(user_id=user_id, logout=False). \ values('user_id', 'app_bundle_id', 'app_type', 'push_type', 'token_val', 'm_code', 'lang', 'm_code', 'tz') if not push_qs.exists(): continue for push_vo in push_qs: kwargs = { 'n_time': mow_time, 'event_type': 1, 'nickname': nickname, } push_type = push_vo['push_type'] token_val = push_vo['token_val'] lang = push_vo['lang'] app_bundle_id = push_vo['app_bundle_id'] # 获取推送所需数据 msg_title = GatewayPushService.get_msg_title(app_bundle_id, nickname) if lang == 'cn': sys_msg_text = "温馨提示:尊敬的客户,您" + nickname + "设备4G流量套餐将在" + time.strftime("%Y-%m-%d", time.localtime( item['expire_time'])) + "到期" else: sys_msg_text = 'Dear customer,the flow package for your device ' + nickname + ' will expire on ' + \ time.strftime('%m-%d-%y', time.localtime(item['expire_time'])) kwargs['app_bundle_id'] = app_bundle_id kwargs['token_val'] = token_val kwargs['msg_title'] = msg_title kwargs['msg_text'] = sys_msg_text try: # ios apns if push_type == 0: GatewayPushService.ios_apns_push(**kwargs) # android gcm elif push_type == 1: GatewayPushService.android_fcm_push(**kwargs) # android 极光推送 elif push_type == 2: GatewayPushService.android_jpush(**kwargs) except Exception as e: logger.info('4G流量预警推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e))) continue return True