微信小程序提供了一個如同瀏覽器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) } })