From 9057e04efad1b7d61c77a72e5c37a504d0aee935 Mon Sep 17 00:00:00 2001 From: doum <doum> Date: 星期五, 26 九月 2025 09:24:03 +0800 Subject: [PATCH] H5静态化 --- admin/src/plugins/cache.js | 144 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 144 insertions(+), 0 deletions(-) diff --git a/admin/src/plugins/cache.js b/admin/src/plugins/cache.js new file mode 100644 index 0000000..64a8812 --- /dev/null +++ b/admin/src/plugins/cache.js @@ -0,0 +1,144 @@ +import md5 from 'js-md5' +const buildCacheImpl = impl => { + return { + __check () { + if (!impl) { + throw new Error('missing cache impl') + } + }, + __get (key) { + this.__check(key) + if (key == null) { + return null + } + const value = impl.getItem(key) + if (value == null) { + return null + } + try { + const valueObj = JSON.parse(value) + // 宸茶繃鏈熷鐞� + if (valueObj.expiredTime != null && new Date().getTime() > valueObj.expiredTime) { + this.remove(key) + return null + } + return valueObj + } catch (e) { + return { value } + } + }, + /** + * 鍐欏叆缂撳瓨 + * + * @param key 閿� + * @param value 鍊� + * @param timeout 瓒呮椂鏃堕棿锛屽崟浣嶆绉掞紝-1琛ㄧず涓嶈秴鏃� + */ + set (key, value, timeout = -1) { + if (key == null) { + return + } + if (value == null) { + return + } + let type = typeof value + if (value instanceof Date) { + type = 'date' + } + let actualValue = value + if (type === 'object') { + actualValue = JSON.stringify(value) + } + if (type === 'date') { + actualValue = value.getTime() + } + const birthtime = new Date().getTime() + const valueObj = { + type, + value: actualValue, + birthtime, + expiredTime: timeout === -1 ? null : birthtime + timeout + } + impl.setItem(key, JSON.stringify(valueObj)) + }, + /** + * 鑾峰彇缂撳瓨鍊� + * + * @param key 閿� + * @returns {SVGPoint | SVGNumber | string | SVGTransform | SVGLength | SVGPathSeg | T|*|any|{value}|null|any} + */ + get (key) { + const valueObj = this.__get(key) + if (valueObj == null) { + return null + } + if (typeof valueObj !== 'object') { + return valueObj + } + if (valueObj.value == null) { + return null + } + if (valueObj.type === 'date') { + return new Date(valueObj.value) + } + if (valueObj.type === 'object') { + return JSON.parse(valueObj.value) + } + return valueObj.value + }, + /** + * 鍒犻櫎缂撳瓨鍊� + * + * @param key 閿� + */ + remove (key) { + impl.removeItem(key) + } + } +} + +export default { + /** + * 榛樿浣跨敤localStorage鏉ヨ褰曠紦瀛� + */ + ...buildCacheImpl(window.localStorage), + /** + * 浼氳瘽绾х紦瀛� + */ + session: buildCacheImpl(window.sessionStorage), + /** + * 鏈湴缂撳瓨 + */ + local: buildCacheImpl(window.localStorage), + /** + * 2FA缂撳瓨 + */ + twoFA: { + ...buildCacheImpl(window.sessionStorage), + /** + * 璁剧疆璁よ瘉瀵嗙爜 + * + * @param value 瀵嗙爜 + * @param rememberPwd 鏄惁璁颁綇瀵嗙爜 + */ + setPassword (value, rememberPwd = false) { + value = md5(value) + // 璁颁綇瀵嗙爜鏃讹紝榛樿璁颁綇5鍒嗛挓 + const timeout = 5 * 60 * 1000 + this.set('eva-2fa-password', value, rememberPwd ? timeout : -1) + }, + /** + * 鑾峰彇璁よ瘉瀵嗙爜 + * @returns {*} + */ + getPassword () { + return this.get('eva-2fa-password') + }, + /** + * 鍒犻櫎璁よ瘉瀵嗙爜 + */ + removePassword () { + this.remove('eva-2fa-password') + } + } +} -- Gitblit v1.9.3