热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

小程序setStorageSync()存储数据丢失问题

场景:近期小程序出现问题,我们在miniAction接口获取openId,openId需要在多个页面跨页面用到,用setStorage存储起来,在需要用到openId的地方用get

场景:

近期小程序出现问题,我们在 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/miniAction",
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;
}
console.log(userInfo,'miniAction')
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();
console.log(userInfo,'logingInfo')
console.log(global.globalData.ceshi)
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/userLogin",
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



推荐阅读
author-avatar
恨透这一切_249
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有