123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- # -*- coding: utf-8 -*-
- """
- @Author : Rocky
- @Time : 2022/9/16 10:39
- @File :PowerWarningController.py
- """
- import logging
- import time
- from django.http import JsonResponse
- from django.views.generic.base import View
- from Model.models import UidPushModel, SysMsgModel, Device_Info
- from Object.RedisObject import RedisObject
- from Service.HuaweiPushService.HuaweiPushService import HuaweiPushObject
- from Service.PushService import PushObject
- class PowerWarningView(View):
- # 低电量推送接口
- def get(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- return self.validation(request.GET)
- def post(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- return self.validation(request.POST)
- @staticmethod
- def validation(request_dict):
- logger = logging.getLogger('info')
- uid = request_dict.get('uid', None)
- # 限制每6小时推一次
- redis_obj = RedisObject()
- is_limit = redis_obj.CONN.setnx(uid + 'limit_power_warning', 1)
- redis_obj.CONN.expire(uid + 'limit_power_warning', 6 * 60 * 60)
- if not is_limit:
- return JsonResponse(status=200, data={'code': 0, 'msg': 'push limited!'})
- channel = request_dict.get('channel', None)
- electricity = request_dict.get('electricity', None)
- logger.info('调用低电量推送接口的uid: {},electricity: {}'.format(uid, electricity))
- try:
- uid_push_qs = UidPushModel.objects.filter(uid_set__uid=uid). \
- values('push_type', 'appBundleId', 'token_val', 'lang', 'tz', 'userID_id')
- if not uid_push_qs.exists():
- res_data = {'code': 173, 'msg': 'uid push data not exit!'}
- return JsonResponse(status=200, data=res_data)
- uid_push_list = [uid_push for uid_push in uid_push_qs]
- # 查询设备数据
- device_info_qs = Device_Info.objects.filter(UID=uid).first()
- nickname = uid if device_info_qs is None else device_info_qs.NickName
- now_time = int(time.time())
- event_type = 704
- sys_msg_list = []
- user_id_list = []
- for uid_push in uid_push_list:
- push_type = uid_push['push_type']
- app_bundle_id = uid_push['appBundleId']
- token_val = uid_push['token_val']
- lang = uid_push['lang']
- tz = uid_push['tz']
- if tz is None or tz == '':
- tz = 0
- # 推送标题和推送内容
- msg_title = PushObject.get_msg_title(nickname=nickname)
- msg_text = PushObject.get_low_power_msg_text(channel=channel, n_time=now_time, lang=lang, tz=tz,
- electricity=electricity)
- kwargs = {
- 'nickname': nickname,
- 'app_bundle_id': app_bundle_id,
- 'token_val': token_val,
- 'n_time': now_time,
- 'event_type': event_type,
- 'msg_title': msg_title,
- 'msg_text': msg_text,
- 'uid': uid,
- 'channel': channel,
- }
- try:
- # 推送消息
- if push_type == 0: # ios apns
- PushObject.ios_apns_push(**kwargs)
- elif push_type == 1: # android gcm
- PushObject.android_fcm_push_v1(**kwargs)
- elif push_type == 2: # android jpush
- kwargs.pop('uid')
- PushObject.android_jpush(**kwargs)
- elif push_type == 3:
- huawei_push_object = HuaweiPushObject()
- huawei_push_object.send_push_notify_message(**kwargs)
- elif push_type == 4: # android 小米推送
- channel_id = 104551
- PushObject.android_xmpush(channel_id=channel_id, **kwargs)
- elif push_type == 5: # android vivo推送
- PushObject.android_vivopush(**kwargs)
- elif push_type == 6: # android oppo推送
- channel_id = 'VALUE_ADDED'
- PushObject.android_oppopush(channel_id=channel_id, **kwargs)
- elif push_type == 7: # android 魅族推送
- PushObject.android_meizupush(**kwargs)
- except Exception as e:
- logger.info('低电量推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
- continue
- # 保存系统消息
- user_id = uid_push['userID_id']
- if user_id not in user_id_list:
- sys_msg_text = PushObject.get_low_power_msg_text(channel=channel, n_time=now_time, lang=lang, tz=tz,
- electricity=electricity, is_sys=1)
- sys_msg_list.append(SysMsgModel(
- userID_id=user_id,
- msg=sys_msg_text,
- addTime=now_time,
- updTime=now_time,
- uid=uid,
- eventType=event_type,
- ))
- user_id_list.append(user_id)
- SysMsgModel.objects.bulk_create(sys_msg_list)
- return JsonResponse(status=200, data={'code': 0})
- except Exception as e:
- logger.info('低电量推送接口异常: {}'.format(e))
- return JsonResponse(status=500, data={'msg': 'power warning error'})
|