218 lines
7.2 KiB
TypeScript
218 lines
7.2 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, giftCardEnabledSetting, isStoreApp } from '@/utils/config'
|
|
import { isDeployAdmin } from '@/utils/deployAdmin'
|
|
import { hasSmsPanelAccess, isSmsBillingEnabled } from '@/utils/smsPanelAccess'
|
|
import { useUserStore } from '@/store/userStore'
|
|
import { cloneDeep, get } from 'lodash'
|
|
import { isSettingFlagOn } from '@/utils/settingFlags'
|
|
//const _router = useRouter()
|
|
//import { cloneDeep } from 'lodash'
|
|
|
|
function supportChatIsEnabled(): boolean {
|
|
try {
|
|
const userStore = useUserStore()
|
|
const settings: any[] = get(userStore.userInfo, 'settings', [])
|
|
if (!Array.isArray(settings)) return false
|
|
const chatFlag = settings.find((s: any) => s?.key === 'show_to_users_chat')
|
|
const hasChat = settings.find((s: any) => s?.key === 'has_chat')
|
|
const chatApi = settings.find((s: any) => s?.key === 'chat_api')
|
|
const flagOk = isSettingFlagOn(chatFlag?.value)
|
|
const hasChatOk = isSettingFlagOn(hasChat?.value)
|
|
const apiOk =
|
|
Boolean(String(chatApi?.value ?? '').trim()) &&
|
|
String(chatApi?.value).toLowerCase() !== 'null'
|
|
return flagOk && hasChatOk && apiOk
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
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 = !giftCardEnabledSetting.value
|
|
}
|
|
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' && !giftCardEnabledSetting.value) return false
|
|
|
|
return true
|
|
})
|
|
|
|
routes.children.forEach((child) => filterRoutesByApp(child, appName))
|
|
}
|
|
|
|
/** زیرمنوی پنل پیامک — فقط deploy admin و وقتی billing فعال است */
|
|
function filterSmsPanelRoutes(route: RouteRecordRaw) {
|
|
const user = useUserStore().userInfo
|
|
|
|
if (!route.children?.length) return
|
|
|
|
route.children = route.children.filter((child) => {
|
|
if (!child.meta?.requiresSmsPanelAccess) return true
|
|
return isSmsBillingEnabled(user) && hasSmsPanelAccess(user)
|
|
})
|
|
|
|
route.children.forEach((child) => filterSmsPanelRoutes(child))
|
|
}
|
|
|
|
/** زیرمنوی تنظیمات پیشرفته — فقط برای ادمین دیپل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)
|
|
filterSmsPanelRoutes(route)
|
|
}
|
|
|
|
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) => {
|
|
const key = item.meta?.permissionsKey
|
|
if (key === 'support') return supportChatIsEnabled()
|
|
return !key || permissionsKeys.includes(key)
|
|
})
|
|
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 (route.meta?.permissionsKey === 'support' && !supportChatIsEnabled()) {
|
|
continue
|
|
}
|
|
|
|
if (permissionsKeys.length && permissionsKeys[0] == '*') {
|
|
prepareRoute(route, appName, userMobile)
|
|
dynamicRoutes.push(route)
|
|
} else {
|
|
if (
|
|
route.meta?.permissionsKey === 'support' && supportChatIsEnabled()
|
|
) {
|
|
walkInRoutes(route)
|
|
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)
|
|
}
|
|
}
|