<?php
function jssdk(){
$appid = $this->appid;
$secret = $this->appserver;
$_title = '微信';
$accesstoken = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."");//获取access_token
$token = json_decode($accesstoken); //对JSON格式的字符串进行编码
$t = get_object_vars($token);//转换成数组
$access_token = $t['access_token'];//输出access_token
$jsapi = file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi");
$jsapi = json_decode($jsapi);
$j = get_object_vars($jsapi);
$jsapi = $j['ticket'];//get JSAPI
$time = time();
$noncestr= $this->createNonceStr();
$jsapi_ticket= $jsapi;
$timestamp=$time;
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$and = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."×tamp=".$timestamp."&url=".$url."";
$signature = sha1($and);
$data = ['sign'=>$signature,'time'=>$time,'noncestr'=>$noncestr, 'appId' => $appid];
return json_encode($data);
}
private function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
$app=new App();
$foo = $app->jssdk();
$data = json_decode($foo);
?>
<script>
.config({
debug:true,
appId:'wx602822de508c1257', // 必填,公众号的唯一标识
timestamp:"<?php echo $data->time;?>", // 必填,生成签名的时间戳
nonceStr:"<?php echo $data->noncestr;?>", // 必填,生成签名的随机串
signature:"<?php echo $data->sign;?>",// 必填,签名,见附录1
jsApiList: ["checkJsApi", "onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", "onMenuShareQZone"]// 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
document.querySelector('#checkJsApi').onclick =function () {
wx.checkJsApi({
jsApiList: [
'getNetworkType',
'previewImage'
],
success:function(res) {
alert(JSON.stringify(res));
}
});
};
//朋友圈
wx.onMenuShareTimeline({
title:'666', // 分享标题
link:'http://sy.etsxz.com/2', // 分享链接
imgUrl:'./qun_meilv.jpg',
success:function (res) {
alert('已分享');
},
cancel:function (res) {
alert('已取消');
},
fail:function (res) {
alert(JSON.stringify(res));
}
});
//朋友
wx.onMenuShareAppMessage({
title:'我就是', // 分享标题
desc:'分享领红包', // 分享描述
link:'http://sy.etsxz.com/1', // 分享链接
imgUrl:"/temp/tc/imgs/qun_meilv.jpg", // 分享图标
type:'', // 分享类型,music、video或link,不填默认为link
dataUrl:'', // 如果type是music或video,则要提供数据链接,默认为空
success:function () {
// 用户确认分享后执行的回调函数
alert("分享1");
},
cancel:function () {
// 用户取消分享后执行的回调函数
alert("取消分享");
}
});
});
</script>