auth.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * 设置带过期时间的数据
  3. * @param {string} key - 存储的键
  4. * @param {array} value - 存储的值
  5. * @param {number} expiryDays - 过期天数
  6. * @returns {void}
  7. */
  8. function setItemWithExpiry(key, value, expiryDays) {
  9. const _val = typeof value === 'object' ? JSON.stringify(value) : value;
  10. const now = new Date();
  11. // 计算过期时间的时间戳(毫秒)
  12. const expiryTime = now.getTime() + expiryDays * 24 * 60 * 60 * 1000;
  13. const item = {
  14. value: _val,
  15. expiry: expiryTime,
  16. };
  17. localStorage.setItem(key, JSON.stringify(item));
  18. }
  19. /**
  20. * 获取数据时判断是否过期
  21. * @param {string} key - 存储的键
  22. * @returns {Object|null}
  23. */
  24. function getItemWithExpiry(key) {
  25. const itemStr = localStorage.getItem(key);
  26. if (!itemStr) {
  27. return null;
  28. }
  29. try {
  30. const item = JSON.parse(itemStr);
  31. const now = new Date();
  32. if (now.getTime() > item.expiry) {
  33. localStorage.removeItem(key);
  34. return null;
  35. }
  36. return JSON.parse(item.value);
  37. } catch (e) {
  38. // 解析失败,直接返回null
  39. return null;
  40. }
  41. }
  42. const TokenKey = 'GCLS_Token';
  43. export function getSessionID() {
  44. const token = getItemWithExpiry(TokenKey);
  45. return token ? (token.session_id ?? '') : '';
  46. }
  47. export function getToken() {
  48. const token = getItemWithExpiry(TokenKey);
  49. return token;
  50. }
  51. export function setToken(token) {
  52. setItemWithExpiry(TokenKey, token, 10);
  53. }
  54. export function removeToken() {
  55. localStorage.removeItem(TokenKey);
  56. }
  57. const ConfigKey = 'GCLS_Config';
  58. export function getConfig() {
  59. const config = getItemWithExpiry(ConfigKey);
  60. return config;
  61. }
  62. export function setConfig(value) {
  63. setItemWithExpiry(ConfigKey, value, 10);
  64. }
  65. export function removeConfig() {
  66. localStorage.removeItem(ConfigKey);
  67. }
  68. /**
  69. * 设置本地存储
  70. * @param {string} key - 存储的键
  71. * @param {any} value - 存储的值
  72. */
  73. export function setLocalStore(key, value) {
  74. try {
  75. if (value === undefined) {
  76. localStorage.removeItem(key);
  77. return;
  78. }
  79. const toStore = typeof value === 'string' ? value : JSON.stringify(value);
  80. localStorage.setItem(key, toStore);
  81. } catch (e) {
  82. // 序列化或存储失败时退回到字符串存储,若仍失败则忽略
  83. try {
  84. localStorage.setItem(key, String(value));
  85. } catch (_) {
  86. /* noop */
  87. }
  88. }
  89. }
  90. /**
  91. * 获取本地存储
  92. * @param {string} key - 存储的键
  93. * @returns {any} 返回存储的值
  94. */
  95. export function getLocalStore(key) {
  96. const value = localStorage.getItem(key);
  97. try {
  98. if (value) {
  99. return JSON.parse(value);
  100. }
  101. return null;
  102. } catch (e) {
  103. return value;
  104. }
  105. }
  106. /**
  107. * 移除本地存储
  108. * @param {string} key - 存储的键
  109. */
  110. export function removeLocalStore(key) {
  111. localStorage.removeItem(key);
  112. }