目的

  1. 简化并统一使用操作
  2. 增加功能(装饰器)
  3. 增加 值 类型标记
    1. 存取数据自动转化类
  1. const Utils = function () { }
  2. // toString 方法
  3. Utils.prototype._toString = Object.prototype.toString
  4. Utils.prototype.class2type = {
  5. '[object Boolean]': 'boolean',
  6. '[object Number]': 'number',
  7. '[object String]': 'string',
  8. '[object Function]': 'function',
  9. '[object Array]': 'array',
  10. '[object Date]': 'date',
  11. '[object RegExp]': 'regExp',
  12. '[object Object]': 'object',
  13. }
  14. // 类型检测, 返回对象类型字符串
  15. Utils.prototype.type = function (obj) {
  16. const type = this._toString.call(obj)
  17. return obj === null ? String(obj) : this.class2type[type] || 'object'
  18. }
  19. // 实例化 Utils
  20. const myUtil = new Utils()
  21. function SessionStorage() { }
  22. // 定义数据类型标识, 存储值前缀
  23. SessionStorage.Types = {
  24. stringType: '00',
  25. numberType: '01',
  26. booleanType: '02',
  27. jsonType: '03',
  28. undefinedType: '04',
  29. }
  30. // 系统配置
  31. // _$sys_session_readOnly: ['_$sys_session_01', '_$sys_session_02']
  32. SessionStorage.SystemReadOnly = '_$sys_session_readOnly'
  33. // 判断是否是系统key
  34. SessionStorage.prototype.isSystemKey = function (key) {
  35. return key && key.indexOf('_$sys_session_') === 0
  36. }
  37. // 设置值
  38. SessionStorage.prototype.put = function put(key, value, readOnly) {
  39. // 1、获取 key 类型
  40. const keyType = myUtil.type(key)
  41. if (keyType !== 'string') {
  42. console.error('the type of key is not string')
  43. return
  44. }
  45. // 2、判断 key 是否和系统保留值冲突
  46. if (this.isSystemKey(key)) {
  47. console.error('the key name is illegal');
  48. return
  49. }
  50. // 3、获取只读 key 配置
  51. let readOnlyKeys = window.sessionStorage.getItem(SessionStorage.SystemReadOnly)
  52. if (!readOnlyKeys) {
  53. readOnlyKeys = []
  54. } else {
  55. readOnlyKeys = JSON.parse(readOnlyKeys)
  56. }
  57. // 4、判断 key 是否为只读
  58. if (readOnlyKeys.indexOf(key) !== -1) {
  59. console.error('the key is read only');
  60. return
  61. }
  62. // 5、定义参数
  63. let param
  64. // 获取 value 类型
  65. const valueType = myUtil.type(value)
  66. switch (valueType) {
  67. case 'object':
  68. const s = JSON.stringify(value)
  69. param = SessionStorage.Types.jsonType + s
  70. break;
  71. case 'string':
  72. param = SessionStorage.Types.stringType + value
  73. break;
  74. case 'number':
  75. param = SessionStorage.Types.numberType + value
  76. break;
  77. case 'boolean':
  78. param = SessionStorage.Types.booleanType + value
  79. break;
  80. default:
  81. console.error('the type of value is not supported');
  82. break;
  83. }
  84. if (!param) {
  85. return
  86. }
  87. // 保存只读 key 集合
  88. if (readOnly && readOnly === true) {
  89. readOnlyKeys.push(key)
  90. readOnlyKeys = JSON.stringify(readOnlyKeys)
  91. window.sessionStorage.setItem(SessionStorage.SystemReadOnly, readOnlyKeys)
  92. }
  93. // 设置值
  94. window.sessionStorage.setItem(key, param)
  95. }
  96. // 获取值
  97. SessionStorage.prototype.get = function get(key) {
  98. // 获取 key 类型
  99. const keyType = typeof key
  100. if (keyType !== 'string') {
  101. console.error('the type of key is not string');
  102. return undefined
  103. }
  104. // 获取 value
  105. const param = window.sessionStorage.getItem(key)
  106. if (param === null || param === undefined) {
  107. return param
  108. }
  109. // 获取 value 类型
  110. const valueType = param.substring(0, 2)
  111. // 获取 value
  112. let value = param.substring(2, param.length)
  113. switch (valueType) {
  114. case SessionStorage.Types.jsonType:
  115. value = JSON.parse(value)
  116. break;
  117. case SessionStorage.Types.numberType:
  118. value = Number(value)
  119. break;
  120. case SessionStorage.Types.booleanType:
  121. value = Boolean(value)
  122. break;
  123. default:
  124. break;
  125. }
  126. return value
  127. }
  128. // 移除项
  129. SessionStorage.prototype.remove = function remove(key) {
  130. // 获取 key 类型
  131. const keyType = typeof key
  132. if (keyType !== 'string') {
  133. console.error('the type of key is not string');
  134. return undefined
  135. }
  136. // 判断 key 值是否和系统保留值冲突
  137. if (this.isSystemKey(key)) {
  138. console.error('the key name is illegal');
  139. return
  140. }
  141. // 获取只读 key 配置
  142. let readOnlyKeys = window.sessionStorage.getItem(SessionStorage.SystemReadOnly)
  143. if (!readOnlyKeys) {
  144. readOnlyKeys = []
  145. } else {
  146. readOnlyKeys = JSON.parse(readOnlyKeys)
  147. }
  148. // 判断 key 是否为只读
  149. if (readOnlyKeys.indexOf(key) !== -1) {
  150. console.error('the key is read only');
  151. return
  152. }
  153. window.sessionStorage.removeItem(key)
  154. }
  155. // 清空所有项
  156. SessionStorage.prototype.clear = function clear() {
  157. // 获取只读 key 配置
  158. let readOnlyKeys = window.sessionStorage.getItem(SessionStorage.SystemReadOnly)
  159. if (!readOnlyKeys) {
  160. readOnlyKeys = []
  161. } else {
  162. readOnlyKeys = JSON.parse(readOnlyKeys)
  163. }
  164. // 临时缓存
  165. const tmpCache = {}
  166. // 保留 read only key
  167. for (let i = 0; i < readOnlyKeys.length; i++) {
  168. const key = readOnlyKeys[i];
  169. const value = window.sessionStorage.getItem(key)
  170. tmpCache[key] = value
  171. }
  172. // 清空
  173. window.sessionStorage.clear()
  174. if (readOnlyKeys) {
  175. // 还原 read only key
  176. for (let i = 0; i < readOnlyKeys.length; i++) {
  177. const key = readOnlyKeys[i];
  178. const value = tmpCache[key]
  179. window.sessionStorage.setItem(key, value)
  180. }
  181. // 保存 readOnlyKeys
  182. readOnlyKeys = JSON.stringify(readOnlyKeys)
  183. window.sessionStorage.setItem(SessionStorage.SystemReadOnly, readOnlyKeys)
  184. }
  185. }
  186. // 定义 session storage
  187. const sessionStorage = new SessionStorage()
  188. export default sessionStorage