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)
    
        }
    
    })

  • 相关阅读:
    FreeRTOS计数型信号量
    FreeRTOS二值信号量
    FreeRTOS队列操作
    ROMTableAddr = 0xE00FF003 错误 Target DLL has been cancelled 错误
    stm32 USART_IT_IDLE中断 一帧数据
    Moving to Express 4
    node.js 模板 ejs 转
    [译]JavaScript中,{}+{}等于多少?
    mongoose简单使用样例
    MongoVUE简单操作手册
  • 原文地址:https://www.cnblogs.com/tt-ff/p/11698738.html
Copyright © 2011-2022 走看看