| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #  | 
| ##@package  | 
| #  | 
| # @todo:   | 
| #  | 
| # @author: Alee  | 
| # @date 2019-1-22 ÉÏÎç11:50:11  | 
| # @version 1.0  | 
| #  | 
| # @note:   | 
| #  | 
| #---------------------------------------------------------------------  | 
|   | 
| from bottle import Bottle, request  | 
| import md5  | 
| import urllib2  | 
| import urllib  | 
| import json  | 
| from lib import mylog  | 
| import DoubleBillDB  | 
|   | 
| myapp = Bottle()  | 
|   | 
| DoubleBillColName = 'tagDoubleBill'  | 
| GMPort = 80  | 
|   | 
|   | 
| @myapp.route('/DoublieBill.php')  | 
| def OnDoubleBill():  | 
|     # Íæ¼ÒµÇ¼´Ó·þÎñÆ÷ÇëÇó¶Ò»»·µÀû£¬±ØÐë¼Ç¼¶Ò»»½á¹û£¬ÓÃÓÚ¸´²é;ͨ¹ý·ÃÎÊIP£¬²éÕÒGMͨµÀ·µ»Ø½á¹û30001  | 
|     # ²ÎÊý£ºÍæ¼ÒÕ˺ţ¬²éѯÊÇ·ñÓгäÖµ·µÀû  | 
|     # ·µ»Ø1£ºÎÞ³äÖµ£¬2£ºÒѶһ»£¨Ö»ÄÜÒ»¸ö·þ£©£¬ÆäËû¶î¶ÈΪÏÉÓñ  | 
|     # Êý¾Ý¿â×ֶΠÇþµÀSpID Õ˺ÅAccID ÏÉÓñGold ¶Ò»»ËùÔÚ·þServerID  | 
|     dataDict = request.GET  | 
|       | 
|     gameAccID = dataDict.get("AccID", "")     # ÔËÓªÌṩµÄAPPID£¬¼´ÇþµÀ  | 
|     if not gameAccID:  | 
|         print "accID Error", gameAccID  | 
|         return  | 
|       | 
|     try:  | 
|         accID, spID, sid = gameAccID.split("@")  | 
|     except:  | 
|         print "accID Error", gameAccID  | 
|         return  | 
|       | 
|     if not sid:  | 
|         return  | 
|       | 
|     dbController = DoubleBillDB.GetDBEventCon()  | 
|     if not dbController:  | 
|         # ÎÞ·¨»ñÈ¡Êý¾Ý¿â  | 
|         mylog.debug("no dbController")  | 
|         return  | 
|     result, data = dbController.find(DoubleBillColName, {"SpID":spID, "AccID":accID})  | 
|     if not data:  | 
|         # Î޼Ǽ²»ÄÜÁìÈ¡£¬GM·µ»Ø1, ¼õÉÙ·´¸´²éѯ  | 
|         SendGm(gameAccID, 1)  | 
|         return  | 
|       | 
|     rec = data[0]  | 
|     if not rec["Gold"]:  | 
|         # ÎÞÏÉÓñ  | 
|         return  | 
|       | 
|     if rec["ServerID"]:  | 
|         if sid == rec["ServerID"]:  | 
|             #ͬ·þÒÑÁìÈ¡µÄ·µ»ØÏàͬ  | 
|             SendGm(gameAccID, rec["Gold"])  | 
|             return  | 
|         # ²»Í¬·þÒÑÁìÈ¡£¬GM·µ»Ø2, ¼õÉÙ·´¸´²éѯ  | 
|         SendGm(gameAccID, 2)  | 
|         return  | 
|       | 
|     updateDoc = {"SpID":spID, "AccID":accID, "Gold":rec["Gold"], "ServerID":sid}  | 
|     dbController.update(DoubleBillColName, {"SpID":spID, "AccID":accID}, updateDoc)  | 
|       | 
|     mylog.info("OnDoubleBill:%s-%s"%(gameAccID, updateDoc))  | 
|     SendGm(gameAccID, rec["Gold"])  | 
|     return  | 
|   | 
| def SendGm(gameAccID, dbState):  | 
|     try:  | 
|         serverIP = request.environ.get("REMOTE_ADDR", "0.0.0.0")  | 
|         # GMÍÆË͵ØÖ·  | 
|         #GMToolPage = http://s1.yhlz.09ge.com:30001/Server/Tool.php  | 
|         gmurl = "http://%s:%s/Server/Tool.php"%(serverIP, GMPort)  | 
|         gmkey = "Y25GVFoyOVFjbWtyTDJJckt5OU1OQ3RtUFQxPV"  | 
|         if not gmkey or not gmurl:  | 
|             return  | 
|           | 
|         pack_data = {}  | 
|         pack_data["AccID"] = gameAccID  | 
|         # ·µ»Ø1£ºÎÞ³äÖµ£¬2£ºÒѶһ»£¨Ö»ÄÜÒ»¸ö·þ£©£¬ÆäËû¶î¶ÈΪÏÉÓñ  | 
|         pack_data["GoldState"] = str(dbState)  | 
|   | 
|         pack_data["pack_type"] = "GMT_QDFLDoubleBill"  | 
|         pack_data["key"] = gmkey;  | 
|         pack_data['coding'] = "utf8"  | 
|       | 
|         #ʹÓÃkey¼ÓÃÜ  | 
|         pack_data_dict = json.dumps(pack_data)  | 
|   | 
|         mylog.debug("OnDoubleBill SendGm:%s-%s"%(gmurl, pack_data_dict))  | 
|         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()  | 
|   | 
|         mylog.info("ÍÆËͽá¹û GMT_QDFLDoubleBill:%s"%(content))  | 
|         return content  | 
|     except Exception, e:  | 
|         mylog.debug("gm error %s"%e)  | 
|     return  | 
|   | 
|   | 
| # ²éѯËùÓеÄÊý¾Ý£¬Ö»ÔÚµÚÒ»ÕŵØÍ¼´¦Àí£¬»º´æ±ÜÃâÇëÇó¹ýÓÚÆµ·±  | 
| @myapp.route('/QueryAllDoubleBill.php')  | 
| def QueryAllDoubleBill():  | 
|     dataDict = request.GET  | 
|       | 
|     checkInfo = dataDict.get("ccccc", "")  | 
|     if not checkInfo:  | 
|         return  | 
|     if checkInfo != "fanggongji":  | 
|         return  | 
|       | 
|     dbController = DoubleBillDB.GetDBEventCon()  | 
|     if not dbController:  | 
|         # ÎÞ·¨»ñÈ¡Êý¾Ý¿â  | 
|         mylog.debug("no dbController")  | 
|         return  | 
|     # ·µ»ØÎ´ÁìÈ¡µÄ  | 
|     result, data = dbController.find(DoubleBillColName, {"ServerID":""})  | 
|     if not data:  | 
|         return  | 
|       | 
|     recDict = {}  | 
|     for rec in data:  | 
|         spID = rec["SpID"]  | 
|         if spID not in recDict:  | 
|             recDict[spID] = [rec["AccID"]]  | 
|         else:  | 
|             recDict[spID].append(rec["AccID"])  | 
|       | 
|     if not recDict:  | 
|         return  | 
|     try:  | 
|         serverIP = request.environ.get("REMOTE_ADDR", "0.0.0.0")  | 
|         # GMÍÆË͵ØÖ·  | 
|         #GMToolPage = http://s1.yhlz.09ge.com:30001/Server/Tool.php  | 
|         gmurl = "http://%s:%s/Server/Tool.php"%(serverIP, GMPort)  | 
|         gmkey = "Y25GVFoyOVFjbWtyTDJJckt5OU1OQ3RtUFQxPV"  | 
|         if not gmkey or not gmurl:  | 
|             return  | 
|           | 
|         mylog.debug("SendGm:%s"%gmurl)  | 
|         pack_data = {}  | 
|         pack_data["tomap"] = json.dumps(recDict)  | 
|         pack_data["pack_type"] = "GMT_AllDoubleBill"  | 
|         pack_data["key"] = gmkey;  | 
|         pack_data['coding'] = "utf8"  | 
|       | 
|         #ʹÓÃkey¼ÓÃÜ  | 
|         pack_data_dict = json.dumps(pack_data)  | 
|           | 
|         mylog.debug("QueryAllDoubleBill SendGm:%s-%s"%(gmurl, pack_data_dict))  | 
|         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)  | 
|   | 
|         result.close()  | 
|   | 
|     except Exception, e:  | 
|         mylog.debug("gm error %s"%e)  | 
|       | 
|       | 
| # 1.²éѯ  | 
| @myapp.route('/QueryDoubleBill.php')  | 
| def QueryDoubleBill():  | 
|     # Íæ¼ÒµÇ¼´Ó·þÎñÆ÷ÇëÇó¶Ò»»·µÀû£¬±ØÐë¼Ç¼¶Ò»»½á¹û£¬ÓÃÓÚ¸´²é;ͨ¹ý·ÃÎÊIP£¬²éÕÒGMͨµÀ·µ»Ø½á¹û30001  | 
|     # ²ÎÊý£ºÍæ¼ÒÕ˺ţ¬²éѯÊÇ·ñÓгäÖµ·µÀû  | 
|     # ·µ»Ø1£ºÎÞ³äÖµ£¬2£ºÒѶһ»£¨Ö»ÄÜÒ»¸ö·þ£©£¬ÆäËû¶î¶ÈΪÏÉÓñ  | 
|     # Êý¾Ý¿â×ֶΠÇþµÀSpID Õ˺ÅAccID ÏÉÓñGold ¶Ò»»ËùÔÚ·þServerID  | 
|     dataDict = request.GET  | 
|       | 
|     gameAccID = dataDict.get("AccID", "")     # ÔËÓªÌṩµÄAPPID£¬¼´ÇþµÀ  | 
|     if not gameAccID:  | 
|         print "accID Error", gameAccID  | 
|         return  | 
|       | 
|     try:  | 
|         accID, spID, sid = gameAccID.split("@")  | 
|     except:  | 
|         print "accID Error", gameAccID  | 
|         return  | 
|       | 
|     if not sid:  | 
|         return  | 
|       | 
|     dbController = DoubleBillDB.GetDBEventCon()  | 
|     if not dbController:  | 
|         # ÎÞ·¨»ñÈ¡Êý¾Ý¿â  | 
|         mylog.debug("no dbController")  | 
|         return  | 
|     result, data = dbController.find(DoubleBillColName, {"SpID":spID, "AccID":accID})  | 
|     if not data:  | 
|         # Î޼Ǽ²»ÄÜÁìÈ¡£¬GM·µ»Ø1, ¼õÉÙ·´¸´²éѯ  | 
|         return  | 
|       | 
|     return data[0]  |