/**
* 获取令牌
*/
function getAccessToken()
{
$wxapp = WxappModel::detail();//获取小程序配置信息
//判断现有的access_token是否过期,过期重新获取
if($wxapp['expires_in'] >= time()){
//不过期直接返回
$access_token=$wxapp['access_token'];
}else{
//重新获取
$config = ConfigModel::detail(); //获取第三方配置
//请求URL
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token='.$config['component_access_token'];
//请求参数
$data ='{"component_appid": "'.$config['app_id'].'","authorizer_appid": "'.$wxapp['app_id'].'","authorizer_refresh_token": "'.$wxapp['authorizer_refresh_token'].'"}';
$result = http_request($url,$data);
$token = json_decode($result,true);
$wxapp['access_token'] = $token['authorizer_access_token'];
$wxapp['expires_in'] = time()+7000; //2个小时候过期,少加200秒
//$wxapp['authorizer_refresh_token'] = $token['authorizer_refresh_token'];
$access_token = $token['authorizer_access_token'];
$wxapp->save();//保存最新的令牌access_token和过期时间
}
return $access_token;
}