这是我同事写的方法,比官网的简单
调用方法
that.weixin_share({
postUrl: '',//微信分享的php
shareData: {
title: ,//分享标题
desc: ,//分享描述
imgUrl: ,//分享图标
link: //分享链接
}
});
分装的方法
function weixin_share(data,fn){
var post_url = encodeURIComponent(window.location.href.split('#')[0]);
tools.ajax(data.postUrl,{//tools也是封装的方法
type: 'post',
data: { origin : post_url },
success: function (ret) {
if(ret.code == 1){
var wxData = data.shareData;
wx.config({
debug: false,
appId: ret.appId,
timestamp: parseInt(ret.timestamp),
nonceStr: ret.nonceStr,
signature: ret.signature,
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'onMenuShareQZone'
]
});
wx.ready(function () {
wx.onMenuShareTimeline(wxData);
wx.onMenuShareAppMessage(wxData);
wx.onMenuShareQQ(wxData);
wx.onMenuShareWeibo(wxData);
wx.onMenuShareQZone(wxData);
fn&&fn();
});
wx.error(function(res){
alert(res.errMsg);
});
}else{
alert(ret.msg);
}
},
error: function(e){
console.log(e);
}
}
);
}
微信分享php
<?php
//ajax 跨域
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
error_reporting(E_ERROR);
const appId = 'wx0a03b48bfe661945';
const screct = 'a5ebd79e5a127c261c154f2fa0056618';
require_once "jssdk.php";
$jssdk = new JSSDK(appId, screct);
$signPackage = $jssdk->GetSignPackage($_POST['origin']);
?>