3
0

updateBackground.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { colorIsDark, lighten, darken } from '/@/utils/color';
  2. import { useAppStore } from '/@/store/modules/app';
  3. import { ThemeEnum } from '/@/enums/appEnum';
  4. import { setCssVar } from './util';
  5. import { SIDE_BAR_BG_COLOR_LIST, SIDER_LOGO_BG_COLOR_LIST } from '/@/settings/designSetting';
  6. const HEADER_BG_COLOR_VAR = '--header-bg-color';
  7. const HEADER_BG_HOVER_COLOR_VAR = '--header-bg-hover-color';
  8. const HEADER_MENU_ACTIVE_BG_COLOR_VAR = '--header-active-menu-bg-color';
  9. const SIDER_LOGO_BG_COLOR = '--sider-logo-bg-color';
  10. const SIDER_DARK_BG_COLOR = '--sider-dark-bg-color';
  11. const SIDER_DARK_DARKEN_BG_COLOR = '--sider-dark-darken-bg-color';
  12. const SIDER_LIGHTEN_BG_COLOR = '--sider-dark-lighten-bg-color';
  13. /**
  14. * Change the background color of the top header
  15. * @param color
  16. */
  17. export function updateHeaderBgColor(color?: string) {
  18. const appStore = useAppStore();
  19. const darkMode = appStore.getDarkMode === ThemeEnum.DARK;
  20. if (!color) {
  21. if (darkMode) {
  22. color = '#151515';
  23. } else {
  24. color = appStore.getHeaderSetting.bgColor;
  25. }
  26. }
  27. // bg color
  28. setCssVar(HEADER_BG_COLOR_VAR, color);
  29. // hover color
  30. const hoverColor = lighten(color!, 6);
  31. setCssVar(HEADER_BG_HOVER_COLOR_VAR, hoverColor);
  32. setCssVar(HEADER_MENU_ACTIVE_BG_COLOR_VAR, hoverColor);
  33. // Determine the depth of the color value and automatically switch the theme
  34. const isDark = colorIsDark(color!);
  35. appStore.setProjectConfig({
  36. headerSetting: {
  37. theme: isDark || darkMode ? ThemeEnum.DARK : ThemeEnum.LIGHT,
  38. },
  39. });
  40. }
  41. /**
  42. * Change the background color of the left menu
  43. * @param color bg color
  44. */
  45. export function updateSidebarBgColor(color?: string) {
  46. const appStore = useAppStore();
  47. // if (!isHexColor(color)) return;
  48. const darkMode = appStore.getDarkMode === ThemeEnum.DARK;
  49. if (!color) {
  50. if (darkMode) {
  51. color = '#212121';
  52. } else {
  53. color = appStore.getMenuSetting.bgColor;
  54. }
  55. }
  56. // 代码逻辑说明: 【QQYUN-5922】logo背景色渐变
  57. const findIndex = SIDE_BAR_BG_COLOR_LIST.findIndex((item) => item === color);
  58. setCssVar(SIDER_LOGO_BG_COLOR, findIndex == -1 ? 'linear-gradient(180deg, #000000, #282828)' : SIDER_LOGO_BG_COLOR_LIST[findIndex]);
  59. setCssVar(SIDER_DARK_BG_COLOR, color);
  60. setCssVar(SIDER_DARK_DARKEN_BG_COLOR, darken(color!, 6));
  61. setCssVar(SIDER_LIGHTEN_BG_COLOR, lighten(color!, 5));
  62. // only #ffffff is light
  63. // Only when the background color is #fff, the theme of the menu will be changed to light
  64. // 代码逻辑说明: 【QQYUN-8922】左侧导航栏文字颜色调整区分彩色和暗黑
  65. let theme;
  66. let isThemeBright = false;
  67. if (['#fff', '#ffffff'].includes(color!.toLowerCase()) && !darkMode) {
  68. theme = ThemeEnum.LIGHT;
  69. } else if (['#009688', '#e74c3c','#037bd5'].includes(color!.toLowerCase()) && !darkMode) {
  70. theme = ThemeEnum.DARK;
  71. isThemeBright = true;
  72. } else {
  73. theme = ThemeEnum.DARK;
  74. }
  75. appStore.setProjectConfig({
  76. menuSetting: {
  77. theme,
  78. isThemeBright,
  79. },
  80. });
  81. }