# 系统实现
# 前端
# 树形组件
# 单选树形
<el-tree
:default-checked-keys="[postForm.parentUuid]"
class="filter-tree"
check-strictly
show-checkbox
highlight-current
:data="postForm.organTree"
node-key="organUuid"
:props="defaultProps"
@check="handleCheckChange"
default-expand-all
:filter-node-method="filterNode"
ref="tree"
/>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
组织选择,不带选中框
# 多选树形
<el-tree
:data="data"
show-checkbox
node-key="id"
:default-expanded-keys="[2, 3]"
:default-checked-keys="[5]">
</el-tree>
<script>
export default {
data() {
return {
data: [{
id: 1,
label: '一级 2',
children: [{
id: 3,
label: '二级 2-1',
children: [{
id: 4,
label: '三级 3-1-1'
}, {
id: 5,
label: '三级 3-1-2',
disabled: true
}]
}, {
id: 2,
label: '二级 2-2',
disabled: true,
children: [{
id: 6,
label: '三级 3-2-1'
}, {
id: 7,
label: '三级 3-2-2',
disabled: true
}]
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
};
}
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
菜单选择,带选择框,自动选中父级菜单
# 自动选中父级前端实现
导出:去层级化的所有选中项
选中一个,就按按菜单id 给角色添加菜单,然后刷新菜单树
# 如何处理带选择框树形控件的选择联动
- 被选中的所有父级会选中
- 所有子树选中
# 编辑/添加表单
编辑和添加表单内容十分相似,区别是呈现方式
故此 抽象出5个xxxDetail 组件,通过isEdit 参数判定是添加还是编辑
用户 编辑/添加 | 组织 编辑/添加 | 角色 编辑/添加 | 岗位 编辑/添加 | 资源 编辑/添加 |
---|---|---|---|---|
userDetail | oragDetail | roleDetail | dutyDetail | resDetail |
# userDetail代码示例
<template>
<el-form ref="postForm" :label-position="labelPosition" :model="postForm" :rules="rules">
<sticky >
<div :class="'sub-navbar '+postForm.status">
<el-button v-if="!isEdit" @click="showGuide">显示帮助</el-button>
<el-button
v-loading="loading"
@click="submitForm"
type="success"
style="margin-left: 10px;"
>
{{isEdit? '编辑用户':'新增用户'}}
</el-button>
</div>
</sticky>
<div class="detail-container">
<el-row>
<!-- <warning/> -->
<el-col :span="24">
<el-form-item prop="真实姓名" >
<Mdinput v-model="postForm.niceName" :maxlength="100" name="niceName" required>
<slot>真实姓名</slot>
</Mdinput>
</el-form-item>
<el-row :gutter="20">
<el-col :span="12">
<el-form-item label="用户名" prop="username" label-width="80px">
<el-input prefix-icon="el-icon-user" v-model="postForm.username" placeholder="用户名" clearable></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item prop="password" label="密码: " label-width="80px">
<el-input show-password prefix-icon="el-icon-key" v-model="postForm.password" placeholder="密码"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :span="12">
<el-form-item label="邮箱:" prop="email" label-width="80px">
<el-input prefix-icon="el-icon-message" v-model="postForm.email" placeholder="邮箱" clearable></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item prop="mobile" label="手机号: " label-width="80px">
<el-input prefix-icon="el-icon-mobile-phone" v-model="postForm.mobile" placeholder="手机号" clearable></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :span="12">
<el-form-item label="是否可用:" prop="status" label-width="100px">
<el-switch v-model="postForm.status" ></el-switch>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item prop="remark" label="备注: " label-width="80px">
<el-input type="textarea" v-model="postForm.remark" placeholder="备注" ></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :span="12">
<el-form-item label="过滤" label-width="80px">
<el-input
placeholder="输入关键字进行过滤"
v-model="filterText">
</el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item prop="organUuid" label="组织: " label-width="80px">
<el-input disabled v-model="postForm.organName" placeholder="组织"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="20" >
<el-col :span="12">
<el-card class="box-card">
<div slot="header" class="clearfix">
<span>组织树:</span>
<!-- <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button> -->
</div>
<el-form-item >
<div
v-if="postForm.organTree && postForm.organTree.length > 0"
class="contents-wrapper"
>
<el-tree
:default-checked-keys="defaultSelected"
class="filter-tree"
check-strictly
show-checkbox
highlight-current
:data="postForm.organTree"
node-key="organUuid"
:props="defaultProps"
default-expand-all
@check="handleCheckChange"
:filter-node-method="filterNode"
ref="tree"
/>
</div>
<span v-else>无</span>
</el-form-item>
</el-card>
</el-col>
<el-col :span="12">
<el-form-item prop="dutyUuid" label="岗位: " label-width="80px">
<el-select
v-model="postForm.dutyUuid"
placeholder="岗位"
clearable
class="filter-item"
>
<el-option
:key="item.dutyid"
v-for=" item in dutyList"
:value="item.dutyid"
:label="item.name"
>
</el-option>
</el-select>
</el-form-item>
</el-col>
</el-row>
</el-col>
</el-row>
</div>
</el-form>
</template>
<script>
import { createUser ,getUser,updateUser} from '../../../api/ptuser.js'
import { getDutyByOrganId} from '../../../api/duty.js'
import { queryOrgan} from '../../../api/organ.js'
import { listRoleAll} from '../../../api/role.js'
import Mdinput from '../../../components/MDinput/index.vue'
import EbookUpload from '@/components/EbookUpload/index.vue'
import Sticky from '@/components/Sticky'
import Warning from './Warning.vue'
//to do 将baseUrl 改成全局
const baseUrl='http://localhost:8082'
const fields={
name:'用户名',
}
const defaultForm={
userUuid:'',
niceName:'',//真实姓名
username:'', // 用户名
password:'',//密码
email:'',//邮箱
mobile:'',//手机号
status:'',//是否可用
remark:'',//备注
organUuid:'',// 组织id
organName:'',// 组织名称
dutyUuid:'',// 岗位id
organTree: [] // 组织树
}
export default{
components:{
Sticky,
Warning,
EbookUpload,
Mdinput
},
props:{
isEdit:Boolean
},
watch: {
filterText(val) {
this.$refs.tree.filter(val);
},
organUuid(){
if(this.organUuid){
this.getDutyList()
}
}
},
computed:{
organUuid(){
return this.postForm.organUuid
}
},
data() {
const validateRequire = (rule,value,callback)=>{
if (value==null||value.length===0){
callback(new Error(fields[rule.field] + '必须填写'))
}else{
callback()
}
}
return{
firstTime:true,
defaultSelected:[],
dutyList:[],
filterText:'',
loading:false,
defaultProps: {
children: 'children',
label: 'organName'
},
postForm:{
userUuid:'',
niceName:'',//真实姓名
username:'', // 用户名
password:'',//密码
email:'',//邮箱
mobile:'',//手机号
status:'',//是否可用
remark:'',//备注
organUuid:'',// 组织id
organName:'',// 组织名称
dutyUuid:'',// 岗位id
organTree: [] // 组织树
},
labelPosition:'left',
rules:{
name:[{validator:validateRequire}],
mobile:
[
{ required: true, message: "请输入手机号", trigger: "blur" },
// 这个只能验证手机号
// { pattern:/^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/, message: "请输入合法手机号", trigger: "blur" }
{ pattern:/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/, message: "请输入合法手机号/电话号", trigger: "blur" }
],
idNumber:
[
{ required: true, message: "请输入身份证号", trigger: "blur" },
{ pattern:/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/, message: "请输入合法身份证号", trigger: "blur"}
],
email:
[
{ required: true, message: "请输入邮箱", trigger: "blur" },
{ pattern:/^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g, message: "请输入正确的邮箱", trigger: "blur"}
],
password:
[
{required:true,message: "请输入密码",trigger:"blur"},
{pattern:/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/,message:"密码须包含数字、字母两种元素,且密码位数为6-16位"}
]
}
}
},
created( ){
// this. getDutyList()
this.getOrganTree()
if(this.isEdit){
console.log('编辑')
const userId=this.$route.params.userId
this.postForm.userUuid=userId
console.log(userId)
this. getUserDate(userId)
}
},
methods:{
getDutyList(){
if(!this.firstTime){
this.postForm.dutyUuid=''
}else{
this.firstTime=false
}
let organId=this.postForm.organUuid
getDutyByOrganId(organId).then(res=>{
this.dutyList=res.data
})
},
getUserDate(userId){
getUser(userId).then(res=>{
this.setData(res.data)
const selectedOrgan=res.data.organUuid
if(selectedOrgan!=null){
this.defaultSelected.push(selectedOrgan)
}
})
},
getOrganTree(){
queryOrgan().then(res=>{
const organTree=[res.data]
const postForm={
...this.postForm,
organTree
}
this.postForm=Object.assign({}, this.postForm, postForm)
})
},
filterNode(value, data) {
if (!value) return true;
return data.organName.indexOf(value) !== -1;
},
showGuide (){
console.log("guide")
},
submitForm (){
const onSuccess=(res)=>{
const {msg}=res;
this.$notify({
title: '操作成功',
message: msg,
type:'success',
duration:2000
})
this.getOrganTree()
this.loading=false
}
console.log(this.postForm)
if(!this.loading){
this.loading=true
this.$refs.postForm.validate((valid,fields) => {
console.log(valid)
if(valid){
console.log("vaild success")
if(this.isEdit){
updateUser(this.postForm).then(res =>{
onSuccess(res)
})
}else{
console.log(this.postForm)
createUser(this.postForm).then(res =>{
onSuccess(res)
})
}
this.loading=false
}else{
const message=fields[Object.keys(fields)[0]][0].message
console.log(message)
this.$message({
message:message,
type:'error'
})
this.loading=false
}
})
}
},
setData (data){
const{
userUuid,
niceName ,//真实姓名
username , // 用户名
password ,//密码
email ,//邮箱
mobile ,//手机号
status ,//是否可用
remark ,//备注
organUuid ,// 组织id
dutyUuid ,// 岗位id
}=data
this.postForm={
...this.postForm,
userUuid,
niceName ,//真实姓名
username , // 用户名
password ,//密码
email ,//邮箱
mobile ,//手机号
status ,//是否可用
remark ,//备注
organUuid ,// 组织id
dutyUuid ,// 岗位id
}
},
toDefault(){
this.postForm=Object.assign({},defaultForm)
this.fileList=[]
},
handleCheckChange (data, checked, indeterminate) {
if (checked) {
let arr = [data.organUuid];
this.postForm.organUuid=arr[0]
this.postForm.organName=data.organName
this.$refs.tree.setCheckedKeys(arr);
}else{
this.$refs.tree.setCheckedKeys([]);
this.postForm.organUuid=''
this.postForm.organName=''
}
},
}
}
</script>
<style lang="scss">
@import "~@/styles/mixin.scss";
.detail-container{
padding: 40px 50px 20px;
.preview-img {
width: 200px;
height: 270px;
}
.contents-wrapper {
margin: 10px 60px;
}
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# 后端
# 菜单的查询
由于组织树和菜单树十分类似,这里以菜单
# mybatis 递归查询
举例selectPtResForestByParentUUIDAndRoleUuid
<resultMap id="BaseResultMapWithChildrenAndRoleUuid" type="com.simo.meitan.model.PtMenu">
<id column="menu_uuid" jdbcType="VARCHAR" property="menuUuid" />
<result column="parent_uuid" jdbcType="VARCHAR" property="parentUuid" />
<result column="sub_count" jdbcType="INTEGER" property="subCount" />
<result column="type" jdbcType="INTEGER" property="type" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="component" jdbcType="VARCHAR" property="component" />
<result column="menu_sort" jdbcType="INTEGER" property="menuSort" />
<result column="path" jdbcType="VARCHAR" property="path" />
<result column="i_frame" jdbcType="BIT" property="iFrame" />
<result column="hidden" jdbcType="BIT" property="hidden" />
<result column="permission" jdbcType="VARCHAR" property="permission" />
<result column="create_by" jdbcType="VARCHAR" property="createBy" />
<result column="update_by" jdbcType="VARCHAR" property="updateBy" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
<result column="title" jdbcType="VARCHAR" property="title"/>
<result column="icon" jdbcType="VARCHAR" property="icon"/>
<association property="meta" javaType="com.simo.meitan.model.MenuMeta">
<result column="title" jdbcType="VARCHAR" property="title"/>
<result column="icon" jdbcType="VARCHAR" property="icon"/>
</association>
<collection property="children" ofType="com.simo.meitan.model.PtMenu" select="com.simo.meitan.mapper.PtMenuMapper.selectPtResForestByParentUUIDAndRoleUuid"
column="{parentUuid=menu_uuid,roleUuid=role_uuid}">
</collection>
</resultMap>
<select id="selectPtResForestByParentUUIDAndRoleUuid" resultMap="BaseResultMapWithChildrenAndRoleUuid">
select
pt_menu.menu_uuid, parent_uuid, sub_count, `type`, title, `name`, component, menu_sort, icon,
`path`, i_frame, `cache`, hidden, permission, create_by, update_by, create_time,
update_time,map.role_uuid
from pt_roles_menus as map,pt_menu
where parent_uuid=#{parentUuid,jdbcType=VARCHAR}
and map.role_uuid=#{roleUuid,jdbcType=VARCHAR}
and map.menu_uuid=pt_menu.menu_uuid
and pt_menu.type=0
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 按角色查询已选中的菜单返回前端
# 解决方案一(二次查询)
- 先查询完整的菜单树
- 按角色查询已选项目(列表)
- 遍历菜单树,每个菜单节点都在角色菜单里查询,已此来确定是否选中
# 解决方案二 ✔️
- 数据库查询完整的菜单树
- 查询角色绑定的菜单列表
我们项目采用方案二,数据结构大概如下
resTress 树形目标菜单
selectedRes 已选中菜单阶id (一一对应)
menuTress: [{id: 1, path: null, component: null, name: "所有", iconCls: null,…}]
selectedMenus: [7, 13, 15]
1
2
3
2
3
# 菜单节点的删除
我们要明白,删除是比较危险的操作,特别是对于在数据的树型数据而言
# 对于删除操作的限制
- 非页节点不可删除
为了代码逻辑清晰,我们把菜单的删除操作,写成一个数据库存储过程
# 删除菜单数据库存储过程
CREATE DEFINER=`simo`@`%` PROCEDURE `deleteMenu`(IN did char(255),OUT recode int)
BEGIN
DECLARE ecount int;
DECLARE puuid char(255);
DECLARE ccount int;
set recode=0;
SELECT COUNT(*) INTO ccount from pt_menu WHERE parent_uuid=did;
IF ccount=0 THEN
DELETE FROM pt_menu WHERE menu_uuid=did;
SELECT ROW_COUNT() INTO recode;
END IF;
END
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12