小程序打开另一个小程序分享另一个小程序分享后返回上一个小程序
目录
小程序打开另一个小程序、分享另一个小程序、分享后返回上一个小程序
小程序是无法直接分享另一个小程序的链接给微信好友的,它只能分享自身的链接。
但是我们可以通过另一种方法去实现这个功能:
- 首先,我们在自己的小程序里面打开要分享的另一个小程序
- 然后,我们可以在打开的小程序里面分享它的链接给微信好友
- 最后,跳转回我们自己的小程序(前提是我们需要知道要跳转分享小程序的appid)
- 如果分享后的success等事件不起效,则是版本问题(详见https://developers.weixin.qq.com/community/develop/doc/0000447a5b431807af57249a551408)
打开另一个小程序
<view bindtap="navigateToMiniProgram"></view>
需要用户自己点击触发事件,开发者无法调用此接口自动跳转至其他小程序
navigateToMiniProgram: funtion(){
wx.navigateToMiniProgram({
appId: '', //要打开的小程序appid
path: '/pages/share/share?userId=' + wx.getStorageSync('userId')+'&nickName='+wx.getStorageSync('nickName'), //要打开另一个小程序的页面和传递的参数
envVersion: 'trial', //打开小程序的版本(体验版trial;开发版develop;正式版release)
success(res) {
··//打开成功之后事件
}
})
}
在app.json里面配置
"navigateToMiniProgramAppIdList": [
"" //要打开的appid
]
跳转的小程序承接参数(前一个小程序跳转的页面)
onLoad: function(options) {
this.setData({
teacherId: options.userId,
nickName: options.nickName
})
},
分享链接给好友
<button style="text-align:center;margin-top:8vh;background:#FEBF55;width:80vw;border-radius:10px;color:#fff;" bindtap="buttonShare" class="buttonSize" open-type="share">请选择您的学生</button>
buttonShare: function (e) {
var that = this;
// that.audioButton();
},
onShareAppMessage: function(res) {
if (res.from === 'button') {
// 来自页面内转发按钮
console.log(res.target)
}
return {
title: this.data.nickName + '邀请您加入',
path: 'pages/index/main?teacherId=' + this.data.teacherId, //链接打开的二面
imageUrl: this.data.image+'studyIcon.jpg', //分享链接的icon
success: function (res) {
console.log(res)
// 转发成功之后的回调
if (res.errMsg == 'shareAppMessage:ok') {
wx.showToast({
title: '分享成功',
icon: 'none'
})
}
},
fail: function () {
// 转发失败之后的回调
if (res.errMsg == 'shareAppMessage:fail cancel') {
wx.showToast({
title: '分享失败',
icon: 'none'
})
// 用户取消转发
} else if (res.errMsg == 'shareAppMessage:fail') {
// 转发失败,其中 detail message 为详细失败信息
wx.showToast({
title: '分享失败',
icon: 'none'
})
}
},
complete: function () {
// 转发结束之后的回调(转发成不成功都会执行)
wx.showToast({
title: '分享完成',
icon: 'none'
})
console.log("success")
wx.navigateBack({
delta: -1
})
}
}
},
分享后关闭打开的小程序(无法判断是否分享成功;如果ios在分享的时候没有去分享而是直接关闭了小程序,则需要在返回小程序的事件上加个定时器,时间为0居然可以成功)
onShareAppMessage: function(res) {
var shareObj = {
title: this.data.nickName + '邀请您加入',
path: '', //要分享的页面路径和参数
imageUrl: this.data.image + 'studyIcon.jpg' //分享链接的图片
};
// 来自页面内的按钮的转发
if (res.from == 'button') {
var eData = res.target.dataset;
console.log(eData.name); // shareBtn
// 此处可以修改 shareObj 中的内容
shareObj.path = ‘’;
}
wx.navigateBackMiniProgram({ //返回上一个小程序
// extraData: {
// foo: 'bar'
// },
success(res) {
console.log('成功')
}
})
// 返回shareObj
return shareObj;
},