112 lines
3.3 KiB
TypeScript
112 lines
3.3 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 } from '@/utils/config'
|
|
//const _router = useRouter()
|
|
//import { cloneDeep } from 'lodash'
|
|
|
|
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)
|
|
const appName = getConfigApp('name')
|
|
|
|
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) {
|
|
/*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' && appName !== 'goldsun') return false
|
|
if (item.name === 'banners' && appName !== 'garoosi') return false
|
|
if (item.name === 'authentication' && appName !== 'garoosi') return false
|
|
if (item.name === 'giftCardMain' && appName !== 'garoosi') return false
|
|
|
|
|
|
return true
|
|
})
|
|
routes.children.forEach((child) => filterRoutesByApp(child))
|
|
}
|
|
|
|
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) {
|
|
// console.log(modulesKey)
|
|
const route: RouteRecordRaw | undefined = modules[modulesKey].default ?? undefined
|
|
if (route) {
|
|
if (permissionsKeys.length && permissionsKeys[0] == '*') {
|
|
filterRoutesByApp(route)
|
|
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())
|
|
}*/
|
|
}
|