diff --git a/config/game1011/weixin/config.php b/config/game1011/weixin/config.php new file mode 100644 index 0000000..8e7c16d --- /dev/null +++ b/config/game1011/weixin/config.php @@ -0,0 +1,8 @@ + $redis_conf['host'], @@ -22,21 +18,27 @@ class KefuController { 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()){ + if ($this->checkSignature()) { echo $echoStr; exit; } else { - echo $echoStr . '+++' . TOKEN; + echo $echoStr . '+++' . WEIXIN_TOKEN; exit; } } @@ -48,14 +50,11 @@ class KefuController { $timestamp = $_REQUEST["timestamp"]; $nonce = $_REQUEST["nonce"]; - $token = TOKEN; - error_log('token:' . $token); + $token = WEIXIN_TOKEN; $tmpArr = array ( $token , $timestamp , $nonce ); sort( $tmpArr , SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1($tmpStr); - error_log('tmpStr:' . $tmpStr); - error_log('signature:' . $signature); if ( $tmpStr == $signature ){ return true ; @@ -64,7 +63,7 @@ class KefuController { } } - private function disposeText($postArr) + private function disposeText($postArr, $gameid) { $openid = $postArr['FromUserName']; $toUserName = $postArr['ToUserName']; @@ -73,71 +72,73 @@ class KefuController { } - private function_exists($postArr) + private function disposeImage($postArr, $gameid) { $openid = $postArr['FromUserName']; $toUserName = $postArr['ToUserName']; $CreateTime = $postArr['CreateTime']; + + } + + private function disposeEvent($postArr, $gameid) + { + $openid = $postArr['FromUserName']; + $toUserName = $postArr['ToUserName']; + $CreateTime = $postArr['CreateTime']; + + if($postArr['Event'] == 'user_enter_tempsession') + { + $content = '您好,有什么能帮助你?' ; + $data = array ( + "touser"=> $openid, + "msgtype"=>"text", + "text"=> array ("content" => $content) + ); + $postarray = json_encode( $data ,JSON_UNESCAPED_UNICODE); + + //POST发送https请求客服接口api + $access_token = $this->getAccessToken($openid, $gameid); + $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token"; + $response = ''; + if (!phpcommon\HttpClient::post($url, $postarray, $response)) { + phpcommon\sendError(ERR_RETRY, '系统繁忙'); + return; + } + $ret_info = json_decode($response, true); + 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 = $GLOBALS ["HTTP_RAW_POST_DATA" ]; - error_log('responseMsg'); - - if (! empty( $postStr ) && is_string( $postStr )){ + error_log($postStr); + if (!empty( $postStr ) && is_string( $postStr )){ $postArr = json_decode( $postStr , true ); - if (! empty ( $postArr ['MsgType']) && $postArr ['MsgType'] == 'text'){ // 文本消息 - $fromUsername = $postArr ['FromUserName']; // 发送者openid - $toUserName = $postArr ['ToUserName']; // 小程序id - $textTpl = array ( - "ToUserName" =>$fromUsername , - "FromUserName"=> $toUserName , - "CreateTime"=> time (), - "MsgType"=>"transfer_customer_service", - ); - exit (json_encode( $textTpl )); - } elseif (! empty ( $postArr ['MsgType']) && $postArr ['MsgType'] == 'image'){ // 图文消息 - $fromUsername = $postArr ['FromUserName']; // 发送者openid - $toUserName = $postArr ['ToUserName']; // 小程序id - $textTpl = array ( - "ToUserName"=> $fromUsername , - "FromUserName"=> $toUserName , - "CreateTime"=> time (), - "MsgType"=>"transfer_customer_service", - ); - exit (json_encode( $textTpl )); - } elseif ( $postArr ['MsgType'] == 'event' && $postArr ['Event']=='user_enter_tempsession'){ // 进入客服动作 - $fromUsername = $postArr ['FromUserName']; // 发送者openid - $content = '您好,有什么能帮助你?' ; - $data = array ( - "touser"=> $fromUsername , - "msgtype"=>"text", - "text"=> array ("content"=> $content ) - ); - $params = json_encode( $data ,JSON_UNESCAPED_UNICODE); + if (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'text'){ // 文本消息 - $access_token = $this->getAccessToken(); - /* - * POST发送https请求客服接口api - */ + $this->disposeText($postArr, $gameid); - $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token; + } elseif (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'image'){ // 图文消息 - $response = ''; - if (!phpcommon\HttpClient::get($url, $params, $response)) { - phpcommon\sendError(ERR_RETRY, '系统繁忙'); - return; - } + $this->disposeImage($postArr, $gameid); - if ( $response == 0 ){ - echo 'success'; exit ; - } + } elseif (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'event' ){ // 进入客服动作 + $this->disposeEvent($postArr, $gameid); } else { - exit ('aaa' ); + exit ('aaa'); } } else { echo "" ; @@ -145,28 +146,38 @@ class KefuController { } } + public function getAccessToken($openid, $gameid) + { - public function getAccessToken(){ - if (S('access_token' )){ - return S('access_token' ); + $r = $this->getRedis($openid); + $access_token = $r->get('weixin_token:' . $gameid . ':' . $openid); + if (!empty($access_token)) { + return $access_token; } else { - $appid = SHAOWEI_APPID; - $appkey = PRIVATE_KEY; - $url = "https://api.weixing.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appkey"; + + $appid = WEIXIN_APP_ID; + $appkey = WEICIN_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_RETRY, '系统繁忙'); + phpcommon\sendError(ERR_INTERNAL, '系统繁忙'); return; } - - $res = json_decode($response, true ); - if ( $res ){ - S( 'access_token', $res ['access_token'],7100 ); - return S('access_token' ); + $res = json_decode($response, true); + if ( $res ) { + $r->set('weixin_token:' . $gameid . ':' . + $openid, $res['access_token']); //刚获取的token放到redis中 + $r->pexpire('weixin_token:' . $openid, 7150); //微信限制过期时间为两小时 + return $res['access_token']; } else { - return 'api return error' ; + phpcommon\sendError(ERR_INTERNAL, '获取access_token失败'); + die; } + } }