automated_uniapp/cool/utils/storage.ts

76 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-01-09 16:16:11 +08:00
export const storage = {
// 后缀标识
suffix: "_deadtime",
/**
*
* @param {*} key
*/
get(key: string): any {
return uni.getStorageSync(key);
},
/**
*
*/
info() {
const { keys } = uni.getStorageInfoSync();
const d: any = {};
keys.forEach((e: string) => {
d[e] = uni.getStorageSync(e);
});
return d;
},
/**
*
* @param {*} key
* @param {*} value
* @param {*} expires
*/
set(key: string, value: any, expires?: number): void {
uni.setStorageSync(key, value);
if (expires) {
uni.setStorageSync(
`${key}${this.suffix}`,
Date.parse(String(new Date())) + expires * 1000
);
}
},
/**
*
* @param {*} key
*/
isExpired(key: string): boolean {
return uni.getStorageSync(`${key}${this.suffix}`) - Date.parse(String(new Date())) <= 0;
},
/**
*
* @param {*} key
*/
remove(key: string) {
return uni.removeStorageSync(key);
},
/**
*
*/
clear() {
uni.clearStorageSync();
},
/**
*
*/
once(key: string) {
const value = this.get(key);
this.remove(key);
return value;
},
};