Files
boursecu_admin/src/router/index.ts
T
2026-06-21 20:27:13 +03:30

171 lines
5.5 KiB
TypeScript

//import { routes } from '@/core/router/routes'
//import router from '@/core/router/index'
import { useRouterStore } from '@core/stores/router'
import type { Router, RouteRecordRaw } from 'vue-router'
import { getConfigApp, isStoreApp } from '@/utils/config'
import { isDeployAdmin } from '@/utils/deployAdmin'
import { cloneDeep } from 'lodash'
//const _router = useRouter()
//import { cloneDeep } from 'lodash'
function applyAppModeMeta(route: RouteRecordRaw, appName: string | null) {
const store = isStoreApp()
if (route.name === 'pricing' && route.meta) {
route.meta.title = 'تاریخچه قیمت'
}
if (!route.children?.length) return
for (const child of route.children) {
if (child.name === 'giftCardMain' && child.meta) {
child.meta.title = store ? 'کارت هدیه و تخفیف' : 'کارت هدیه'
child.meta.hideMenu = !store && appName !== 'garoosi'
}
if (child.name === 'discounts' && child.meta) {
child.meta.hideMenu = store || appName !== 'goldsun'
}
if (child.name === 'pricingMain' && child.meta) {
child.meta.title = 'تاریخچه قیمت'
}
applyAppModeMeta(child, appName)
}
}
function filterRoutesByApp(routes: RouteRecordRaw, appName: string | null) {
if (!routes.children) return
routes.children = routes.children.filter((item) => {
if (item.name === 'discounts' && (isStoreApp() || appName !== 'goldsun')) return false
if (item.name === 'authentication' && appName !== 'garoosi') return false
if (item.name === 'giftCardMain' && !isStoreApp() && appName !== 'garoosi') return false
return true
})
routes.children.forEach((child) => filterRoutesByApp(child, appName))
}
/** زیرمنوی تنظیمات پیشرفته — فقط برای ادمین دیپلoyi */
function filterDeployAdminRoutes(route: RouteRecordRaw, mobile?: string | number | null) {
if (!route.children?.length) return
route.children = route.children.filter((child) => {
if (!child.meta?.deployAdminOnly) return true
return isDeployAdmin(mobile)
})
route.children.forEach((child) => filterDeployAdminRoutes(child, mobile))
}
const notFoundRoute: RouteRecordRaw = {
path: '/:pathMatch(.*)*',
name: 'notFound',
component: () => import('@/core/pages/404.vue'),
meta: {
hideMenu: true
}
}
function basePreservedRoutes(): RouteRecordRaw[] {
return [
{
name: 'factor',
path: '/withdraw/factor/:id',
component: () => import('@/pages/menuPages/withdraw/factor/index.vue'),
meta: {
permissionsKey: 'factor',
hideMenu: true
}
}
]
}
/** notFound باید آخر لیست باشد وگرنه /dashboard و بقیه مسیرها 404 می‌شوند */
function composeRoutes(dynamicRoutes: RouteRecordRaw[]): RouteRecordRaw[] {
return [...basePreservedRoutes(), ...dynamicRoutes, notFoundRoute]
}
function prepareRoute(route: RouteRecordRaw, appName: string | null, mobile?: string | number | null) {
applyAppModeMeta(route, appName)
filterRoutesByApp(route, appName)
filterDeployAdminRoutes(route, mobile)
}
function loadDynamicRoutes(appName: string | null, mobile?: string | number | null): RouteRecordRaw[] {
const modules = import.meta.glob<any>('./routes/*.ts', { eager: true })
const dynamicRoutes: RouteRecordRaw[] = []
for (const modulesKey in modules) {
const sourceRoute: RouteRecordRaw | undefined = modules[modulesKey].default ?? undefined
if (!sourceRoute) continue
if (sourceRoute.meta?.deployAdminOnly) continue
const route = cloneDeep(sourceRoute)
prepareRoute(route, appName, mobile)
dynamicRoutes.push(route)
}
return dynamicRoutes
}
/** بعد از تغییر is_store — منو را از روت‌های تازه (clone) دوباره می‌سازد */
export function refreshAppModeMenu(mobile?: string | number | null) {
const routerStore = useRouterStore()
const appName = getConfigApp('name')
routerStore.routes = composeRoutes(loadDynamicRoutes(appName, mobile))
}
export function setupRoutes(
permissionsKeys: string[],
router?: Router,
userMobile?: string | number | null
) {
const routerStore = useRouterStore()
const appName = getConfigApp('name')
const modules = import.meta.glob<any>('./routes/*.ts', { eager: true })
function walkInRoutes(routes: RouteRecordRaw) {
if (!routes.children) return
routes.children = routes?.children?.filter(
(item) =>
!item.meta?.permissionsKey ||
(item.meta?.permissionsKey && permissionsKeys.includes(item.meta?.permissionsKey))
)
for (const route of routes.children) {
walkInRoutes(route)
}
}
const dynamicRoutes: RouteRecordRaw[] = []
for (const modulesKey in modules) {
const sourceRoute: RouteRecordRaw | undefined = modules[modulesKey].default ?? undefined
if (!sourceRoute) continue
if (sourceRoute.meta?.deployAdminOnly) continue
const route = cloneDeep(sourceRoute)
if (permissionsKeys.length && permissionsKeys[0] == '*') {
prepareRoute(route, appName, userMobile)
dynamicRoutes.push(route)
} else {
if (
!route?.meta?.permissionsKey ||
(route.meta.permissionsKey && permissionsKeys.includes(route.meta.permissionsKey))
) {
walkInRoutes(route)
prepareRoute(route, appName, userMobile)
dynamicRoutes.push(route)
}
}
}
routerStore.routes = composeRoutes(dynamicRoutes)
if (router) {
for (const route of dynamicRoutes) {
router.addRoute(route)
}
router.addRoute(notFoundRoute)
}
}