1.首先应该自定义一个button按钮 自带属性 open-type="share"
wxml代码:
<button open-type="share">分享给朋友,邀请许愿</button>
//记住:设置样式必须在button里面设置内样式
2.点击微信小程序自带的分享按钮实现转发效果。同时满足以下条件:若是分享点进去的页面,实现返回键的按钮图片改变--房子图标。若是直接查看的页面将不改变图标,同时也可回到主页面。
wxml代码:
//1.首先先做一个if else的判断判断
<image wx:if="{{isShare}}" class="back-btn" src="/images/home/school_back@2x.png" bindtap="backHome"></image>
<image wx:else class="back-btn" src="/images/common/nav_back_def@2x.png" bindtap="backHome"></image>
//切记:一定要记得把id的参数传进去,防止分享点进去没有值。
js代码:
//1.在onload里面先变量及赋值
var that = this
var id = options.id
var reseller = options.reseller
if (!reseller) {
reseller = 0
}
var normal = true
if (options.normal == 'false') {
normal = false
}
that.setData({
id: id,
user: app.globalData.userInfo,
reseller: reseller,
normal: normal,
})
var shared = false
var isShare = options.isShare
if (isShare == "1") {
shared = true}
this.setData({
id: id,
user: app.globalData.userInfo,
isShare: shared
})
var recommender = options.recommender
if (recommender != null && recommender != undefined && recommender != "") {
app.globalData.recommender = recommender
}
//2.在onShareAppMessage里面写路径以及传递参数。
onShareAppMessage: function () {
var user = this.data.user
if (user) {
return {
path: '/pages/wish/detail?recommender=' + user.uid + '&isShare=1' + "&id=" + this.data.id
}
} else {
return {
path: '/pages/wish/detail?id=' + this.data.id + '&isShare=1'
}
}
}
//3.点击左上角的按钮 都将回到主页面。
backHome: function (e) {
if (this.data.isShare) {
wx.switchTab({
url: '/pages/index/index',
})
}
else {
wx.navigateBack({
delta: 1
})
}
},