1
1
<template >
2
- <!-- 添加或修改参数配置对话框 -->
3
- <Dialog :title =" title" :modelValue =" showDialog" width =" 600px" append-to-body @close =" closeDialog" >
4
- <el-form ref =" formRef" :model =" formData" :rules =" rules" label-width =" 80px" >
2
+ <Dialog :title =" modelTitle" v-model =" modelVisible" >
3
+ <el-form
4
+ ref =" formRef"
5
+ :model =" formData"
6
+ :rules =" formRules"
7
+ label-width =" 80px"
8
+ v-loading =" formLoading"
9
+ >
5
10
<el-row >
6
11
<el-col :span =" 12" >
7
12
<el-form-item label =" 用户昵称" prop =" nickname" >
13
18
<el-tree-select
14
19
node-key =" id"
15
20
v-model =" formData.deptId"
16
- :data =" deptOptions "
21
+ :data =" deptList "
17
22
:props =" defaultProps"
18
23
check-strictly
19
24
placeholder =" 请选择归属部门"
67
72
<el-form-item label =" 岗位" >
68
73
<el-select v-model =" formData.postIds" multiple placeholder =" 请选择" >
69
74
<el-option
70
- v-for =" item in postOptions "
75
+ v-for =" item in postList "
71
76
:key =" item.id"
72
77
:label =" item.name"
73
78
:value =" item.id"
85
90
</el-row >
86
91
</el-form >
87
92
<template #footer >
88
- <div class =" dialog-footer" >
89
- <el-button type =" primary" @click =" submitForm" >确 定</el-button >
90
- <el-button @click =" cancel" >取 消</el-button >
91
- </div >
93
+ <el-button @click =" submitForm" type =" primary" :disabled =" formLoading" >确 定</el-button >
94
+ <el-button @click =" modelVisible = false" >取 消</el-button >
92
95
</template >
93
96
</Dialog >
94
97
</template >
95
98
<script lang="ts" setup>
96
- import { PostVO } from ' @/api/system/post'
97
- import * as PostApi from ' @/api/system/post'
98
- import { createUserApi , getUserApi , updateUserApi } from ' @/api/system/user'
99
- import * as DeptApi from ' @/api/system/dept'
100
-
101
99
import { DICT_TYPE , getIntDictOptions } from ' @/utils/dict'
100
+ import { CommonStatusEnum } from ' @/utils/constants'
102
101
import { defaultProps , handleTree } from ' @/utils/tree'
103
- import { ElForm , FormItemRule } from ' element-plus'
104
- import { Arrayable } from ' element-plus/es/utils'
105
-
106
- type Form = InstanceType <typeof ElForm >
107
-
108
- const emits = defineEmits ([' success' ])
109
-
102
+ import * as PostApi from ' @/api/system/post'
103
+ import * as DeptApi from ' @/api/system/dept'
104
+ import * as UserApi from ' @/api/system/user'
110
105
const { t } = useI18n () // 国际化
111
106
const message = useMessage () // 消息弹窗
112
107
113
- const showDialog = ref (false )
114
- // 弹出层标题
115
- const title = computed (() => {
116
- return formData .value ?.id ? ' 修改用户' : ' 添加用户'
117
- })
118
-
119
- const deptOptions = ref <Tree []>([]) // 树形结构
120
- const getTree = async () => {
121
- const res = await DeptApi .getSimpleDeptList ()
122
- deptOptions .value = []
123
- deptOptions .value .push (... handleTree (res ))
124
- }
125
- // 获取岗位列表
126
- const postOptions = ref <PostVO []>([]) // 岗位列表
127
- const getPostOptions = async () => {
128
- const res = (await PostApi .getSimplePostList ()) as PostVO []
129
- postOptions .value .push (... res )
130
- }
131
-
132
- // 表单初始化参数
133
- const initParams = {
108
+ const modelVisible = ref (false ) // 弹窗的是否展示
109
+ const modelTitle = ref (' ' ) // 弹窗的标题
110
+ const formLoading = ref (false ) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
111
+ const formType = ref (' ' ) // 表单的类型:create - 新增;update - 修改
112
+ const formData = ref ({
134
113
nickname: ' ' ,
135
114
deptId: ' ' ,
136
115
mobile: ' ' ,
137
116
email: ' ' ,
138
117
id: undefined ,
139
118
username: ' ' ,
140
119
password: ' ' ,
141
- sex: 1 ,
120
+ sex: undefined ,
142
121
postIds: [],
143
122
remark: ' ' ,
144
- status: ' 0 ' ,
123
+ status: CommonStatusEnum . ENABLE ,
145
124
roleIds: []
146
- }
147
-
148
- // 校验规则
149
- const rules = {
125
+ })
126
+ const formRules = reactive ({
150
127
username: [{ required: true , message: ' 用户名称不能为空' , trigger: ' blur' }],
151
128
nickname: [{ required: true , message: ' 用户昵称不能为空' , trigger: ' blur' }],
152
129
password: [{ required: true , message: ' 用户密码不能为空' , trigger: ' blur' }],
@@ -164,74 +141,75 @@ const rules = {
164
141
trigger: ' blur'
165
142
}
166
143
]
167
- } as Partial <Record <string , Arrayable <FormItemRule >>>
168
- const formRef = ref <Form | null >()
169
- const formData = ref <Recordable >({ ... initParams })
144
+ })
145
+ const formRef = ref () // 表单 Ref
146
+ const deptList = ref <Tree []>([]) // 树形结构
147
+ const postList = ref ([]) // 岗位列表
170
148
171
- const resetForm = () => {
172
- let form = formRef ?.value
173
- if (! form ) return
174
- formData .value = { ... initParams }
175
- form && (form as Form ).resetFields ()
176
- }
177
- const closeDialog = () => {
178
- showDialog .value = false
179
- }
180
- // 操作成功
181
- const operateOk = () => {
182
- emits (' success' , true )
183
- closeDialog ()
184
- }
185
- const submitForm = () => {
186
- let form = formRef .value as Form
187
- form .validate (async (valid ) => {
188
- let data = formData .value
189
- if (valid ) {
190
- try {
191
- if (data ?.id !== undefined ) {
192
- await updateUserApi (data )
193
- message .success (t (' common.updateSuccess' ))
194
- operateOk ()
195
- } else {
196
- await createUserApi (data )
197
- message .success (t (' common.createSuccess' ))
198
- operateOk ()
199
- }
200
- } catch (err ) {
201
- console .error (err )
202
- }
149
+ /** 打开弹窗 */
150
+ const open = async (type : string , id ? : number ) => {
151
+ modelVisible .value = true
152
+ modelTitle .value = t (' action.' + type )
153
+ formType .value = type
154
+ resetForm ()
155
+ // 修改时,设置数据
156
+ if (id ) {
157
+ formLoading .value = true
158
+ try {
159
+ formData .value = await UserApi .getUser (id )
160
+ } finally {
161
+ formLoading .value = false
203
162
}
204
- })
205
- }
206
- /* 取消 */
207
- const cancel = () => {
208
- closeDialog ()
163
+ }
164
+ // 加载部门树
165
+ deptList . value = handleTree ( await DeptApi . getSimpleDeptList ())
166
+ // 加载岗位列表
167
+ postList . value = await PostApi . getSimplePostList ()
209
168
}
169
+ defineExpose ({ open }) // 提供 open 方法,用于打开弹窗
210
170
211
- /* 打开弹框 */
212
- const open = (type : string , id ? : number ) => {
213
- console .log (type , id )
214
- resetForm ()
215
- getTree () // 部门树
216
- if (row && row .id ) {
217
- const id = row .id
218
- getUserApi (id ).then ((response ) => {
219
- formData .value = response
220
- })
221
- } else {
222
- formData .value = { ... initParams }
171
+ /** 提交表单 */
172
+ const emit = defineEmits ([' success' ]) // 定义 success 事件,用于操作成功后的回调
173
+ const submitForm = async () => {
174
+ // 校验表单
175
+ if (! formRef ) return
176
+ const valid = await formRef .value .validate ()
177
+ if (! valid ) return
178
+ // 提交请求
179
+ formLoading .value = true
180
+ try {
181
+ const data = formData .value as unknown as UserApi .UserVO
182
+ if (formType .value === ' create' ) {
183
+ await UserApi .createUser (data )
184
+ message .success (t (' common.createSuccess' ))
185
+ } else {
186
+ await UserApi .updateUser (data )
187
+ message .success (t (' common.updateSuccess' ))
188
+ }
189
+ modelVisible .value = false
190
+ // 发送操作成功的事件
191
+ emit (' success' )
192
+ } finally {
193
+ formLoading .value = false
223
194
}
224
-
225
- showDialog .value = true
226
195
}
227
196
228
- // ========== 初始化 ==========
229
- onMounted (async () => {
230
- await getPostOptions ()
231
- })
232
-
233
- defineExpose ({
234
- resetForm ,
235
- open
236
- })
197
+ /** 重置表单 */
198
+ const resetForm = () => {
199
+ formData .value = {
200
+ nickname: ' ' ,
201
+ deptId: ' ' ,
202
+ mobile: ' ' ,
203
+ email: ' ' ,
204
+ id: undefined ,
205
+ username: ' ' ,
206
+ password: ' ' ,
207
+ sex: undefined ,
208
+ postIds: [],
209
+ remark: ' ' ,
210
+ status: CommonStatusEnum .ENABLE ,
211
+ roleIds: []
212
+ }
213
+ formRef .value ?.resetFields ()
214
+ }
237
215
</script >
0 commit comments