181 lines
5.1 KiB
TypeScript
181 lines
5.1 KiB
TypeScript
import { createRouter, createWebHistory, type RouteLocationNormalized } from "vue-router";
|
|
import RootLayout from "@/layout/RootLayout.vue";
|
|
import aiRoutes from "@/router/routes/aiRoutes";
|
|
import chartRoutes from "@/router/routes/chartRoutes";
|
|
import customerRoutes from "@/router/routes/customerRoutes";
|
|
import { managementAccountRoutes, managementSettingRoutes, storeRoutes } from "@/router/routes/managementRoutes";
|
|
import permissionRoutes from "@/router/routes/permissionRoutes";
|
|
import pointRoutes from "@/router/routes/pointRoutes";
|
|
import packageRoutes from "@/router/routes/packageRoutes";
|
|
import registrationRoutes from "@/router/routes/registrationRoutes";
|
|
import tabRoutes from "@/router/routes/tabRoutes";
|
|
import userRoutes from "@/router/routes/userRoutes";
|
|
import notifyRoutes from "@/router/routes/notifyRoutes";
|
|
import followUpReminderRoutes from "@/router/routes/followUpReminderRoutes";
|
|
import productRoutes from "@/router/routes/productRoutes";
|
|
import salesOrderRoutes from "@/router/routes/salesOrderRoutes";
|
|
import fulfillmentRoutes from "@/router/routes/fulfillmentRoutes";
|
|
import { useCurrentStoreStore } from "@/store/currentStoreStore";
|
|
import { useGlobalUIStore } from "@/store/globalUI";
|
|
import { gaoRoleCodes } from "@/utils/permission";
|
|
import { useUserStore } from "timi-web-api/store";
|
|
|
|
const detailRoutes = [
|
|
...customerRoutes,
|
|
...userRoutes,
|
|
...registrationRoutes,
|
|
...managementSettingRoutes,
|
|
...storeRoutes,
|
|
...permissionRoutes,
|
|
...pointRoutes,
|
|
...packageRoutes,
|
|
...chartRoutes,
|
|
...managementAccountRoutes,
|
|
...notifyRoutes,
|
|
...followUpReminderRoutes,
|
|
...productRoutes,
|
|
...salesOrderRoutes,
|
|
...fulfillmentRoutes
|
|
];
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory("/"),
|
|
scrollBehavior(_to, _from, savedPosition) {
|
|
if (savedPosition) {
|
|
return savedPosition;
|
|
}
|
|
|
|
return {
|
|
left: 0,
|
|
top: 0
|
|
};
|
|
},
|
|
routes: [
|
|
{
|
|
path: "/",
|
|
component: RootLayout,
|
|
children: [
|
|
{
|
|
path: "",
|
|
redirect: "/customers"
|
|
},
|
|
{
|
|
path: "login",
|
|
name: "Login",
|
|
component: () => import("@/pages/user/LoginIndex.vue"),
|
|
meta: {
|
|
depth: 1,
|
|
navBarVisible: false,
|
|
tabBarVisible: false,
|
|
bodyBackground: "#F5F5F8",
|
|
darkBodyBackground: "#10161f"
|
|
}
|
|
},
|
|
{
|
|
path: "gao/invite/register",
|
|
name: "InviteRegister",
|
|
component: () => import("@/pages/user/InviteRegister.vue"),
|
|
meta: {
|
|
depth: 1,
|
|
navBarVisible: false,
|
|
tabBarVisible: false,
|
|
bodyBackground: "#F5F5F8",
|
|
darkBodyBackground: "#10161f"
|
|
}
|
|
},
|
|
{
|
|
path: "",
|
|
component: () => import("@/layout/MainLayout.vue"),
|
|
meta: {
|
|
depth: 1,
|
|
navBarVisible: false,
|
|
tabBarVisible: false,
|
|
bodyBackground: "#F5F5F8",
|
|
darkBodyBackground: "#10161f"
|
|
},
|
|
children: [
|
|
...tabRoutes,
|
|
...aiRoutes,
|
|
...detailRoutes
|
|
]
|
|
}
|
|
]
|
|
},
|
|
{
|
|
path: "/:pathMatch(.*)*",
|
|
redirect: "/customers"
|
|
}
|
|
]
|
|
});
|
|
|
|
router.beforeEach(async (to) => {
|
|
const userStore = useUserStore();
|
|
if (to.path === "/login" || to.path === "/gao/invite/register") {
|
|
return userStore.isLogged() ? "/management" : true;
|
|
}
|
|
|
|
if (!userStore.isLogged()) {
|
|
return {
|
|
path: "/login",
|
|
query: {
|
|
redirect: to.fullPath
|
|
}
|
|
};
|
|
}
|
|
|
|
const storeAccessRedirect = await resolveStoreAccessRedirect(to);
|
|
if (storeAccessRedirect) {
|
|
return storeAccessRedirect;
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
async function resolveStoreAccessRedirect(to: RouteLocationNormalized): Promise<string | undefined> {
|
|
const userStore = useUserStore();
|
|
const routeName = String(to.name || "");
|
|
if (routeName === "AiChat" && !userStore.hasRoles(gaoRoleCodes.storeManager)) {
|
|
return "/management";
|
|
}
|
|
const storeDiaryRouteNameList = ["StoreDiaryList", "StoreDiaryCreate", "StoreDiaryDetail", "StoreDiaryEdit"];
|
|
if (storeDiaryRouteNameList.includes(routeName)) {
|
|
if (!userStore.hasRoles(gaoRoleCodes.storeManager)) {
|
|
return "/management";
|
|
}
|
|
const currentStoreStore = useCurrentStoreStore();
|
|
const store = await currentStoreStore.loadLoginStore();
|
|
return store?.id ? undefined : "/management";
|
|
}
|
|
|
|
if (!userStore.hasRoles(gaoRoleCodes.storeManager) || userStore.hasRoles(gaoRoleCodes.globalManager)) {
|
|
return undefined;
|
|
}
|
|
|
|
const storeRouteNameList = ["StoreList", "StoreCreate", "StoreSort", "StoreEdit", "StoreBusinessDayCalendar"];
|
|
if (!storeRouteNameList.includes(routeName)) {
|
|
return undefined;
|
|
}
|
|
|
|
const currentStoreStore = useCurrentStoreStore();
|
|
const store = await currentStoreStore.loadLoginStore();
|
|
if (!store?.id) {
|
|
return "/management";
|
|
}
|
|
|
|
if (to.name === "StoreEdit" || to.name === "StoreBusinessDayCalendar") {
|
|
return String(to.params.id || "") === store.id ? undefined : `/management/stores/${store.id}`;
|
|
}
|
|
|
|
return `/management/stores/${store.id}`;
|
|
}
|
|
|
|
router.afterEach((to) => {
|
|
const globalUIStore = useGlobalUIStore();
|
|
const isDarkMode = globalUIStore.themeMode === "dark";
|
|
const fallbackBackground = isDarkMode ? "#10161f" : "#F5F5F8";
|
|
const targetBackground = isDarkMode ? to.meta.darkBodyBackground : to.meta.bodyBackground;
|
|
globalUIStore.setBodyBackground(typeof targetBackground === "string" ? targetBackground : fallbackBackground);
|
|
});
|
|
|
|
export default router;
|