Showing
3 changed files
with
162 additions
and
156 deletions
1 | -// const { encrypt, decrypt } = CryptoJS.AES | ||
2 | -// const parse = CryptoJS.enc.Utf8; | ||
3 | -// const pkcs7 = CryptoJS.pad.Pkcs7; | ||
4 | -// const ECB = CryptoJS.mode.ECB; | ||
5 | -// const UTF8 = CryptoJS.enc.Utf8; | ||
6 | -// | ||
7 | -// const cacheCipher = { | ||
8 | -// key: '_11111000001111@', | ||
9 | -// iv: '@11111000001111_', | ||
10 | -// } | ||
11 | -// | ||
12 | -// const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7; | ||
13 | -// | ||
14 | -// const isNullOrUnDef = (val) => typeof val === 'undefined' || val === null | ||
15 | -// | ||
16 | -// class AesEncryption { | ||
17 | -// key; | ||
18 | -// iv; | ||
19 | -// | ||
20 | -// constructor(opt = {}) { | ||
21 | -// const { key, iv } = opt; | ||
22 | -// if (key) { | ||
23 | -// this.key = parse(key); | ||
24 | -// } | ||
25 | -// if (iv) { | ||
26 | -// this.iv = parse(iv); | ||
27 | -// } | ||
28 | -// } | ||
29 | -// | ||
30 | -// get getOptions() { | ||
31 | -// return { | ||
32 | -// mode: ECB, | ||
33 | -// padding: pkcs7, | ||
34 | -// iv: this.iv, | ||
35 | -// }; | ||
36 | -// } | ||
37 | -// | ||
38 | -// encryptByAES(cipherText) { | ||
39 | -// return encrypt(cipherText, this.key, this.getOptions).toString(); | ||
40 | -// } | ||
41 | -// | ||
42 | -// decryptByAES(cipherText) { | ||
43 | -// return decrypt(cipherText, this.key, this.getOptions).toString(UTF8); | ||
44 | -// } | ||
45 | -// } | ||
46 | -// | ||
47 | -// | ||
48 | -// const createStorage = ( | ||
49 | -// { | ||
50 | -// prefixKey = '', | ||
51 | -// storage = sessionStorage, | ||
52 | -// key = cacheCipher.key, | ||
53 | -// iv = cacheCipher.iv, | ||
54 | -// timeout = null, | ||
55 | -// hasEncrypt = true, | ||
56 | -// } = {}) => { | ||
57 | -// if (hasEncrypt && [ key.length, iv.length ].some((item) => item !== 16)) { | ||
58 | -// throw new Error('When hasEncrypt is true, the key or iv must be 16 bits!'); | ||
59 | -// } | ||
60 | -// | ||
61 | -// const encryption = new AesEncryption({ key, iv }); | ||
62 | -// | ||
63 | -// /** | ||
64 | -// *Cache class | ||
65 | -// *Construction parameters can be passed into sessionStorage, localStorage, | ||
66 | -// * @class Cache | ||
67 | -// * @example | ||
68 | -// */ | ||
69 | -// const WebStorage = class WebStorage { | ||
70 | -// storage | ||
71 | -// prefixKey | ||
72 | -// encryption | ||
73 | -// hasEncrypt | ||
74 | -// | ||
75 | -// /** | ||
76 | -// * | ||
77 | -// * @param {*} storage | ||
78 | -// */ | ||
79 | -// constructor() { | ||
80 | -// this.storage = storage; | ||
81 | -// this.prefixKey = prefixKey; | ||
82 | -// this.encryption = encryption; | ||
83 | -// this.hasEncrypt = hasEncrypt; | ||
84 | -// } | ||
85 | -// | ||
86 | -// getKey(key) { | ||
87 | -// return `${ this.prefixKey }${ key }`.toUpperCase(); | ||
88 | -// } | ||
89 | -// | ||
90 | -// /** | ||
91 | -// * | ||
92 | -// * Set cache | ||
93 | -// * @param {string} key | ||
94 | -// * @param {*} value | ||
95 | -// * @param {*} expire | ||
96 | -// * @expire Expiration time in seconds | ||
97 | -// * @memberof Cache | ||
98 | -// */ | ||
99 | -// set(key, value, expire = timeout) { | ||
100 | -// const stringData = JSON.stringify({ | ||
101 | -// value, | ||
102 | -// time: Date.now(), | ||
103 | -// expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null, | ||
104 | -// }); | ||
105 | -// const stringifyValue = this.hasEncrypt | ||
106 | -// ? this.encryption.encryptByAES(stringData) | ||
107 | -// : stringData; | ||
108 | -// this.storage.setItem(this.getKey(key), stringifyValue); | ||
109 | -// } | ||
110 | -// | ||
111 | -// /** | ||
112 | -// *Read cache | ||
113 | -// * @param {string} key | ||
114 | -// * @param {any} [def=null] def | ||
115 | -// * @memberof Cache | ||
116 | -// */ | ||
117 | -// get(key, def = null) { | ||
118 | -// const val = this.storage.getItem(this.getKey(key)); | ||
119 | -// if (!val) return def; | ||
120 | -// | ||
121 | -// try { | ||
122 | -// const decVal = this.hasEncrypt ? this.encryption.decryptByAES(val) : val; | ||
123 | -// const data = JSON.parse(decVal); | ||
124 | -// const { value, expire } = data; | ||
125 | -// if (isNullOrUnDef(expire) || expire >= new Date().getTime()) { | ||
126 | -// return value; | ||
127 | -// } | ||
128 | -// this.remove(key); | ||
129 | -// } catch (e) { | ||
130 | -// return def; | ||
131 | -// } | ||
132 | -// } | ||
133 | -// | ||
134 | -// /** | ||
135 | -// * Delete cache based on key | ||
136 | -// * @param {string} key | ||
137 | -// * @memberof Cache | ||
138 | -// */ | ||
139 | -// remove(key) { | ||
140 | -// this.storage.removeItem(this.getKey(key)); | ||
141 | -// } | ||
142 | -// | ||
143 | -// /** | ||
144 | -// * Delete all caches of this instance | ||
145 | -// */ | ||
146 | -// clear() { | ||
147 | -// this.storage.clear(); | ||
148 | -// } | ||
149 | -// }; | ||
150 | -// return new WebStorage(); | ||
151 | -// } | ||
152 | - | ||
153 | const GLOBAL_STORAGE_KEY = (() => { | 1 | const GLOBAL_STORAGE_KEY = (() => { |
154 | - const isDev = location.href.includes('dev=1') | ||
155 | const DEVELOPMENT = 'DEVELOPMENT' | 2 | const DEVELOPMENT = 'DEVELOPMENT' |
156 | const PRODUCTION = 'PRODUCTION' | 3 | const PRODUCTION = 'PRODUCTION' |
157 | - return `UNDEFINED__${ isDev ? DEVELOPMENT : PRODUCTION }__2.7.1__COMMON__LOCAL__KEY__` | 4 | + return `UNDEFINED__${ isDEV ? DEVELOPMENT : PRODUCTION }__2.7.1__COMMON__LOCAL__KEY__` |
158 | })() | 5 | })() |
159 | 6 | ||
160 | const GLOBAL_TOKEN = (() => { | 7 | const GLOBAL_TOKEN = (() => { |
161 | - return JSON.parse(localStorage.getItem(GLOBAL_STORAGE_KEY)).value.JWT_TOKEN.value | 8 | + const ls = createStorage({ storage: localStorage }) |
9 | + /** | ||
10 | + * @description user info | ||
11 | + * @type {{JWT_TOKEN: {value: string}}} | ||
12 | + */ | ||
13 | + const common = ls.get(GLOBAL_STORAGE_KEY) | ||
14 | + return common.JWT_TOKEN.value | ||
162 | })() | 15 | })() |
163 | 16 | ||
164 | const GLOBAL_WS_URL = (() => { | 17 | const GLOBAL_WS_URL = (() => { |
1 | +const { encrypt, decrypt } = CryptoJS.AES | ||
2 | +const { parse } = CryptoJS.enc.Utf8; | ||
3 | +const pkcs7 = CryptoJS.pad.Pkcs7; | ||
4 | +const ECB = CryptoJS.mode.ECB; | ||
5 | +const UTF8 = CryptoJS.enc.Utf8; | ||
6 | + | ||
7 | +const isDEV = location.href.includes('dev=1') | ||
8 | + | ||
9 | +const cacheCipher = { | ||
10 | + key: '_11111000001111@', | ||
11 | + iv: '@11111000001111_', | ||
12 | +} | ||
13 | + | ||
14 | +const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7; | ||
15 | + | ||
16 | +const isNullOrUnDef = (val) => typeof val === 'undefined' || val === null | ||
17 | + | ||
18 | +class AesEncryption { | ||
19 | + key; | ||
20 | + iv; | ||
21 | + | ||
22 | + constructor(opt = {}) { | ||
23 | + const { key, iv } = opt; | ||
24 | + if (key) { | ||
25 | + this.key = parse(key); | ||
26 | + } | ||
27 | + if (iv) { | ||
28 | + this.iv = parse(iv); | ||
29 | + } | ||
30 | + } | ||
31 | + | ||
32 | + get getOptions() { | ||
33 | + return { | ||
34 | + mode: ECB, | ||
35 | + padding: pkcs7, | ||
36 | + iv: this.iv, | ||
37 | + }; | ||
38 | + } | ||
39 | + | ||
40 | + encryptByAES(cipherText) { | ||
41 | + return encrypt(cipherText, this.key, this.getOptions).toString(); | ||
42 | + } | ||
43 | + | ||
44 | + decryptByAES(cipherText) { | ||
45 | + return decrypt(cipherText, this.key, this.getOptions).toString(UTF8); | ||
46 | + } | ||
47 | +} | ||
48 | + | ||
49 | + | ||
50 | +const createStorage = ( | ||
51 | + { | ||
52 | + prefixKey = '', | ||
53 | + storage = sessionStorage, | ||
54 | + key = cacheCipher.key, | ||
55 | + iv = cacheCipher.iv, | ||
56 | + timeout = null, | ||
57 | + hasEncrypt = !isDEV, | ||
58 | + } = {}) => { | ||
59 | + if (hasEncrypt && [ key.length, iv.length ].some((item) => item !== 16)) { | ||
60 | + throw new Error('When hasEncrypt is true, the key or iv must be 16 bits!'); | ||
61 | + } | ||
62 | + | ||
63 | + const encryption = new AesEncryption({ key, iv }); | ||
64 | + | ||
65 | + /** | ||
66 | + *Cache class | ||
67 | + *Construction parameters can be passed into sessionStorage, localStorage, | ||
68 | + * @class Cache | ||
69 | + * @example | ||
70 | + */ | ||
71 | + const WebStorage = class WebStorage { | ||
72 | + storage | ||
73 | + prefixKey | ||
74 | + encryption | ||
75 | + hasEncrypt | ||
76 | + | ||
77 | + /** | ||
78 | + * | ||
79 | + * @param {*} storage | ||
80 | + */ | ||
81 | + constructor() { | ||
82 | + this.storage = storage; | ||
83 | + this.prefixKey = prefixKey; | ||
84 | + this.encryption = encryption; | ||
85 | + this.hasEncrypt = hasEncrypt; | ||
86 | + } | ||
87 | + | ||
88 | + getKey(key) { | ||
89 | + return `${ this.prefixKey }${ key }`.toUpperCase(); | ||
90 | + } | ||
91 | + | ||
92 | + /** | ||
93 | + * | ||
94 | + * Set cache | ||
95 | + * @param {string} key | ||
96 | + * @param {*} value | ||
97 | + * @param {*} expire | ||
98 | + * @expire Expiration time in seconds | ||
99 | + * @memberof Cache | ||
100 | + */ | ||
101 | + set(key, value, expire = timeout) { | ||
102 | + const stringData = JSON.stringify({ | ||
103 | + value, | ||
104 | + time: Date.now(), | ||
105 | + expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null, | ||
106 | + }); | ||
107 | + const stringifyValue = this.hasEncrypt | ||
108 | + ? this.encryption.encryptByAES(stringData) | ||
109 | + : stringData; | ||
110 | + this.storage.setItem(this.getKey(key), stringifyValue); | ||
111 | + } | ||
112 | + | ||
113 | + /** | ||
114 | + *Read cache | ||
115 | + * @param {string} key | ||
116 | + * @param {any} [def=null] def | ||
117 | + * @memberof Cache | ||
118 | + */ | ||
119 | + get(key, def = null) { | ||
120 | + const val = this.storage.getItem(this.getKey(key)); | ||
121 | + if (!val) return def; | ||
122 | + | ||
123 | + try { | ||
124 | + const decVal = this.hasEncrypt ? this.encryption.decryptByAES(val) : val; | ||
125 | + const data = JSON.parse(decVal); | ||
126 | + const { value, expire } = data; | ||
127 | + if (isNullOrUnDef(expire) || expire >= new Date().getTime()) { | ||
128 | + return value; | ||
129 | + } | ||
130 | + this.remove(key); | ||
131 | + } catch (e) { | ||
132 | + return def; | ||
133 | + } | ||
134 | + } | ||
135 | + | ||
136 | + /** | ||
137 | + * Delete cache based on key | ||
138 | + * @param {string} key | ||
139 | + * @memberof Cache | ||
140 | + */ | ||
141 | + remove(key) { | ||
142 | + this.storage.removeItem(this.getKey(key)); | ||
143 | + } | ||
144 | + | ||
145 | + /** | ||
146 | + * Delete all caches of this instance | ||
147 | + */ | ||
148 | + clear() { | ||
149 | + this.storage.clear(); | ||
150 | + } | ||
151 | + }; | ||
152 | + return new WebStorage(); | ||
153 | +} |
@@ -597,7 +597,7 @@ App.main = function(callback, createUi) | @@ -597,7 +597,7 @@ App.main = function(callback, createUi) | ||
597 | defHttp = createAxios({ | 597 | defHttp = createAxios({ |
598 | timeout:10*1000, | 598 | timeout:10*1000, |
599 | baseURL:'/api', | 599 | baseURL:'/api', |
600 | - token: JSON.parse(localStorageInfo).value.JWT_TOKEN.value | 600 | + token: GLOBAL_TOKEN |
601 | }) | 601 | }) |
602 | // Logs uncaught errors | 602 | // Logs uncaught errors |
603 | window.onerror = function(message, url, linenumber, colno, err) | 603 | window.onerror = function(message, url, linenumber, colno, err) |