'use strict'; let wxDefaultOptions = { debug: true, appId: '', timestamp: 0, nonceStr: '', signature: '', jsApiList: [ 'checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'translateVoice', 'startRecord', 'stopRecord', 'onRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'uploadVoice', 'downloadVoice', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'getNetworkType', 'openLocation', 'getLocation', 'hideOptionMenu', 'showOptionMenu', 'closeWindow', 'scanQRCode', 'chooseWXPay', 'openProductSpecificView', 'addCard', 'chooseCard', 'openCard' ] }; //let shareUrl = 'http://xxx' + location.pathname; let getWxParam = (url, wxOptions) => { let url = location.href.split('#')[0]; url = encodeURIComponent(url); let promise = new Promise((resolve, reject)=> { $.ajax({ type:'GET', url: 'http://xxx/xxx?param='+url, dataType: 'jsonp' }) .then((data)=> { let wxParam = data; console.log(wxParam); wxOptions.appId = 'wxeb5c3f4a03b880f0'; wxOptions.timestamp = wxParam.timestamp; wxOptions.nonceStr = wxParam.nonceStr; wxOptions.signature = wxParam.signature; wx.config(wxOptions); wx.error((res)=> { alert(res.errMsg); }) resolve(); }, (error)=> { console.log(error); }) }); return promise; } //分享到朋友圈 let shareSocial = (param)=> { wx.onMenuShareTimeline({ title: param.title, // 分享标题 link: param.link, // 分享链接 imgUrl: param.imgUrl, // 分享图标 success: function () { // 用户确认分享后执行的回调函数 param.suCallback(); }, cancel: function () { // 用户取消分享后执行的回调函数 param.failCalback(); } }); } //分享给好友 let shareToFriends = (param) => { wx.onMenuShareAppMessage({ title: param.title, desc: param.desc, link: param.link, imgUrl: param.imgUrl, trigger: function (res) { }, success: function (res) { param.suCallback(); }, cancel: function (res) { }, fail: function (res) { param.failCalback(); console.alert(JSON.stringify(res)); } }); } //title,desc,link,imgUrl,suCallback, failCalback let jssdk = (param) => { wx.ready(()=> { //分享到朋友圈 shareSocial(param); shareToFriends(param); }) } function callWx(param, wxoptions) { getWxParam(param.url, wxOptions).then(()=> { jssdk(param); }) } //param = {url: '', title:'',desc:'',link:'',imgUrl:'',suCallback:func, failCalback: func} module.exports = { wxDefaultOptions,//更改配置 callWx, //默认配置,进行config配置 和ready后定制微信分享内容, }
本文已被整理到了《JavaScript微信开发技巧汇总》,欢迎大家学习阅读。
为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。