From 0420d0aa69c97095fbb9ae93be44d24fb871f836 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 18 Jan 2019 16:50:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A5=96=E5=8A=B1=E9=82=AE=E4=BB=B6=E5=8F=91?= =?UTF-8?q?=E9=80=81=20base?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/accounts_kefu.sql | 51 ++++ sql/kefudb.sql | 50 ++++ webapp/controller/KefuController.class.php | 256 ++++++++++++++------- 3 files changed, 279 insertions(+), 78 deletions(-) create mode 100644 sql/accounts_kefu.sql create mode 100644 sql/kefudb.sql diff --git a/sql/accounts_kefu.sql b/sql/accounts_kefu.sql new file mode 100644 index 0000000..56d2e84 --- /dev/null +++ b/sql/accounts_kefu.sql @@ -0,0 +1,51 @@ +-- MySQL dump 10.14 Distrib 5.5.41-MariaDB, for Linux (x86_64) +-- +-- Host: localhost Database: kefudb +-- ------------------------------------------------------ +-- Server version 5.5.41-MariaDB + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `accounts_kefu` +-- + +DROP TABLE IF EXISTS `accounts_kefu`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `accounts_kefu` ( + `idx` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id', + `accountid` varchar(60) DEFAULT '' COMMENT '账号id(channel + "_" + gameid + "_" + openid)', + `channel` int(11) NOT NULL COMMENT 'channel', + `gameid` int(11) NOT NULL COMMENT 'gameid', + `openid` varchar(50) NOT NULL COMMENT '平台openid', + `nickname` tinyblob COMMENT 'nickname', + `awardtime` int(11) NOT NULL DEFAULT '0' COMMENT '最后领奖时间', + PRIMARY KEY (`idx`), + UNIQUE KEY `accountid` (`accountid`), + UNIQUE KEY `channel_gameid_openid_index` (`channel`,`gameid`,`openid`), + KEY `openid_index` (`openid`), + KEY `channel_index` (`channel`), + KEY `gameid_index` (`gameid`) +) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +/*!40101 SET character_set_client = @saved_cs_client */; + + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2015-08-19 18:51:22 diff --git a/sql/kefudb.sql b/sql/kefudb.sql new file mode 100644 index 0000000..1cd2a20 --- /dev/null +++ b/sql/kefudb.sql @@ -0,0 +1,50 @@ +-- MySQL dump 10.14 Distrib 5.5.41-MariaDB, for Linux (x86_64) +-- +-- Host: localhost Database: kefudb +-- ------------------------------------------------------ +-- Server version 5.5.41-MariaDB + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `accounts_kefu` +-- + +DROP TABLE IF EXISTS `accounts_kefu`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `accounts_kefu` ( + `idx` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id', + `accountid` varchar(60) DEFAULT '' COMMENT '账号id(channel + "_" + gameid + "_" + openid)', + `channel` int(11) NOT NULL COMMENT 'channel', + `gameid` int(11) NOT NULL COMMENT 'gameid', + `openid` varchar(50) NOT NULL COMMENT '平台openid', + `awardtime` int(11) NOT NULL DEFAULT '0' COMMENT '最后领奖时间', + PRIMARY KEY (`idx`), + UNIQUE KEY `accountid` (`accountid`), + UNIQUE KEY `channel_gameid_openid_index` (`channel`,`gameid`,`openid`), + KEY `openid_index` (`openid`), + KEY `channel_index` (`channel`), + KEY `gameid_index` (`gameid`) +) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; +/*!40101 SET character_set_client = @saved_cs_client */; + + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2015-08-19 18:51:22 diff --git a/webapp/controller/KefuController.class.php b/webapp/controller/KefuController.class.php index 832292e..8b64791 100644 --- a/webapp/controller/KefuController.class.php +++ b/webapp/controller/KefuController.class.php @@ -1,15 +1,15 @@ $redis_conf['host'], 'port' => $redis_conf['port'], @@ -18,6 +18,56 @@ class KefuController { return $r; } + protected function getMysql() + { + $mysql_conf = require('../config/kefu.mysql.cluster.php'); + $conn = new phpcommon\Mysql(array( + 'host' => $mysql_conf[0]['host'], + 'port' => $mysql_conf[0]['port'], + 'user' => $mysql_conf[0]['user'], + 'passwd' => $mysql_conf[0]['passwd'], + 'dbname' => 'kefudb' + )); + return $conn; + } + + #kefudb_info = array(accountid, channel, gameid, openid, awardtime) + private function insertKefuInfoToDB($accountid, $kefu_info) + { + $conn = $this->getMysql(); + $tbl_name = 'accounts_kefu'; + $ret = $conn->execScript("INSERT INTO $tbl_name(" . + 'accountid, channel, gameid, openid, awardtime) ' . + 'VALUES(:accountid, :channel, :gameid, :openid, :awardtime);', + array( + ':accountid' => $kefu_info['accountid'], + ':channel' => $kefu_info['channel'], + ':gameid' => $kefu_info['gameid'], + ':openid' => $kefu_info['openid'], + ':awardtime' => $kefu_info['awardtime'] + )); + return $ret; + + } + + private function checkAccountKefuExist($accountid) + { + $conn = $this->getMysql($accountid); + $select_tbl_name = 'accounts_kefu'; + + $row = $conn->execQueryOne('SELECT accountid ' . + "FROM $select_tbl_name WHERE accountid = :accountid ;", + array( + ':accountid' => $accountid, + )); + error_log('check_row:' . json_encode($row)); + if ($row) { + return $row; + }else{ + return ''; + } + } + private function getAwardConfig($gameid, $condition) { @@ -25,50 +75,79 @@ class KefuController { if (SERVER_ENV != _ONLINE) { $url = 'https://center-test.kingsome.cn/api/replays/'; } else { - $url = 'https://center-test.kingsome.cn/api/replays/'; + $url = 'https://center.kingsome.cn/api/replays/'; } $url .= $gameid . '/' . $condition; + error_log($url); $response = ''; $params = array(); if (!phpcommon\HttpClient::get($url,$params,$response)) { - phpcommon\sendError(100,'获取失败'); + error_log('奖励配置发送失败'); die(); return; } $ret = json_decode($response, true); + error_log('awardconfig:' . $response); + if($ret['errcode'] == 0) { + return $ret['items']; + } else { + return; + die(); + } - for($i = 0; $i < count($ret); $i++) { + } - $itemid = $ret[$i]['item_id']; - $itemcount = $ret[$i]['count']; - $url = ''; - $response = ''; - if (!phpcommon\HttpClient::post($url, - json_encode( - array( - 'gameid' => $gameid, - 'itemid' => $itemid, - 'itemcount' => $itemcount - ) - ) - , - $response + private function translateAttachments($config) + { + $attachments = ''; + error_log('config:' . json_encode($config)); + error_log('attachment0:' . $config[0]); + foreach ($config as $attachment) { + error_log('attachment:'. json_encode($attachment)); + $attachments .= $attachment['item_id'] . ':' . $attachment['count'] . ';'; + } + return $attachments; + } - )) { - phpcommon\sendError(100,'获取失败'); - return; - } + private function sendAward($accountid, $config) + { + if (SERVER_ENV != _ONLINE) { + $url = 'https://gamemail-test.kingsome.cn/webapp/index.php?'; + } else { + $url = 'https://gamemail.kingsome.cn/webapp/index.php?'; + } + $to = $accountid; + $from = 'from'; + $mail_type = '1'; + $mail_subtype = '0'; + $subject = '奖励'; + $content = '领取奖励'; + $sendtime = time(); + $ext = ''; + $expire_time = time() + 60 * 60 * 24 * 1; + error_log('congifsendaward:' . json_encode($config)); + $attachments = $this->translateAttachments($config); + $game_id = phpcommon\extractGameId($accountid); - $data = json_decode($response, true); + $url .= "c=Mail&a=sendMail&to=$to&from=$from&mail_type=$mail_type" . + "&mail_subtype=$mail_subtype&subject=$subject&content=$content&sendtime=$sendtime" . + "&ext=$ext&expire_time=$expire_time&attachments=$attachments&game_id=$game_id"; - if(isset($data) && $data['ErrorCode'] == 0) { + error_log('sendAward.url:' . $url); - } else{ - phpcommon\sendError(100,'服务器内部错误'); - return; - - } + $response = ''; + error_log(); + if (!phpcommon\HttpClient::get($url, array(), $response)) { + error_log('邮件发送失败'); + return; + } + $data = json_decode($response, true); + error_log('邮件返回:' . $response); + if(isset($data) && $data['ErrorCode'] == 0) { + return true; + } else{ + return; } @@ -125,51 +204,95 @@ class KefuController { private function sendMsg() { + $pc2 = new WXBizMsgCrypt(WEIXIN_TOKEN, WEIXIN_MSG_KEY, WEIXIN_APP_ID); + $encryptMsg = ''; + $text = $postStr; + $errCode = $pc2->encryptJsonMsg($text, $timeStamp, $nonce, $encryptMsg_str); + if ($errCode == 0) { + error_log("加密后: " . $encryptMsg_str . "\n"); + $encryptMsg = json_decode($encryptMsg_str, true); + + $errCode = $pc2->decryptJsonMsg($encryptMsg['MsgSignature'], $timeStamp, $nonce, $encryptMsg_str, $postStr2); + error_log('error_log:' . json_encode($errCode)); + if ($errCode == 0) { + + error_log("解密后: " . $postStr2 . "\n"); + } + } else { + error_log($errCode . "\n"); + } } - private function disposeText($postArr, $gameid) + private function disposeText($postArr, $accountid) { - error_log('test'); + error_log('text'); $openid = $postArr['FromUserName']; - $toUserName = $postArr['ToUserName']; $CreateTime = $postArr['CreateTime']; + $test = $postArr['Content']; + $gameid = phpcommon\extractGameId($accountid); + $awardConfig = $this->getAwardConfig($gameid, $test); + error_log('awardConfig:' . json_encode($awardConfig)); + if (isset($awardConfig)) { + $awardRet = $this->sendAward($accountid, $awardConfig); + if ($awardRet) { + error_log('发送奖励成功!'); + } + } } - private function disposeImage($postArr, $gameid) + private function disposeImage($postArr, $accountid) { $openid = $postArr['FromUserName']; - $toUserName = $postArr['ToUserName']; $CreateTime = $postArr['CreateTime']; } - private function disposeEvent($postArr, $gameid) + private function disposeEvent($postArr, $accountid) { + + error_log('disposeEvent'); $openid = $postArr['FromUserName']; - $toUserName = $postArr['ToUserName']; $CreateTime = $postArr['CreateTime']; if($postArr['Event'] == 'user_enter_tempsession') { - $content = '您好,有什么能帮助你?' ; + $check_ret = $this->checkAccountKefuExist($accountid); + error_log('check_ret:' . json_encode($check_ret)); + if (empty($check_ret)) { + + error_log('dot save to db'); + + $kefu_info = array( + 'accountid' => $accountid, + 'channel' => 6001, + 'gameid' => phpcommon\extractGameId($accountid), + 'openid' => $openid, + 'awardtime' => '0' + ); + $this->insertKefuInfoToDB($accountid, $kefu_info); + } + $content = '您好,有什么能帮助你? 回复【1】领取奖励。 ' ; $data = array ( "touser"=> $openid, "msgtype"=>"text", "text"=> array ("content" => $content) ); $postarray = json_encode( $data ,JSON_UNESCAPED_UNICODE); + error_log($postarray); //POST发送https请求客服接口api - $access_token = $this->getAccessToken($openid, $gameid); + $access_token = $this->getAccessToken($openid, $accountid); $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token"; + error_log('aturl:' . $url); $response = ''; if (!phpcommon\HttpClient::post($url, $postarray, $response)) { phpcommon\sendError(ERR_RETRY, '系统繁忙'); return; } $ret_info = json_decode($response, true); + error_log($response); if ($ret_info['errcode'] == 0) { echo(json_encode(array( 'errcode' => 0, @@ -192,6 +315,7 @@ class KefuController { $pc = new WXBizMsgCrypt(WEIXIN_TOKEN, WEIXIN_MSG_KEY, WEIXIN_APP_ID); $gameid = $_REQUEST['gameid']; + $msg = ''; $msg_sign = $_REQUEST['msg_signature']; $timeStamp = $_REQUEST['timestamp']; @@ -202,42 +326,17 @@ class KefuController { error_log("解密后: " . $postStr . "\n"); - - $pc2 = new WXBizMsgCrypt(WEIXIN_TOKEN, WEIXIN_MSG_KEY, WEIXIN_APP_ID); - $encryptMsg = ''; - $text = $postStr; - $errCode = $pc2->encryptJsonMsg($text, $timeStamp, $nonce, $encryptMsg_str); - if ($errCode == 0) { - error_log("加密后: " . $encryptMsg_str . "\n"); - $encryptMsg = json_decode($encryptMsg_str, true); - - $errCode = $pc2->decryptJsonMsg($encryptMsg['MsgSignature'], $timeStamp, $nonce, $encryptMsg_str, $postStr2); - error_log('error_log:' . json_encode($errCode)); - if ($errCode == 0) { - - error_log("解密后: " . $postStr2 . "\n"); - } - } else { - error_log($errCode . "\n"); - } - - error_log('end'); - exit; - if (!empty( $postStr ) && is_string( $postStr )){ $postArr = json_decode( $postStr , true ); - if (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'text'){ // 文本消息 - - $this->disposeText($postArr, $gameid); - - } elseif (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'image'){ // 图文消息 - - $this->disposeImage($postArr, $gameid); + $accountid = phpcommon\createAccountId(6001, $gameid, $postArr['FromUserName']); + if (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'text'){ // 文本消息 + $this->disposeText($postArr, $accountid); + } elseif (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'image'){ // 图文消息 + $this->disposeImage($postArr, $accountid); } elseif (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'event' ){ // 进入客服动作 - $this->disposeEvent($postArr, $gameid); - + $this->disposeEvent($postArr, $accountid); } else { exit ('aaa'); } @@ -254,11 +353,12 @@ class KefuController { } - public function getAccessToken($openid, $gameid) + public function getAccessToken($openid, $accountid) { $r = $this->getRedis($openid); - $access_token = $r->get('weixin_token:' . $gameid . ':' . $openid); + $access_token = $r->get('kf_token:' . $accountid . ':'); + error_log('acc_token:' . $access_token); if (!empty($access_token)) { return $access_token; } @@ -275,14 +375,14 @@ class KefuController { phpcommon\sendError(ERR_INTERNAL, '系统繁忙'); return; } + error_log('response_token:' . $response); $res = json_decode($response, true); - if ( $res ) { - $r->set('weixin_token:' . $gameid . ':' . - $openid, $res['access_token']); //刚获取的token放到redis中 - $r->pexpire('weixin_token:' . $gameid . $openid, 7150); //微信限制过期时间为两小时 + if ( isset($res['access_token']) ) { + //刚获取的token放到redis中 //微信限制过期时间为两小时 + $r->set('kf_token:' . $accountid . ':', $res['access_token']); + $r->pexpire('kf_token:' . $accountid . ':', 1000 * ($res['expires_in'] - 60 * 1)); return $res['access_token']; } else { - phpcommon\sendError(ERR_INTERNAL, '获取access_token失败'); die; }