分享链接
参数携带:
<button open-type="share" hover-class="none">分享</button>
onShareAppMessage: function (res) {
if (res.from === 'button') { // 来自页面内转发按钮
wx.showLoading();
return new Promise((resolve, reject) => {
wx.showLoading({
title: '正在请求分享数据...',
icon: 'none'
})
var vm = this
//此处为发送请求,可以使用ajax或者axios代替
request(`/getLink`, 请求参数, 'POST').then((res) => {
wx.hideLoading();
resolve({
title: '职位详情',
path: `/pages/detail/main?jobId=${vm.jobId}&linkContent=${res.linkContent}`, // 分享出去之后要进入的页面路径
})
})
})
} else {//来自页面外右上角转发
console.log('不是button', res)
}
},
参数接收:
onLoad(options) {
//获取分享的参数
console.log('进入界面传入的参数:', options)
wx.showToast({
title: JSON.stringify(options),
icon: 'none'
})
//将获取到的分享数据发送到后台(测试用)
request('/getFxLinkParam', options).then((res) => {
console.log(res)
})
},
测试结果:(后台接收到onLoad方法发送的请求及参数接收)
分享朋友圈
先点亮分享链接及分享朋友圈的图标
代码如下:
onShow: function () {
this._getData()
// wx.startPullDownRefresh()
wx.showShareMenu({
withShareTicket: true,
menus: ["shareAppMessage", "shareTimeline"]
})
},
参数携带:
//分享到朋友圈
onShareTimeline: function (res) {
var type = '测试';
return {
title: 'XXX招聘平台', // 可不填
query: `id=${123}&type=${type}`, // 可不填 传递的参数,只能是这种格式
imageUrl: '' // 可不填,可以是网络路径也可以是本地路径,分享到朋友圈显示的图标
}
},
参数接收:
onLoad(options) {
//获取分享的参数
console.log('进入界面传入的参数:', options)
wx.showToast({
title: JSON.stringify(options),
icon: 'none'
})
//将获取到的分享数据发送到后台(测试用)
request('/getFxPyqParam', options).then((res) => {
console.log(res)
})
},
另外:此处讲一个小插曲,一个mpvue的bug。
分享朋友圈时无法使用参数,只能使用默认参数。即以下红框内容无效:
解决办法:(修改mpvue的源码)
1. LIFECYCLE_HOOKS 的数组中添加元素 onShareTimeline
2.添加代码:
// 分享朋友圈
onShareTimeline: rootVueVM.$options.onShareTimeline
? function (options) {
return callHook$1(rootVueVM, 'onShareTimeline', options);
} : null,
添加位置如下:(搜索:onShareAppMessage)
3.项目重启即可。