12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import config from "../model/config"
- import dayjs from "dayjs";
- const EXSPACE = "ex_space";
- const isUp = false;// 升级本地存储 新增过期失效功能 默认不打开 设置为true后生效
- export default {
- getStorage(space = config.NAMESPACE) {
- const res = wx.getStorageSync(space);
- return JSON.parse(res || "{}");
- },
- setItem(key, val, ex) {
- const storage = this.getStorage();
- storage[key] = val;
- if (isUp && ex){
- let exObj = this.getStorage(EXSPACE);
- exObj[key] = dayjs().valueOf() +ex;
- wx.setStorageSync(EXSPACE, JSON.stringify(exObj))
- }
- wx.setStorageSync(config.NAMESPACE, JSON.stringify(storage))
- },
- getItem(key) {
- const storage = this.getStorage();
- const exObj = this.getStorage(EXSPACE);
- if (isUp){
- if (exObj && exObj[key] && exObj[key] > dayjs().valueOf()){
- }else {
- this.clearItem(key)
- return null;
- }
- }
- return storage[key];
- },
- clearItem(key) {
- const storage = this.getStorage();
- delete storage[key]
- wx.setStorageSync(config.NAMESPACE, JSON.stringify(storage))
- },
- clearAll() {
- wx.removeStorageSync(config.NAMESPACE)
- }
- }
|