云
17 小时以前 c2e8526f8f857be5e3a8c6f2b39c5efd424de33e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { watch } from 'vue';
import { initPreferences, preferences, preferencesManager } from './packages/preferences/src';
import { unmountGlobalLoading } from './packages/utils/src';
 
import { overridesPreferences, preferencesExtension } from './preferences';
 
declare const __APP_LOGO__: string;
declare const __APP_FAVICON__: string;
declare const __APP_NAME__: string;
 
/**
 * 应用初始化完成之后再进行页面加载渲染
 */
async function initApplication() {
  // name用于指定项目唯一标识
  // 用于区分不同项目的偏好设置以及存储数据的key前缀以及其他一些需要隔离的数据
  const env = import.meta.env.PROD ? 'prod' : 'dev';
  const appVersion = import.meta.env.VITE_APP_VERSION;
  const namespace = `${import.meta.env.VITE_APP_NAMESPACE}-${appVersion}-${env}`;
 
  // app偏好设置初始化
  await initPreferences({
    extension: preferencesExtension,
    namespace,
    overrides: overridesPreferences,
  });
 
  // 首次升级同步品牌主色,避免旧的蓝色偏好遮盖本次主题更新。
  // 升级后用户在偏好设置中选择的颜色和明暗模式仍会保留。
  document.documentElement.dataset.brandTheme = 'herbal';
  const brandThemeKey = `${namespace}-herbal-theme-v1`;
  try {
    if (!localStorage.getItem(brandThemeKey)) {
      preferencesManager.updatePreferences({
        theme: { builtinType: 'custom', colorPrimary: '#a51d24' },
      });
      localStorage.setItem(brandThemeKey, '1');
    }
  } catch {
    preferencesManager.updatePreferences({
      theme: { builtinType: 'custom', colorPrimary: '#a51d24' },
    });
  }
  watch(
    () => preferences.theme.colorPrimary,
    (color) => { document.documentElement.dataset.herbalPalette = String(color.toLowerCase() === '#a51d24'); },
    { immediate: true, flush: 'sync' },
  );
 
  // 强制覆盖公司 logo 和 favicon,绕过 localStorage 缓存
  if (__APP_LOGO__) {
    preferencesManager.updatePreferences({ logo: { source: __APP_LOGO__ } });
  }
  // 强制覆盖公司名称,绕过 localStorage 缓存
  if (__APP_NAME__) {
    preferencesManager.updatePreferences({
      app: { name: __APP_NAME__ },
      copyright: { companyName: __APP_NAME__ },
    });
  }
  if (__APP_FAVICON__) {
    const link = document.querySelector('link[rel="icon"]');
    if (link) {
      link.setAttribute('href', __APP_FAVICON__);
    }
  }
 
  // 启动应用并挂载
  // vue应用主要逻辑及视图
  const { bootstrap } = await import('./bootstrap');
  await bootstrap(namespace);
 
  // 移除并销毁loading
  unmountGlobalLoading();
}
 
initApplication();