小程序新用户第一次打开 偶尔会出现不调用页面onload函数的情况,导致用户无法注册 卡在初始化页面,由于出现频率较低 无法复现提交代码片段,下图为我司客户提供bug截图 一直卡在这里
该页面代码:
WXML
<wxs module="Config" src='../../utils/config.wxs'></wxs><view class='container'> <view class='content'> <!-- todo 修改logo --> <image id='logo' mode='widthFix' src='https://file.jinrikanfaapp.com/dlxcx/shouquanImg.png'></image> <button id='shouquan' open-type='getUserInfo' bindgetuserinfo='getUserInfo'>授权</button> </view> <view wx:if='{{isUser}}' id='masker'> 初始化中... </view></view> |
JS:
// pages/user/wechat/wechat.jsconst Req = require("../../utils/request.js");const App = getApp();var isPage = null;Page({ data: { isUser: true }, onLoad: function(options) { isPage = options.page; let that = this; // 判断用户是否授权获取信息 Req.getTokenByCode().then(res => { wx.setStorageSync('TOKEN', res.token); wx.setStorageSync('userToken', res); return Req.isGetUserInfo(); }).then(res => { // 用户已授权 let wxuser = res; wx.setStorageSync('wxUser', wxuser.userInfo); setUser(wxuser); }).catch(rej => { // 用户未授权 that.setData({ isUser: false }); }) }, onShareAppMessage: function() { // 获取默认的分享参数 return App.getDefSahreJson(); }, // 微信授权获取用户信息 getUserInfo(res) { wx.showLoading({ title: '加载中', mask: true }) if (res.detail.errMsg == "getUserInfo:ok") { let wxuser = res.detail; wx.setStorageSync('wxUser', wxuser.userInfo); setUser(wxuser); } else { wx.hideLoading(); wx.showToast({ title: '你拒绝了授权,请重试', icon: "none" }) } }});// 获取服务器用户信息function setUser(wxuser) { Req.setUser(wxuser).then(res => { wx.showLoading({ title: '跳转中', mask: true }) if (isPage == "getGive") { wx.reLaunch({ url: '/pages/getGive/getGive?key=' + wx.getStorageSync("getGiveKey"), }); } else { wx.switchTab({ url: '/pages/stock/stock', }) } })} |
CSS:
/* pages/user/wechat/wechat.wxss */.container { display: flex; flex-direction: column; align-items: center; width: 100%;}.container .content { width: 100%; display: flex; flex-direction: column; padding-top: 100rpx;}#logo { margin: 0 60rpx; box-sizing: border-box; width: auto;}#shouquan { background-color: #56bb37; color: white; margin: 90rpx 60rpx 0 60rpx; /* display: block; */ box-sizing: border-box;}#masker { width: 100%; height: 100%; position: fixed; top: 350rpx; left: 0; background-color: white; z-index: 9; font-size: 30rpx; padding: 200rpx 0; text-align: center;} |
JSON
{ "usingComponents": {}, "navigationBarTitleText": "YOOZ商品中心", "disableScroll": true} |

估计用户没有授权,你代码中未授权isUser就是false,那肯定就会卡住了,我要做这种至少要提示未授权不能做XX,请重新点击授权等等,这样就可以判断是小程序问题还是用户问题了