PHP页面
/ **
*获取临时令牌
* @return bool | mixed | string
* /
function getToken()
{
$ token = \ think \ Cache :: get('wxtoken');
if(empty($ token)){
//请求获取令牌
$ url = config('wxtoken')。'&appid ='。config('appid')。'&secret ='。配置( 'appsecret');
$ token_json = file_get_contents($ url);
$ token_json = json_decode($ token_json,true);
if(isset($ token_json ['errcode'])){
return false;
}
$ wxtoken = base64_encode($ token_json ['access_token']);
//把token加入缓存
$ token = \ think \ Cache :: store('redis') - > set(' wxtoken',$ wxtoken,$ token_json ['expires_in']);
}
$ token = base64_decode($ token);
return $ token;
}
/ **
*获取票证
* @return [类型] [描述]
* /
function getTicket()
{
$ wxticket = \ think \ Cache :: get('wxticket');
if(empty($ ticket)){
//请求获取令牌
$ token = getToken();
$ url = config('ticket')。'?access_token ='。$令牌。'&类型= wx_card';
$ ticket_json = file_get_contents($ url);
$ ticket_json = json_decode($ ticket_json,true);
if($ ticket_json ['errcode']!= 0){
return false;
}
$ wxticket = base64_encode($ ticket_json ['ticket']);
//把令牌加入缓存
$ wxticket = \ think \ Cache :: store('redis') - > set('wxticket',$ wxticket,$ ticket_json ['expires_in']);
}
$ wxticket = base64_decode($ wxticket);
return $ wxticket;
}
/ *
*获取16位随机字符串
* /
function getRandStr($ length = 16)
{
$ str ='abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ randString ='';
$ len = strlen($ str) - 1;
for($ i = 0; $ i <$ length; $ i ++){
$ num = mt_rand(0,$ len);
$ randString。= $ str [$ num];
}
return $ randString;
}
//控制器页 调用上面 的几个方法
public function wxSign(){
$noncestr = getRandStr();
$jsapi_ticket = getTicket();
$timestamp = time();
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$string = 'jsapi_ticket='.$jsapi_ticket.'&noncestr='.$noncestr.'×tamp='.$timestamp.'&url='.$url;
$signature = sha1($string);
$this->assign([
'appid' => config('appid'),
'noncestr' => $noncestr,
'timestamp' => $timestamp,
'signature' => $signature,
]);
return $this->fetch();
}
html
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript">
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId:"{$appid}", // 必填,公众号的唯一标识
timestamp:"{$timestamp}" , // 必填,生成签名的时间戳
nonceStr: "{$noncestr}", // 必填,生成签名的随机串
signature: "{$signature}",// 必填,签名,见附录1
jsApiList: [
'onMenuShareAppMessage',
'onMenuShareTimeline'
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
//分享到朋友圈
wx.onMenuShareTimeline({
title: "zzy", // 分享标题
link:"https://www.baidu.com/",//分享链接
imgUrl: "", // 分享图标
success: function () {
// 分享成功执行此回调函数
alert('success');
},
cancel: function () {
alert('cancel');
}
});
//分享给朋友
wx.onMenuShareAppMessage({
title: "zzy", // 分享标题
desc: "",// 分享描述
link:"https://www.baidu.com/",//分享链接
imgUrl: "", // 分享图标
success: function (res) {
alert('已分享');
},
cancel: function (res) {
alert('已取消');
}
});
});
</script>