提交 | 用户 | 时间
|
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-form-item label="上级部门" prop="parentId"> |
|
11 |
<el-tree-select |
|
12 |
v-model="formData.parentId" |
|
13 |
:data="deptTree" |
|
14 |
:props="defaultProps" |
|
15 |
check-strictly |
|
16 |
default-expand-all |
|
17 |
placeholder="请选择上级部门" |
|
18 |
value-key="deptId" |
|
19 |
/> |
|
20 |
</el-form-item> |
|
21 |
<el-form-item label="部门名称" prop="name"> |
|
22 |
<el-input v-model="formData.name" placeholder="请输入部门名称" /> |
|
23 |
</el-form-item> |
|
24 |
<el-form-item label="显示排序" prop="sort"> |
|
25 |
<el-input-number v-model="formData.sort" :min="0" controls-position="right" /> |
|
26 |
</el-form-item> |
|
27 |
<el-form-item label="负责人" prop="leaderUserId"> |
|
28 |
<el-select v-model="formData.leaderUserId" clearable placeholder="请输入负责人"> |
|
29 |
<el-option |
|
30 |
v-for="item in userList" |
|
31 |
:key="item.id" |
|
32 |
:label="item.nickname" |
|
33 |
:value="item.id" |
|
34 |
/> |
|
35 |
</el-select> |
|
36 |
</el-form-item> |
|
37 |
<el-form-item label="联系电话" prop="phone"> |
|
38 |
<el-input v-model="formData.phone" maxlength="11" placeholder="请输入联系电话" /> |
|
39 |
</el-form-item> |
|
40 |
<el-form-item label="邮箱" prop="email"> |
|
41 |
<el-input v-model="formData.email" maxlength="50" placeholder="请输入邮箱" /> |
|
42 |
</el-form-item> |
|
43 |
<el-form-item label="状态" prop="status"> |
|
44 |
<el-select v-model="formData.status" clearable placeholder="请选择状态"> |
|
45 |
<el-option |
|
46 |
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" |
|
47 |
:key="dict.value" |
|
48 |
:label="dict.label" |
|
49 |
:value="dict.value" |
|
50 |
/> |
|
51 |
</el-select> |
|
52 |
</el-form-item> |
|
53 |
</el-form> |
|
54 |
<template #footer> |
|
55 |
<el-button type="primary" @click="submitForm">确 定</el-button> |
|
56 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
57 |
</template> |
|
58 |
</Dialog> |
|
59 |
</template> |
|
60 |
<script lang="ts" setup> |
|
61 |
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict' |
|
62 |
import { defaultProps, handleTree } from '@/utils/tree' |
|
63 |
import * as DeptApi from '@/api/system/dept' |
|
64 |
import * as UserApi from '@/api/system/user' |
|
65 |
import { CommonStatusEnum } from '@/utils/constants' |
|
66 |
import { FormRules } from 'element-plus' |
|
67 |
|
|
68 |
defineOptions({ name: 'SystemDeptForm' }) |
|
69 |
|
|
70 |
const { t } = useI18n() // 国际化 |
|
71 |
const message = useMessage() // 消息弹窗 |
|
72 |
|
|
73 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
74 |
const dialogTitle = ref('') // 弹窗的标题 |
|
75 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
76 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
77 |
const formData = ref({ |
|
78 |
id: undefined, |
|
79 |
title: '', |
|
80 |
parentId: undefined, |
|
81 |
name: undefined, |
|
82 |
sort: undefined, |
|
83 |
leaderUserId: undefined, |
|
84 |
phone: undefined, |
|
85 |
email: undefined, |
|
86 |
status: CommonStatusEnum.ENABLE |
|
87 |
}) |
|
88 |
const formRules = reactive<FormRules>({ |
|
89 |
parentId: [{ required: true, message: '上级部门不能为空', trigger: 'blur' }], |
|
90 |
name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }], |
|
91 |
sort: [{ required: true, message: '显示排序不能为空', trigger: 'blur' }], |
|
92 |
email: [{ type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }], |
|
93 |
phone: [ |
|
94 |
{ pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '请输入正确的手机号码', trigger: 'blur' } |
|
95 |
], |
|
96 |
status: [{ required: true, message: '状态不能为空', trigger: 'blur' }] |
|
97 |
}) |
|
98 |
const formRef = ref() // 表单 Ref |
|
99 |
const deptTree = ref() // 树形结构 |
|
100 |
const userList = ref<UserApi.UserVO[]>([]) // 用户列表 |
|
101 |
|
|
102 |
/** 打开弹窗 */ |
|
103 |
const open = async (type: string, id?: number) => { |
|
104 |
dialogVisible.value = true |
|
105 |
dialogTitle.value = t('action.' + type) |
|
106 |
formType.value = type |
|
107 |
resetForm() |
|
108 |
// 修改时,设置数据 |
|
109 |
if (id) { |
|
110 |
formLoading.value = true |
|
111 |
try { |
|
112 |
formData.value = await DeptApi.getDept(id) |
|
113 |
} finally { |
|
114 |
formLoading.value = false |
|
115 |
} |
|
116 |
} |
|
117 |
// 获得用户列表 |
|
118 |
userList.value = await UserApi.getSimpleUserList() |
|
119 |
// 获得部门树 |
|
120 |
await getTree() |
|
121 |
} |
|
122 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
123 |
|
|
124 |
/** 提交表单 */ |
|
125 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
126 |
const submitForm = async () => { |
|
127 |
// 校验表单 |
|
128 |
if (!formRef) return |
|
129 |
const valid = await formRef.value.validate() |
|
130 |
if (!valid) return |
|
131 |
// 提交请求 |
|
132 |
formLoading.value = true |
|
133 |
try { |
|
134 |
const data = formData.value as unknown as DeptApi.DeptVO |
|
135 |
if (formType.value === 'create') { |
|
136 |
await DeptApi.createDept(data) |
|
137 |
message.success(t('common.createSuccess')) |
|
138 |
} else { |
|
139 |
await DeptApi.updateDept(data) |
|
140 |
message.success(t('common.updateSuccess')) |
|
141 |
} |
|
142 |
dialogVisible.value = false |
|
143 |
// 发送操作成功的事件 |
|
144 |
emit('success') |
|
145 |
} finally { |
|
146 |
formLoading.value = false |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
/** 重置表单 */ |
|
151 |
const resetForm = () => { |
|
152 |
formData.value = { |
|
153 |
id: undefined, |
|
154 |
title: '', |
|
155 |
parentId: undefined, |
|
156 |
name: undefined, |
|
157 |
sort: undefined, |
|
158 |
leaderUserId: undefined, |
|
159 |
phone: undefined, |
|
160 |
email: undefined, |
|
161 |
status: CommonStatusEnum.ENABLE |
|
162 |
} |
|
163 |
formRef.value?.resetFields() |
|
164 |
} |
|
165 |
|
|
166 |
/** 获得部门树 */ |
|
167 |
const getTree = async () => { |
|
168 |
deptTree.value = [] |
|
169 |
const data = await DeptApi.getSimpleDeptList() |
|
170 |
let dept: Tree = { id: 0, name: '顶级部门', children: [] } |
|
171 |
dept.children = handleTree(data) |
|
172 |
deptTree.value.push(dept) |
|
173 |
} |
|
174 |
</script> |