doum
2025-09-26 9057e04efad1b7d61c77a72e5c37a504d0aee935
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')
    }
  }
}