Skip to content

Commit 7abe698

Browse files
author
pixel
committed
支持不同角色自定义首页
1 parent 3d01706 commit 7abe698

File tree

6 files changed

+56
-16
lines changed

6 files changed

+56
-16
lines changed

server/model/sys_authority.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ type SysAuthority struct {
1414
DataAuthorityId []SysAuthority `json:"dataAuthorityId" gorm:"many2many:sys_data_authority_id"`
1515
Children []SysAuthority `json:"children" gorm:"-"`
1616
SysBaseMenus []SysBaseMenu `json:"menus" gorm:"many2many:sys_authority_menus;"`
17+
DefaultRouter string `json:"defaultRouter" gorm:"comment:默认菜单;default:dashboard"`
1718
}

web/src/permission.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ router.beforeEach(async(to, from, next) => {
1313
document.title = getPageTitle(to.meta.title)
1414
if (whiteList.indexOf(to.name) > -1) {
1515
if (token) {
16-
next({ path: '/layout/dashboard' })
16+
next({ name: store.getters["user/userInfo"].authority.defaultRouter })
1717
} else {
1818
next()
1919
}
2020
} else {
2121
// 不在白名单中并且已经登陆的时候
2222
if (token) {
2323
// 添加flag防止多次获取动态路由和栈溢出
24-
if (!asyncRouterFlag) {
24+
if (!asyncRouterFlag && store.getters['router/asyncRouters'].length == 0) {
2525
asyncRouterFlag++
2626
await store.dispatch('router/SetAsyncRouter')
2727
const asyncRouters = store.getters['router/asyncRouters']

web/src/store/module/router.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const router = {
2828
// 设置动态路由
2929
setAsyncRouter(state, asyncRouters) {
3030
state.asyncRouters = asyncRouters
31-
}
31+
},
3232
},
3333
actions: {
3434
// 从后台获取动态路由
@@ -73,6 +73,9 @@ export const router = {
7373
},
7474
routerList(state) {
7575
return state.routerList
76+
},
77+
defaultRouter(state) {
78+
return state.defaultRouter
7679
}
7780
}
7881
}

web/src/store/module/user.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,19 @@ export const user = {
3535
}
3636
},
3737
actions: {
38-
async LoginIn({ commit }, loginInfo) {
38+
async LoginIn({ commit, dispatch, rootGetters, getters }, loginInfo) {
3939
const res = await login(loginInfo)
4040
if (res.code == 0) {
4141
commit('setUserInfo', res.data.user)
4242
commit('setToken', res.data.token)
43+
await dispatch('router/SetAsyncRouter', {}, { root: true })
44+
const asyncRouters = rootGetters['router/asyncRouters']
45+
router.addRoutes(asyncRouters)
4346
const redirect = router.history.current.query.redirect
4447
if (redirect) {
4548
router.push({ path: redirect })
4649
} else {
47-
router.push({ path: '/layout/dashboard' })
50+
router.push({ name: getters["userInfo"].authority.defaultRouter })
4851
}
4952
return true
5053
}

web/src/view/layout/aside/historyComponent/history.vue

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="router-history">
33
<el-tabs
4-
:closable="!(historys.length==1&&this.$route.name=='dashboard')"
4+
:closable="!(historys.length==1&&this.$route.name==defaultRouter)"
55
@contextmenu.prevent.native="openContextMenu($event)"
66
@tab-click="changeTab"
77
@tab-remove="removeTab"
@@ -27,12 +27,14 @@
2727
</div>
2828
</template>
2929
<script>
30+
import {mapGetters} from "vuex"
31+
3032
export default {
3133
name: 'HistoryComponent',
3234
data() {
3335
return {
3436
historys: [],
35-
activeValue: 'dashboard',
37+
activeValue: '',
3638
contextMenuVisible: false,
3739
left: 0,
3840
top: 0,
@@ -41,7 +43,15 @@ export default {
4143
rightActive: ''
4244
}
4345
},
46+
47+
computed:{
48+
...mapGetters("user",["userInfo"]),
49+
defaultRouter(){
50+
return this.userInfo.authority.defaultRouter
51+
}
52+
},
4453
created() {
54+
this.activeValue = this.defaultRouter
4555
this.$bus.on('mobile', isMobile => {
4656
this.isMobile = isMobile
4757
})
@@ -50,9 +60,9 @@ export default {
5060
})
5161
const initHistorys = [
5262
{
53-
name: 'dashboard',
63+
name: this.defaultRouter,
5464
meta: {
55-
title: '仪表盘'
65+
title: '首页'
5666
}
5767
}
5868
]
@@ -67,7 +77,7 @@ export default {
6777
},
6878
methods: {
6979
openContextMenu(e) {
70-
if (this.historys.length == 1 && this.$route.name == 'dashboard') {
80+
if (this.historys.length == 1 && this.$route.name == this.defaultRouter) {
7181
return false
7282
}
7383
if (e.srcElement.id) {
@@ -89,13 +99,13 @@ export default {
8999
closeAll() {
90100
this.historys = [
91101
{
92-
name: 'dashboard',
102+
name: this.defaultRouter,
93103
meta: {
94-
title: '仪表盘'
104+
title: '首页'
95105
}
96106
}
97107
]
98-
this.$router.push({ name: 'dashboard' })
108+
this.$router.push({ name: this.defaultRouter })
99109
this.contextMenuVisible = false
100110
sessionStorage.setItem('historys', JSON.stringify(this.historys))
101111
},
@@ -169,7 +179,7 @@ export default {
169179
const index = this.historys.findIndex(item => item.name == tab)
170180
if (this.$route.name == tab) {
171181
if (this.historys.length == 1) {
172-
this.$router.push({ name: 'dashboard' })
182+
this.$router.push({ name: this.defaultRouter })
173183
} else {
174184
if (index < this.historys.length - 1) {
175185
this.$router.push({ name: this.historys[index + 1].name,query:this.historys[index + 1].query,params:this.historys[index + 1].params })

web/src/view/superAdmin/authority/components/menus.vue

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,28 @@
1313
node-key="ID"
1414
ref="menuTree"
1515
show-checkbox
16-
></el-tree>
16+
>
17+
<span class="custom-tree-node" slot-scope="{ node , data }">
18+
<span>{{ node.label }}</span>
19+
<span>
20+
<el-button
21+
type="text"
22+
size="mini"
23+
:style="{color:row.defaultRouter == data.name?'#E6A23C':'#85ce61'}"
24+
:disabled="!node.checked"
25+
@click="() => setDefault(data)">
26+
{{row.defaultRouter == data.name?"首页":"设为首页"}}
27+
</el-button>
28+
</span>
29+
</span>
30+
</el-tree>
1731
</div>
1832
</template>
1933
<script>
2034
import { getBaseMenuTree, getMenuAuthority, addMenuAuthority } from '@/api/menu'
21-
35+
import {
36+
updateAuthority,
37+
} from "@/api/authority";
2238
export default {
2339
name: 'Menus',
2440
props: {
@@ -43,6 +59,13 @@ export default {
4359
}
4460
},
4561
methods: {
62+
async setDefault(data){
63+
const res = await updateAuthority({authorityId: this.row.authorityId,AuthorityName: this.row.authorityName,parentId: this.row.parentId,defaultRouter:data.name})
64+
if(res.code == 0){
65+
this.$message({type:"success",message:"设置成功"})
66+
this.row.defaultRouter = res.data.authority.defaultRouter
67+
}
68+
},
4669
nodeChange(){
4770
this.needConfirm = true
4871
},

0 commit comments

Comments
 (0)