当前位置:   article > 正文

localStorage方法封装_localstorage封装

localstorage封装

import { validatenull } from '@/util/validate'   是引入的下面这个正则

https://blog.csdn.net/Yangpengxunb/article/details/129186733?spm=1001.2014.3001.5501

import website from '@/const/website'    是配置主键,用于存储 

  1. import { validatenull } from '@/util/validate'
  2. import website from '@/const/website'
  3. const keyName = website.key + '-'
  4. /**
  5. * 存储localStorage
  6. */
  7. export const setStore = (params = {}) => {
  8. let {
  9. name,
  10. content,
  11. type
  12. } = params
  13. name = keyName + name
  14. const obj = {
  15. dataType: typeof (content),
  16. content: content,
  17. type: type,
  18. datetime: new Date().getTime()
  19. }
  20. if (type) window.sessionStorage.setItem(name, JSON.stringify(obj))
  21. else window.localStorage.setItem(name, JSON.stringify(obj))
  22. }
  23. /**
  24. * 获取localStorage
  25. */
  26. export const getStore = (params = {}) => {
  27. let {
  28. name,
  29. debug
  30. } = params
  31. name = keyName + name
  32. let obj = {}
  33. let content
  34. obj = window.sessionStorage.getItem(name)
  35. if (validatenull(obj)) obj = window.localStorage.getItem(name)
  36. if (validatenull(obj)) return
  37. try {
  38. obj = JSON.parse(obj)
  39. } catch (e) {
  40. return obj
  41. }
  42. if (debug) {
  43. return obj
  44. }
  45. if (obj.dataType === 'string') {
  46. content = obj.content
  47. } else if (obj.dataType === 'number') {
  48. content = Number(obj.content)
  49. } else if (obj.dataType === 'boolean') {
  50. content = eval(obj.content)
  51. } else if (obj.dataType === 'object') {
  52. content = obj.content
  53. }
  54. return content
  55. }
  56. /**
  57. * 删除localStorage
  58. */
  59. export const removeStore = (params = {}) => {
  60. let {
  61. name,
  62. type
  63. } = params
  64. name = keyName + name
  65. if (type) {
  66. window.sessionStorage.removeItem(name)
  67. } else {
  68. window.localStorage.removeItem(name)
  69. }
  70. }
  71. /**
  72. * 获取全部localStorage
  73. */
  74. export const getAllStore = (params = {}) => {
  75. const list = []
  76. const {
  77. type
  78. } = params
  79. if (type) {
  80. for (let i = 0; i <= window.sessionStorage.length; i++) {
  81. list.push({
  82. name: window.sessionStorage.key(i),
  83. content: getStore({
  84. name: window.sessionStorage.key(i),
  85. type: 'session'
  86. })
  87. })
  88. }
  89. } else {
  90. for (let i = 0; i <= window.localStorage.length; i++) {
  91. list.push({
  92. name: window.localStorage.key(i),
  93. content: getStore({
  94. name: window.localStorage.key(i)
  95. })
  96. })
  97. }
  98. }
  99. return list
  100. }
  101. /**
  102. * 清空全部localStorage
  103. */
  104. export const clearStore = (params = {}) => {
  105. const { type } = params
  106. if (type) {
  107. window.sessionStorage.clear()
  108. } else {
  109. window.localStorage.clear()
  110. }
  111. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/482284
推荐阅读
相关标签
  

闽ICP备14008679号