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

  • 相关阅读:
    python如何编译py文件生成pyc、pyo、pyd以及如何和C语言结合使用
    urllib.parse:很底层,但是是一个处理url路径的好模块
    pandas中的Series
    pandas中Series对象下的str所拥有的方法(df["xx"].str)
    10.集成学习与随机森林
    9.决策树
    8.支撑向量机SVM
    HTTP协议详细介绍
    mysql 总结
    MySql练习题参考答案
  • 原文地址:https://www.cnblogs.com/tt-ff/p/11698738.html
Copyright © 2011-2022 走看看