my-uniapp-tools
Version:
一个简洁稳定的 uni-app 开发工具库,提供剪贴板、本地存储、导航、系统信息等常用功能
2 lines (1 loc) • 4.72 kB
JavaScript
"use strict";var e=require("./core.cjs");const t=e.ErrorHandler.getInstance();function r(){return"undefined"==typeof uni?null:uni}class n{static instance;cache=new Map;maxCacheSize=100;static getInstance(){return n.instance||(n.instance=new n),n.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(n,o,a={}){try{if(!n||"string"!=typeof n)return!1;const e=r();if(!e||"function"!=typeof e.setStorageSync)return!1;const t=this.createItem(o,a),c=JSON.stringify(t);if(c.length>1048576)throw new Error("存储数据过大");return e.setStorageSync(n,c),this.remember(n,t),!0}catch(r){return t.handleError(new e.UniAppToolsError("SET_STORAGE_ERROR",`设置存储失败: ${r instanceof Error?r.message:String(r)}`,"localStorage"),"localStorage"),!1}}async setAsync(n,o,a={}){try{if(!n||"string"!=typeof n)return!1;const e=r();if(!e||"function"!=typeof e.setStorage)return this.set(n,o,a);const t=this.createItem(o,a),c=JSON.stringify(t);return!(c.length>1048576)&&await new Promise(r=>{e.setStorage({key:n,data:c,success:()=>{this.remember(n,t),r(!0)},fail:()=>r(!1)})})}catch(r){return t.handleError(new e.UniAppToolsError("SET_STORAGE_ASYNC_ERROR",`异步设置存储失败: ${r instanceof Error?r.message:String(r)}`,"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 n=r();if(!n||"function"!=typeof n.getStorageSync)return t;const o=n.getStorageSync(e);if(!o||"string"!=typeof o)return t;const a=JSON.parse(o);let c;return c=a&&"object"==typeof a&&"value"in a&&"timestamp"in a?a:{value:a,timestamp:Date.now()},this.isExpired(c)?(this.remove(e),t):(this.cache.set(e,c),c.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 n=r();if(!n||"function"!=typeof n.getStorage)return this.get(e,t);const o=await new Promise(t=>{n.getStorage({key:e,success:e=>t(e.data),fail:()=>t(void 0)})});if(!o||"string"!=typeof o)return t;const a=JSON.parse(o),c=a&&"object"==typeof a&&"value"in a&&"timestamp"in a?a:{value:a,timestamp:Date.now()};return this.isExpired(c)?(this.remove(e),t):(this.remember(e,c),c.value)}catch(r){return console.warn(`[localStorage] 异步获取存储失败 [${e}]:`,r),t}}remove(n){try{if(!n||"string"!=typeof n)return!1;const e=r();return!(!e||"function"!=typeof e.removeStorageSync||(e.removeStorageSync(n),this.cache.delete(n),0))}catch(r){return t.handleError(new e.UniAppToolsError("REMOVE_STORAGE_ERROR",`删除存储失败: ${n}`,"localStorage"),"localStorage"),!1}}clear(){try{const e=r();return!(!e||"function"!=typeof e.clearStorageSync||(e.clearStorageSync(),this.cache.clear(),0))}catch(r){return t.handleError(new e.UniAppToolsError("CLEAR_STORAGE_ERROR","清空存储失败","localStorage"),"localStorage"),!1}}getInfo(){try{const e=r();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(r){return t.handleError(new e.UniAppToolsError("GET_STORAGE_INFO_ERROR",`获取存储信息失败: ${r instanceof Error?r.message:String(r)}`,"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(r){return t.handleError(new e.UniAppToolsError("CLEAN_EXPIRED_ERROR",`清理过期数据失败: ${r instanceof Error?r.message:String(r)}`,"localStorage"),"localStorage"),0}}}const o=n.getInstance();function a(e,t,r={}){return o.set(e,t,r)}function c(e,t){return o.get(e,t)}exports.batchGetStorage=function(e){const t={};return Array.isArray(e)?(e.forEach(e=>{t[e]=c(e)}),t):t},exports.batchSetStorage=function(e,t={}){if(!e||"object"!=typeof e||Array.isArray(e))return 0;let r=0;return Object.entries(e).forEach(([e,n])=>{a(e,n,t)&&r++}),r},exports.cleanExpiredStorage=function(){return o.cleanExpired()},exports.clearStorageSync=function(e){return void 0!==e?o.remove(e):o.clear()},exports.getStorage=async function(e,t){return await o.getAsync(e,t)},exports.getStorageInfo=function(){return o.getInfo()},exports.getStorageSync=c,exports.setStorage=async function(e,t,r={}){return await o.setAsync(e,t,r)},exports.setStorageSync=a;