UNPKG

ipink-util

Version:

util.js

2 lines (1 loc) 1.89 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./config.js");(()=>{try{return"undefined"!=typeof uni?uni:"undefined"!=wx?wx:"undefined"!=my?my:"undefined"!=qq&&qq}catch(e){return!1}})();class Cache{static#e=null;#n="";static t(e){return Cache.#e||(Cache.#e=new Cache(e||"")),Cache.#e}constructor(e){this.#n=e||""}i(e){e.id&&(this.#n=e.id)}set(n,t,r=-1){try{n=this.o(n);const i={value:t,u:r,h:Date.now()};e.l()?"undefined"!=typeof uni?uni.setStorageSync(n,JSON.stringify(i)):wx.setStorageSync(n,JSON.stringify(i)):e.p?e.p.localStorage.setItem(n,JSON.stringify(i)):new Error("uni or window is not undefined !")}catch(i){return console.log("Cache.set.error: "+i),!1}return!0}setItem=this.set;get(n){try{let r=e.l();n=this.o(n);let i=r?("undefined"!=typeof uni?uni.getStorageSync(n):wx.getStorageSync(n))||{}:e.p?e.p.localStorage.getItem(n)||{}:new Error("uni or window is not undefined !");try{i=JSON.parse(i)}catch(t){i={}}const{value:o=null,u:u=-1,h:c=0}=i;if(!o)return"";if(u>0){const e=(new Date).getTime();if((e-c)/1e3>u)return this.remove(n),""}return o}catch(r){return console.log("Cache.get.error: "+r),""}}getItem=this.get;remove(e){try{return e=this.o(e),"undefined"!=typeof uni?uni.removeStorageSync(e):"undefined"!=typeof wx?wx.removeStorageSync(e):"undefined"!=typeof window&&window.localStorage&&window.localStorage.removeItem(e),!0}catch(n){return console.log("Cache.remove.error: ",n),!1}}removeItem=this.remove;clear(){try{return"undefined"!=typeof uni?uni.clearStorageSync():"undefined"!=typeof wx?wx.clearStorageSync():"undefined"!=typeof window&&window.localStorage&&window.localStorage.clear(),!0}catch(e){return console.log("Cache.clear.error: "+e),!1}}C(){return this.#n||e.v.m}o(n){const t=this.C();let r=n+"_"+e.v.language;return t&&(r=n+"_"+t+"_"+e.v.language),r}}const n=Cache.t();exports.Cache=Cache,exports.Storage=n;