update 路由隐藏

This commit is contained in:
g7hoo 2022-08-25 13:53:28 +08:00
parent 9df5f3b336
commit 38afe6cbb9
4 changed files with 41 additions and 13 deletions

View File

@ -29,7 +29,7 @@ export const pageRoutes = [
meta: { title: '登录' }, meta: { title: '登录' },
beforeEnter(to, from, next) { beforeEnter(to, from, next) {
if (Cookies.get('token')) { if (Cookies.get('token')) {
Vue.prototype.$message({ message:'已经登录过了', type: 'error' }) Vue.prototype.$message({ message: '已经登录过了', type: 'error' })
next(false) next(false)
} else { } else {
next() next()
@ -91,6 +91,7 @@ router.beforeEach((to, from, next) => {
if (window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] || fnCurrentRouteIsPageRoute(to, pageRoutes)) { if (window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] || fnCurrentRouteIsPageRoute(to, pageRoutes)) {
return next() return next()
} }
// 获取字典列表, 添加并全局变量保存 // 获取字典列表, 添加并全局变量保存
http.get(http.adornUrl('/sys/dict/type/all')).then(({ data: res }) => { http.get(http.adornUrl('/sys/dict/type/all')).then(({ data: res }) => {
if (res.code !== 0) { if (res.code !== 0) {
@ -98,20 +99,15 @@ router.beforeEach((to, from, next) => {
} }
window.SITE_CONFIG['dictList'] = res.data window.SITE_CONFIG['dictList'] = res.data
}).catch((err) => { }).catch((err) => {
// console.log("catch /sys/dict/type/all", err)
}) })
// 获取菜单列表, 添加并全局变量保存 // 获取菜单列表, 添加并全局变量保存
http.get(http.adornUrl('/sys/menu/nav')).then(({ data: res }) => { http.get(http.adornUrl('/sys/menu/nav')).then(({ data: res }) => {
if (res.code !== 0) { /** axios 的拦截器已经拦截出错情况,此处只考虑正确情况即可 */
Vue.prototype.$message.error(res.msg)
return next({ name: 'login' })
}
console.log('menulist: ', res.data)
window.SITE_CONFIG['menuList'] = res.data window.SITE_CONFIG['menuList'] = res.data
fnAddDynamicMenuRoutes(window.SITE_CONFIG['menuList']) fnAddDynamicMenuRoutes(window.SITE_CONFIG['menuList'])
next({ ...to, replace: true }) next({ ...to, replace: true })
}).catch((err) => { }).catch((err) => {
next({ name: 'login' }) // Vue.prototype.$message.error(err)
}) })
}) })
@ -140,6 +136,7 @@ function fnCurrentRouteIsPageRoute(route, pageRoutes = []) {
*/ */
function fnAddDynamicMenuRoutes(menuList = [], routes = []) { function fnAddDynamicMenuRoutes(menuList = [], routes = []) {
var temp = [] var temp = []
for (var i = 0; i < menuList.length; i++) { for (var i = 0; i < menuList.length; i++) {
if (menuList[i].children && menuList[i].children.length >= 1) { if (menuList[i].children && menuList[i].children.length >= 1) {
temp = temp.concat(menuList[i].children) temp = temp.concat(menuList[i].children)
@ -168,9 +165,11 @@ function fnAddDynamicMenuRoutes(menuList = [], routes = []) {
} }
routes.push(route) routes.push(route)
} }
if (temp.length >= 1) { if (temp.length >= 1) {
return fnAddDynamicMenuRoutes(temp, routes) return fnAddDynamicMenuRoutes(temp, routes)
} }
// 添加路由 // 添加路由
router.addRoutes([ router.addRoutes([
{ {

View File

@ -48,6 +48,7 @@ export function clearLoginInfo () {
store.commit('resetStore') store.commit('resetStore')
Cookies.remove('token') Cookies.remove('token')
window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = false window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = false
window.SITE_CONFIG['dynamicMenuRoutes'] = []
} }
/** /**

View File

@ -8,7 +8,7 @@
:collapseTransition="false" :collapseTransition="false"
class="aui-sidebar__menu" class="aui-sidebar__menu"
> >
<sub-menu v-for="menu in $store.state.sidebarMenuList" :key="menu.id" :menu="menu" /> <sub-menu v-for="menu in unhiddenMenuList" :key="menu.id" :menu="menu" />
</el-menu> </el-menu>
</div> </div>
</aside> </aside>
@ -18,13 +18,40 @@
import SubMenu from './main-sidebar-sub-menu' import SubMenu from './main-sidebar-sub-menu'
export default { export default {
data() { data() {
return {} return {
unhiddenMenuList: []
}
}, },
components: { components: {
SubMenu SubMenu
}, },
created() { mounted() {
this.$store.state.sidebarMenuList = window.SITE_CONFIG['menuList'] // this.$store.state.sidebarMenuList = window.SITE_CONFIG['menuList']
this.$nextTick(() => {
this.unhiddenMenuList = this.getUnhiddenRoutesListFrom(window.SITE_CONFIG['menuList'])
/** 本地保存一份store保存一份感觉 store 都不需要保存... */
this.$store.state.sidebarMenuList = this.unhiddenMenuList
})
},
methods: {
getUnhiddenRoutesListFrom(fullList) {
const list = []
if (fullList.length) {
fullList.forEach(menu => {
if (menu.sort !== 99) {
/** 前后端约定,路由排序值为 99 时不在前端的侧边栏展示该路由 */
const newRouteItem = JSON.parse(JSON.stringify(menu))
if (menu.children) {
newRouteItem.children = this.getUnhiddenRoutesListFrom(menu.children)
}
list.push(newRouteItem)
} else {
console.log(menu.name, '是应该被隐藏的路由')
}
})
}
return list
}
} }
} }
</script> </script>

View File

@ -105,7 +105,8 @@ export default {
confirmPassword: [{ validator: validateConfirmPassword, trigger: 'blur' }], confirmPassword: [{ validator: validateConfirmPassword, trigger: 'blur' }],
realName: [{ required: true, message: this.$t('validate.required'), trigger: 'blur' }], realName: [{ required: true, message: this.$t('validate.required'), trigger: 'blur' }],
email: [{ validator: validateEmail, trigger: 'blur' }], email: [{ validator: validateEmail, trigger: 'blur' }],
mobile: [{ validator: validateMobile, trigger: 'blur' }] mobile: [{ validator: validateMobile, trigger: 'blur' }],
roleIdList: [{ required: true, message: '至少选择一个角色', trigger: 'change' }]
} }
} }
}, },