作者 jinglong
1 个管道 的构建 通过 耗费 0 秒

调式微信分享

... ... @@ -554,20 +554,28 @@ class LoginController extends HomeBaseController
return $str;
}
//获取微信分享签名
public function getSignature(){
//获取access_token
public function getWxAccessToken(){
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appkey2.'&secret='.$this->appsecret2;
$res = $this->http_get($url);
$json_arr = json_decode($res,true);
if(isset($json_arr['errcode'])&&!empty($json_arr['errcode'])){
//用户取消登录
$this->apiResponse(0, '获取配置失败');
}
$token = $json_arr['access_token'];
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';
$res = $this->http_get($url);
$json_arr = json_decode($res,true);
$ticket = $json_arr['ticket'];
return $token;
}
//获取微信分享签名
public function getSignature(){
if($_SESSION['ticket_expire_time'] > time() && $_SESSION['ticket']){
$ticket = $_SESSION['ticket'];
}else{
$token = $this->getWxAccessToken();
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';
$res = $this->http_get($url);
$json_arr = json_decode($res,true);
$ticket = $json_arr['ticket'];
$_SESSION['ticket'] = $ticket;
$_SESSION['ticket_expire_time'] = time()+7000;
}
return $ticket;
}
... ...
... ... @@ -38,52 +38,51 @@
<script src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script>
$(function () {
var appid = '';
var timestamp = '';
var nonceStr = '';
var signature = '';
$.ajax({
type: 'POST',
url: '/portal/login/configShareWx',
data: {
},
dataType: 'json',
async: false,
success: function(data) {
var appid = '';
var timestamp = '';
var nonceStr = '';
var signature = '';
$.ajax({
type: 'POST',
url: '/portal/login/configShareWx',
data: {
},
dataType: 'json',
async: false,
success: function(data) {
console.log(data);
if (data.code == 1) {
appid = data.appid;
timestamp = data.timestamp;
nonceStr = data.nonceStr;
signature = data.signature;
} else {
console.log(data);
if (data.code == 1) {
appid = data.appid;
timestamp = data.timestamp;
nonceStr = data.nonceStr;
signature = data.signature;
} else {
console.log(data);
}
}
});
wx.config({
debug: false,
appId: appid,
timestamp: timestamp,
nonceStr: nonceStr,
signature: signature,
jsApiList: [
'checkJsApi',
'updateAppMessageShareData',
]
});
wx.ready(function () { //需在用户可能点击分享按钮前就先调用
wx.updateAppMessageShareData({
title: '分享标题', // 分享标题
desc: '分享描述', // 分享描述
link: 'http://www.starplanet.cn/portal/enjoy/getEnjoyDetail?id=81', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: '', // 分享图标
success: function (e) {
// 设置成功
console.log(e);
}
})
});
}
});
wx.config({
debug: true,
appId: appid,
timestamp: timestamp,
nonceStr: nonceStr,
signature: signature,
jsApiList: [
'updateAppMessageShareData',
]
});
wx.ready(function () { //需在用户可能点击分享按钮前就先调用
wx.updateAppMessageShareData({
title: '分享标题', // 分享标题
desc: '分享描述', // 分享描述
link: 'http://www.starplanet.cn/portal/enjoy/getEnjoyDetail?id=81', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: '', // 分享图标
success: function (e) {
// 设置成功
console.log(e);
}
})
});
});
</script>
... ...