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