homeStyle.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { ThemeEnum } from '/@/enums/appEnum';
  2. import { HOME_STYLE_KEY } from '/@/enums/cacheEnum';
  3. /**
  4. * 首页风格选择:localStorage 的存放与读取工具。
  5. * key 定义见 /@/enums/cacheEnum 的 HOME_STYLE_KEY;
  6. * 最终可选首页由 HomeStyleSelect.vue 的前端配置数组与 /sys/permissionNew/list 后端数据按 component 匹配得出。
  7. */
  8. export interface HomeStyleOption {
  9. /** 唯一标识(持久化用,取前端配置的 component,稳定) */
  10. id: string;
  11. /** 首页风格名称(下拉框显示) */
  12. name: string;
  13. /** 首页路由(对应后端菜单 url) */
  14. route: string;
  15. /** 主题模式:ThemeEnum.VENT1 / DEEPBLUE / GREEN / LIGHT / DARK */
  16. theme: ThemeEnum;
  17. /** 页面样式变量,可选(如 ThemeModel.theme5_5) */
  18. cssVars?: Record<string, string>;
  19. }
  20. /** 读取已保存的首页风格(无则返回 null,损坏数据安全兜底) */
  21. export function getSavedHomeStyle(): HomeStyleOption | null {
  22. try {
  23. const raw = localStorage.getItem(HOME_STYLE_KEY);
  24. return raw ? (JSON.parse(raw) as HomeStyleOption) : null;
  25. } catch {
  26. return null;
  27. }
  28. }
  29. /** 保存首页风格选择;传入 null 表示清除(恢复系统默认) */
  30. export function saveHomeStyle(option: HomeStyleOption | null): void {
  31. if (option) {
  32. localStorage.setItem(HOME_STYLE_KEY, JSON.stringify({ id: option.id, route: option.route }));
  33. } else {
  34. localStorage.removeItem(HOME_STYLE_KEY);
  35. }
  36. }
  37. /** 供路由守卫 / 登录流程读取已保存的首页路由(无则返回 '') */
  38. export function getSavedHomeRoute(): string {
  39. return getSavedHomeStyle()?.route || '';
  40. }