storage.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * 本地存储方法封装
  3. */
  4. export const Local = {
  5. set(key: string, val: any) {
  6. uni.setStorageSync(key, val);
  7. },
  8. get(key: string) {
  9. return uni.getStorageSync(key);
  10. },
  11. remove(key: string) {
  12. uni.removeStorageSync(key);
  13. },
  14. clear() {
  15. uni.clearStorageSync();
  16. }
  17. };
  18. export const Session = {
  19. set(key: string, val: any) {
  20. // #ifdef H5
  21. window.sessionStorage.setItem(key, JSON.stringify(val));
  22. // #endif
  23. // #ifndef H5
  24. uni.setStorageSync(key, val);
  25. // #endif
  26. },
  27. get(key: string) {
  28. // #ifdef H5
  29. const json = window.sessionStorage.getItem(key);
  30. if (!json) return null;
  31. try {
  32. return JSON.parse(json);
  33. } catch (e) {
  34. return json;
  35. }
  36. // #endif
  37. // #ifndef H5
  38. return uni.getStorageSync(key);
  39. // #endif
  40. },
  41. remove(key: string) {
  42. // #ifdef H5
  43. window.sessionStorage.removeItem(key);
  44. // #endif
  45. // #ifndef H5
  46. uni.removeStorageSync(key);
  47. // #endif
  48. },
  49. clear() {
  50. // #ifdef H5
  51. window.sessionStorage.clear();
  52. // #endif
  53. }
  54. };