zoukankan      html  css  js  c++  java
  • 小程序 setStorageSync( ) 存储数据丢失问题

    场景:

    近期小程序出现问题,我们在 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

  • 相关阅读:
    webpack4.x相关笔记整理
    vue原理探索--响应式系统
    git常用命令备忘
    小程序开发知识点及坑点总结
    无异常日志,就不能排查问题了???
    【从单体架构到分布式架构】(三)请求增多,单点变集群(2):Nginx
    【从单体架构到分布式架构】(二)请求增多,单点变集群(1):负载均衡
    【从单体架构到分布式架构】(一)万丈高楼平地起:环境准备
    一个接口查询关联了十几张表,响应速度太慢?那就提前把它们整合到一起
    docker基础:容器操作命令
  • 原文地址:https://www.cnblogs.com/lpp-11-15/p/13182953.html
Copyright © 2011-2022 走看看