<script>
var url = "{SITE_URL}";
var weixinJssdk = {json_encode(dr_weixin_jssdk())};
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId:weixinJssdk.appId, // 必填,公众号的唯一标识
timestamp:""+weixinJssdk.timestamp, // 必填,生成签名的时间戳
nonceStr:weixinJssdk.nonceStr, // 必填,生成签名的随机串
signature:weixinJssdk.signature,// 必填,签名,见附录1
jsApiList: [
'scanQRCode',
'getLocation',
'openLocation',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQZone',
'onMenuShareWeibo'
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
var shareData = {//朋友
title: '{$title}', // 分享标题
desc: '{$description}', // 分享描述
link: url, // 分享链接
imgUrl: "{dr_thumb($thumb)}", // 分享图标
success: function (data) {
},
Error:function(data) {
console.log(JSON.stringify(data))
}
}
wx.ready(function(){
wx.onMenuShareAppMessage(shareData);
wx.onMenuShareQQ(shareData);
wx.onMenuShareQZone(shareData);
wx.onMenuShareTimeline(shareData);
});
</script>
**weixinJssdk **
public function __construct() {
$ci = &get_instance();
$cfg = $ci->get_cache('weixin-'.SITE_ID, 'config');
$this->appId = 'wxd76cc35335b541af';
$this->appSecret = 'fccac3575bbe4e471ee480be7aa0586a';
$this->filepath = WEBPATH.'cache/';
}
function dr_weixin_jssdk() {
$ci = &get_instance();
$ci->load->library('jssdk');
$signPackage = $ci->jssdk->GetSignPackage();
return $signPackage;
}
**#上面引用类库 并调下面的方法:**
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
// 注意 URL 一定要动态获取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}
调用
private function getJsApiTicket() {
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode($this->get_php_file("jsapi_ticket.php"));
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
// 如果是企业号用以下 URL 获取 ticket
// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$this->set_php_file("jsapi_ticket.php", json_encode($data));
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}