From a75e10e4af05a74ff599c7a3c2673bad0d553e95 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sun, 27 Jan 2019 21:09:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90getAccessToken=E6=94=B9?= =?UTF-8?q?=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webapp/controller/KefuController.class.php | 61 ++++++++++------------ 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/webapp/controller/KefuController.class.php b/webapp/controller/KefuController.class.php index eed0f3c..ae55418 100644 --- a/webapp/controller/KefuController.class.php +++ b/webapp/controller/KefuController.class.php @@ -249,22 +249,19 @@ class KefuController { private function sendKefuMsg($accountid, $data) { - $postarray = json_encode($data, JSON_UNESCAPED_UNICODE); - //POST发送https请求客服接口api $access_token = $this->getAccessToken($accountid); $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token"; $response = ''; - if (!phpcommon\HttpClient::post($url, $postarray, $response)) { + if (!phpcommon\HttpClient::post($url, + json_encode($data, JSON_UNESCAPED_UNICODE), + $response) + ) { phpcommon\sendError(ERR_RETRY, '系统繁忙'); return; } $ret_info = json_decode($response, true); - #error_log('sendKefuMsgRet:' . $response); - if ($ret_info['errcode'] == 0) { - } else { - echo(0); - } + return !empty($ret_info) && $ret_info['errcode'] == 0; } private function sendKefuMsgSomeUser($arrayInfo) @@ -383,33 +380,29 @@ class KefuController { $access_token = $r->get('kf_token:' . $accountid . ':'); if (!empty($access_token)) { return $access_token; + } + $gameid = phpcommon\extractGameId($accountid); + $config_name = "../config/game$gameid/weixin/config.php"; + @require $config_name; + + $response = ''; + if (!phpcommon\HttpClient::get('https://api.weixin.qq.com/cgi-bin/token', + array( + 'grant_type' => 'client_credential', + 'appid' => WEIXIN_APP_ID, + 'secret' => WEIXIN_APP_SECRET + ), + $response)) { + phpcommon\sendError(ERR_INTERNAL, '系统繁忙'); + return; + } + $respobj = json_decode($response, true); + if (isset($respobj['access_token'])) { + $r->set('kf_token:' . $accountid . ':', $respobj['access_token']); + $r->pexpire('kf_token:' . $accountid . ':', 1000 * ($respobj['expires_in'] - 60 * 1)); + return $respobj['access_token']; } else { - $gameid = phpcommon\extractGameId($accountid); - $config_name = "../config/game$gameid/weixin/config.php"; - @require $config_name; - - $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(); - } - + return ''; } }