前端直接读取接口getApiTicketAction并传入url。
vue里url获取
var urls = window.location.href.split('#')[0]; //当前网页的URL,不包含#及其后面部分
php端
//拿tokeng
public function gettonken()
{
appid = appsecret = token = Db::name('wxtoken')->where('id',2)->find();
token['create_time'])%86400/60);
token['token'];
if (minute>100){
url = "api.weixin.qq.com/cgi-bin/tok…" . appid . "&secret=" . appsecret;
url);
weixin); //对JSON格式的字符串进行编码
jsondecode);//转换成数组
array['access_token'];//输出token
array['expires_in'];//输出token
Db::name('wxtoken')->where('id',2)->update(['token'=>access_token,'create_time'=>time()]);
}
return access_token;
}
//拿Ticket
public function getJsApiTicket()
{
appid = appsecret = token = Db::name('wxtoken')->where('id',3)->find();
token['create_time'])%86400/60);
token['token'];
if (minute>100){
url = "api.weixin.qq.com/cgi-bin/tic…;
url);
weixin); //对JSON格式的字符串进行编码
jsondecode);//转换成数组
array['ticket'];//输出token
array['expires_in'];//输出token
Db::name('wxtoken')->where('id',3)->update(['token'=>access_token,'create_time'=>time()]);
}
return access_token;
}
public function getApiTicketAction(Request request)
{
site = xn_cfg('base');
request->param();
param['url'];
site['wxchat_appid'];
this->getJsApiTicket();
nonceStr = $this->createNonceStr(); //构造一个随机数,用来生成签名的一部分
jsapiTicket&noncestr=nonceStr×tamp=timestamp&url=signature = sha1(signPackage = array(
"appId" => nonceStr,
"timestamp" => signature,
);
return success($signPackage);
}
private function createNonceStr(length = 16) { //生成随机16个字符的字符串
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
i = 0; length; i++) {
str .= substr(chars) - 1), 1);
}
return $str;
}