一、login.js (小程序进入后首先进入login.js)
const {
appId,
UapServiceNewUrl
} = require('../../Settings/index').default
Page({
data: {},
// 事件处理函数
onLoad() {
Promise.race([this.mounOpenidFunc(), this.pocketFunc()]).then(() => {
wx.redirectTo({
url: `/pages/index/index`
});
})
},
mounOpenidFunc() {
let that = this;
return new Promise((resolve) => {
wx.checkSession({
success() {
//session_key 未过期,并且在本生命周期一直有效
console.log('session_key 未过期 请继续使用')
console.log("wx.getStorageSync('openId'): ", wx.getStorageSync('openId'))
that.handleLogin(resolve)
},
fail() {
// session_key 已经失效,需要重新执行登录流程
//重新登录
console.log("session_key 已过期, 重新登录")
console.log("我正在准备登录")
that.handleLogin(resolve)
}
})
})
},
//兜底函数,如果5秒内请求openid过程中出现意外 依然可进入h5
pocketFunc() {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 5000)
})
},
handleLogin(resolve) {
wx.login({
success(res) {
if (res.code) {
console.log("res.code: ", res.code)
var dataVal = {
code: res.code,
appId
};
wx.request({
url: `${UapServiceNewUrl}/yonbip-mkt-dinghuo/cyberbank/getWxMiniAppSession`,
method: 'POST',
data: dataVal,
header: {
'content-type': 'application/json' // 默认值
},
success(res) {
console.log("res:: ", res)
if (res.data && res.data.data && res.data.data.openid) {
wx.setStorageSync('openId', res.data.data.openid)
}
resolve()
},
fail() {
resolve();
}
})
}
},
fail() {
resolve();
}
})
}
})
二、 跳到到index,
index.js
// index.js
// 获取应用实例
const app = getApp()
const { PACK_ADDRESS } = require('../../Settings/index').default
Page({
data: {
packAddress: PACK_ADDRESS,
},
// 事件处理函数
onShareAppMessage() {
const promise = new Promise(resolve => {
setTimeout(() => {
resolve({
title: 'YonBIP订货门户'
})
}, 2000)
})
return {
title: 'YonBIP订货门户',
path: '../index/index.wxml',
promise
}
},
// 页面初次渲染完成时触发
onReady() {
},
onLoad(options) {
console.log("options: ", options)
console.log("wx.getStorageSync('openId')1111: ", wx.getStorageSync('openId'))
if(wx.getStorageSync('openId')) {
this.setData({
packAddress: PACK_ADDRESS + `?OpenID=${wx.getStorageSync('openId')}`
})
console.log("packAddress::: ", this.data.packAddress)
}
}
})
三、 index.wxml
index.wxml
<!--index.wxml-->
<view class="container">
<web-view src="{{packAddress}}"></web-view>
</view>
确定接口肯定返回了openId, 微信开发者工具调试一切正常, 真机连微信开发者工具在hwebview的h5中也可以取到openid, 就是上传到体验版之后,取不到openid, 麻烦微信相关人员或者其他大牛们帮我看看是哪方面的问题,很急,拜托了。
console.log("packAddress::: ", this.data.packAddress)
这一句打印有问题吗, 可以在setData之前拼接Url试试