分享的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;
}