Commit ec7bef792b2a780736c2b1713af3638fa0b69eed

Authored by Vben
1 parent efb27aff

fix: ensure that storage is deleted correctly close #292

package.json
... ... @@ -103,10 +103,10 @@
103 103 "vite-plugin-imagemin": "^0.2.8",
104 104 "vite-plugin-mock": "^2.1.5",
105 105 "vite-plugin-purge-icons": "^0.7.0",
106   - "vite-plugin-pwa": "^0.5.4",
  106 + "vite-plugin-pwa": "^0.5.5",
107 107 "vite-plugin-style-import": "^0.7.5",
108 108 "vite-plugin-theme": "^0.4.8",
109   - "vite-plugin-windicss": "0.5.0",
  109 + "vite-plugin-windicss": "0.5.3",
110 110 "vue-eslint-parser": "^7.5.0",
111 111 "yargs": "^16.2.0"
112 112 },
... ...
src/utils/cache/storageCache.ts
... ... @@ -4,6 +4,8 @@ import type { EncryptionParams } from '/@/utils/cipher';
4 4  
5 5 import { AesEncryption } from '/@/utils/cipher';
6 6  
  7 +import { isNullOrUnDef } from '/@/utils/is';
  8 +
7 9 export interface CreateStorageParams extends EncryptionParams {
8 10 prefixKey: string;
9 11 storage: Storage;
... ... @@ -61,7 +63,8 @@ export const createStorage = ({
61 63 set(key: string, value: any, expire: number | null = timeout) {
62 64 const stringData = JSON.stringify({
63 65 value,
64   - expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
  66 + time: Date.now(),
  67 + expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null,
65 68 });
66 69 const stringifyValue = this.hasEncrypt
67 70 ? this.encryption.encryptByAES(stringData)
... ... @@ -75,21 +78,20 @@ export const createStorage = ({
75 78 * @memberof Cache
76 79 */
77 80 get(key: string, def: any = null): any {
78   - const item = this.storage.getItem(this.getKey(key));
79   - if (item) {
80   - try {
81   - const decItem = this.hasEncrypt ? this.encryption.decryptByAES(item) : item;
82   - const data = JSON.parse(decItem);
83   - const { value, expire } = data;
84   - if (expire === null || expire >= new Date().getTime()) {
85   - return value;
86   - }
87   - this.remove(this.getKey(key));
88   - } catch (e) {
89   - return def;
  81 + const val = this.storage.getItem(this.getKey(key));
  82 + if (!val) return def;
  83 +
  84 + try {
  85 + const decVal = this.hasEncrypt ? this.encryption.decryptByAES(val) : val;
  86 + const data = JSON.parse(decVal);
  87 + const { value, expire } = data;
  88 + if (isNullOrUnDef(expire) || expire >= new Date().getTime()) {
  89 + return value;
90 90 }
  91 + this.remove(key);
  92 + } catch (e) {
  93 + return def;
91 94 }
92   - return def;
93 95 }
94 96  
95 97 /**
... ...
yarn.lock
... ... @@ -1718,12 +1718,12 @@
1718 1718 dependencies:
1719 1719 vue-demi latest
1720 1720  
1721   -"@windicss/plugin-utils@0.5.0":
1722   - version "0.5.0"
1723   - resolved "https://registry.npmjs.org/@windicss/plugin-utils/-/plugin-utils-0.5.0.tgz#dab121dd33074eccb4b7c85e7757eb5704f1c76e"
1724   - integrity sha512-d2oi4cY+PV0mcQ2wij1nI+Ku0s6Zw2/+zllxX8TtMyEknYqe5zHE69DbUyCjIyVg2DYCje2vIc8XnTWYDFEkjQ==
  1721 +"@windicss/plugin-utils@0.5.3":
  1722 + version "0.5.3"
  1723 + resolved "https://registry.npmjs.org/@windicss/plugin-utils/-/plugin-utils-0.5.3.tgz#5302864cb2f94dd3d71bb5af958dab6ce3f74dab"
  1724 + integrity sha512-BXNvMSRmi1TyM8j3EnF/li9HViJPN4EPeGKTiIDWemxrfkRNlgN+Gmv31CFtZwnjiAlXRO58B6LIqVTSVYaTfQ==
1725 1725 dependencies:
1726   - esbuild "^0.8.50"
  1726 + esbuild "^0.8.52"
1727 1727 esbuild-register "^2.0.0"
1728 1728 fast-glob "^3.2.5"
1729 1729 micromatch "^4.0.2"
... ... @@ -3616,6 +3616,11 @@ esbuild@^0.8.50:
3616 3616 resolved "https://registry.npmjs.org/esbuild/-/esbuild-0.8.50.tgz#ebf24fde0cdad1a369789dd6fd7a820b0a01e46c"
3617 3617 integrity sha512-oidFLXssA7IccYzkqLVZSqNJDwDq8Mh/vqvrW+3fPWM7iUiC5O2bCllhnO8+K9LlyL/2Z6n+WwRJAz9fqSIVRg==
3618 3618  
  3619 +esbuild@^0.8.52:
  3620 + version "0.8.52"
  3621 + resolved "https://registry.npmjs.org/esbuild/-/esbuild-0.8.52.tgz#6dabf11c517af449a96d66da20dfc204ee7b5294"
  3622 + integrity sha512-b5KzFweLLXoXQwdC/e2+Z80c8uo2M5MgP7yQEEebkFw6In4T9CvYcNoM2ElvJt8ByO04zAZUV0fZkXmXoi2s9A==
  3623 +
3619 3624 escalade@^3.1.1:
3620 3625 version "3.1.1"
3621 3626 resolved "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
... ... @@ -8935,10 +8940,10 @@ vite-plugin-purge-icons@^0.7.0:
8935 8940 "@purge-icons/generated" "^0.7.0"
8936 8941 rollup-plugin-purge-icons "^0.7.0"
8937 8942  
8938   -vite-plugin-pwa@^0.5.4:
8939   - version "0.5.4"
8940   - resolved "https://registry.npmjs.org/vite-plugin-pwa/-/vite-plugin-pwa-0.5.4.tgz#ce6fb85da140359057290e5eba3c22548392bea5"
8941   - integrity sha512-Zcr190GixdvvHBS1poTevtuw0irRvRi9rLFdXUbkPyY5hozQ+JhR8i/ORRvl6a9wV6Gl/mVwJ3IaY5IjTf3zFw==
  8943 +vite-plugin-pwa@^0.5.5:
  8944 + version "0.5.5"
  8945 + resolved "https://registry.npmjs.org/vite-plugin-pwa/-/vite-plugin-pwa-0.5.5.tgz#f6bcaf6f6f1af0882fff7a9334aec685b798cceb"
  8946 + integrity sha512-gwPg+pDm87iMOLORz/fOZiNNWNXhHFrMPW34XpX3F9JLl6ytcNZ6cJMYJ1FRKQPtVADqkbZjk3g3AOi1oI6HKQ==
8942 8947 dependencies:
8943 8948 debug "^4.3.2"
8944 8949 fast-glob "^3.2.5"
... ... @@ -8968,12 +8973,12 @@ vite-plugin-theme@^0.4.8:
8968 8973 es-module-lexer "^0.3.26"
8969 8974 tinycolor2 "^1.4.2"
8970 8975  
8971   -vite-plugin-windicss@0.5.0:
8972   - version "0.5.0"
8973   - resolved "https://registry.npmjs.org/vite-plugin-windicss/-/vite-plugin-windicss-0.5.0.tgz#63189c772feab50b3860ddec4c8a0ac69555f9d2"
8974   - integrity sha512-8mT03wppL3/ehyNEIJHVpMlWsS8jlSBaW/zSUuGjFiUC0zhB0GSzTbQkvtpJLqlFvvU4oHF4uhvN9tb7Uoq1ww==
  8976 +vite-plugin-windicss@0.5.3:
  8977 + version "0.5.3"
  8978 + resolved "https://registry.npmjs.org/vite-plugin-windicss/-/vite-plugin-windicss-0.5.3.tgz#7929b5fbaed7f0530cbe79947e7bc9e4f471b683"
  8979 + integrity sha512-hVfj0QjgxBch4j5M6BfMj9Dsm7iKioP9uQUvs00RGkuUdG+Gc0PQcT9SuLrcZJjUE/2Tp9+J44BEbOYxjesqlA==
8975 8980 dependencies:
8976   - "@windicss/plugin-utils" "0.5.0"
  8981 + "@windicss/plugin-utils" "0.5.3"
8977 8982 windicss "^2.2.0"
8978 8983  
8979 8984 vite@2.0.3:
... ...