Skip to content

Commit 9dc3659

Browse files
committed
✨ 组织架构 treeSelect 组件抽取
1 parent 2a1ca35 commit 9dc3659

File tree

4 files changed

+182
-17
lines changed

4 files changed

+182
-17
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<template>
2+
<a-tree-select
3+
:value="selectedValue"
4+
:multiple="multiple"
5+
:placeholder="placeholder"
6+
style="width: 100%"
7+
:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
8+
:tree-data="treeData"
9+
:tree-default-expand-all="treeDefaultExpandAll"
10+
:allow-clear="allowClear"
11+
:replace-fields="{
12+
title: 'name',
13+
key: 'id',
14+
value: 'id'
15+
}"
16+
@change="handleChange"
17+
/>
18+
</template>
19+
20+
<script>
21+
import { getTree } from '@/api/system/organization'
22+
23+
export default {
24+
name: 'SysOrganizationTreeSelect',
25+
props: {
26+
value: {
27+
type: [Array, Number],
28+
default: () => []
29+
},
30+
organizationTree: {
31+
type: [Array],
32+
default: () => null
33+
},
34+
multiple: {
35+
type: Boolean,
36+
default: false
37+
},
38+
allowClear: {
39+
type: Boolean,
40+
default: true
41+
},
42+
placeholder: {
43+
type: String,
44+
default: ''
45+
},
46+
treeDefaultExpandAll: {
47+
type: Boolean,
48+
default: true
49+
}
50+
},
51+
data () {
52+
return {
53+
selectedValue: [],
54+
treeData: []
55+
}
56+
},
57+
watch: {
58+
value (newVal) {
59+
this.selectedValue = newVal
60+
}
61+
},
62+
created () {
63+
if (this.organizationTree) {
64+
this.treeData = this.organizationTree
65+
} else {
66+
getTree().then(res => {
67+
this.treeData = res.data
68+
})
69+
}
70+
},
71+
methods: {
72+
handleChange (val) {
73+
if (val && val.target) {
74+
this.selectedValue = val.target.value
75+
} else {
76+
this.selectedValue = val
77+
}
78+
// v-decorator 方式的表单值联动
79+
this.$emit('change', this.selectedValue)
80+
// v-model 方式的表单值联动
81+
this.$emit('input', this.selectedValue)
82+
}
83+
}
84+
}
85+
</script>
86+
87+
<style scoped>
88+
89+
</style>

src/views/system/role/SysRoleModalForm.vue

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,31 @@
4444
/>
4545
</a-form-item>
4646

47+
<a-form-item label="数据权限">
48+
<a-select v-decorator="['scopeType', decoratorOptions.scopeType]" @change="handleScopeTypeChange">
49+
<a-select-option :value="0">全部</a-select-option>
50+
<a-select-option :value="1">个人</a-select-option>
51+
<a-select-option :value="2">本人及子级</a-select-option>
52+
<a-select-option :value="3">本级</a-select-option>
53+
<a-select-option :value="4">本级及子级</a-select-option>
54+
<a-select-option :value="5">自定义</a-select-option>
55+
</a-select>
56+
</a-form-item>
57+
58+
<a-form-item
59+
v-if="scopeType === 5"
60+
label="数据范围"
61+
:validate-status="scopeResources.validateStatus"
62+
:help="scopeResources.errorMsg"
63+
>
64+
<sys-organization-tree-select
65+
v-model="scopeResources.value"
66+
:organization-tree="organizationTree"
67+
:multiple="true"
68+
@change="validateScopeResources"
69+
/>
70+
</a-form-item>
71+
4772
<a-form-item label="备注">
4873
<a-textarea
4974
v-decorator="['note']"
@@ -58,10 +83,18 @@
5883
<script>
5984
import { PopUpFormMixin } from '@/mixins'
6085
import { addObj, putObj } from '@/api/system/role'
86+
import SysOrganizationTreeSelect from '@/views/system/organization/SysOrganizationTreeSelect'
6187
6288
export default {
6389
name: 'RoleModalForm',
90+
components: { SysOrganizationTreeSelect },
6491
mixins: [PopUpFormMixin],
92+
props: {
93+
organizationTree: {
94+
type: [Array],
95+
default: () => []
96+
},
97+
},
6598
data () {
6699
return {
67100
reqFunctions: {
@@ -88,17 +121,62 @@ export default {
88121
]
89122
},
90123
type: {
91-
initialValue: 1
124+
initialValue: 1,
125+
rules: [{ required: true, message: '请选择角色类型!' }]
126+
},
127+
scopeType: {
128+
initialValue: 1,
129+
rules: [{ required: true, message: '请选择数据权限!' }]
92130
}
131+
},
132+
133+
scopeType: 1,
134+
scopeResources: {
135+
value: [],
136+
validateStatus: null,
137+
errorMsg: null,
93138
}
94139
}
95140
},
96141
methods: {
142+
/**
143+
* 钩子函数,回显数据处理
144+
* 默认无操作,子组件可重写此方法,拿到回显数据,对回显数据做处理,或者从回显数据中取值
145+
* @param data 回显数据
146+
*/
147+
echoDataProcess (data) {
148+
this.scopeType = data.scopeType
149+
this.$nextTick(() => {
150+
this.scopeResources.value = data.scopeResources ? data.scopeResources.split(','): []
151+
})
152+
},
153+
/**
154+
* 表单提交数据处理函数
155+
* 子组件可复写此方法,在这里进行偷梁换柱
156+
* @param data 表单待提交数据
157+
* @returns {*} 真正的提交数据
158+
*/
159+
submitDataProcess (data) {
160+
data.scopeResources = this.scopeResources.value.join(',')
161+
return data
162+
},
97163
validateCode (rule, value, callback) {
98164
if (value && value.indexOf('ROLE_') !== 0) {
99165
callback('角色标识必须以ROLE_开头!')
100166
}
101167
callback()
168+
},
169+
handleScopeTypeChange (scopeType) {
170+
this.scopeType = scopeType
171+
},
172+
validateScopeResources (scopeResources) {
173+
if(scopeResources && scopeResources.length > 0){
174+
this.scopeResources.validateStatus = "success"
175+
this.scopeResources.errorMsg = null
176+
}else {
177+
this.scopeResources.validateStatus = "error"
178+
this.scopeResources.errorMsg = "请选择权限范围!"
179+
}
102180
}
103181
}
104182
}

src/views/system/role/SysRolePage.vue

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
</div>
7979

8080
<!-- 表单弹窗 -->
81-
<role-modal-form ref="formModal" @reload-page-table="reloadTable" />
81+
<role-modal-form ref="formModal" :organization-tree="organizationTree" @reload-page-table="reloadTable" />
8282

8383
<!-- 角色授权弹窗 -->
8484
<role-grant-drawer ref="roleGrantDrawer" />
@@ -94,6 +94,7 @@ import RoleModalForm from './SysRoleModalForm'
9494
import RoleGrantDrawer from './SysRoleGrantDrawer'
9595
import { TablePageMixin } from '@/mixins'
9696
import RoleUserModal from '@/views/system/role/SysRoleUserModal'
97+
import { getTree } from '@/api/system/organization'
9798
9899
export default {
99100
name: 'SysRolePage',
@@ -155,9 +156,16 @@ export default {
155156
width: 180,
156157
scopedSlots: { customRender: 'action-slot' }
157158
}
158-
]
159+
],
160+
161+
organizationTree: []
159162
}
160163
},
164+
created () {
165+
getTree().then(res => {
166+
this.organizationTree = res.data
167+
})
168+
},
161169
methods: {
162170
// 新建角色
163171
handleAdd() {

src/views/system/user/SysUserModalForm.vue

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,12 @@
3434
</a-form-item>
3535

3636
<a-form-item label="组织">
37-
<a-tree-select
37+
<sys-organization-tree-select
3838
v-decorator="['organizationId']"
39-
placeholder="请选择"
40-
style="width: 100%"
41-
:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
42-
:tree-data="organizationTree"
43-
tree-default-expand-all
44-
allow-clear
45-
:replace-fields="{
46-
title: 'name',
47-
key: 'id',
48-
value: 'id'
49-
}"
39+
:organization-tree="organizationTree"
5040
/>
5141
</a-form-item>
5242

53-
5443
<a-form-item label="状态">
5544
<a-radio-group v-decorator="['status', decoratorOptions.status]">
5645
<a-radio :value="1">开启</a-radio>
@@ -86,10 +75,11 @@ import { addObj, putObj } from '@/api/system/user'
8675
import { PopUpFormMixin } from '@/mixins'
8776
import { passEncrypt } from '@/utils/password'
8877
import SysRoleSelect from '@/views/system/role/SysRoleSelect'
78+
import SysOrganizationTreeSelect from '@/views/system/organization/SysOrganizationTreeSelect'
8979
9080
export default {
9181
name: 'SysUserModalForm',
92-
components: { SysRoleSelect },
82+
components: { SysOrganizationTreeSelect, SysRoleSelect },
9383
mixins: [PopUpFormMixin],
9484
props: {
9585
organizationTree: {

0 commit comments

Comments
 (0)