hxp
2023-12-19 0391cc7daf3d0236c24594f0266ff3b56b1b6b82
Tool/webbottle/webapp.py
@@ -18,11 +18,13 @@
import jpush
import requests
import urllib
from lib import ConfigIniReader
import json
app_key = u'22186239fee975f883198cf4'
master_secret = u'cb2b72ba143a3f3fb13a6f45'
#app_key = u'22186239fee975f883198cf4'
#master_secret = u'cb2b72ba143a3f3fb13a6f45'
_jpush = jpush.JPush(app_key, master_secret)
#_jpush = jpush.JPush(app_key, master_secret)
#_jpush.set_logging("DEBUG")
requests.packages.urllib3.disable_warnings()
@@ -37,55 +39,63 @@
myapp = Bottle()
Def_Split_Mark = "|"
Def_NoName = "亲"
NotifyFormat = "【%s】%s" # 通知格式  【玩家名】信息
# 文本格式GBK取出出来转unicode,服务器发过来也是unicode 推送统一unicode
Def_NoName = ConfigIniReader.GetConfig().GetValue('Def_NoName').decode("gbk")
NotifyFormat = ConfigIniReader.GetConfig().GetValue('NotifyFormat').decode("gbk")
# 推送消息
# ?RegID=111141|1414|2141&PlayerName=熟练度空间|圣诞快乐|sjlkd&OSName=ios&NotifyMsg=BOSS复活
# 混服使用对应不同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",""))
    logging.info("%s-%s-%s"%(getDict.get("RegID",""), getDict.get("PlayerName",""), notifyMsg))
    if not getDict.get("RegID",""):
    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
    
    regIDs = getDict.get("RegID","").split(Def_Split_Mark)
    playrNames = getDict.get("PlayerName","")
    osName = getDict.get("OSName","")
    if not osName or osName == jpush.all_:
        osName = jpush.all_
    else:
        osName = getDict.get("OSName","").split(Def_Split_Mark)
    osName = jpush.all_
        
    #logging.info("%s-%s-%s"%(getDict.get("RegID",""), playrNames, notifyMsg))
    #return "账号:" + getDict.get("AccountID",""), "充值", getDict.get("money","") 
    
    if not playrNames:
        PushOne(regIDs, notifyMsg, osName)
    else:
        # 不支持notifymsg内容不一样的多推,只能一一对应推送
        playrNames = playrNames.split(Def_Split_Mark)
        index = 0
        for regID in regIDs:
            playerName = playrNames[index] if index < len(playrNames) else Def_NoName
            playerName = urllib.unquote(playerName)
            geTuiMsg = NotifyFormat%(playerName, notifyMsg)
            #logging.info("PushOne%s-%s-%s"%([regID], geTuiMsg, osName))
            PushOne([regID], geTuiMsg, osName)
            index += 1
    # 约定 {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)
        
def PushOne(regIDList, notifyMsg, osName=jpush.all_):
        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 = _jpush.create_push()
    push = jpushObj.create_push()
    push.audience =  {"registration_id" : regIDList}
    push.notification = jpush.notification(alert=notifyMsg)
    push.platform = osName