Skip to content

Commit 398313c

Browse files
committed
feat(*): permissions
1 parent 1ec1ea9 commit 398313c

12 files changed

Lines changed: 71 additions & 101 deletions

File tree

src/components/layout/User.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,14 @@ export default {
270270
// }
271271
return this.$axios({
272272
method: 'put',
273-
url: '/cms/user/avatar',
273+
url: '/cms/user',
274274
data: {
275275
avatar: res[0].path,
276276
},
277277
})
278278
.then(putRes => {
279279
// eslint-disable-line
280-
if (putRes.error_code === 0) {
280+
if (putRes.error_code < 100) {
281281
this.$message({
282282
type: 'success',
283283
message: '更新头像成功',
@@ -325,11 +325,11 @@ export default {
325325
// 触发重新获取用户信息
326326
return User.getInformation()
327327
}
328-
this.nickname = user.nickname
329328
})
330329
.then(res => {
331330
// eslint-disable-line
332331
this.setUserAndState(res)
332+
this.nickname = res.nickname
333333
})
334334
}
335335
}

src/lin/directives/authorize.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import Vue from 'vue'
22
import store from '@/store'
33

4-
function isAllowed(_auth, user, auths) {
4+
function isAllowed(permission, user, permissions) {
55
if (user.isSuper) {
66
return true
77
}
8-
if (typeof _auth === 'string') {
9-
return auths.includes(_auth)
8+
if (typeof permission === 'string') {
9+
return permissions.includes(permission)
1010
}
11-
if (_auth instanceof Array) {
12-
return _auth.some(auth => auths.indexOf(auth) >= 0)
11+
if (permission instanceof Array) {
12+
return permission.some(auth => permissions.indexOf(auth) >= 0)
1313
}
1414
return false
1515
}
@@ -26,7 +26,7 @@ Vue.directive('auth', {
2626
} else {
2727
auth = binding.value
2828
}
29-
const isAllow = isAllowed(auth, store.state.user || {}, store.state.auths)
29+
const isAllow = isAllowed(auth, store.state.user || {}, store.state.permissions)
3030
const element = el
3131
if (!isAllow && auth) {
3232
if (type) {

src/lin/mixin/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ const globalMixin = {
1313
isAllowed(_auth) {
1414
/* eslint-disable no-restricted-syntax */
1515
/* eslint-disable guard-for-in */
16-
const { auths } = this.user
17-
for (const mod of auths) {
16+
const { permissions } = this.user
17+
for (const mod of permissions) {
1818
for (const item in mod) {
1919
for (const a of mod[item]) {
2020
// console.log(a.auth)
21-
if (a.auth === _auth) {
21+
if (a.permission === _auth) {
2222
return true
2323
}
2424
// console.log(a.module)

src/lin/models/user.js

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import { post, get, put } from '@/lin/plugins/axios'
22
import { saveTokens } from '../utils/token'
33

4-
// const SUPER_VALUE = 2
5-
const ACTIVE_VALUE = 1
6-
74
export default class User {
8-
// 当前用户是否在激活状态
9-
isActive = null
5+
// 昵称
6+
nickname = null
107

118
// 邮箱
129
email = null
1310

14-
// 权限分组id
15-
groupId = null
11+
avatar = null // 头像
12+
13+
// 所属分组信息
14+
groups = []
1615

1716
// 用户名
1817
username = null
@@ -21,24 +20,16 @@ export default class User {
2120
isSuper = null
2221

2322
// 拥有的权限
24-
auths = []
25-
26-
// 昵称
27-
nickname = null
28-
29-
// 分组名称
30-
groupName = null
23+
permissions = []
3124

32-
constructor(active, email, groupId, username, admin, avatar, permissions, nickname, groupName) {
33-
this.isActive = active === ACTIVE_VALUE
25+
constructor(nickname, username, admin, groups, permissions, email, avatar) {
3426
this.email = email
35-
this.groupId = groupId
27+
this.groups = groups
3628
this.username = username
3729
this.avatar = avatar
3830
this.isSuper = admin
39-
this.auths = permissions || []
31+
this.permissions = permissions || []
4032
this.nickname = nickname
41-
this.groupName = groupName
4233
}
4334

4435
/**
@@ -68,35 +59,15 @@ export default class User {
6859
*/
6960
static async getInformation() {
7061
const info = await get('cms/user/information')
71-
return new User(
72-
info.active,
73-
info.email,
74-
info.group_id,
75-
info.username,
76-
info.admin,
77-
info.avatar,
78-
info.auths,
79-
info.nickname,
80-
info.group_name,
81-
)
62+
return new User(info.nickname, info.username, info.admin, info.groups, info.permissions, info.email, info.avatar)
8263
}
8364

8465
/**
8566
* 获取当前用户信息和所拥有的权限
8667
*/
87-
static async getAuths() {
68+
static async getPermissions() {
8869
const info = await get('cms/user/permissions')
89-
return new User(
90-
info.active,
91-
info.email,
92-
info.group_id,
93-
info.username,
94-
info.admin,
95-
info.avatar,
96-
info.auths,
97-
info.nickname,
98-
info.group_name,
99-
)
70+
return new User(info.nickname, info.username, info.admin, info.groups, info.permissions, info.email, info.avatar)
10071
}
10172

10273
/**

src/store/getters.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ function IterationDelateMenuChildren(arr) {
4040
return arr
4141
}
4242

43-
function permissionShaking(stageConfig, auths, currentUser) {
43+
function permissionShaking(stageConfig, permissions, currentUser) {
4444
// eslint-disable-line
4545
const shookConfig = stageConfig.filter(route => {
46-
if (Util.hasPermission(auths, route, currentUser)) {
46+
if (Util.hasPermission(permissions, route, currentUser)) {
4747
if (route.children && route.children.length) {
48-
route.children = permissionShaking(route.children, auths, currentUser) // eslint-disable-line
48+
route.children = permissionShaking(route.children, permissions, currentUser) // eslint-disable-line
4949
}
5050
return true
5151
}
@@ -56,9 +56,9 @@ function permissionShaking(stageConfig, auths, currentUser) {
5656

5757
// 获取有权限的舞台配置
5858
export const authStageConfig = state => {
59-
const { stageConfig, auths, user } = state // eslint-disable-line
59+
const { stageConfig, permissions, user } = state // eslint-disable-line
6060
const tempStageConfig = Util.deepClone(stageConfig)
61-
const shookConfig = permissionShaking(tempStageConfig, auths, user)
61+
const shookConfig = permissionShaking(tempStageConfig, permissions, user)
6262

6363
// 设置舞台缓存
6464
const list = {}
@@ -159,7 +159,7 @@ export const getStageByRoute = () => {
159159

160160
export const stageList = () => stageMap
161161

162-
export const auths = state => state.auths
162+
export const permissions = state => state.permissions
163163

164164
export const getStageInfo = state => {
165165
const { stageConfig } = state

src/store/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const vuexLocal = new VuexPersistence({
1414
// eslint-disable-line
1515
logined: stateData.logined,
1616
user: stateData.user,
17-
auths: stateData.auths,
17+
permissions: stateData.permissions,
1818
}),
1919
})
2020

src/store/mutation-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export const REMOVE_UNREAD_MESSAGE = 'REMOVE_UNREAD_MESSAGE'
1010

1111
export const ADD_UNREAD_MESSAGE = 'ADD_UNREAD_MESSAGE'
1212

13-
export const SET_USER_AUTHS = 'SET_USER_AUTHS'
13+
export const SET_USER_PERMISSIONS = 'SET_USER_PERMISSIONS'
1414

1515
export const SET_REFERSH_OPTION = 'SET_REFERSH_OPTION'

src/store/mutations.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export default {
1313

1414
[types.SET_USER](state, payload) {
1515
state.user = payload
16+
// state.user.avatar = payload.avatar ? payload.avatar : ''
17+
// state.user.email = payload.email ? payload.email : ''
18+
// state.user.isSuper = payload.isSuper ? payload.isSuper : false
19+
// state.user.nickname = payload.nickname ? payload.nickname : '佚名'
20+
// state.user.groups = payload.groups ? payload.groups : []
21+
// state.user.permissions = payload.permissions ? payload.permissions : []
22+
// state.user.username = payload.username ? payload.username : ''
1623
},
1724

1825
[types.ADD_READED_MESSAGE](state, payload) {
@@ -31,17 +38,17 @@ export default {
3138
unreadMessages.splice(index, 1)
3239
},
3340

34-
[types.SET_USER_AUTHS](state, auths) {
35-
const _auths = []
36-
for (let i = 0; i < auths.length; i++) {
37-
for (const key in auths[i]) {
38-
// console.log(i, state.user.auths[i][key])
39-
for (let j = 0; j < auths[i][key].length; j++) {
40-
_auths.push(auths[i][key][j].auth)
41+
[types.SET_USER_PERMISSIONS](state, permissions) {
42+
const _permissions = []
43+
for (let i = 0; i < permissions.length; i++) {
44+
for (const key in permissions[i]) {
45+
// console.log(i, state.user.permissions[i][key])
46+
for (let j = 0; j < permissions[i][key].length; j++) {
47+
_permissions.push(permissions[i][key][j].permission)
4148
}
4249
}
4350
}
44-
state.auths = _auths
51+
state.permissions = _permissions
4552
},
4653

4754
[types.SET_REFERSH_OPTION](state, option) {

src/store/state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default {
1010
// 推送消息
1111
readedMessages: [],
1212
unreadMessages: [],
13-
auths: [], // 每个用户的所有权限
13+
permissions: [], // 每个用户的所有权限
1414

1515
// 舞台配置
1616
stageConfig,

src/views/admin/user/UserList.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export default {
107107
this.loading = true
108108
res = await Admin.getAdminUsers({ group_id: this.group_id, count: this.pageCount, page: currentPage }) // eslint-disable-line
109109
this.loading = false
110-
this.tableData = [...res.items]
110+
this.tableData = this.shuffleList(res.items)
111111
this.total_nums = res.total
112112
} catch (e) {
113113
this.loading = false
@@ -244,11 +244,23 @@ export default {
244244
})
245245
}
246246
},
247+
shuffleList(users) {
248+
const list = []
249+
users.forEach(element => {
250+
const groups = []
251+
element.groups.forEach(item => {
252+
groups.push(item.name)
253+
})
254+
element.groupNames = groups.join(',')
255+
list.push(element)
256+
})
257+
return list
258+
},
247259
},
248260
async created() {
249261
await this.getAdminUsers()
250262
this.getAllGroups()
251-
this.tableColumn = [{ prop: 'username', label: '名称' }, { prop: 'group_name', label: '所属分组' }] // 设置表头信息
263+
this.tableColumn = [{ prop: 'username', label: '名称' }, { prop: 'groupNames', label: '所属分组' }] // 设置表头信息
252264
this.operate = [
253265
{ name: '编辑', func: 'handleEdit', type: 'primary' },
254266
{ name: '删除', func: 'handleDelete', type: 'danger' },

0 commit comments

Comments
 (0)