ipink-util
Version:
util.js
133 lines (130 loc) • 3.6 kB
JavaScript
import { getSdk, win, Config } from './config.mjs';
(() => {
try {
return "undefined" != typeof uni ? uni : "undefined" != wx ? wx : "undefined" != my ? my : "undefined" != qq ? qq : false;
} catch (e) {
return false;
}
})();
class Cache {
static #instance = null;
#id = "";
static createInstance(id) {
if (!Cache.#instance) {
Cache.#instance = new Cache(id || "");
}
return Cache.#instance;
}
constructor(id) {
this.#id = id || "";
}
setConfig(option) {
if (option.id) {
this.#id = option.id;
}
}
/**
* @desc 设置缓存
* @param key 存储Key { string }
* @param value 存储内容 { any }
* @param expire 过期时间( s > 0 ) { number }
* @return: boolean
*/
set(key, value, expire = -1) {
try {
key = this.genKey(key);
const cacheValue = {
value,
expire,
createTime: Date.now()
};
let sdk = getSdk();
sdk ? typeof uni !== "undefined" ? uni.setStorageSync(key, JSON.stringify(cacheValue)) : wx.setStorageSync(key, JSON.stringify(cacheValue)) : win ? win.localStorage.setItem(key, JSON.stringify(cacheValue)) : new Error("uni or window is not undefined !");
} catch (e) {
console.log("Cache.set.error: " + e);
return false;
}
return true;
}
setItem = this.set;
/**
* @desc 获取缓存
* @param key 存储Key { string }
* @return: any
*/
get(key) {
try {
let sdk = getSdk();
key = this.genKey(key);
let cacheValue = sdk ? (typeof uni !== "undefined" ? uni.getStorageSync(key) : wx.getStorageSync(key)) || {} : win ? win.localStorage.getItem(key) || {} : new Error("uni or window is not undefined !");
try {
cacheValue = JSON.parse(cacheValue);
} catch (error) {
cacheValue = {};
}
const {
value = null,
expire = -1,
createTime = 0
} = cacheValue;
if (!value) return "";
if (expire > 0) {
const nowTime = (/* @__PURE__ */ new Date()).getTime();
const isExpire = (nowTime - createTime) / 1e3 > expire;
if (isExpire) {
this.remove(key);
return "";
}
}
return value;
} catch (e) {
console.log("Cache.get.error: " + e);
return "";
}
}
getItem = this.get;
/**
* @desc 删除指定缓存
* @param key 存储Key { string }
* @return: boolean
*/
remove(key) {
try {
key = this.genKey(key);
if (typeof uni !== "undefined") uni.removeStorageSync(key);
else if (typeof wx !== "undefined") wx.removeStorageSync(key);
else typeof window !== "undefined" && window.localStorage && window.localStorage.removeItem(key);
return true;
} catch (e) {
console.log("Cache.remove.error: ", e);
return false;
}
}
removeItem = this.remove;
/**
* @desc 清除缓存
* @return: boolean
*/
clear() {
try {
if (typeof uni !== "undefined") uni.clearStorageSync();
else if (typeof wx !== "undefined") wx.clearStorageSync();
else typeof window !== "undefined" && window.localStorage && window.localStorage.clear();
return true;
} catch (e) {
console.log("Cache.clear.error: " + e);
return false;
}
}
genID() {
return this.#id || Config.appId;
}
genKey(key) {
const id = this.genID();
let cacheKey = key + "_" + Config.language;
if (id) cacheKey = key + "_" + id + "_" + Config.language;
return cacheKey;
}
}
const Storage = Cache.createInstance();
export { Cache, Storage };