Files
boursecu_admin/src/router/index.ts
T

142 lines
4.4 KiB
TypeScript
Raw Normal View History

2026-01-24 17:45:54 +03:30
//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'
2026-06-11 22:29:28 +03:30
import { getConfigApp, isStoreApp } from '@/utils/config'
2026-06-13 12:23:07 +03:30
import { cloneDeep } from 'lodash'
2026-01-24 17:45:54 +03:30
//const _router = useRouter()
//import { cloneDeep } from 'lodash'
2026-06-13 12:23:07 +03:30
function applyAppModeMeta(route: RouteRecordRaw, appName: string | null) {
const store = isStoreApp()
2026-06-13 12:42:10 +03:30
if (route.name === 'pricing' && route.meta) {
2026-06-13 17:38:35 +03:30
route.meta.title = 'تاریخچه قیمت'
2026-06-13 12:42:10 +03:30
}
2026-06-13 12:23:07 +03:30
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'
}
2026-06-13 12:42:10 +03:30
if (child.name === 'pricingMain' && child.meta) {
2026-06-13 17:38:35 +03:30
child.meta.title = 'تاریخچه قیمت'
2026-06-13 12:42:10 +03:30
}
2026-06-13 12:23:07 +03:30
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))
}
/** بعد از تغییر is_store — منو را از روت‌های تازه (clone) دوباره می‌سازد */
export function refreshAppModeMenu() {
const routerStore = useRouterStore()
const modules = import.meta.glob<any>('./routes/*.ts', { eager: true })
const appName = getConfigApp('name')
const preserved = routerStore.routes.filter(
(route) => route.name === 'notFound' || route.name === 'factor'
)
const dynamicRoutes: RouteRecordRaw[] = []
for (const modulesKey in modules) {
const sourceRoute: RouteRecordRaw | undefined = modules[modulesKey].default ?? undefined
if (!sourceRoute) continue
const route = cloneDeep(sourceRoute)
applyAppModeMeta(route, appName)
filterRoutesByApp(route, appName)
dynamicRoutes.push(route)
}
routerStore.routes = [...preserved, ...dynamicRoutes]
}
2026-01-24 17:45:54 +03:30
export function setupRoutes(permissionsKeys: string[], router?: Router) {
//console.log('this is permission', permissionsKeys)
//console.log('this is use router', _router)
console.log('salammm')
const routerStore = useRouterStore()
const modules = import.meta.glob<any>('./routes/*.ts', { eager: true })
console.log(modules)
2026-02-21 20:24:59 +03:30
const appName = getConfigApp('name')
2026-01-24 17:45:54 +03:30
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)
}
return
}
2026-02-21 20:24:59 +03:30
2026-01-24 17:45:54 +03:30
routerStore.routes = [
{
path: '/:pathMatch(.*)*',
name: 'notFound',
component: () => import('@/core/pages/404.vue'),
meta: {
hideMenu: true
}
},
{
name: 'factor',
path: '/withdraw/factor/:id',
component: () => import('@/pages/menuPages/withdraw/factor/index.vue'),
meta: {
permissionsKey: 'factor',
hideMenu: true
}
},
]
for (const modulesKey in modules) {
2026-06-13 12:23:07 +03:30
const sourceRoute: RouteRecordRaw | undefined = modules[modulesKey].default ?? undefined
if (!sourceRoute) continue
const route = cloneDeep(sourceRoute)
if (permissionsKeys.length && permissionsKeys[0] == '*') {
applyAppModeMeta(route, appName)
filterRoutesByApp(route, appName)
routerStore.routes.push(route)
if (router) {
router.addRoute(route)
}
} else {
if (
!route?.meta?.permissionsKey ||
(route.meta.permissionsKey && permissionsKeys.includes(route.meta.permissionsKey))
) {
walkInRoutes(route)
applyAppModeMeta(route, appName)
filterRoutesByApp(route, appName)
2026-01-24 17:45:54 +03:30
routerStore.routes.push(route)
if (router) {
router.addRoute(route)
}
}
}
}
}