bug
jiangping
2023-12-06 1f4e7d0f73a73e7350cf5a1df279d5f30904c5d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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')
    }
  }
}