1.wx_share.js(1)导出二次分享方法:export const wechatShare = async (option = {}, successCB = () => {}) => { const shareOption = { link: location.href, title: 'aaa', desc:'bbb', imgUrl: '', ...option }; wx_config(); wx_share(shareOption, successCB); }; (2)配置微信分享信息 export const wx_config= () => { axios.get(‘url’).then(res => { // 通过url可以得到相关配置信息,这个url一般后台给 let configs = res.data.data.signMap; wx.config({ debug: false, appId: configs.appId, nonceStr: configs.nonceStr, timestamp: configs.timestamp, signature: configs.signature, jsApiList: [ 'hideMenuItems', 'previewImage', 'checkJsApi', 'chooseWXPay', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'getLocation', 'openLocation', 'translateVoice' ] }); }); }; (3)分享 export const wx_share = (option = {}, successCB = () => {}) => { wx.ready(() => { wx.onMenuShareAppMessage({ // 分享给朋友 title: option.title, desc: option.desc, link: option.link, imgUrl: option.imgUrl, success: function(res) { successCB(res); // 用户确认分享后执行的回调函数 }, cancel: function() { // 用户取消分享后执行的回调函数 } }); wx.onMenuShareTimeline({ // 分享到朋友圈 title: option.friendtitle || option.title, // 标题 desc: option.desc, link: option.link, // 链接 imgUrl: option.imgUrl, // 分享图标 success: function(res) { successCB(res); }, cancel: function() { // 用户取消分享后执行的回调函数 } }); }); };
2.页面引用wechatShare( { title: 'cccc', desc: 'ddddd', imgUrl: "" }, () => { console.log("success..."); } );
来源:https://www./content-4-280751.html |
|