add.vue
11.2 KB
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
<template>
<div>
<el-dialog
class="user-unfreeze-dialog"
:title="dialog.title + '用户'"
:type="dialog.type"
:visible.sync="dialog.isVisible"
:before-close="handleClose"
>
<div class="unfreeze-form dialogScroll">
<el-form
ref="ruleForm"
:rules="formRules"
:model="userData"
label-width="100px"
class="demo-ruleForm"
>
<el-form-item label="账号:" prop="account">
<el-input v-if="dialog.title === '添加'" v-model="userData.account" minlength="3" maxlength="20" />
<span v-else>{{ userData.account }}</span>
</el-form-item>
<el-form-item label="姓名:" prop="name">
<el-input v-model="userData.name" autocomplete="off" minlength="1" maxlength="12" />
</el-form-item>
<el-form-item label="密码:">
<el-tooltip
content="用户的默认密码为123456"
class="item"
effect="dark"
placement="top-start"
>
<span>123456</span>
</el-tooltip>
<el-button
v-if="dialog.type === 'edit'"
type="primary"
@click="handleReset"
>
重置密码
</el-button>
</el-form-item>
<el-form-item label="手机号:" prop="mobile">
<el-input v-model="userData.mobile" autocomplete="off" minlength="11" maxlength="11" />
</el-form-item>
<el-form-item label="组织:" prop="orgId">
<select-tree
ref="treeSelect"
v-model="values"
popover-class="fas"
:styles="styles"
:select-params="selectParams"
:tree-params="treeParams"
:org-data="orgData"
:org-id="userData.orgId"
@getValue="getValue($event)"
/>
</el-form-item>
<el-form-item label="上级领导:" prop="superior">
<el-select v-model="userData.superior" placeholder="请选择">
<el-option
v-for="item in superiorData"
:key="item.id"
:disabled="!item.status"
:label="item.name"
:value="item.id"
/>
</el-select>
</el-form-item>
<el-form-item label="岗位:" prop="stationId">
<el-select v-model="userData.stationId" placeholder="请选择">
<el-option
v-for="item in stationData"
:key="item.id"
:disabled="!item.status"
:label="item.name"
:value="item.id"
/>
</el-select>
</el-form-item>
<el-form-item label="角色:" prop="roles">
<el-select
ref="roles"
v-model="userData.roles"
multiple
value-key="id"
:multiple-limit="limitNumber"
placeholder="请选择"
@change="handleRepel(userData.roles)"
>
<el-option
v-for="item in roleData"
:key="item.id"
:disabled="item.disabled"
:label="item.name"
:value="item.id"
/>
</el-select>
</el-form-item>
<el-form-item label="性别:" class="boxPt">
<el-radio-group v-model="userData.sex.code">
<el-radio label="M">男</el-radio>
<el-radio label="W">女</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="状态:" class="boxPt">
<el-radio-group v-model="userData.status">
<el-radio :label="true">启用</el-radio>
<el-radio :label="false">禁用</el-radio>
</el-radio-group>
</el-form-item>
</el-form>
</div>
<div class="subBox">
<el-button @click="handleClose"> 取 消 </el-button>
<el-button type="primary" :disabled="isDisable" @click="handleSubmit">
确 定
</el-button>
</div>
</el-dialog>
<!-- 启用禁用 -->
<base-dialog
ref="status"
:dialog="dialogData"
@handle-close="handleCloseStatus"
@state-submit="handleResetSubmit"
/>
<!-- end -->
</div>
</template>
<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator'
import { ElForm } from 'element-ui/types/form'
import { getUser } from '@/utils/cookies'
// 表单验证
import { validateAccounts, validateName, validatePhone } from '@/utils/validate'
// 公用组件
import UploadImage from '@/components/UploadImage/index.vue'
import SelectTree from '@/components/SelectTree/index.vue'
import BaseDialog from '@/components/BaseStatus/index.vue'
// api
import { getAllStation } from '@/api/api'
import { addUser, editUser, resetUser, getUserList } from '@/pages/users/api/index'
@Component({
name: 'UserAddDialog',
components: {
UploadImage,
SelectTree,
BaseDialog
}
})
export default class extends Vue {
// 角色验证
private validateRole = (rule: any, value: string, callback: Function) => {
if (value && value.length === 0) {
callback(new Error('请选择角色'))
} else {
callback()
}
}
@Prop() private dialog!: any
@Prop() private orgData!: []
@Prop() private roleData!: []
@Prop() private treeData!: {}
@Prop() private userData!: {}
// private firstLoad = true
private accept: string = 'image/jpeg, image/gif, image/png'
private imgFileList = []
private stationData = []
private superiorData = []
private values = ''
private userIds:any = []
private imgFileData = {
bizId: '',
bizType: 'USER_AVATAR'
}
private fileOtherData = {
bizId: '',
bizType: '',
imageUrl: '',
isSingle: true
}
private dialogData = {
isStatusVisible: false,
msg: ''
}
private formRules = {
account: [
{ validator: validateAccounts, required: true, trigger: 'blur' }
],
mobile: [{ validator: validatePhone, required: true, trigger: 'blur' }],
name: [{ validator: validateName, required: true, trigger: 'blur' }],
orgId: [{ required: true, message: '请选择组织', trigger: 'change' }],
// superior: [{ required: true, message: '请选择上级领导', trigger: 'change' }],
stationId: [{ required: true, message: '请选择岗位', trigger: 'change' }]
// roles: [{ validator: this.validateRole, required: true, trigger: 'fouse' }]
}
private styles = {
width: '300px'
}
// private test = ''
private selectParams = {
clearable: true,
placeholder: '请选择'
}
private treeParams = {
clickParent: false,
filterable: true,
data: [],
props: {
children: 'children',
label: 'name',
disabled: 'disabled',
value: 'id'
}
}
private el: any = this.$refs
private limitNumber: Number = 10
private isDisable: boolean = false
// 解决异步数据子组件获取不到数据
@Watch('userData')
getUserInfo(value: any) {
if (value.roles && value.roles.length > 0) {
this.rolesGet(value.roles, this.roleData)
}
}
@Watch('orgData', { immediate: true })
getOrg(value: any) {
this.treeParams.data = value
}
created() {
this.getStation()
this.getSuperiorData()
}
/// // 功能函数 /////
private init() {
this.clearData()
;(this.$refs.ruleForm as ElForm).resetFields()
}
// 获取岗位
private async getStation() {
const params = {
status: true
}
const { data } = await getAllStation({ status: '' })
this.stationData = data.data
}
// 获取用户列表
private async getSuperiorData() {
const params = {
status: true
}
const { data } = await getUserList({ })
this.superiorData = data.data
}
// 重置密码
handleReset() {
const ids: any[] = []
const userData: any = getUser()
const data = JSON.parse(userData)
this.userIds.push(data.id)
this.dialogData.isStatusVisible = true
this.dialogData.msg = '确认重置该用户的密码吗?'
}
// 重置密码提交
private async handleResetSubmit() {
const { data } = await resetUser({ ids: this.userIds })
if (data.isSuccess === true) {
this.dialogData.isStatusVisible = false
this.$message.success('操作成功')
} else {
this.$message.error(data.msg)
}
}
// 添加用户
private async addSave() {
const { data } = await addUser(this.userData)
if (data.isSuccess && data.code === 0) {
this.dialog.isVisible = false
this.$message({
message: '操作成功',
type: 'success'
})
this.$emit('getList')
this.clearData()
;(this.$refs.ruleForm as ElForm).resetFields()
}
}
// 编辑用户
private async updateSave() {
delete (this.userData as any).createTime
delete (this.userData as any).updateTime
const { data } = await editUser(this.userData)
if (data.isSuccess) {
this.dialog.isVisible = false
this.$message({
message: '操作成功',
type: 'success'
})
this.$emit('getList')
this.clearData()
;(this.userData as any).id = ''
;(this.$refs.ruleForm as ElForm).resetFields()
}
}
// 表单提交
private handleSubmit() {
this.isDisable = true
setTimeout(() => {
this.isDisable = false // 点击一次时隔两秒后才能再次点击
}, 2000)
;(this.$refs.ruleForm as ElForm).validate(async(valid: boolean) => {
if (valid) {
if (this.dialog.type === 'add') {
await this.addSave()
} else {
await this.updateSave()
}
}
})
}
// 表单取消
handleClose() {
const userData = this.userData as any
(this.$refs.ruleForm as ElForm).resetFields()
userData.id = ''
this.clearData()
this.dialog.isVisible = false
}
// 关闭重置弹层
handleCloseStatus() {
this.dialogData.isStatusVisible = false
}
// 获取组织树id
getValue(value: string) {
(this.userData as any).orgId = value
}
// 触发角色
handleRepel(val:any) {
this.rolesGet(val, this.roleData)
}
// 触发角色下拉,禁用互斥角色
rolesGet(objData:any, roleData:Array<string>) {
let objArr = {} as any
let dataArr = [] as any
roleData.forEach((obj:any) => {
objData.forEach((item:any) => {
if (item === obj.id) {
objArr = {
id: obj.id,
name: obj.name,
repel: obj.repel
}
dataArr.push(objArr)
}
})
})
roleData.forEach((obj:any) => {
if (!obj.status) {
obj.disabled = true
} else {
obj.disabled = false
}
dataArr.forEach((item:any) => {
if (item.id === obj.repel || item.repel === obj.id) {
obj.disabled = true
}
})
})
}
clearData() {
(this.userData as any).sex.code = 'M'
;(this.userData as any).status = true
;(this.userData as any).avatar = ''
}
mounted() {
this.fileOtherData.bizType = this.imgFileData.bizType
this.fileOtherData.isSingle = true
}
}
</script>
<style lang="scss">
</style>