場景:
近期小程序出現問題,我們在 miniAction接口 獲取openId,openId需要在多個頁面跨頁面用到,用setStorage存儲起來,在需要用到openId的地方用 getStorage取,但是上線后,有部分用戶反映小程序登錄不上去,經查是openId在傳輸過程中丟失了,查資料,發現 setStorage存儲 會出現有的用戶丟失的問題。
辦法:
通過global全局來跨頁面共用數據
存儲:
獲取:
代碼:
js
export function login(index) { global.globalData.ceshi = '測試' mpvue.login({ success(res) { wx.showLoading({ title: "瘋狂加載中...", mask: true, }); wx.setStorageSync("code", res.code); global.globalData.code = res.code if (res.code) { getOpenId(res.code); } }, // 處理登錄失敗 fail(res) { index++; console.log(index,'index+++++') if (index < 3) { login(index); } else { wx.showModal({ title: "提示", content: "登錄失敗,請檢查您的當前網絡。", //提示錯誤 confirmText: "確定", confirmColor: "#b8193f", showCancel: false, }); } }, }); } export function getOpenId(code) { https.request({ url: "Welcome/Action", data: { code: code, }, needToken: false, // showLoading: true }) .then((ress) => { mpvue.hideLoading(); if (ress.status) { let userInfo = getUserInfo(); userInfo.openid = ress.data.openId; userInfo.unionid = ress.data.unionid; userInfo.dxOpenid = ress.data.dxOpenid; userInfo.grid_code = ress.data.grid_code wx.setStorageSync("openid",ress.data.openId) global.globalData.openid = ress.data.openId if (ress.data.userInfo.status) { userInfo.userInfo = ress.data.userInfo.status; userInfo.mainBureauName = ress.data.userInfo.gis.main_bureau_name; userInfo.subBureauName = ress.data.userInfo.gis.sub_bureau_name; userInfo.mobile = ress.data.userInfo.data.deviceno; } else { userInfo.userInfo = ress.data.userInfo; } setUserInfo(userInfo); } else { wx.showModal({ title: "提示", content: "當前網絡繁忙,請1分鍾后再嘗試登錄哦~~", //提示錯誤 confirmText: "確定", showCancel: false, }); console.log("login failed"); } }); } export function setUserInfo(userInfo){ wx.setStorageSync('user_info',userInfo) global.globalData.userInfo = userInfo } // 獲取用戶信息 export function getUserInfo(settings){ let userInfo = wx.getStorageSync('user_info') || global.globalData.userInfo || {} return userInfo }
js
getUserInfo(e) { const that = this; if (that.checkVal === "A") { // 登錄鎖 if (that.logining) { console.log("loginning"); return true; } else if(!(wx.getStorageSync("openid")||global.globalData.openid)){ login(0) this.loginingInfo() }else { this.loginingInfo() } } else { wx.showModal({ title: "溫馨提示", content: "請閱讀並同意協議", showCancel: false, success: (res) => {}, }); } }, loginingInfo(){ const that = this that.logining = true; wx.showLoading({ title: "瘋狂加載中...", mask: true, }); mpvue.getUserInfo({ success(res) { let userInfo = getUserInfo() userInfo = Object.assign(res.userInfo, userInfo); let shareId = global.globalData.shareId; if (shareId) { userInfo.wid = shareId; } if (wx.getStorageSync("fbc") || global.globalData.fbc) { userInfo.source = wx.getStorageSync("fbc") || global.globalData.fbc; } if (wx.getStorageSync("jobNum") || global.globalData.jobNum) { userInfo.jobNum = wx.getStorageSync("jobNum") || global.globalData.jobNum; userInfo.mainBureauName = wx.getStorageSync("mainBureauName") || global.globalData.mainBureauName; } userInfo.step = "1"; that .https({ url: "User/Login", needToken: false, needUid: true, data: userInfo, }) .then((res) => { that.logining = false; mpvue.hideLoading(); if (res.status) { console.log("第一步登錄成功"); if (res.data.id) { userInfo.id = res.data.id; } userInfo.user_type = res.data.type; userInfo.wid = res.data.wid; userInfo.wname = res.data.wname; let token = res.data.token; global.globalData.token = token; wx.setStorageSync("token", token); setUserInfo(userInfo); wx.setStorageSync("hasLogin", true); global.globalData.hasLogin = true; // get_no_read_msg(); // 判斷step if (res.data.step == 2) { // 獲取手機號 that.getUserInfos = false; that.isgetPhone = true; that.modalName = true; that.getPhoneNumber(); } else if (res.data.step == 3) { that.modalName = true; that.isgetPhone = false; that.getUserInfos = false; that.getLocations = true; } else if (userInfo.wid) { mpvue.redirectTo({ url: "/pages/second/main", }); } else { // 獲取手機號 that.getUserInfos = false; that.modalName = true; that.isgetPhone = true; that.getPhoneNumber(); } } }); }, fail(res) { mpvue.hideLoading(); that.logining = false; console.log("fail......."); mpvue.openSetting({ success(res) { console.log(res.authSetting); }, }); }, complete(res) { console.log("complete"); }, }); },
注意:
在用global存儲時,需要注意現在 main.js 定義 global,如下圖
否則 直接用global.globalData.user_type 會報錯 user_type is not definition