You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

localStorage.ts 1.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { parseJsonText } from './index';
  2. export default class LocalStorage {
  3. // 登录的用户,包括用户名、密码和版本号
  4. static readonly loginUserKey = 'login-user';
  5. // 记住密码
  6. static readonly rememberPasswordKey = 'login-remember-password';
  7. /**
  8. * 获取 LocalStorage 值
  9. * @param key - LocalStorage key
  10. * @param isObject - 是不是对象
  11. */
  12. static getItem(key: string, isObject: boolean = false) {
  13. const jsonStr = localStorage.getItem(key);
  14. if (!isObject) {
  15. return jsonStr;
  16. }
  17. if (jsonStr) {
  18. return parseJsonText(jsonStr);
  19. }
  20. return null;
  21. }
  22. /**
  23. * 设置 LocalStorage 值
  24. * @param key - LocalStorage key
  25. * @param state - SessionStorage state
  26. * @param isObject - 是不是对象
  27. */
  28. static setItem(key: string, state?: any, isObject: boolean = false) {
  29. if (state) {
  30. localStorage.setItem(key, isObject ? JSON.stringify(state) : state);
  31. }
  32. }
  33. /**
  34. * 移除 LocalStorage 值
  35. * @param key - LocalStorage key
  36. */
  37. static removeItem(key: string) {
  38. localStorage.removeItem(key);
  39. }
  40. }