今天接了一個(gè)新的需求赖阻,需要在小程序內(nèi)打開一個(gè)h5界面進(jìn)行授權(quán),之后再返回信息赔嚎。
測(cè)試了官方文檔wx.miniProgram.postMessage
沒(méi)有效果后魄咕,想了個(gè)折中的辦法。
web-view =》打開頁(yè)面 =》進(jìn)行授權(quán) =》頁(yè)面內(nèi)部自己跳轉(zhuǎn) =》跳轉(zhuǎn)小程序某個(gè)承接頁(yè)骂维。
實(shí)際上是使用了wx.miniProgram.navigateTo({url: '/path/to/page'})
方法惹资,讓后端在h5頁(yè)面跳回小程序時(shí)跳到一個(gè)特定頁(yè)面,將參數(shù)拼接在頁(yè)面后綴席舍,這才實(shí)現(xiàn)了傳輸信息布轿。
- web-view代碼:
<web-view src="{{url}}"></web-view>
onLoad: function (options) {
let _this = this;
wx.request({
url: 'xxx/weiXin/returnUrl', //僅為示例,并非真實(shí)的接口地址
header: {
'content-type': 'application/json' // 默認(rèn)值
},
success(res) {
let url = 'https://xxxx&state=redirect&sUrl=' + res.data
_this.setData({
url: encodeURI(url)
})
console.log(encodeURI)
},
fail(res) {
console.log(res)
}
})
}
- h5頁(yè)面
wx.miniProgram.navigateTo({url: '/pages/index/login'+ data });
- 承接頁(yè)面
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad: function (options) {
this.getUserInfo(options)
},
/**
* 獲取用戶信息
* @pamars 用戶信息
*/
getUserInfo(options) {
let _this = this;
let userInfo = options;
app.setUser(userInfo);
wx.reLaunch({
url: '/pages/index/index'
})
}