You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.1 KiB
66 lines
2.1 KiB
import { createRouter, createWebHashHistory } from 'vue-router'
|
|
import { staticRoutes } from './routes'
|
|
import { useStore } from '@/store/root'
|
|
import { getIsShowVoiceBtn, getIndexList } from '@/http/api'
|
|
import { ERR_OK } from '@/http/config'
|
|
|
|
const router = createRouter({
|
|
history: createWebHashHistory(process.env.BASE_URL),
|
|
routes: staticRoutes
|
|
})
|
|
|
|
function dynamicRoutes(list) {
|
|
return {
|
|
path: '/index',
|
|
name: 'home',
|
|
component: list.length ? () => import(/* webpackChunkName: "Home" */ '@/views/Index/Index') : () => import(/* webpackChunkName: "Home" */ '@/views/Guide/Guide'),
|
|
|
|
meta: {
|
|
showMenu: true,
|
|
showMap: list.length ? false : true
|
|
}
|
|
}
|
|
}
|
|
|
|
router.beforeEach(async to => {
|
|
try {
|
|
const store = useStore()
|
|
|
|
const voiceRes = await getIsShowVoiceBtn()
|
|
store.isUseSpeech !== voiceRes.data.isOpen && store.SET_ISUSE_SPEECH(voiceRes.data.isOpen)
|
|
|
|
const res = await getIndexList()
|
|
const { data, code } = res
|
|
if (code === ERR_OK) {
|
|
if (!router.hasRoute('home')) {
|
|
//推荐卡片列表长度为0时说明不需要推荐页面 直接从导航栏列表删除
|
|
if (!data.columnList.length) {
|
|
store.sidebarList.splice(0, 1)
|
|
}
|
|
//store存储的首页卡片数据长度不等于请求的卡片数据长度时才能去重新提交到store中 防止重复提交
|
|
if (store.indexList?.columnList?.length !== data.columnList.length) {
|
|
store.SET_INDEX_LIST(data)
|
|
}
|
|
store.SET_SELECTED_MODULE(store.sidebarList[0].title)
|
|
router.addRoute(dynamicRoutes(data.columnList))
|
|
data.columnList.length &&
|
|
router.addRoute('home', {
|
|
path: 'waterfall',
|
|
name: 'Waterfall',
|
|
component: () => import(/* webpackChunkName: "waterfall" */ '@/views/Waterfall/Waterfall'),
|
|
meta: {
|
|
showMenu: false,
|
|
showMap: false
|
|
}
|
|
})
|
|
return to.fullPath
|
|
}
|
|
}
|
|
} catch (error) {
|
|
return to.fullPath
|
|
}
|
|
})
|
|
|
|
export function initRouter(app) {
|
|
app.use(router)
|
|
}
|
|
|