引入js
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js">
分享js代码
var appId= "{$appid}";
var timestamp= '{$timestamp}';
var nonceStr= '{$time}';
var signature= '{$signature}';
var title="{$title}";
var link= "{$link}";
var imgUrl="{$imgUrl}";
var desc= "{$desc}";
/*微信接口*/
wx.config({
debug: false,//这里是开启测试,如果设置为true,则打开每个步骤,都会有提示,是否成功或者失败
appId: appId,
timestamp: timestamp,//这个一定要与上面的php代码里的一样。
nonceStr: nonceStr,//这个一定要与上面的php代码里的一样。
signature: signature,
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
//'checkJsApi',
// 'openLocation',
// 'getLocation'
]
});
wx.ready(function () {
wx.onMenuShareTimeline({
//朋友圈
title: title, // 分享标题
link: link, // 分享链接
imgUrl: imgUrl, // 分享图标
success: function () {
},
cancel: function () {
}
});
wx.onMenuShareAppMessage({
//好友
title: title, // 分享标题
desc: desc, //
link: link, // 分享链接
imgUrl: imgUrl, // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
},
cancel: function () {
}
});
wx.onMenuShareQQ({
//QQ
title: title, // 分享标题
desc: desc, // 分享描述
link: link, // 分享链接
imgUrl: imgUrl, // 分享图标
success: function () {
},
cancel: function () {
}
});
wx.onMenuShareWeibo({
//微博
title: title, // 分享标题
desc: desc, // 分享描述
link: link, // 分享链接
imgUrl: imgUrl, // 分享图标
success: function () {
},
cancel: function () {
}
});
});
后台php代码
$link = Config::get('protocol') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$time = time();
.....
$this->view->assign('link2', urlencode($link));
$this->view->assign('timestamp', $time);
$this->view->assign('signature', $signature);
$this->view->assign('title', $title);
$this->view->assign('desc', mb_substr($desc, 0, 30, 'utf-8'));
$this->view->assign('imgUrl', $imgUrl);
$this->view->assign('link', $link);
$this->view->assign('appid', $appid);
$this->view->assign('time', $time);
原文链接:微信分享朋友
个人博客:www.liusongs.com