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

  • 相关阅读:
    css常用属性记录
    js字符串常用方法总结
    mongoose基本操作
    本地存储API
    历史相关API
    自定义播放器
    HTML5全屏操作API
    HTML5自定义属性操作
    HTML5类操作
    案例:3D切割轮播图
  • 原文地址:https://www.cnblogs.com/tt-ff/p/11698738.html
Copyright © 2011-2022 走看看