224 lines
6.5 KiB
Vue
Raw Normal View History

2022-11-15 17:44:28 +08:00
<template>
2022-11-17 20:20:09 +08:00
<ContentWrap>
2022-11-17 22:55:09 +08:00
<!-- 列表 -->
<vxe-grid ref="xGrid" v-bind="gridOptions" show-overflow class="xtable-scrollbar">
<template #toolbar_buttons>
2022-11-17 20:20:09 +08:00
<!-- 操作新增 -->
<XButton
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
v-hasPermi="['system:dept:create']"
@click="handleCreate()"
/>
2022-11-17 22:55:09 +08:00
<XButton title="展开所有" @click="xGrid?.setAllTreeExpand(true)" />
<XButton title="关闭所有" @click="xGrid?.clearTreeExpand()" />
2022-11-15 17:44:28 +08:00
</template>
2022-11-17 23:05:39 +08:00
<template #leaderUserId_default="{ row }">
<span>{{ userNicknameFormat(row) }}</span>
</template>
2022-11-17 22:55:09 +08:00
<template #actionbtns_default="{ row }">
<!-- 操作修改 -->
<XTextButton
preIcon="ep:edit"
:title="t('action.edit')"
v-hasPermi="['system:dept:update']"
@click="handleUpdate(row.id)"
/>
<!-- 操作删除 -->
<XTextButton
preIcon="ep:delete"
:title="t('action.del')"
v-hasPermi="['system:dept:delete']"
@click="handleDelete(row.id)"
/>
</template>
</vxe-grid>
2022-11-17 20:20:09 +08:00
</ContentWrap>
<!-- 添加或修改菜单对话框 -->
<XModal id="deptModel" v-model="dialogVisible" :title="dialogTitle">
<!-- 对话框(添加 / 修改) -->
<!-- 操作工具栏 -->
2022-11-17 22:55:09 +08:00
<Form ref="formRef" :schema="allSchemas.formSchema" :rules="rules">
2022-11-17 20:20:09 +08:00
<template #parentId>
<el-tree-select
2022-11-15 17:44:28 +08:00
node-key="id"
2022-11-17 20:20:09 +08:00
v-model="deptParentId"
2022-11-15 17:44:28 +08:00
:props="defaultProps"
2022-11-17 20:20:09 +08:00
:data="deptOptions"
:default-expanded-keys="[100]"
check-strictly
2022-11-15 17:44:28 +08:00
/>
2022-11-17 20:20:09 +08:00
</template>
<template #leaderUserId>
<el-select v-model="leaderUserId">
<el-option
v-for="item in userOption"
:key="parseInt(item.id)"
:label="item.nickname"
:value="parseInt(item.id)"
/>
</el-select>
</template>
</Form>
<template #footer>
<!-- 按钮保存 -->
<XButton
v-if="['create', 'update'].includes(actionType)"
type="primary"
:loading="actionLoading"
@click="submitForm()"
:title="t('action.save')"
/>
<!-- 按钮关闭 -->
<XButton :loading="actionLoading" @click="dialogVisible = false" :title="t('dialog.close')" />
</template>
</XModal>
2022-11-15 17:44:28 +08:00
</template>
2022-07-18 19:06:37 +08:00
<script setup lang="ts">
2022-11-17 20:20:09 +08:00
import { nextTick, onMounted, reactive, ref, unref } from 'vue'
2022-07-18 19:06:37 +08:00
import { useI18n } from '@/hooks/web/useI18n'
2022-07-21 17:42:25 +08:00
import { useMessage } from '@/hooks/web/useMessage'
2022-11-17 22:55:09 +08:00
import { VxeGridInstance } from 'vxe-table'
import { ElSelect, ElTreeSelect, ElOption } from 'element-plus'
import { allSchemas } from './dept.data'
2022-11-17 20:20:09 +08:00
import * as DeptApi from '@/api/system/dept'
2022-08-01 21:05:42 +08:00
import { getListSimpleUsersApi } from '@/api/system/user'
2022-11-17 20:20:09 +08:00
import { required } from '@/utils/formRules.js'
import { handleTree } from '@/utils/tree'
import { FormExpose } from '@/components/Form'
2022-11-17 22:55:09 +08:00
import { useVxeGrid } from '@/hooks/web/useVxeGrid'
2022-07-18 19:06:37 +08:00
2022-11-17 20:20:09 +08:00
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
// 列表相关的变量
2022-11-17 22:55:09 +08:00
const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
const treeConfig = {
transform: true,
rowField: 'id',
parentField: 'parentId',
expandAll: true
}
const { gridOptions, getList, deleteData } = useVxeGrid<DeptApi.DeptVO>({
allSchemas: allSchemas,
treeConfig: treeConfig,
getListApi: DeptApi.getDeptPageApi,
deleteApi: DeptApi.deleteDeptApi
})
2022-11-17 20:20:09 +08:00
// 弹窗相关的变量
const dialogVisible = ref(false) // 是否显示弹出层
const dialogTitle = ref('edit') // 弹出层标题
const actionType = ref('') // 操作按钮的类型
const actionLoading = ref(false) // 遮罩层
const deptParentId = ref(0) // 上级ID
const leaderUserId = ref()
const formRef = ref<FormExpose>() // 表单 Ref
const deptOptions = ref() // 树形结构
const userOption = ref()
// 新增和修改的表单校验
const rules = reactive({
name: [required],
sort: [required],
path: [required],
status: [required]
})
// 下拉框[上级]的配置项目
2022-07-18 19:06:37 +08:00
const defaultProps = {
2022-11-17 20:20:09 +08:00
checkStrictly: true,
2022-07-18 19:06:37 +08:00
children: 'children',
label: 'name',
value: 'id'
}
2022-11-17 20:20:09 +08:00
// 获取下拉框[上级]的数据
2022-07-18 19:06:37 +08:00
const getTree = async () => {
2022-11-17 20:36:02 +08:00
deptOptions.value = []
2022-08-01 21:05:42 +08:00
const res = await DeptApi.listSimpleDeptApi()
2022-11-17 20:36:02 +08:00
let dept: Tree = { id: 0, name: '顶级部门', children: [] }
dept.children = handleTree(res)
deptOptions.value.push(dept)
2022-07-18 19:06:37 +08:00
}
2022-08-01 21:05:42 +08:00
const getUserList = async () => {
const res = await getListSimpleUsersApi()
userOption.value = res
}
2022-11-17 20:20:09 +08:00
// ========== 新增/修改 ==========
// 设置标题
const setDialogTile = (type: string) => {
dialogTitle.value = t('action.' + type)
actionType.value = type
dialogVisible.value = true
}
// 新增操作
const handleCreate = async () => {
deptParentId.value = 0
leaderUserId.value = null
setDialogTile('create')
2022-07-18 19:06:37 +08:00
}
2022-11-17 20:20:09 +08:00
// 修改操作
const handleUpdate = async (rowId: number) => {
setDialogTile('update')
// 设置数据
const res = await DeptApi.getDeptApi(rowId)
2022-11-17 20:36:02 +08:00
deptParentId.value = res.parentId
2022-08-01 21:05:42 +08:00
leaderUserId.value = res.leaderUserId
2022-11-17 20:20:09 +08:00
await nextTick()
2022-07-18 19:06:37 +08:00
unref(formRef)?.setValues(res)
}
2022-11-17 20:20:09 +08:00
// 提交新增/修改的表单
2022-07-18 19:06:37 +08:00
const submitForm = async () => {
2022-10-17 11:24:22 +08:00
const elForm = unref(formRef)?.getElFormRef()
if (!elForm) return
elForm.validate(async (valid) => {
if (valid) {
2022-11-17 20:20:09 +08:00
actionLoading.value = true
2022-10-17 11:24:22 +08:00
// 提交请求
try {
2022-11-17 20:20:09 +08:00
const data = unref(formRef)?.formModel as DeptApi.DeptVO
2022-10-17 11:24:22 +08:00
data.parentId = deptParentId.value
data.leaderUserId = leaderUserId.value
2022-11-17 20:20:09 +08:00
if (dialogTitle.value.startsWith('新增')) {
2022-10-17 11:24:22 +08:00
await DeptApi.createDeptApi(data)
2022-11-17 22:55:09 +08:00
message.success(t('common.createSuccess'))
2022-11-17 20:20:09 +08:00
} else if (dialogTitle.value.startsWith('修改')) {
2022-10-17 11:24:22 +08:00
await DeptApi.updateDeptApi(data)
2022-11-17 22:55:09 +08:00
message.success(t('common.updateSuccess'))
2022-10-17 11:24:22 +08:00
}
dialogVisible.value = false
} finally {
2022-11-17 20:20:09 +08:00
actionLoading.value = false
2022-11-17 22:55:09 +08:00
await getList(xGrid)
2022-10-17 11:24:22 +08:00
}
2022-07-18 19:06:37 +08:00
}
2022-10-17 11:24:22 +08:00
})
2022-07-18 19:06:37 +08:00
}
2022-11-17 20:20:09 +08:00
// 删除操作
const handleDelete = async (rowId: number) => {
2022-11-17 22:55:09 +08:00
await deleteData(xGrid, rowId)
2022-11-17 20:20:09 +08:00
}
2022-11-17 23:05:39 +08:00
const userNicknameFormat = (row) => {
if (!row || !row.leaderUserId || row.leaderUserId == null) {
return '未设置'
}
for (const user of userOption.value) {
if (row.leaderUserId === user.id) {
return user.nickname
}
}
return '未知【' + row.leaderUserId + '】'
}
2022-11-17 20:20:09 +08:00
// ========== 初始化 ==========
2022-07-18 19:06:37 +08:00
onMounted(async () => {
2022-08-01 21:05:42 +08:00
await getUserList()
2022-11-17 23:05:39 +08:00
await getTree()
2022-07-18 19:06:37 +08:00
})
</script>