$redis_conf['host'], 'port' => $redis_conf['port'], 'passwd' => $redis_conf['passwd'] )); 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) { $url = ''; if (SERVER_ENV != _ONLINE) { $url = 'https://center-test.kingsome.cn/api/replays/'; } else { $url = 'https://center.kingsome.cn/api/replays/'; } $url .= $gameid . '/' . $condition; error_log($url); $response = ''; $params = array(); if (!phpcommon\HttpClient::get($url,$params,$response)) { error_log('奖励配置发送失败'); die(); return; } $ret = json_decode($response, true); error_log('awardconfig:' . $response); if($ret['errcode'] == 0) { return $ret['items']; } else { return; die(); } } 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; } 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); $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"; error_log('sendAward.url:' . $url); $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; } } public function checkServer() // 校验服务器地址URL { $gameid = $_REQUEST['gameid']; error_log($gameid); $config_name = "../config/game$gameid/weixin/config.php"; require $config_name; if ( isset($_REQUEST['echostr'])) { $this->valid(); } else { $this->responseMsg(); } } public function valid() { $echoStr = $_REQUEST['echostr']; if ($this->checkSignature()) { echo $echoStr; exit; } else { echo $echoStr . '+++' . WEIXIN_TOKEN; exit; } } private function checkSignature() { $signature = $_REQUEST["signature"]; $timestamp = $_REQUEST["timestamp"]; $nonce = $_REQUEST["nonce"]; $token = WEIXIN_TOKEN; $tmpArr = array ( $token , $timestamp , $nonce ); sort( $tmpArr , SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1($tmpStr); if ( $tmpStr == $signature ){ return true ; } else { return false ; } } 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, $accountid) { error_log('text'); $openid = $postArr['FromUserName']; $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, $accountid) { $openid = $postArr['FromUserName']; $CreateTime = $postArr['CreateTime']; } private function disposeEvent($postArr, $accountid) { error_log('disposeEvent'); $openid = $postArr['FromUserName']; $CreateTime = $postArr['CreateTime']; if($postArr['Event'] == 'user_enter_tempsession') { $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, $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, 'errmsg' => '' ))); exit ; } else { phpcommon\sendError(ERR_RETRY, 'token失败'); error_log($response); } } } public function responseMsg() { $postStr_key = $GLOBALS ["HTTP_RAW_POST_DATA" ]; error_log('!!!begin:' . $postStr_key); error_log('request:' . json_encode($_REQUEST)); $pc = new WXBizMsgCrypt(WEIXIN_TOKEN, WEIXIN_MSG_KEY, WEIXIN_APP_ID); $gameid = $_REQUEST['gameid']; $msg = ''; $msg_sign = $_REQUEST['msg_signature']; $timeStamp = $_REQUEST['timestamp']; $nonce = $_REQUEST['nonce']; $errCode = $pc->decryptJsonMsg($msg_sign, $timeStamp, $nonce, $postStr_key, $postStr); error_log('error_log:' . json_encode($errCode)); if ($errCode == 0) { error_log("解密后: " . $postStr . "\n"); if (!empty( $postStr ) && is_string( $postStr )){ $postArr = json_decode( $postStr , true ); $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, $accountid); } else { exit ('aaa'); } } else { echo "" ; exit ; } } else { error_log($errCode . "\n"); } } public function getAccessToken($openid, $accountid) { $r = $this->getRedis($openid); $access_token = $r->get('kf_token:' . $accountid . ':'); error_log('acc_token:' . $access_token); if (!empty($access_token)) { return $access_token; } else { $appid = WEIXIN_APP_ID; $appkey = WEIXIN_APP_SECRET; $url = "https://api.weixin.qq.com/cgi-bin/token?" . "grant_type=client_credential&appid=$appid&secret=$appkey"; $params = array(); $response = ''; if (!phpcommon\HttpClient::get($url, $params, $response)) { phpcommon\sendError(ERR_INTERNAL, '系统繁忙'); return; } error_log('response_token:' . $response); $res = json_decode($response, true); 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 { die; } } } }