分享的js代碼
必須先引入一個微信的JS
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript">
wx.config({
debug: false, // 開啓調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時纔會打印。
appId: '{$wechatpz.appId}', // 必填,公衆號的唯一標識
timestamp: {$wechatpz.timestamp|default=0}, // 必填,生成簽名的時間戳
nonceStr: '{$wechatpz.nonceStr}', // 必填,生成簽名的隨機串
signature: '{$wechatpz.signature}', // 必填,簽名,見附錄1
jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline'] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2
});
wx.ready(function() {
//分享給朋友
wx.onMenuShareAppMessage({
title: { $info.share_title }, // 分享標題 此處$title可在控制器端傳遞也可在頁面傳遞 頁面傳遞講解在下面哦
desc: { $info.share_des }, //分享描述
link: "http://www.qpqx.com/index.php?m=WechatShop&c=Coupon&a=GiveLq&cm_id=30", // 分享鏈接
// imgUrl: { $imgurl }, // 分享圖標
type: '', // 分享類型,music、video或link,不填默認爲link
dataUrl: '', // 如果type是music或video,則要提供數據鏈接,默認爲空
success: function() {
layer.open({
type:0,
time: 1,
content: '分享成功!',
skin: 'msg',
});
},
cancel: function() {
layer.open({
type:0,
time: 1,
content: '分享失敗!',
skin: 'msg',
});
}
});
//分享到朋友圈
wx.onMenuShareTimeline({
title: { $info.share_title }, // 分享標題
desc: { $info.share_des }, // 分享描述
link: "http://www.qpqx.com/index.php?m=WechatShop&c=Coupon&a=GiveLq&cm_id=30", // 分享鏈接
// imgUrl: { $imgurl }, // 分享圖標
success: function(data) {
alert("分享成功")
},
cancel: function() {
alert("分享失敗")
}
});
});
</script>
其中那些分享標題,分享描述,鏈接什麼的都是查詢數據庫傳過來的
微信配置需要在控制器中在表中查詢出公衆號appid,公衆號secret,必須要引入'Wxshare.jssdk'文件
// 獲取微信配置
function getLatLon($store_id){
$wechatconfig = M('store_admin')->where("s_id = $store_id")->find();//在你需要的表中查詢出微信的appid和secret
$appid = $wechatconfig['appid'];
$appsecret = $wechatconfig['appsecret'];
Vendor('Wxshare.jssdk');
$token = get_access_token($appid,$appsecret);
$jssdk = new \JSSDK($appid,$appsecret,$token);
$signPackage = $jssdk->getSignPackage();
return $signPackage;
}
//獲取微信公衆號的access_token
function get_access_token($appid= '',$appsecret='')
{
$access_token = S($appid);
if(empty($access_token))
{
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
$access_token = go_curl($url);
$access_token = json_decode($access_token,true);
S($appid,$access_token,7000);
return $access_token['access_token'];
}
else
{
return $access_token['access_token'];
}
}
function go_curl($url, $type, $data = false, &$err_msg = null, $timeout = 20, $cert_info = array()){
$type = strtoupper($type);
if ($type == 'GET' && is_array($data)) {
$data = http_build_query($data);
}
$option = array();
if ( $type == 'POST' ) {
$option[CURLOPT_POST] = 1;
}
if ($data) {
if ($type == 'POST') {
$option[CURLOPT_POSTFIELDS] = $data;
} elseif ($type == 'GET') {
$url = strpos($url, '?') !== false ? $url.'&'.$data : $url.'?'.$data;
}
}
$option[CURLOPT_URL] = $url;
$option[CURLOPT_FOLLOWLOCATION] = TRUE;
$option[CURLOPT_MAXREDIRS] = 4;
$option[CURLOPT_RETURNTRANSFER] = TRUE;
$option[CURLOPT_TIMEOUT] = $timeout;
//設置證書信息
if(!empty($cert_info) && !empty($cert_info['cert_file'])) {
$option[CURLOPT_SSLCERT] = $cert_info['cert_file'];
$option[CURLOPT_SSLCERTPASSWD] = $cert_info['cert_pass'];
$option[CURLOPT_SSLCERTTYPE] = $cert_info['cert_type'];
}
//設置CA
if(!empty($cert_info['ca_file'])) {
// 對認證證書來源的檢查,0表示阻止對證書的合法性的檢查。1需要設置CURLOPT_CAINFO
$option[CURLOPT_SSL_VERIFYPEER] = 1;
$option[CURLOPT_CAINFO] = $cert_info['ca_file'];
} else {
// 對認證證書來源的檢查,0表示阻止對證書的合法性的檢查。1需要設置CURLOPT_CAINFO
$option[CURLOPT_SSL_VERIFYPEER] = 0;
}
$ch = curl_init();
curl_setopt_array($ch, $option);
$response = curl_exec($ch);
$curl_no = curl_errno($ch);
$curl_err = curl_error($ch);
curl_close($ch);
// error_log
if($curl_no > 0) {
if($err_msg !== null) {
$err_msg = '('.$curl_no.')'.$curl_err;
}
}
return $response;
}