This commit is contained in:
2026-06-13 12:23:07 +03:30
parent a14f1587c6
commit 164dbbfe53
14 changed files with 376 additions and 127 deletions
+83 -53
View File
@@ -3,9 +3,72 @@
import { useRouterStore } from '@core/stores/router'
import type { Router, RouteRecordRaw } from 'vue-router'
import { getConfigApp, isStoreApp } from '@/utils/config'
import { cloneDeep } from 'lodash'
//const _router = useRouter()
//import { cloneDeep } from 'lodash'
function applyAppModeMeta(route: RouteRecordRaw, appName: string | null) {
const store = isStoreApp()
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 === 'banners' && child.meta) {
child.meta.hideMenu = appName !== 'garoosi'
}
if (child.name === 'productsMain_categories' && child.meta) {
child.meta.hideMenu = !store
}
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 === 'banners' && appName !== 'garoosi') 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]
}
export function setupRoutes(permissionsKeys: string[], router?: Router) {
//console.log('this is permission', permissionsKeys)
//console.log('this is use router', _router)
@@ -23,42 +86,11 @@ export function setupRoutes(permissionsKeys: string[], router?: Router) {
(item.meta?.permissionsKey && permissionsKeys.includes(item.meta?.permissionsKey))
)
for (const route of routes.children) {
/*if (!route.children || (route.children && route.children.length == 0)) {
//path.push(route.name)
//return
continue
}*/
/*if (routes.children) {
}*/
walkInRoutes(route)
/*if (route.children && route.children.length > 0) {
path.push(route.name)
const res = walkInRoutes(route.children)
if (!res) {
path.pop()
} else {
return true
}
}*/
}
return
}
function filterRoutesByApp(routes: RouteRecordRaw) {
if (!routes.children) return
routes.children = routes.children.filter((item) => {
if (item.name === 'discounts' && (isStoreApp() || appName !== 'goldsun')) return false
if (item.name === 'banners' && appName !== 'garoosi') 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))
}
routerStore.routes = [
{
path: '/:pathMatch(.*)*',
@@ -79,33 +111,31 @@ export function setupRoutes(permissionsKeys: string[], router?: Router) {
},
]
for (const modulesKey in modules) {
// console.log(modulesKey)
const route: RouteRecordRaw | undefined = modules[modulesKey].default ?? undefined
if (route) {
if (permissionsKeys.length && permissionsKeys[0] == '*') {
filterRoutesByApp(route)
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)
routerStore.routes.push(route)
if (router) {
router.addRoute(route)
}
} else {
if (
!route?.meta?.permissionsKey ||
(route.meta.permissionsKey && permissionsKeys.includes(route.meta.permissionsKey))
) {
walkInRoutes(route)
filterRoutesByApp(route)
routerStore.routes.push(route)
if (router) {
router.addRoute(route)
}
}
}
}
}
// console.log(router)
//console.log('this is store', routerStore.routes)
/* if (router) {
console.log('this is router routes', router.getRoutes())
}*/
}