storage.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import Cookies from 'js-cookie';
  2. /**
  3. * window.localStorage 浏览器永久缓存
  4. * @method set 设置永久缓存
  5. * @method get 获取永久缓存
  6. * @method remove 移除永久缓存
  7. * @method clear 移除全部永久缓存
  8. */
  9. export const Local = {
  10. // 设置永久缓存
  11. set(key: string, val: any) {
  12. window.localStorage.setItem(key, JSON.stringify(val));
  13. },
  14. // 获取永久缓存
  15. get(key: string) {
  16. let json = <string>window.localStorage.getItem(key);
  17. return JSON.parse(json);
  18. },
  19. // 移除永久缓存
  20. remove(key: string) {
  21. window.localStorage.removeItem(key);
  22. },
  23. // 移除全部永久缓存
  24. clear() {
  25. window.localStorage.clear();
  26. },
  27. };
  28. /**
  29. * window.sessionStorage 浏览器临时缓存
  30. * @method set 设置临时缓存
  31. * @method get 获取临时缓存
  32. * @method remove 移除临时缓存
  33. * @method clear 移除全部临时缓存
  34. */
  35. export const Session = {
  36. // 设置临时缓存
  37. set(key: string, val: any) {
  38. // if (key === 'token') return Cookies.set(key, val);
  39. window.sessionStorage.setItem(key, JSON.stringify(val));
  40. },
  41. // 获取临时缓存
  42. get(key: string) {
  43. // if (key === 'token') return Cookies.get(key);
  44. let json = <string>window.sessionStorage.getItem(key);
  45. return JSON.parse(json);
  46. },
  47. // 移除临时缓存
  48. remove(key: string) {
  49. // if (key === 'token') return Cookies.remove(key);
  50. window.sessionStorage.removeItem(key);
  51. },
  52. // 移除全部临时缓存
  53. clear() {
  54. // Cookies.remove('token');
  55. window.sessionStorage.clear();
  56. },
  57. };