|
|
|
@ -205,6 +205,7 @@ class WebApps extends Service
|
|
|
|
|
*/
|
|
|
|
|
public function share()
|
|
|
|
|
{
|
|
|
|
|
$this->grant_type = "client_credential";
|
|
|
|
|
// 文件名
|
|
|
|
|
$file = "{$this->app->getRootPath()}runtime/{$this->app_id}_access_token.json";
|
|
|
|
|
// 获取数据
|
|
|
|
@ -216,24 +217,30 @@ class WebApps extends Service
|
|
|
|
|
];
|
|
|
|
|
if (empty($accessToken['expires_time'])) {
|
|
|
|
|
$accessToken_res = HttpService::instance()
|
|
|
|
|
->url("{$this->api_url}cgi-bin/token/grant_type?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
|
|
|
|
|
->url("{$this->api_url}cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
|
|
|
|
|
->toArray();
|
|
|
|
|
$accessToken_res['expires_time'] = time() + 6000;
|
|
|
|
|
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
|
|
|
|
|
$accessToken = $accessToken_res;
|
|
|
|
|
} else if (!isset($accessToken['access_token'])) {
|
|
|
|
|
$accessToken_res = HttpService::instance()
|
|
|
|
|
->url("{$this->api_url}cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
|
|
|
|
|
->toArray();
|
|
|
|
|
$accessToken_res['expires_time'] = time() + 6000;
|
|
|
|
|
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
|
|
|
|
|
$accessToken = $accessToken_res;
|
|
|
|
|
} else if ($accessToken['expires_time'] <= time()) {
|
|
|
|
|
$accessToken_res = HttpService::instance()
|
|
|
|
|
->url("{$this->api_url}cgi-bin/token/grant_type?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
|
|
|
|
|
->url("{$this->api_url}cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
|
|
|
|
|
->toArray();
|
|
|
|
|
$accessToken_res['expires_time'] = time() + 6000;
|
|
|
|
|
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
|
|
|
|
|
$accessToken = $accessToken_res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($accessToken['access_token'])) throw new WeChatException("获取access_token错误," . $accessToken['errmsg']);
|
|
|
|
|
$res = HttpService::instance()
|
|
|
|
|
->url("{$this->api_url}cgi-bin/ticket/getticket?access_token={$accessToken['expires_time']}&type=jsapi")
|
|
|
|
|
->url("{$this->api_url}cgi-bin/ticket/getticket?access_token={$accessToken['access_token']}&type=jsapi")
|
|
|
|
|
->toArray();
|
|
|
|
|
dump($res);
|
|
|
|
|
if (!empty($res['errcode'])) throw new WeChatException('accessToken已过期');
|
|
|
|
|
// 注意 URL 一定要动态获取,不能 hardcode.
|
|
|
|
|
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
|
|
|
|
|