From c2bc7f00ca8508e767f478109970f1707fb590fe Mon Sep 17 00:00:00 2001
From: hch <305670599@qq.com>
Date: 星期五, 21 九月 2018 18:08:30 +0800
Subject: [PATCH] 1 礼包卡以权限分应用

---
 Tool/WebCenter/CouponCode/webapp.py |  323 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 323 insertions(+), 0 deletions(-)

diff --git a/Tool/WebCenter/CouponCode/webapp.py b/Tool/WebCenter/CouponCode/webapp.py
new file mode 100644
index 0000000..97cb5b8
--- /dev/null
+++ b/Tool/WebCenter/CouponCode/webapp.py
@@ -0,0 +1,323 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+##@package
+#
+# @todo: 婵�娲荤爜
+#
+# @author: Alee
+# @date 2018-5-4 22:10:51
+# @version 1.0
+#
+# @note: 涓枃蹇呴』浼爑tf8锛屾敹鍒扮殑涓簎rlencode鍐呭  .decode('gbk').encode('utf8')
+# 1.浣跨敤绀煎寘鍗″彿
+#---------------------------------------------------------------------
+
+from bottle import Bottle, request, static_file
+import md5
+import CouponDB
+import datetime
+from lib import ReadConfig
+import json
+import urllib2
+import urllib
+from lib import mylog, CommFunc
+import os.path
+import zipfile
+
+# 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):
+# client_ip = request.environ.get('REMOTE_ADDR')
+# client_ip = request.environ.get('HTTP_X_FORWARDED_FOR') or request.environ.get('REMOTE_ADDR')
+#===============================================================================
+
+ConfigIO = ReadConfig.ReadConfig(os.getcwd() + "\\..\\Coupon\\config.ini")
+
+# 搴旂敤绔彛
+CenterPort = ConfigIO.GetValue("Coupon", "CenterPort")
+#mobiletest.173on.com:55000
+Def_ExportUrl = ConfigIO.GetValue("Coupon", "exporturl") + "/Coupon/CouponLoad.php?couponid=%s&channel=%s"
+
+# GM绔彛
+PushPort = ConfigIO.GetValue("Coupon", "PushPort")
+PushKey = ConfigIO.GetValue("Coupon", "PushKey")
+CommonCards = eval(ConfigIO.GetValue("Coupon", "CommonCards"))
+                
+        
+Def_CardMsg = {
+               0:"CodeRewardSys2", #  鍏戞崲鐮佹棤鏁�
+               1: "CodeRewardSys1", #   鍏戞崲鍗″鍔卞凡鍙戣嚦閭欢
+               2:"CodeRewardSys3", #  鍏戞崲鐮佸凡浣跨敤杩�
+               3:"CodeRewardSys4",  # 鍚岀被鍨嬪厬鎹㈢爜鍙兘浣跨敤涓�娆�
+         }
+                
+                
+                
+myapp = Bottle()
+
+
+
+#/api/Coupon/index.php?couponBatchId=%s&agentName=%s"
+# 涓嬭浇鎸囧畾绀煎寘鎵规 
+@myapp.route('/Coupon/CouponLoad.php')
+def LoadCouponBatch():
+    dataDict = request.GET
+    appid = dataDict.get("channel", "")     # 杩愯惀鎻愪緵鐨凙PPID锛屽嵆娓犻亾
+    if not appid:
+        mylog.debug("no appid")
+        return json.dumps({"error":"param appid"}, ensure_ascii=False)
+    couponid = dataDict.get("couponid", "")
+    if not couponid:
+        return json.dumps({"error":"param couponid"}, ensure_ascii=False)
+    
+    downFile = r".\download\%s.zip"%couponid
+    if os.path.isfile(downFile):
+        return static_file("%s.zip"%couponid, r".\download", download=True)
+    
+    dbController = CouponDB.GetDBEventCon()
+    if not dbController:
+        # 鏃犳硶鑾峰彇鏁版嵁搴�
+        mylog.debug("no dbController")
+        return json.dumps({"error":"db"}, ensure_ascii=False)
+    result, data = dbController.find(CouponDB.CouponCodeColName + "_" + appid, {'channel':appid, 'couponid':couponid})
+    if not data:
+        return json.dumps({"error":"no couponid"}, ensure_ascii=False)
+    
+    fileIO = open(r".\download\%s.txt"%couponid, 'w')
+
+    for codeInfo in data:
+        fileIO.write(codeInfo["code"] + "\n")
+    fileIO.close() 
+    
+    f = zipfile.ZipFile(downFile, 'w', zipfile.ZIP_DEFLATED)
+    f.write(r".\download\%s.txt"%couponid)
+    f.close()
+    return static_file("%s.zip"%couponid, r".\download", download=True)
+
+
+# 鍗$被鍨� 寮�澶村瓧姣嶅仛鏍囪瘑濡� g1~999浠h〃姝ゅ崱鍙兘浣跨敤涓�娆� h1~999鍗″彲浠ラ噸澶嶄娇鐢� z寮�澶翠唬琛ㄥ浐瀹氱爜
+# 鐜╁浣跨敤鍗″彿 鍙傛暟 骞冲彴 鍖烘湇 璐﹀彿 鍖烘湇閾炬帴锛圙M鐢級鍗″彿
+# http://center.xxx.com:53003/Coupon/CouponCode.php?channel=appid&code=XXX&accid=YYY&sid=222&pushurl=s222.xxx.com
+@myapp.route('/Coupon/CouponCode.php')
+def CouponCode():
+    dataDict = request.GET
+    
+    for key, value in dataDict.items():
+        mylog.debug("CouponCode key:%s  value:%s"%(key, value))
+    
+    agentName = dataDict.get("channel", "")     # 杩愯惀鎻愪緵鐨凙PPID锛屽嵆娓犻亾
+    if not agentName:
+        mylog.debug("no appid")
+        return
+    codeStr = dataDict.get("code", "")
+    if not codeStr:
+        return
+    
+    accid = dataDict.get("accid", "")
+    if not accid:
+        return
+    
+    sid = CommFunc.ToIntDef(dataDict.get("sid", 0), 0)
+    if not sid:
+        return
+    pushurl = dataDict.get("pushurl", "")
+    if not pushurl:
+        return
+    
+    # 杞寲涓烘父鎴忚处鍙� 
+    accid = "%s@%s@s%s"%(accid, agentName, sid)
+    
+    dbController = CouponDB.GetDBEventCon()
+    if not dbController:
+        # 鏃犳硶鑾峰彇鏁版嵁搴�
+        mylog.debug("no dbController")
+        return
+
+    #閫氳繃GM鎺ュ彛鍛婄煡鐜╁浣跨敤鎯呭喌
+    gmresult = {'accID':accid, 'sid':sid, 'channel':agentName}
+
+    #-----------缁熶竴鍥哄畾鐮佸鐞�----------------------
+    if codeStr in CommonCards:
+        # 涓嶅悓鍥哄畾鐮佸悇鑷彧鑳介涓�娆�
+        result, commondata = dbController.find_one(CouponDB.CouponCodeColName + "_Common", {"code":codeStr, "accid":accid})
+        if commondata:
+            # 宸蹭娇鐢�
+            gmresult['status'] = 2
+            mylog.debug("common status=2")
+            return SendGm(gmresult, pushurl)
+        
+        gmresult['status'] = 1
+        gmresult['code'] = codeStr
+        gmresult['coupontype'] = codeStr  # 缁熶竴鏍煎紡锛屾湇鍔″櫒涓嶄竴瀹氭湁鐢紝鍥哄畾鐮佹牴鎹� codeStr 鍙戝鍔�
+        
+        dbController.insert(CouponDB.CouponCodeColName + "_Common", {"code":codeStr, "accid":accid})
+        mylog.debug("common ok")
+        return SendGm(gmresult, pushurl)
+
+    result, data = dbController.find_one(CouponDB.CouponCodeColName + "_" + agentName, {"code":codeStr, "channel":agentName})
+
+    if not data:
+        #鏃犳鍗�
+        gmresult['status'] = 0
+        mylog.debug("no card")
+        return SendGm(gmresult, pushurl)
+    if int(data['status']) == 1:
+        #宸蹭娇鐢紝鍚屽崱鍙疯褰曢粯璁ゅ彲鐢紝閬垮厤鏂嚎鍙戦�佸け璐ョ殑鎯呭喌锛堟湭杩斿洖缁撴灉楠岃瘉鎯呭喌涓嬶級
+        gmresult['status'] = 2
+        mylog.debug("card used!")
+        return SendGm(gmresult, pushurl)
+    
+    result, data2 = dbController.find_one(CouponDB.CouponBatchColName, {"couponid":data["couponid"]})
+    if not data2:
+        #姝ゆ壒娆″崱宸插垹闄�
+        gmresult['status'] = 0
+        mylog.debug("no couponid")
+        return SendGm(gmresult, pushurl)
+    if int(data2['status']) == 1:
+        #鏆傚仠浣跨敤璇ユ壒娆″崱
+        gmresult['status'] = 0
+        mylog.debug("couponid frozed")
+        return SendGm(gmresult, pushurl)
+    if (data2["minserverno"] != 0 and data2["maxserverno"] != 0):
+        if int(data2["minserverno"]) > sid or int(data2["maxserverno"]) < sid:
+            #涓嶅湪鎸囧畾鍖�
+            gmresult['status'] = 0
+            mylog.debug("no sid")
+            return SendGm(gmresult, pushurl)
+    if data2["expiretime"] != "" and datetime.datetime.today() > GetDateTimeByStr(data2["expiretime"]):
+        #宸茶繃鏈�
+        gmresult['status'] = 0
+        mylog.debug("time pass")
+        return SendGm(gmresult, pushurl)
+    #鍐嶆煡涓�娆℃槸鍚︾敤杩囨鎵规鐨勫叾浠栧崱, 
+    result, data3 = dbController.find_one(CouponDB.CouponCodeColName + "_" + agentName, \
+                                          {"couponid":data["couponid"], "accid":accid})
+    if data3:
+        #鐢ㄨ繃鍚岀被鍗★紝宸茶褰曡繃涓嶅啀鍋氭柊璁板綍
+        gmresult['status'] = 3
+        gmresult['coupontype'] = data["coupontype"]
+        gmresult['code'] = codeStr
+        mylog.debug("use again")
+        return SendGm(gmresult, pushurl)
+    
+    data['status'] = 1
+    data['accid'] = accid
+    data['usetime'] = str(datetime.datetime.today())
+    
+    dbController.update(CouponDB.CouponCodeColName + "_" + agentName, {"code":codeStr, "channel":agentName}, data)
+    # 0 涓嶅彲鐢紝1.鍙敤鍙戞斁濂栧姳 锛�2.宸蹭娇鐢� 3.鍚岀被鍨嬪崱涓嶈兘浣跨敤涓ゆ
+    gmresult['status'] = 1
+    gmresult['code'] = codeStr
+    gmresult['coupontype'] = data["coupontype"]
+    
+    mylog.debug("card ok!")
+    return SendGm(gmresult, pushurl)
+
+
+
+def SendGm(gmresult, pushurl):
+    try:
+        # GM鎺ㄩ�佸湴鍧�
+        #GMToolPage = http://s1.yhlz.09ge.com:30001/Server/Tool.php
+        gmurl = "http://%s:%s/Server/Tool.php"%(pushurl, PushPort)
+        gmkey = PushKey
+        if not gmkey or not gmurl:
+            return
+        
+        useStatus = gmresult['status']
+        
+        gmresult['notifyMsg'] = Def_CardMsg.get(useStatus, "CodeRewardSys2")
+        
+        
+        if useStatus != 1:
+            #澶辫触绯荤粺鎻愮ず
+            pack_data = {};
+            pack_data["queryType"] = "accID"
+            pack_data["playerFind"] = gmresult["accID"]
+            pack_data["pack_type"] = "GMT_MediaCard"
+            pack_data["notifyMsg"] = gmresult['notifyMsg']
+        
+            pack_data["key"] = gmkey;
+            pack_data['coding'] = "utf8";
+        
+            #浣跨敤key鍔犲瘑
+            pack_data_dict = json.dumps(pack_data)
+            sign = md5.md5(pack_data_dict+gmkey).hexdigest()
+            post = {}
+            post['pack'] = pack_data_dict;
+            post['sign'] = sign;
+            urllib2.urlopen(gmurl, urllib.urlencode(post), 3)
+            return
+        
+        mylog.debug("SendGm:%s"%gmurl)
+        pack_data = {};
+        pack_data["queryType"] = "accID"
+        pack_data["playerList"] = gmresult["accID"]
+        pack_data["Title"] = ConfigIO.GetValue(gmresult["coupontype"], "MailTitle").decode("gbk")
+        pack_data["Text"] = ConfigIO.GetValue(gmresult["coupontype"], "MailText").decode("gbk")
+        pack_data["EndTime"] = str(datetime.datetime.today() + datetime.timedelta(days=15)).split('.')[0]
+        pack_data["Sender"] = ConfigIO.GetValue(gmresult["coupontype"], "MailSender").decode("gbk")
+
+        
+        Items =  eval(ConfigIO.GetValue(gmresult["coupontype"], "Items"))
+        pack_data["itemNums"] = ','.join([str(i) for i in range(len(Items))])
+        i = 0
+        for itemList in Items:
+            pack_data["ItemID%s"%i] = str(itemList[0])
+            pack_data["ItemCnt%s"%i] = str(itemList[1])
+            pack_data["IsBind%s"%i] = '1'
+            i += 1
+            
+        pack_data["pack_type"] = "GMT_AddPersonalCompensation"
+        pack_data["Detail"] = "cardCode:" + gmresult["code"]
+
+        pack_data["key"] = gmkey;
+        pack_data['coding'] = "utf8";
+    
+        #浣跨敤key鍔犲瘑
+        pack_data_dict = json.dumps(pack_data)
+        sign = md5.md5(pack_data_dict+gmkey).hexdigest()
+        post = {}
+        post['pack'] = pack_data_dict;
+        post['sign'] = sign;
+        result = urllib2.urlopen(gmurl, urllib.urlencode(post), 10)
+
+        content = result.read()
+        result.close()
+        
+        # 鎴愬姛鎻愮ず
+        pack_data = {};
+        pack_data["queryType"] = "accID"
+        pack_data["playerFind"] = gmresult["accID"]
+        pack_data["pack_type"] = "GMT_MediaCard"
+        pack_data["notifyMsg"] = gmresult['notifyMsg']
+    
+        pack_data["key"] = gmkey;
+        pack_data['coding'] = "utf8";
+    
+        #浣跨敤key鍔犲瘑
+        pack_data_dict = json.dumps(pack_data)
+        sign = md5.md5(pack_data_dict+gmkey).hexdigest()
+        post = {}
+        post['pack'] = pack_data_dict;
+        post['sign'] = sign;
+        urllib2.urlopen(gmurl, urllib.urlencode(post), 3)
+        
+        return content
+    except Exception, e:
+        mylog.debug("gm error %s"%e)
+    return
+    
+    
+def GetDateTimeByStr(timeStr, timeFomat="%Y-%m-%d"):
+    try:
+        return datetime.datetime.strptime(timeStr, timeFomat)
+    
+    except:
+        mylog.debug("GetDateTimeByStr error %s"%timeStr)
+    
+    return
+    

--
Gitblit v1.8.0