完成getAccessToken改造

This commit is contained in:
aozhiwei 2019-01-27 21:09:26 +08:00
parent 07f28b3c05
commit a75e10e4af

View File

@ -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 '';
}
}