hxp
5 天以前 388823edfe6308cba6f76ca6dc4f20022c5cb2be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
##@package
#
# @todo: 受理HTTP请求
#
# @author: Alee
# @date 2018-5-4 22:10:51
# @version 1.0
#
# @note: 中文必须传utf8,收到的为urlencode内容  .decode('gbk').encode('utf8')
#
#---------------------------------------------------------------------
 
from bottle import Bottle, request
import logging
import jpush
import requests
import urllib
from lib import ConfigIniReader
import json
 
#app_key = u'22186239fee975f883198cf4'
#master_secret = u'cb2b72ba143a3f3fb13a6f45'
 
#_jpush = jpush.JPush(app_key, master_secret)
#_jpush.set_logging("DEBUG")
 
requests.packages.urllib3.disable_warnings()
 
# get: request.query.username request.GET.get('username','')
# post: request.forms.get('username')  request.POST.get('username')
#===============================================================================
# @myapp.route('/cool/kk/:name3/:count#\\d+#')
# def maybe(name3, count):
#===============================================================================
 
myapp = Bottle()
 
Def_Split_Mark = "|"
 
# 文本格式GBK取出出来转unicode,服务器发过来也是unicode 推送统一unicode
Def_NoName = ConfigIniReader.GetConfig().GetValue('Def_NoName').decode("gbk")
NotifyFormat = ConfigIniReader.GetConfig().GetValue('NotifyFormat').decode("gbk")
 
 
# 推送消息
# 混服使用对应不同key {平台ID:[[玩家个推ID, 玩家名],[玩家个推ID2, 玩家名2]。。。]}
# {u'822055139': [[u'160a3797c8504368d60', u'\u76f8\u601d\u59ff\u6653']]}-1234567
 
@myapp.route('/getui/index.php', method='POST')
def JGGeTui():
    getDict = request.POST
    notifyMsg = urllib.unquote(getDict.get("NotifyMsg","")).decode("utf8")
    playerDict = json.loads(urllib.unquote_plus(getDict.get("PlayerInfo", "")))
    
    logging.info("%s-%s"%(playerDict, notifyMsg))
    if not playerDict:
        # 只能单\多推,不能全部推
        return
    
    osName = jpush.all_
        
    #logging.info("%s-%s-%s"%(getDict.get("RegID",""), playrNames, notifyMsg))
    #return "账号:" + getDict.get("AccountID",""), "充值", getDict.get("money","") 
    
    # 约定 {appID:[推送ID1, 推送ID2...]} 为群推
    # 约定 {appID:[[推送ID1, 玩家名称1], [推送ID2, 玩家名称2]...]} 为一一个推
    for appID in playerDict:
        # 找到对应KEY
        try:
            app_key = ConfigIniReader.GetConfig().GetValue('app_key_%s'%appID)
            master_secret = ConfigIniReader.GetConfig().GetValue('master_secret_%s'%appID)
        except:
            logging.warn("no key = %s"%appID)
            continue
        jpushObj = jpush.JPush(app_key, master_secret)
        
        if type(playerDict[appID][0]) != list:  # 列表中的元素不是列表说明是群推
            PushOne(jpushObj, playerDict[appID], notifyMsg, osName)
        else:
            # 不支持notifymsg内容不一样的多推,只能一一对应推送
            for regID, playerName in playerDict[appID]:
                playerName = playerName if playerName else Def_NoName
                #playerName = urllib.unquote(playerName)
                #playerName = playerName.encode('utf-8')
                geTuiMsg = NotifyFormat%(playerName, notifyMsg)
                #logging.info("PushOne%s-%s-%s"%([regID], geTuiMsg, osName))
                PushOne(jpushObj, [regID], geTuiMsg, osName)
 
        
def PushOne(jpushObj, regIDList, notifyMsg, osName=jpush.all_):
    # 1.推送对象 单推(多推);  群推只能提交中心推
    # 2.平台 ios  安卓  默认全平台
    # 3.通知消息
    
    push = jpushObj.create_push()
    push.audience =  {"registration_id" : regIDList}
    push.notification = jpush.notification(alert=notifyMsg)
    push.platform = osName
    push.options = {"time_to_live":300}
    #push.options = {"apns_production": False,}
 
    try:
        response=push.send()
    except:
        pass