- 新建 share.js
async function shareWx(title, desc, imgUrl) {
var title = arguments[0] ? arguments[0] : '';
var desc = arguments[1] ? arguments[1] : '';
var imgUrl = arguments[2] ? arguments[2] : '';
var link = window.location.href;
await this.$api.shareWx({
url: window.location.href
}).then(res => {
var wxshare = require('weixin-js-sdk');
wxshare.config({
debug: false,
appId: res.data.appId,
timestamp: res.data.timestamp,
nonceStr: res.data.noncestr,
signature: res.data.signature,
jsApiList: [
"updateAppMessageShareData",
"updateTimelineShareData",
]
});
this.$store.dispatch('set_systemData', res.data.data)
title = title == '' ? res.data.data.gzhname : title;
desc = desc == '' ? res.data.data.gzhdesc : desc;
imgUrl = imgUrl == '' ? (res.data.data.apihost + res.data.data.gzhlogo) : imgUrl;
wxshare.ready(function() {
wxshare.updateAppMessageShareData({
title: title,
desc: desc,
link: link,
imgUrl: imgUrl,
success: function() {
},
cancel: function() {
}
});
wxshare.updateTimelineShareData({
title: title,
desc: desc,
link: link,
imgUrl: imgUrl,
success: function() {},
cancel: function() {}
});
})
});
}
export default {
shareWx
};
- main.js 中引入
import share from './common/share.js'
Vue.prototype.shareWx = share.shareWx;
- 需要引入的文件中调用
this.shareWx('标题', '简介', '封面图')