Commit 2a916455 authored by jiangweijia's avatar jiangweijia

路由相关改动,添加页面跳转方法

parent 4cd63863
......@@ -17,7 +17,7 @@ export const TheMenu = defineComponent({
setup(props) {
const menuStore = useMenuStore()
const { menu, selectedMenu, openedMenu } = storeToRefs(menuStore)
function handleOpenChange(openKeys: any) {
menuStore.setOpenedMenu([...openKeys])
}
......
......@@ -3,7 +3,7 @@ import { setupLayouts } from 'virtual:generated-layouts'
import generatedRoutes from 'virtual:generated-pages'
import { postGetUserInfo, postQueryMenu } from '@/apis'
import { useConfigStore, useMenuStore, useUserStore } from '@/store/modules'
import { message } from 'ant-design-vue'
export const useAppStore = defineStore('app', () => {
const route = location.hash.slice(1)
const userId = localStorage.getItem('userId')
......@@ -12,6 +12,7 @@ export const useAppStore = defineStore('app', () => {
const menuStore = useMenuStore()
const configStore = useConfigStore()
const userStore = useUserStore()
const traansformRouter: any = ref([])
if (!userId) {
location.hash = '#/login'
......@@ -45,6 +46,22 @@ export const useAppStore = defineStore('app', () => {
}
})
}
function transformToRoute(routerList: any[], item: Menu[]) {
let flatB = flatten(item)
let pathToTitle: any = {}
for (let i = 0; i < flatB.length; i++) {
pathToTitle[flatB[i].path] = flatB[i].title
}
let result = routerList.map((item) => {
if (item.path in pathToTitle) {
return { ...item, meta: { title: pathToTitle[item.path] } }
} else {
return item
}
})
return result
}
// 递归查找菜单
function findMenu(menu: MenuRouteConfig[], openedMenu: MenuRouteConfig[] = []) {
......@@ -77,11 +94,40 @@ export const useAppStore = defineStore('app', () => {
menuStore.setMenu(menu)
}
function setRoutes() {
function setRoutes(data: any[]) {
const routes = generatedRoutes.filter((r) => authRoutes.includes(r.path))
traansformRouter.value = transformToRoute(routes, data)
router.addRoutes(setupLayouts(routes))
}
function getRoutes(name: string) {
return traansformRouter.value.find((item: { name: string }) => item.name == name)
}
// 打开新标签页
function openPage(pageName: string) {
let targMenu = getRoutes(pageName)
if (targMenu?.path) {
menuStore.addVisitedMenu(targMenu)
menuStore.setSelectedMenu(targMenu.name)
router.push(targMenu.path)
} else {
message.warning('未找到菜单')
}
}
// 平铺数组
function flatten(arr: any[]) {
let result: any[] = []
for (let i = 0; i < arr.length; i++) {
if (arr[i].children) {
result = result.concat(flatten(arr[i].children))
} else {
result.push(arr[i])
}
}
return result
}
function setState() {
let openedMenu: MenuRouteConfig[] = []
let currentMenu = findMenu(menuStore.menu, openedMenu)
......@@ -101,7 +147,7 @@ export const useAppStore = defineStore('app', () => {
postQueryMenu().then((r) => {
if (r.data) {
setMenu(r.data)
setRoutes()
setRoutes(r.data)
setState()
loading.value = false
}
......@@ -116,6 +162,7 @@ export const useAppStore = defineStore('app', () => {
return {
loading,
openPage,
logout
}
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment