UNPKG

my-uniapp-tools

Version:

一个简洁稳定的 uni-app 开发工具库,提供剪贴板、本地存储、导航、系统信息等常用功能

2 lines (1 loc) 4.61 kB
import{ErrorHandler as e,UniAppToolsError as t}from"./core.js";const r=e.getInstance();function n(){return"undefined"==typeof uni?null:uni}class c{static instance;cache=new Map;maxCacheSize=100;static getInstance(){return c.instance||(c.instance=new c),c.instance}isExpired(e){return!!e.ttl&&Date.now()-e.timestamp>e.ttl}remember(e,t){if(this.cache.size>=this.maxCacheSize){const e=this.cache.keys().next().value;e&&this.cache.delete(e)}this.cache.set(e,t)}createItem(e,t={}){return{value:e,timestamp:Date.now(),ttl:t.ttl}}set(e,c,a={}){try{if(!e||"string"!=typeof e)return!1;const t=n();if(!t||"function"!=typeof t.setStorageSync)return!1;const r=this.createItem(c,a),o=JSON.stringify(r);if(o.length>1048576)throw new Error("存储数据过大");return t.setStorageSync(e,o),this.remember(e,r),!0}catch(e){return r.handleError(new t("SET_STORAGE_ERROR",`设置存储失败: ${e instanceof Error?e.message:String(e)}`,"localStorage"),"localStorage"),!1}}async setAsync(e,c,a={}){try{if(!e||"string"!=typeof e)return!1;const t=n();if(!t||"function"!=typeof t.setStorage)return this.set(e,c,a);const r=this.createItem(c,a),o=JSON.stringify(r);return!(o.length>1048576)&&await new Promise(n=>{t.setStorage({key:e,data:o,success:()=>{this.remember(e,r),n(!0)},fail:()=>n(!1)})})}catch(e){return r.handleError(new t("SET_STORAGE_ASYNC_ERROR",`异步设置存储失败: ${e instanceof Error?e.message:String(e)}`,"localStorage"),"localStorage"),!1}}get(e,t){try{if(!e||"string"!=typeof e)return t;if(this.cache.has(e)){const r=this.cache.get(e);return this.isExpired(r)?(this.cache.delete(e),this.remove(e),t):r.value}const r=n();if(!r||"function"!=typeof r.getStorageSync)return t;const c=r.getStorageSync(e);if(!c||"string"!=typeof c)return t;const a=JSON.parse(c);let o;return o=a&&"object"==typeof a&&"value"in a&&"timestamp"in a?a:{value:a,timestamp:Date.now()},this.isExpired(o)?(this.remove(e),t):(this.cache.set(e,o),o.value)}catch(r){return console.warn(`[localStorage] 获取存储失败 [${e}]:`,r),t}}async getAsync(e,t){try{if(!e||"string"!=typeof e)return t;if(this.cache.has(e)){const r=this.cache.get(e);return this.isExpired(r)?(this.cache.delete(e),this.remove(e),t):r.value}const r=n();if(!r||"function"!=typeof r.getStorage)return this.get(e,t);const c=await new Promise(t=>{r.getStorage({key:e,success:e=>t(e.data),fail:()=>t(void 0)})});if(!c||"string"!=typeof c)return t;const a=JSON.parse(c),o=a&&"object"==typeof a&&"value"in a&&"timestamp"in a?a:{value:a,timestamp:Date.now()};return this.isExpired(o)?(this.remove(e),t):(this.remember(e,o),o.value)}catch(r){return console.warn(`[localStorage] 异步获取存储失败 [${e}]:`,r),t}}remove(e){try{if(!e||"string"!=typeof e)return!1;const t=n();return!(!t||"function"!=typeof t.removeStorageSync||(t.removeStorageSync(e),this.cache.delete(e),0))}catch(n){return r.handleError(new t("REMOVE_STORAGE_ERROR",`删除存储失败: ${e}`,"localStorage"),"localStorage"),!1}}clear(){try{const e=n();return!(!e||"function"!=typeof e.clearStorageSync||(e.clearStorageSync(),this.cache.clear(),0))}catch(e){return r.handleError(new t("CLEAR_STORAGE_ERROR","清空存储失败","localStorage"),"localStorage"),!1}}getInfo(){try{const e=n();if(!e||"function"!=typeof e.getStorageInfoSync)return{keys:[],currentSize:0,limitSize:0};const t=e.getStorageInfoSync();return{keys:t.keys||[],currentSize:t.currentSize||0,limitSize:t.limitSize||0}}catch(e){return r.handleError(new t("GET_STORAGE_INFO_ERROR",`获取存储信息失败: ${e instanceof Error?e.message:String(e)}`,"localStorage"),"localStorage"),{keys:[],currentSize:0,limitSize:0}}}cleanExpired(){try{const e=this.getInfo();let t=0;return e.keys.forEach(e=>{void 0===this.get(e)&&t++}),t}catch(e){return r.handleError(new t("CLEAN_EXPIRED_ERROR",`清理过期数据失败: ${e instanceof Error?e.message:String(e)}`,"localStorage"),"localStorage"),0}}}const a=c.getInstance();function o(e,t,r={}){return a.set(e,t,r)}function i(e,t){return a.get(e,t)}function s(e){return void 0!==e?a.remove(e):a.clear()}async function u(e,t,r={}){return await a.setAsync(e,t,r)}async function l(e,t){return await a.getAsync(e,t)}function f(){return a.getInfo()}function h(){return a.cleanExpired()}function g(e,t={}){if(!e||"object"!=typeof e||Array.isArray(e))return 0;let r=0;return Object.entries(e).forEach(([e,n])=>{o(e,n,t)&&r++}),r}function S(e){const t={};return Array.isArray(e)?(e.forEach(e=>{t[e]=i(e)}),t):t}export{S as batchGetStorage,g as batchSetStorage,h as cleanExpiredStorage,s as clearStorageSync,l as getStorage,f as getStorageInfo,i as getStorageSync,u as setStorage,o as setStorageSync};