小程序分享之系列
1.分享页面(detail/detail.js)
//分享
onShareAppMessage: function() {
var accountId = wx.getStorageSync('accountId');
return {
title: this.data.listData.caption,//分享的标题
path: '/pages/index/detail/detail?upper=' + accountId + "&roomId=" + this.data.roomId + "&isshare=" + 1,//传递是否是分享
imageUrl: this.data.listData.url,//分享页面的图片
}
}
2.app.js页面
onShow: function (e) {
var historyUrl = e.path + '?'//分享的页面路径
for (var key in e.query) {
historyUrl = historyUrl + key + '=' + e.query[key]+'&'//分享页面传递的参数
}
this.globalData.historyPath = historyUrl
var upper
var parkId
var roomId
if(e.query.parkId){
parkId = e.query.parkId
upper = e.query.upper
}
else if(e.query.roomId){
roomId = e. query.roomId
upper = e.query.upper
}
else{
upper = e.query.upper
}
}
})
// 登录
var accountId = wx.getStorageSync('accountId');
var that = this;
if (accountId == "") {
if (this.globalData.openid == null) {
wx.login({
success: function (res) {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
if (res.code) {
var url = that.globalData.serverBaseAddr + "/apic/wx/getopenid/" + res.code;
var params = that.GetSign("code=" + res.code);
url = url + "?" + params
wx.request({
url: url,
success: function (result) {
if (result.data.code == 0) {
that.globalData.openid = result.data.data.openid;
wx.redirectTo({//授权登录
url: '/pages/authorize/authorize?upper=' + upper + "&parkId=" + parkId + "&roomId=" + roomId + "&scene=" + e.scene + '&path=' + e.path,
})
} else {
wx.showToast({
title: result.data.msg,
icon: 'none'
})
}
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
} else {
wx.redirectTo({
url: '/pages/authorize/authorize?upper=' + upper + "&parkId=" + parkId + "&roomId=" + roomId + "&scene=" + e.scene + "&path=' + e.path",
})
}
}
},
3.授权登录页
通过判断分享出的页面是导航页面还是详情页面,控制跳转页面
var url = app.globalData.historyPath;
if (url.indexOf("pages/index/index/index")==0 || url.indexOf("pages/park/park/park")==0 || url.indexOf("pages/usercenter/index/index")==0){
wx.switchTab({
url: "/" + url
})
}else{
wx.redirectTo({
url: "/" + url
})
}
4.如果是详情页面需要设置返回首页按钮,代码如下:
详情页
onLoad: function(e){
if (e.isshare == 1) {
console.log('是分享进入');
this.setData({
'isshare': e.isshare
})
}
}
data: {
isshare:0//控制返回首页按钮是否显示
},
// 返回首页
getBack:function(){
wx.reLaunch({
url: '/pages/index/index/index',
})
},
5.wxml中代码
<view wx:if="{{isshare}}" class='back_btn'><button bindtap='getBack'>返回首页</button></view>
6.wxss代码如下:
//父类元素
.b_box{
position: relative;
}
.back_btn{
position: absolute;
left: 0;
top: 20rxp;
}
.back_btn button{
padding: 0;
margin: 0;
width:140rpx;
height: 40rpx;
line-height: 40rpx;
font-size: 20rpx;
border-radius: 5rpx;
color: white;
background-color: rgba(0, 0, 0, 0.1)
}