提交 | 用户 | 时间
|
314507
|
1 |
<template> |
H |
2 |
<Dialog v-model="dialogVisible" :title="dialogTitle"> |
|
3 |
<el-form |
|
4 |
ref="formRef" |
|
5 |
v-loading="formLoading" |
|
6 |
:model="formData" |
|
7 |
:rules="formRules" |
|
8 |
label-width="80px" |
|
9 |
> |
|
10 |
<el-row> |
|
11 |
<el-col :span="12"> |
|
12 |
<el-form-item label="用户昵称" prop="nickname"> |
|
13 |
<el-input v-model="formData.nickname" placeholder="请输入用户昵称" /> |
|
14 |
</el-form-item> |
|
15 |
</el-col> |
|
16 |
<el-col :span="12"> |
|
17 |
<el-form-item label="归属部门" prop="deptId"> |
|
18 |
<el-tree-select |
|
19 |
v-model="formData.deptId" |
|
20 |
:data="deptList" |
|
21 |
:props="defaultProps" |
|
22 |
check-strictly |
|
23 |
node-key="id" |
|
24 |
placeholder="请选择归属部门" |
|
25 |
/> |
|
26 |
</el-form-item> |
|
27 |
</el-col> |
|
28 |
</el-row> |
|
29 |
<el-row> |
|
30 |
<el-col :span="12"> |
|
31 |
<el-form-item label="手机号码" prop="mobile"> |
|
32 |
<el-input v-model="formData.mobile" maxlength="11" placeholder="请输入手机号码" /> |
|
33 |
</el-form-item> |
|
34 |
</el-col> |
|
35 |
<el-col :span="12"> |
|
36 |
<el-form-item label="邮箱" prop="email"> |
|
37 |
<el-input v-model="formData.email" maxlength="50" placeholder="请输入邮箱" /> |
|
38 |
</el-form-item> |
|
39 |
</el-col> |
|
40 |
</el-row> |
|
41 |
<el-row> |
|
42 |
<el-col :span="12"> |
|
43 |
<el-form-item v-if="formData.id === undefined" label="用户名称" prop="username"> |
|
44 |
<el-input v-model="formData.username" placeholder="请输入用户名称" /> |
|
45 |
</el-form-item> |
|
46 |
</el-col> |
|
47 |
<el-col :span="12"> |
|
48 |
<el-form-item v-if="formData.id === undefined" label="用户密码" prop="password"> |
|
49 |
<el-input |
|
50 |
v-model="formData.password" |
|
51 |
placeholder="请输入用户密码" |
|
52 |
show-password |
|
53 |
type="password" |
|
54 |
/> |
|
55 |
</el-form-item> |
|
56 |
</el-col> |
|
57 |
</el-row> |
|
58 |
<el-row> |
|
59 |
<el-col :span="12"> |
|
60 |
<el-form-item label="用户性别"> |
|
61 |
<el-select v-model="formData.sex" placeholder="请选择"> |
|
62 |
<el-option |
|
63 |
v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_SEX)" |
|
64 |
:key="dict.value" |
|
65 |
:label="dict.label" |
|
66 |
:value="dict.value" |
|
67 |
/> |
|
68 |
</el-select> |
|
69 |
</el-form-item> |
|
70 |
</el-col> |
|
71 |
<el-col :span="12"> |
|
72 |
<el-form-item label="岗位"> |
|
73 |
<el-select v-model="formData.postIds" multiple placeholder="请选择"> |
|
74 |
<el-option |
|
75 |
v-for="item in postList" |
|
76 |
:key="item.id" |
|
77 |
:label="item.name" |
|
78 |
:value="item.id!" |
|
79 |
/> |
|
80 |
</el-select> |
|
81 |
</el-form-item> |
|
82 |
</el-col> |
|
83 |
</el-row> |
|
84 |
<el-row> |
|
85 |
<el-col :span="24"> |
|
86 |
<el-form-item label="备注"> |
|
87 |
<el-input v-model="formData.remark" placeholder="请输入内容" type="textarea" /> |
|
88 |
</el-form-item> |
|
89 |
</el-col> |
|
90 |
</el-row> |
|
91 |
</el-form> |
|
92 |
<template #footer> |
|
93 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
94 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
95 |
</template> |
|
96 |
</Dialog> |
|
97 |
</template> |
|
98 |
<script lang="ts" setup> |
|
99 |
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict' |
|
100 |
import { CommonStatusEnum } from '@/utils/constants' |
|
101 |
import { defaultProps, handleTree } from '@/utils/tree' |
|
102 |
import * as PostApi from '@/api/system/post' |
|
103 |
import * as DeptApi from '@/api/system/dept' |
|
104 |
import * as UserApi from '@/api/system/user' |
|
105 |
import { FormRules } from 'element-plus' |
|
106 |
|
|
107 |
defineOptions({ name: 'SystemUserForm' }) |
|
108 |
|
|
109 |
const { t } = useI18n() // 国际化 |
|
110 |
const message = useMessage() // 消息弹窗 |
|
111 |
|
|
112 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
113 |
const dialogTitle = ref('') // 弹窗的标题 |
|
114 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
115 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
116 |
const formData = ref({ |
|
117 |
nickname: '', |
|
118 |
deptId: '', |
|
119 |
mobile: '', |
|
120 |
email: '', |
|
121 |
id: undefined, |
|
122 |
username: '', |
|
123 |
password: '', |
|
124 |
sex: undefined, |
|
125 |
postIds: [], |
|
126 |
remark: '', |
|
127 |
status: CommonStatusEnum.ENABLE, |
|
128 |
roleIds: [] |
|
129 |
}) |
|
130 |
const formRules = reactive<FormRules>({ |
|
131 |
username: [{ required: true, message: '用户名称不能为空', trigger: 'blur' }], |
|
132 |
nickname: [{ required: true, message: '用户昵称不能为空', trigger: 'blur' }], |
|
133 |
password: [{ required: true, message: '用户密码不能为空', trigger: 'blur' }], |
|
134 |
email: [ |
|
135 |
{ |
|
136 |
type: 'email', |
|
137 |
message: '请输入正确的邮箱地址', |
|
138 |
trigger: ['blur', 'change'] |
|
139 |
} |
|
140 |
], |
|
141 |
mobile: [ |
|
142 |
{ |
|
143 |
pattern: /^(?:(?:\+|00)86)?1(?:3[\d]|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8[\d]|9[189])\d{8}$/, |
|
144 |
message: '请输入正确的手机号码', |
|
145 |
trigger: 'blur' |
|
146 |
} |
|
147 |
] |
|
148 |
}) |
|
149 |
const formRef = ref() // 表单 Ref |
|
150 |
const deptList = ref<Tree[]>([]) // 树形结构 |
|
151 |
const postList = ref([] as PostApi.PostVO[]) // 岗位列表 |
|
152 |
|
|
153 |
/** 打开弹窗 */ |
|
154 |
const open = async (type: string, id?: number) => { |
|
155 |
dialogVisible.value = true |
|
156 |
dialogTitle.value = t('action.' + type) |
|
157 |
formType.value = type |
|
158 |
resetForm() |
|
159 |
// 修改时,设置数据 |
|
160 |
if (id) { |
|
161 |
formLoading.value = true |
|
162 |
try { |
|
163 |
formData.value = await UserApi.getUser(id) |
|
164 |
} finally { |
|
165 |
formLoading.value = false |
|
166 |
} |
|
167 |
} |
|
168 |
// 加载部门树 |
|
169 |
deptList.value = handleTree(await DeptApi.getSimpleDeptList()) |
|
170 |
// 加载岗位列表 |
|
171 |
postList.value = await PostApi.getSimplePostList() |
|
172 |
} |
|
173 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
174 |
|
|
175 |
/** 提交表单 */ |
|
176 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
177 |
const submitForm = async () => { |
|
178 |
// 校验表单 |
|
179 |
if (!formRef) return |
|
180 |
const valid = await formRef.value.validate() |
|
181 |
if (!valid) return |
|
182 |
// 提交请求 |
|
183 |
formLoading.value = true |
|
184 |
try { |
|
185 |
const data = formData.value as unknown as UserApi.UserVO |
|
186 |
if (formType.value === 'create') { |
|
187 |
await UserApi.createUser(data) |
|
188 |
message.success(t('common.createSuccess')) |
|
189 |
} else { |
|
190 |
await UserApi.updateUser(data) |
|
191 |
message.success(t('common.updateSuccess')) |
|
192 |
} |
|
193 |
dialogVisible.value = false |
|
194 |
// 发送操作成功的事件 |
|
195 |
emit('success') |
|
196 |
} finally { |
|
197 |
formLoading.value = false |
|
198 |
} |
|
199 |
} |
|
200 |
|
|
201 |
/** 重置表单 */ |
|
202 |
const resetForm = () => { |
|
203 |
formData.value = { |
|
204 |
nickname: '', |
|
205 |
deptId: '', |
|
206 |
mobile: '', |
|
207 |
email: '', |
|
208 |
id: undefined, |
|
209 |
username: '', |
|
210 |
password: '', |
|
211 |
sex: undefined, |
|
212 |
postIds: [], |
|
213 |
remark: '', |
|
214 |
status: CommonStatusEnum.ENABLE, |
|
215 |
roleIds: [] |
|
216 |
} |
|
217 |
formRef.value?.resetFields() |
|
218 |
} |
|
219 |
</script> |