2024-12-10 14:36:25 +00:00
|
|
|
import { route as defineRouter } from 'quasar/wrappers';
|
2023-03-15 10:28:18 +00:00
|
|
|
import {
|
|
|
|
createRouter,
|
|
|
|
createMemoryHistory,
|
|
|
|
createWebHistory,
|
|
|
|
createWebHashHistory,
|
|
|
|
} from 'vue-router';
|
2022-12-20 11:00:36 +00:00
|
|
|
import routes from './routes';
|
2022-12-20 11:30:25 +00:00
|
|
|
import { i18n } from 'src/boot/i18n';
|
|
|
|
import { useState } from 'src/composables/useState';
|
|
|
|
import { useRole } from 'src/composables/useRole';
|
2023-05-12 07:15:45 +00:00
|
|
|
import { useUserConfig } from 'src/composables/useUserConfig';
|
2024-04-22 05:37:35 +00:00
|
|
|
import { useTokenConfig } from 'src/composables/useTokenConfig';
|
2024-05-03 11:18:46 +00:00
|
|
|
import { useAcl } from 'src/composables/useAcl';
|
2024-12-10 14:36:25 +00:00
|
|
|
import { isLoggedIn } from 'src/utils/session';
|
|
|
|
import { useSession } from 'src/composables/useSession';
|
2025-01-23 12:49:21 +00:00
|
|
|
import { Notify } from 'quasar';
|
2025-01-25 08:20:07 +00:00
|
|
|
import { useStateStore } from 'stores/useStateStore';
|
2022-12-20 11:30:25 +00:00
|
|
|
|
2025-01-13 11:30:07 +00:00
|
|
|
let session = null;
|
2024-03-14 14:16:50 +00:00
|
|
|
const { t, te } = i18n.global;
|
2022-12-20 11:00:36 +00:00
|
|
|
|
2023-03-15 10:28:18 +00:00
|
|
|
const createHistory = process.env.SERVER
|
|
|
|
? createMemoryHistory
|
|
|
|
: process.env.VUE_ROUTER_MODE === 'history'
|
2025-01-23 12:49:21 +00:00
|
|
|
? createWebHistory
|
|
|
|
: createWebHashHistory;
|
2023-03-15 10:28:18 +00:00
|
|
|
|
|
|
|
const Router = createRouter({
|
|
|
|
scrollBehavior: () => ({ left: 0, top: 0 }),
|
|
|
|
routes,
|
|
|
|
|
|
|
|
// Leave this as is and make changes in quasar.conf.js instead!
|
|
|
|
// quasar.conf.js -> build -> vueRouterMode
|
|
|
|
// quasar.conf.js -> build -> publicPath
|
|
|
|
history: createHistory(process.env.VUE_ROUTER_BASE),
|
|
|
|
});
|
|
|
|
|
2022-12-20 11:00:36 +00:00
|
|
|
/*
|
|
|
|
* If not building with SSR mode, you can
|
|
|
|
* directly export the Router instantiation;
|
|
|
|
*
|
|
|
|
* The function below can be async too; either use
|
|
|
|
* async/await or return a Promise which resolves
|
|
|
|
* with the Router instance.
|
|
|
|
*/
|
2023-03-15 10:28:18 +00:00
|
|
|
export { Router };
|
2024-12-10 14:36:25 +00:00
|
|
|
export default defineRouter(function (/* { store, ssrContext } */) {
|
|
|
|
const state = useState();
|
2022-12-20 11:30:25 +00:00
|
|
|
Router.beforeEach(async (to, from, next) => {
|
2025-01-13 11:30:07 +00:00
|
|
|
if (!session) session = useSession();
|
2024-08-13 10:25:08 +00:00
|
|
|
const outLayout = Router.options.routes[0].children.map((r) => r.name);
|
2024-12-10 14:36:25 +00:00
|
|
|
if (!session.isLoggedIn() && !outLayout.includes(to.name)) {
|
2022-12-20 11:30:25 +00:00
|
|
|
return next({ name: 'Login', query: { redirect: to.fullPath } });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLoggedIn()) {
|
|
|
|
const stateRoles = state.getRoles().value;
|
|
|
|
if (stateRoles.length === 0) {
|
2023-05-12 07:15:45 +00:00
|
|
|
await useRole().fetch();
|
2024-05-03 11:18:46 +00:00
|
|
|
await useAcl().fetch();
|
2023-05-12 07:15:45 +00:00
|
|
|
await useUserConfig().fetch();
|
2024-04-22 05:37:35 +00:00
|
|
|
await useTokenConfig().fetch();
|
2022-12-20 11:30:25 +00:00
|
|
|
}
|
|
|
|
const matches = to.matched;
|
2024-09-09 09:35:49 +00:00
|
|
|
const hasRequiredAcls = matches.every((route) => {
|
2022-12-20 11:30:25 +00:00
|
|
|
const meta = route.meta;
|
2024-09-09 09:35:49 +00:00
|
|
|
if (!meta?.acls) return true;
|
|
|
|
return useAcl().hasAny(meta.acls);
|
2022-12-20 11:30:25 +00:00
|
|
|
});
|
2024-09-09 09:35:49 +00:00
|
|
|
if (!hasRequiredAcls) return next({ path: '/' });
|
2022-12-20 11:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
Router.afterEach((to) => {
|
|
|
|
let title = t(`login.title`);
|
|
|
|
|
|
|
|
const matches = to.matched;
|
|
|
|
if (matches && matches.length > 1) {
|
|
|
|
const module = matches[1];
|
|
|
|
const moduleTitle = module.meta && module.meta.title;
|
|
|
|
if (moduleTitle) {
|
2024-06-19 06:44:40 +00:00
|
|
|
title = t(`globals.pageTitles.${moduleTitle}`);
|
2022-12-20 11:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const childPage = to.meta;
|
|
|
|
const childPageTitle = childPage && childPage.title;
|
|
|
|
if (childPageTitle && matches.length > 2) {
|
|
|
|
if (title != '') title += ': ';
|
|
|
|
|
2024-06-19 06:44:40 +00:00
|
|
|
const moduleLocale = `globals.pageTitles.${childPageTitle}`;
|
2024-03-14 14:16:50 +00:00
|
|
|
const pageTitle = te(moduleLocale)
|
|
|
|
? t(moduleLocale)
|
|
|
|
: t(`globals.pageTitles.${childPageTitle}`);
|
2022-12-20 11:30:25 +00:00
|
|
|
const idParam = to.params && to.params.id;
|
|
|
|
const idPageTitle = `${idParam} - ${pageTitle}`;
|
|
|
|
const builtTitle = idParam ? idPageTitle : pageTitle;
|
|
|
|
|
|
|
|
title += builtTitle;
|
|
|
|
}
|
2025-01-25 08:20:07 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
state.set('error', true);
|
|
|
|
}, 1000);
|
2022-12-20 11:30:25 +00:00
|
|
|
document.title = title;
|
|
|
|
});
|
|
|
|
|
2025-01-23 12:49:21 +00:00
|
|
|
Router.onError((error, to) => {
|
|
|
|
if (
|
|
|
|
error.message.includes('Failed to fetch dynamically imported module') ||
|
|
|
|
error.message.includes('Importing a module script failed')
|
2025-01-25 08:20:07 +00:00
|
|
|
) {
|
|
|
|
state.set('error', {
|
|
|
|
message: t('globals.errorLoadingPage'),
|
|
|
|
timeout: 5000,
|
|
|
|
type: 'negative',
|
|
|
|
});
|
2025-01-23 12:49:21 +00:00
|
|
|
Notify.create({
|
|
|
|
message: t('globals.noSelectedRows'),
|
|
|
|
type: 'negative',
|
|
|
|
timeout: 5000,
|
|
|
|
progress: true,
|
|
|
|
});
|
2025-01-25 08:20:07 +00:00
|
|
|
}
|
2025-01-23 12:49:21 +00:00
|
|
|
});
|
2022-12-20 11:00:36 +00:00
|
|
|
return Router;
|
|
|
|
});
|