2024-08-04 22:28:52 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\utils;
|
|
|
|
|
|
|
|
use app\constant\ResponseCode;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use support\Cache;
|
|
|
|
use think\Exception;
|
|
|
|
|
|
|
|
class WechatUtil
|
|
|
|
{
|
|
|
|
const GENERAL_ACCESS_TOKEN = 'general_access_token';
|
|
|
|
const CODE_ACCESS_TOKEN = 'code_access_token';
|
|
|
|
const BASE_URI = 'https://api.weixin.qq.com';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc 获取access token
|
|
|
|
* @return array|mixed|void
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*/
|
|
|
|
public static function getAccessToken()
|
|
|
|
{
|
|
|
|
try {
|
2024-09-13 22:13:49 +08:00
|
|
|
// $str = '83_0jVm82U7ri-UENTGoTTFh2jowRpAN379ahJvK_3HpLPbdIheIr-ahgYhc5kkHv6dDYyaHklwqffKRQYOxURrQ-H8xq8kYvjoajYZQBNFhMCaBJs0VOU4PE5_-bYPUXfAAAXCF';
|
|
|
|
// Cache::set(self::GENERAL_ACCESS_TOKEN, $str, 3500);
|
2024-09-13 22:18:32 +08:00
|
|
|
Cache::delete(self::GENERAL_ACCESS_TOKEN);
|
2024-08-04 22:28:52 +08:00
|
|
|
if (Cache::has(self::GENERAL_ACCESS_TOKEN)) {
|
|
|
|
return Cache::get(self::GENERAL_ACCESS_TOKEN);
|
|
|
|
} else {
|
|
|
|
$client = new Client(['base_uri' => self::BASE_URI]);
|
|
|
|
$response = $client->request('post', 'cgi-bin/stable_token', [
|
|
|
|
'json' => [
|
|
|
|
'grant_type' => 'client_credential',
|
|
|
|
'appid' => getenv('APP_ID'),
|
|
|
|
'secret' => getenv('APP_SECRET'),
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
$response_contents = $response->getBody()->getContents();
|
|
|
|
raw_log('wechat/access_token', ['result' => $response_contents]);
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
$result = json_decode($response_contents, true);
|
|
|
|
raw_log('wechat/access_token', ['result' => $result]);
|
|
|
|
if (isset($result['errcode'])) {
|
|
|
|
throw new Exception($result['errmsg']);
|
|
|
|
}
|
|
|
|
Cache::set(self::GENERAL_ACCESS_TOKEN, $result['access_token'], $result['expires_in'] - 60);
|
|
|
|
|
|
|
|
return $result['access_token'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return [
|
|
|
|
'code' => ResponseCode::FAIL,
|
|
|
|
'msg' => $e->getMessage()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc 获取网页授权code的access_token
|
|
|
|
* @param $code
|
|
|
|
* @return array|void
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*/
|
|
|
|
public static function getCodeAccessToken($code)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$client = new Client(['base_uri'=>self::BASE_URI]);
|
|
|
|
$response = $client->request('get', 'sns/oauth2/access_token', [
|
|
|
|
'query' => [
|
|
|
|
'appid' => getenv('APPID'),
|
|
|
|
'secret' => getenv('APPSECRET'),
|
|
|
|
'code'=>$code,
|
|
|
|
'grant_type'=>'authorization_code'
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response_contents = $response->getBody()->getContents();
|
|
|
|
raw_log('wechat/code_access_token', ['result' => $response_contents]);
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
$result = json_decode($response_contents, true);
|
|
|
|
raw_log('wechat/code_access_token', ['result' => $result]);
|
|
|
|
if (isset($result['errcode'])) {
|
|
|
|
throw new Exception($result['errmsg']);
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch (Exception $e){
|
|
|
|
return [
|
|
|
|
'code'=>ResponseCode::FAIL,
|
|
|
|
'msg'=>$e->getMessage()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|