2025-02-27 14:38:47 +08:00
|
|
|
import {defineStore} from "pinia";
|
2025-03-07 17:35:39 +08:00
|
|
|
import {ref} from "vue";
|
2025-03-10 19:01:21 +08:00
|
|
|
import router from "../../router/index.js";
|
|
|
|
|
import generateRouter from "../../router/generateRouter.js";
|
2025-04-19 15:28:32 +08:00
|
|
|
import Api from "../../api/index.js";
|
2025-02-27 14:38:47 +08:00
|
|
|
|
|
|
|
|
export const useSystemStore = defineStore("SystemStore", () => {
|
2025-03-07 17:35:39 +08:00
|
|
|
const isRoot = ref(false);
|
2025-03-10 19:01:21 +08:00
|
|
|
const RoutesTemp = ref([]);
|
|
|
|
|
|
|
|
|
|
const installRoute = async () => {
|
|
|
|
|
const routes = generateRouter(RoutesTemp.value);
|
|
|
|
|
router.removeRoute('home');
|
|
|
|
|
router.addRoute({
|
|
|
|
|
path: '/home',
|
|
|
|
|
name: 'home',
|
|
|
|
|
component: () => import('../../pages/layout/index.vue'),
|
|
|
|
|
redirect: `/home/${routes[0].path}`,
|
|
|
|
|
children: routes
|
|
|
|
|
});
|
|
|
|
|
await router.replace(router.currentRoute.value.fullPath);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 17:41:02 +08:00
|
|
|
const setRouter = async (_isRoot) => {
|
|
|
|
|
isRoot.value = _isRoot;
|
|
|
|
|
|
2025-03-10 19:01:21 +08:00
|
|
|
RoutesTemp.value.length = 0;
|
|
|
|
|
// 请求资源 mockRoutes
|
2025-04-19 15:28:32 +08:00
|
|
|
const {data} = await Api.admin.getMenu();
|
|
|
|
|
RoutesTemp.value.push(...data);
|
2025-03-10 19:01:21 +08:00
|
|
|
await installRoute();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 17:41:02 +08:00
|
|
|
const clearRouter = async () => {
|
|
|
|
|
const routes = generateRouter(RoutesTemp.value);
|
|
|
|
|
RoutesTemp.value.length = 0;
|
|
|
|
|
router.removeRoute('home');
|
|
|
|
|
router.addRoute({
|
|
|
|
|
path: '/home',
|
|
|
|
|
name: 'home',
|
|
|
|
|
component: () => import('../../pages/layout/index.vue'),
|
|
|
|
|
redirect: `/home/${routes[0].path}`,
|
|
|
|
|
children: []
|
|
|
|
|
});
|
|
|
|
|
await router.replace(router.currentRoute.value.fullPath);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-27 14:38:47 +08:00
|
|
|
return {
|
2025-03-10 19:01:21 +08:00
|
|
|
isRoot,
|
|
|
|
|
RoutesTemp,
|
|
|
|
|
setRouter,
|
|
|
|
|
installRoute,
|
2025-03-17 17:41:02 +08:00
|
|
|
clearRouter,
|
2025-03-10 19:01:21 +08:00
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
persist: {
|
|
|
|
|
key: 'SystemStore',
|
|
|
|
|
storage: localStorage,
|
|
|
|
|
afterHydrate: (val) => {
|
|
|
|
|
val.store.installRoute && val.store.installRoute();
|
|
|
|
|
},
|
|
|
|
|
pick: ['RoutesTemp']
|
2025-02-27 14:38:47 +08:00
|
|
|
}
|
|
|
|
|
});
|