修改Jssdk.php文件首先我们需要在构造函数中设置 $this->path = __DIR__ . DS;即:namespace wechat;
class Jssdk {
private $appId;
private $appSecret;
private $path;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
$this->path = __DIR__ . DS;
}
...
}
将get_php_file函数返回值中的$filename改为 $this->path.$filename,即:private function get_php_file($filename) {
return trim(substr(file_get_contents($this->path.$filename), 15));
}设置token的缓存,修改getAccessToken,加入cacheprivate function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
// cache('at', ['access_token'=>'sss', 'expire_time' => '123']);
// $data = json_decode($this->get_php_file("access_token.php"));
$data = cache('at');
if ($data['expire_time'] < time()) {
// 如果是企业号用以下URL获取access_token
// $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
if ($access_token) {
// $data->expire_time = time() + 7000;
// $data->access_token = $access_token;
cache('at', ['access_token'=>$access_token, 'expire_time' => time() + 7000]);
// $this->set_php_file("access_token.php", json_encode($data));
}
} else {
$access_token = $data['access_token'];
}
return $access_token;
}