mirror of
https://github.com/halo-dev/halo.git
synced 2025-12-20 16:44:38 +08:00
#### What type of PR is this? /area ui /kind feature /milestone 2.22.x #### What this PR does / why we need it: This PR moves the `currentUser` and `globalInfo` stores to the `@halo-dev/console-shared` package, making them easily accessible for plugins. In addition, it’s now possible for plugins to define their own global stores using Pinia. #### Does this PR introduce a user-facing change? ```release-note - 在 `@halo-dev/console-shared` 包中提供 `stores` 对象,包含 currentUser 和 globalInfo。 - 支持在插件的 UI 中使用 Pinia 定义全局 Store ```
106 lines
2.6 KiB
TypeScript
106 lines
2.6 KiB
TypeScript
import { consoleApiClient } from "@halo-dev/api-client";
|
|
import { createPinia } from "pinia";
|
|
import type { DirectiveBinding } from "vue";
|
|
import { createApp } from "vue";
|
|
import App from "./App.vue";
|
|
import router from "./router";
|
|
// setup
|
|
import { getBrowserLanguage, i18n, setupI18n } from "@/locales";
|
|
import { setupComponents } from "@/setup/setupComponents";
|
|
import "@/setup/setupStyles";
|
|
// core modules
|
|
import { setupApiClient } from "@/setup/setupApiClient";
|
|
import { setupVueQuery } from "@/setup/setupVueQuery";
|
|
import { useRoleStore } from "@/stores/role";
|
|
import { getCookie } from "@/utils/cookie";
|
|
import {
|
|
setupCoreModules,
|
|
setupPluginModules,
|
|
} from "@console/setup/setupModules";
|
|
import { useThemeStore } from "@console/stores/theme";
|
|
import { stores, utils } from "@halo-dev/console-shared";
|
|
import "core-js/es/object/has-own";
|
|
|
|
const app = createApp(App);
|
|
|
|
setupComponents(app);
|
|
setupI18n(app);
|
|
setupVueQuery(app);
|
|
setupApiClient();
|
|
|
|
app.use(createPinia());
|
|
|
|
async function loadUserPermissions() {
|
|
const { data: currentPermissions } =
|
|
await consoleApiClient.user.getPermissions({
|
|
name: "-",
|
|
});
|
|
const roleStore = useRoleStore();
|
|
roleStore.$patch({
|
|
permissions: currentPermissions,
|
|
});
|
|
|
|
// Set permissions in shared utils
|
|
utils.permission.setUserPermissions(currentPermissions.uiPermissions);
|
|
|
|
app.directive(
|
|
"permission",
|
|
(el: HTMLElement, binding: DirectiveBinding<string[]>) => {
|
|
const { value } = binding;
|
|
const { any } = binding.modifiers;
|
|
|
|
if (utils.permission.has(value, any ?? false)) {
|
|
return;
|
|
}
|
|
|
|
el?.remove?.();
|
|
}
|
|
);
|
|
}
|
|
|
|
async function loadActivatedTheme() {
|
|
const themeStore = useThemeStore();
|
|
await themeStore.fetchActivatedTheme();
|
|
}
|
|
|
|
(async function () {
|
|
await initApp();
|
|
})();
|
|
|
|
async function initApp() {
|
|
try {
|
|
setupCoreModules(app);
|
|
|
|
const currentUserStore = stores.currentUser();
|
|
await currentUserStore.fetchCurrentUser();
|
|
|
|
// set locale
|
|
i18n.global.locale.value = getCookie("language") || getBrowserLanguage();
|
|
utils.date.setLocale(i18n.global.locale.value);
|
|
|
|
const globalInfoStore = stores.globalInfo();
|
|
await globalInfoStore.fetchGlobalInfo();
|
|
|
|
if (currentUserStore.isAnonymous) {
|
|
return;
|
|
}
|
|
|
|
await loadUserPermissions();
|
|
|
|
try {
|
|
await setupPluginModules(app);
|
|
} catch (e) {
|
|
console.error("Failed to load plugins", e);
|
|
}
|
|
|
|
if (globalInfoStore.globalInfo?.userInitialized) {
|
|
await loadActivatedTheme();
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
} finally {
|
|
app.use(router);
|
|
app.mount("#app");
|
|
}
|
|
}
|