storage.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import Cookies from 'js-cookie';
  2. const cookieKeys = ['admin_uuid', 'admin_uid'];
  3. /**
  4. * 判断是否需要储存为Cookie
  5. * @param key
  6. * @returns {boolean}
  7. */
  8. function isCookieKey(key) {
  9. return cookieKeys.indexOf(key) !== -1;
  10. }
  11. /**
  12. * SetItem
  13. * @param key
  14. * @param value
  15. * @param options
  16. */
  17. function setItem(key, value, options = {}) {
  18. console.log(`Setting item: ${key}, value: ${value}, options:`, options); // 添加日志
  19. if (options.expires === undefined) {
  20. options.expires = 365 * 10;
  21. }
  22. return isCookieKey(key)
  23. ? setCookieItem(key, value, options)
  24. : setLocalItem(key, value, options);
  25. }
  26. /**
  27. * GetItem
  28. * @param key
  29. * @returns {*|string}
  30. */
  31. function getItem(key) {
  32. return isCookieKey(key)
  33. ? getCookieItem(key)
  34. : getLocalItem(key);
  35. }
  36. /**
  37. * RemoveItem
  38. * @param key
  39. * @param options
  40. */
  41. function removeItem(key, options) {
  42. return isCookieKey(key)
  43. ? removeCookieItem(key, options)
  44. : removeLocalItem(key);
  45. }
  46. // ========== LocalStorage ========== //
  47. /**
  48. * 设置LocalStorageItem
  49. * @param key
  50. * @param value
  51. * @param options
  52. */
  53. function setLocalItem(key, value, options = {}) {
  54. let { expires } = options;
  55. if (typeof expires === 'number') {
  56. expires = new Date().getTime() + expires * 86400 * 1000;
  57. } else if (typeof expires === 'object') {
  58. expires = expires.getTime();
  59. }
  60. value = JSON.stringify({
  61. expires,
  62. data: value
  63. });
  64. localStorage.setItem(key, value);
  65. }
  66. /**
  67. * 获取LocalStorageItem
  68. * @param key
  69. * @returns {string}
  70. */
  71. function getLocalItem(key) {
  72. let object = localStorage.getItem(key);
  73. if (!object) return '';
  74. object = JSON.parse(object);
  75. const { expires } = object;
  76. if (typeof expires === 'number' && expires <= Date.now()) {
  77. localStorage.removeItem(key);
  78. return '';
  79. }
  80. return object.data;
  81. }
  82. /**
  83. * 移除LocalStorageItem
  84. * @param key
  85. */
  86. function removeLocalItem(key) {
  87. localStorage.removeItem(key);
  88. }
  89. // ========== Cookie ========== //
  90. /**
  91. * 获取根域名
  92. * @returns {string}
  93. */
  94. function getRootDomain() {
  95. const parts = document.domain.split('.').reverse();
  96. if (parts.length >= 2) {
  97. return parts[1] + '.' + parts[0];
  98. }
  99. return document.domain;
  100. }
  101. /**
  102. * 设置CookieItem
  103. * @param key
  104. * @param value
  105. * @param options
  106. */
  107. function setCookieItem(key, value, options = {}) {
  108. const domain = getRootDomain();
  109. options = { domain, ...options };
  110. Cookies.set(key, value, options);
  111. }
  112. /**
  113. * 获取CookieItem
  114. * @param key
  115. * @returns {*}
  116. */
  117. function getCookieItem(key) {
  118. return Cookies.get(key);
  119. }
  120. /**
  121. * 移除CookieItem
  122. * @param key
  123. * @param options
  124. */
  125. function removeCookieItem(key, options = {}) {
  126. const domain = getRootDomain();
  127. options = { domain, ...options };
  128. Cookies.remove(key, options);
  129. }
  130. export default {
  131. setItem,
  132. getItem,
  133. removeItem
  134. };