From fa4c36d0848b047b853dcea33f1b995a8b9067da Mon Sep 17 00:00:00 2001
From: doum <doum>
Date: 星期五, 19 九月 2025 17:47:42 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'
---
admin/src/plugins/cache.js | 191 +++++++++++++++++++++++++++++++++--------------
1 files changed, 132 insertions(+), 59 deletions(-)
diff --git a/admin/src/plugins/cache.js b/admin/src/plugins/cache.js
index 82d800e..64a8812 100644
--- a/admin/src/plugins/cache.js
+++ b/admin/src/plugins/cache.js
@@ -1,71 +1,144 @@
-const sessionCache = {
- set (key, value) {
- if (!sessionStorage) {
- return
- }
- if (key != null && value != null) {
- sessionStorage.setItem(key, value)
- }
- },
- get (key) {
- if (!sessionStorage) {
- return null
- }
- if (key == null) {
- return null
- }
- return sessionStorage.getItem(key)
- },
- setJSON (key, jsonValue) {
- if (jsonValue != null) {
- this.set(key, JSON.stringify(jsonValue))
- }
- },
- getJSON (key) {
- const value = this.get(key)
- if (value != null) {
- return JSON.parse(value)
- }
- }
-}
-const localCache = {
- set (key, value) {
- if (!localStorage) {
- return
- }
- if (key != null && value != null) {
- localStorage.setItem(key, value)
- }
- },
- get (key) {
- if (!localStorage) {
- return null
- }
- if (key == null) {
- return null
- }
- return localStorage.getItem(key)
- },
- setJSON (key, jsonValue) {
- if (jsonValue != null) {
- this.set(key, JSON.stringify(jsonValue))
- }
- },
- getJSON (key) {
- const value = this.get(key)
- if (value != null) {
- return JSON.parse(value)
+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: sessionCache,
+ session: buildCacheImpl(window.sessionStorage),
/**
* 鏈湴缂撳瓨
*/
- local: localCache
+ 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