$redis_conf['host'], 'port' => $redis_conf['port'], 'passwd' => $redis_conf['passwd'] )); return $r; } 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 disposeText($postArr, $gameid) { $openid = $postArr['FromUserName']; $toUserName = $postArr['ToUserName']; $CreateTime = $postArr['CreateTime']; } 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($postStr); 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); } elseif (!empty( $postArr ['MsgType']) && $postArr['MsgType'] == 'event' ){ // 进入客服动作 $this->disposeEvent($postArr, $gameid); } else { exit ('aaa'); } } else { echo "" ; exit ; } } public function getAccessToken($openid, $gameid) { $r = $this->getRedis($openid); $access_token = $r->get('weixin_token:' . $gameid . ':' . $openid); if (!empty($access_token)) { return $access_token; } else { $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_INTERNAL, '系统繁忙'); return; } $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 { phpcommon\sendError(ERR_INTERNAL, '获取access_token失败'); die; } } } }