zoukankan      html  css  js  c++  java
  • 微信小程序 本地缓存保持登录状态之wx.setStorageSync()使用技巧

    微信小程序提供了一个如同浏览器cookie本地缓存方法,那就是今天要说的wx.setStorageSync() 

    注意,该方法是同步请求,还有个异步请求的方法是wx.setStorage(),参考官方文档【https://developers.weixin.qq.com/miniprogram/dev/api/storage/wx.setStorage.html

    取出本地缓存方法wx.getStorageSync,同样的,它也是异步请求,它也有一个同步请求方法wx.getStorage(),

    使用方法如下 

    登录时候,将所需要存的字段 存入本地缓存中

    wx.setStorageSync('userIdEnc', userIdEnc); //将userIdEnc存入本地缓存
    wx.setStorageSync('loginDevice', loginDevice);//将loginDevice存入本地缓存

    使用时,再从本地缓存 中取出

    var userIdEnc = wx.getStorageSync('userIdEnc'); //获取本地缓存中的userIdEnc //用户唯一识别码
    var loginDevice = wx.getStorageSync('loginDevice');//获取本地缓存中的loginDevice
    
      
        
    var header = {
          'content-type': 'application/json',
          'cookie': "devimark=" + loginDevice + ";" + "usenc=" + userIdEnc,
        };
    
    
    wx.request({ 
          method: "post",
          url: 'http://*****.com/createWashingOrder',
          data: '{"appId": "' + appid + '", "timestamp": ' + timestamp + ', "version": "' + version + '", "sign": "' + sign + '", "orderAmount": "' + orderAmount + '","modeId": "' + modeId + '","deviceId": "' + deviceId + '","userIdEnc": "' + userIdEnc + '", }@#@1100310183560349',
          header: header,
          dataType: "json",
          success: function (res) {
    console.log("请求成功", res)

        }
    , fail: function (res) {
          console.log("请求失败", res)
        }
    
    })
  • 相关阅读:
    [转载]oracle中的exists 和not exists 用法详解
    oracle中sql语句的优化(转帖)
    BizTalk Server 2010 使用 WCF Service [ 上篇 ]
    冒泡排序
    一起复习几何(4)
    手把手教你升级到 Mysql 5.5
    BizTalk Server 2010 映射器(Mapper) [ 上篇 ]
    基于OpenGL的渲染引擎
    BizTalk Server 2010 映射器(Mapper) [ 下篇 ]
    BizTalk Server 2010 映射器(Mapper) [ 中篇 ]
  • 原文地址:https://www.cnblogs.com/zhixi/p/9661913.html
Copyright © 2011-2022 走看看