微信小程序 本地缓存保持登录状态之wx.setStorageSync()使用技巧
2024-09-26 21:47:47
微信小程序提供了一个如同浏览器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)
}
})
最新文章
- OpenCV2:特征匹配及其优化
- 升级react 15.4,常见的错误及解决方案
- 《Linux内核设计与实现》CHAPTER5阅读梳理
- iOS移动开发周报-第21期
- 动态加载JS文件,并根据JS文件的加载状态来执行自己的回调函数
- 导入cocos2d-x samples android官方示例
- 使用 Camtasia Recorder显示 “ camtasia an error occurred in the recorder: video codec open failed ”
- VisualStudio中的代码段
- PyCrpyto windows安装使用方法
- T-SQL编程的基本语法和思想
- Neutron 不健全的HA ROUTER
- numpy中关于*和dot的区别
- Huffman 哈夫曼编码与译码的原理剖析及C++实现
- android -------- 沉浸式状态栏和沉浸式导航栏(ImmersionBar)
- js cookie 工具
- 配置ssh服务允许root管理员直接登录
- 【转】Keil ARM开发 error L6236E错误解决
- POJ3581 后缀数组
- C#中IDisposable的用法
- ios8 UITableView section不显示
热门文章
- 【python】python环境搭建
- 怎样处理jmeter中文乱码
- 面试:如何把xxx.sh使用/etc/init.d/xxx.sh start启动,并且可以用chkconfig配置开机自启动
- linux的一些权限的操作 chmod
- hessian应用示例
- Python9-网络编程4-day33
- centos7 安装显卡驱动方法
- UVA - 11572 Unique Snowflakes 滑动扫描
- HTTPS的请求与响应
- install redis and used in golang on ubuntu 14.04