refactor(dept/index.vue): 部门TypeScript类型完善
This commit is contained in:
@@ -1,100 +1,95 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="app-container">
|
<div class="app-container">
|
||||||
|
<el-form ref="queryFormRef" :model="queryParams" :inline="true">
|
||||||
<el-form
|
|
||||||
:model="queryParams"
|
|
||||||
ref="queryFormRef"
|
|
||||||
:inline="true"
|
|
||||||
>
|
|
||||||
<el-form-item>
|
<el-form-item>
|
||||||
<el-button type="success" :icon="Plus" @click="handleAdd">新增</el-button>
|
<el-button type="success" :icon="Plus" @click="handleAdd"
|
||||||
<el-button type="danger" :icon="Delete" :disabled="single" @click="handleDelete">删除
|
>新增</el-button
|
||||||
|
>
|
||||||
|
<el-button
|
||||||
|
type="danger"
|
||||||
|
:icon="Delete"
|
||||||
|
:disabled="single"
|
||||||
|
@click="handleDelete"
|
||||||
|
>删除
|
||||||
</el-button>
|
</el-button>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
|
||||||
<el-form-item prop="name">
|
<el-form-item prop="name">
|
||||||
<el-input
|
<el-input
|
||||||
v-model="queryParams.name"
|
v-model="queryParams.name"
|
||||||
placeholder="请输入部门名称"
|
placeholder="请输入部门名称"
|
||||||
@keyup.enter="handleQuery"
|
@keyup.enter="handleQuery"
|
||||||
/>
|
/>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
|
||||||
<el-form-item prop="status">
|
<el-form-item prop="status">
|
||||||
<el-select
|
<el-select
|
||||||
v-model="queryParams.status"
|
v-model="queryParams.status"
|
||||||
placeholder="部门状态"
|
placeholder="部门状态"
|
||||||
clearable
|
clearable
|
||||||
>
|
>
|
||||||
<el-option :value="1" label="正常"/>
|
<el-option :value="1" label="正常" />
|
||||||
<el-option :value="0" label="禁用"/>
|
<el-option :value="0" label="禁用" />
|
||||||
</el-select>
|
</el-select>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item>
|
<el-form-item>
|
||||||
<el-button
|
<el-button
|
||||||
class="filter-item"
|
class="filter-item"
|
||||||
type="primary"
|
type="primary"
|
||||||
:icon="Search"
|
:icon="Search"
|
||||||
@click="handleQuery"
|
@click="handleQuery"
|
||||||
>
|
>
|
||||||
搜索
|
搜索
|
||||||
</el-button>
|
</el-button>
|
||||||
<el-button
|
<el-button :icon="Refresh" @click="resetQuery"> 重置 </el-button>
|
||||||
:icon="Refresh"
|
|
||||||
@click="resetQuery"
|
|
||||||
>
|
|
||||||
重置
|
|
||||||
</el-button>
|
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-form>
|
</el-form>
|
||||||
|
|
||||||
<el-table
|
<el-table
|
||||||
v-loading="loading"
|
v-loading="loading"
|
||||||
:data="deptList"
|
:data="deptList"
|
||||||
row-key="id"
|
row-key="id"
|
||||||
default-expand-all
|
default-expand-all
|
||||||
:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
|
:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
|
||||||
@selection-change="handleSelectionChange"
|
@selection-change="handleSelectionChange"
|
||||||
>
|
>
|
||||||
<el-table-column type="selection" width="55" align="center"/>
|
<el-table-column type="selection" width="55" align="center" />
|
||||||
<el-table-column prop="name" label="部门名称"/>
|
<el-table-column prop="name" label="部门名称" />
|
||||||
<el-table-column prop="status" label="状态" width="100">
|
<el-table-column prop="status" label="状态" width="100">
|
||||||
<template #default="scope">
|
<template #default="scope">
|
||||||
<el-tag v-if="scope.row.status==1" type="success">正常</el-tag>
|
<el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
|
||||||
<el-tag v-else type="info">禁用</el-tag>
|
<el-tag v-else type="info">禁用</el-tag>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
|
|
||||||
<el-table-column prop="sort" label="显示排序" width="200"/>
|
<el-table-column prop="sort" label="显示排序" width="200" />
|
||||||
|
|
||||||
<el-table-column
|
<el-table-column label="操作" align="center" width="150">
|
||||||
label="操作"
|
|
||||||
align="center" width="150"
|
|
||||||
>
|
|
||||||
<template #default="scope">
|
<template #default="scope">
|
||||||
<el-button
|
<el-button
|
||||||
type="primary"
|
type="primary"
|
||||||
:icon="Edit"
|
:icon="Edit"
|
||||||
circle
|
circle
|
||||||
plain
|
plain
|
||||||
@click.stop="handleUpdate(scope.row)"
|
@click.stop="handleUpdate(scope.row)"
|
||||||
>
|
>
|
||||||
</el-button>
|
</el-button>
|
||||||
<el-button
|
<el-button
|
||||||
type="success"
|
type="success"
|
||||||
:icon="Plus"
|
:icon="Plus"
|
||||||
circle
|
circle
|
||||||
plain
|
plain
|
||||||
@click.stop="handleAdd(scope.row)"
|
@click.stop="handleAdd(scope.row)"
|
||||||
>
|
>
|
||||||
</el-button>
|
</el-button>
|
||||||
|
|
||||||
<el-button
|
<el-button
|
||||||
type="danger"
|
type="danger"
|
||||||
:icon="Delete"
|
:icon="Delete"
|
||||||
circle
|
circle
|
||||||
plain
|
plain
|
||||||
@click.stop="handleDelete(scope.row)">
|
@click.stop="handleDelete(scope.row)"
|
||||||
|
>
|
||||||
</el-button>
|
</el-button>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
@@ -102,32 +97,34 @@
|
|||||||
|
|
||||||
<!-- 添加或修改部门对话框 -->
|
<!-- 添加或修改部门对话框 -->
|
||||||
<el-dialog
|
<el-dialog
|
||||||
:title="dialog.title"
|
:title="dialog.title"
|
||||||
v-model="dialog.visible"
|
v-model="dialog.visible"
|
||||||
width="600px"
|
width="600px"
|
||||||
@closed="cancel"
|
@closed="cancel"
|
||||||
>
|
>
|
||||||
<el-form
|
<el-form
|
||||||
ref="dataFormRef"
|
ref="dataFormRef"
|
||||||
:model="formData"
|
:model="formData"
|
||||||
:rules="rules"
|
:rules="rules"
|
||||||
label-width="80px"
|
label-width="80px"
|
||||||
>
|
>
|
||||||
<el-form-item
|
<el-form-item label="上级部门" prop="parentId">
|
||||||
label="上级部门"
|
|
||||||
prop="parentId"
|
|
||||||
>
|
|
||||||
<tree-select
|
<tree-select
|
||||||
:options="deptOptions"
|
:options="deptOptions"
|
||||||
placeholder="选择上级部门"
|
placeholder="选择上级部门"
|
||||||
v-model="formData.parentId"
|
v-model="formData.parentId"
|
||||||
/>
|
/>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="部门名称" prop="name">
|
<el-form-item label="部门名称" prop="name">
|
||||||
<el-input v-model="formData.name" placeholder="请输入部门名称"/>
|
<el-input v-model="formData.name" placeholder="请输入部门名称" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="显示排序" prop="sort">
|
<el-form-item label="显示排序" prop="sort">
|
||||||
<el-input-number v-model="formData.sort" controls-position="right" style="width: 100px" :min="0"/>
|
<el-input-number
|
||||||
|
v-model="formData.sort"
|
||||||
|
controls-position="right"
|
||||||
|
style="width: 100px"
|
||||||
|
:min="0"
|
||||||
|
/>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="部门状态">
|
<el-form-item label="部门状态">
|
||||||
<el-radio-group v-model="formData.status">
|
<el-radio-group v-model="formData.status">
|
||||||
@@ -139,14 +136,8 @@
|
|||||||
|
|
||||||
<template #footer>
|
<template #footer>
|
||||||
<div class="dialog-footer">
|
<div class="dialog-footer">
|
||||||
<el-button
|
<el-button type="primary" @click="submitForm"> 确 定 </el-button>
|
||||||
type="primary"
|
<el-button @click="cancel"> 取 消 </el-button>
|
||||||
@click="submitForm">
|
|
||||||
确 定
|
|
||||||
</el-button>
|
|
||||||
<el-button @click="cancel">
|
|
||||||
取 消
|
|
||||||
</el-button>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</el-dialog>
|
</el-dialog>
|
||||||
@@ -155,170 +146,173 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
// Vue依赖
|
// Vue依赖
|
||||||
import {onMounted, reactive, unref, ref, toRefs} from 'vue'
|
import { onMounted, reactive, unref, ref, toRefs } from "vue";
|
||||||
|
|
||||||
// API依赖
|
// API依赖
|
||||||
import {listDeptTable, getDeptDetail, deleteDept, updateDept, addDept, listDeptSelect} from '@/api/system/dept'
|
import {
|
||||||
|
getDeptFormDetail,
|
||||||
|
deleteDept,
|
||||||
|
updateDept,
|
||||||
|
addDept,
|
||||||
|
listSelectDepartments,
|
||||||
|
listTableDepartments,
|
||||||
|
} from "@/api/system/dept";
|
||||||
|
|
||||||
// 组件依赖
|
// 组件依赖
|
||||||
import {Search, Plus, Edit, Refresh, Delete} from '@element-plus/icons-vue'
|
import { Search, Plus, Edit, Refresh, Delete } from "@element-plus/icons-vue";
|
||||||
import TreeSelect from '@/components/TreeSelect/index.vue'
|
import TreeSelect from "@/components/TreeSelect/index.vue";
|
||||||
import {ElForm, ElMessage, ElMessageBox} from 'element-plus'
|
import { ElForm, ElMessage, ElMessageBox } from "element-plus";
|
||||||
|
import {
|
||||||
|
DeptFormData,
|
||||||
|
DeptItem,
|
||||||
|
DeptQueryParam,
|
||||||
|
Dialog,
|
||||||
|
Option,
|
||||||
|
} from "@/types";
|
||||||
|
|
||||||
// DOM元素的引用声明定义
|
// DOM元素的引用声明定义
|
||||||
const queryFormRef = ref(ElForm)
|
const queryFormRef = ref(ElForm);
|
||||||
const dataFormRef = ref(ElForm)
|
const dataFormRef = ref(ElForm);
|
||||||
|
|
||||||
const state = reactive({
|
const state = reactive({
|
||||||
// 选中ID数组
|
// 选中ID数组
|
||||||
ids: [],
|
ids: [] as number[],
|
||||||
// 非单个禁用
|
// 非单个禁用
|
||||||
single: true,
|
single: true,
|
||||||
disabled: false,
|
disabled: false,
|
||||||
originOptions: [],
|
|
||||||
loading: true,
|
loading: true,
|
||||||
// 表格树数据
|
// 表格树数据
|
||||||
deptList: [],
|
deptList: [] as DeptItem[],
|
||||||
// 部门树选项
|
// 部门下拉选项
|
||||||
deptOptions: [] as Array<any>,
|
deptOptions: [] as Option[],
|
||||||
// 弹窗属性
|
// 弹窗属性
|
||||||
dialog: {
|
dialog: {visible:false} as Dialog,
|
||||||
title: '',
|
|
||||||
visible: false
|
|
||||||
},
|
|
||||||
// 查询参数
|
// 查询参数
|
||||||
queryParams: {
|
queryParams: {} as DeptQueryParam,
|
||||||
name: undefined,
|
|
||||||
status: undefined
|
|
||||||
},
|
|
||||||
// 表单数据
|
// 表单数据
|
||||||
formData: {
|
formData: {
|
||||||
id: undefined,
|
|
||||||
parentId: '',
|
|
||||||
name: '',
|
|
||||||
sort: 1,
|
sort: 1,
|
||||||
status: 1
|
status: 1,
|
||||||
},
|
} as DeptFormData,
|
||||||
// 表单参数校验
|
// 表单参数校验
|
||||||
rules: {
|
rules: {
|
||||||
parentId: [
|
parentId: [
|
||||||
{required: true, message: '上级部门不能为空', trigger: 'blur'}
|
{ required: true, message: "上级部门不能为空", trigger: "blur" },
|
||||||
],
|
],
|
||||||
name: [
|
name: [{ required: true, message: "部门名称不能为空", trigger: "blur" }],
|
||||||
{required: true, message: '部门名称不能为空', trigger: 'blur'}
|
sort: [{ required: true, message: "显示排序不能为空", trigger: "blur" }],
|
||||||
],
|
},
|
||||||
sort: [
|
});
|
||||||
{required: true, message: '显示排序不能为空', trigger: 'blur'}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
const {ids, single, disabled, loading, deptList, deptOptions, queryParams, formData, rules, dialog} = toRefs(state)
|
|
||||||
|
|
||||||
|
const {
|
||||||
|
ids,
|
||||||
|
single,
|
||||||
|
disabled,
|
||||||
|
loading,
|
||||||
|
deptList,
|
||||||
|
deptOptions,
|
||||||
|
queryParams,
|
||||||
|
formData,
|
||||||
|
rules,
|
||||||
|
dialog,
|
||||||
|
} = toRefs(state);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门查询
|
* 部门查询
|
||||||
*/
|
*/
|
||||||
function handleQuery() {
|
function handleQuery() {
|
||||||
state.loading = true
|
state.loading = true;
|
||||||
listDeptTable(state.queryParams).then((response: any) => {
|
listTableDepartments(state.queryParams).then(({ data }) => {
|
||||||
state.deptList = response.data
|
state.deptList = data;
|
||||||
state.loading = false
|
state.loading = false;
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重置查询
|
* 重置查询
|
||||||
*/
|
*/
|
||||||
function resetQuery() {
|
function resetQuery() {
|
||||||
const queryForm = unref(queryFormRef)
|
queryFormRef.value.resetFields();
|
||||||
queryForm.resetFields()
|
handleQuery();
|
||||||
handleQuery()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleSelectionChange(selection: any) {
|
function handleSelectionChange(selection: any) {
|
||||||
state.ids = selection.map((item: any) => item.id)
|
state.ids = selection.map((item: any) => item.id);
|
||||||
state.single = selection.length === 0
|
state.single = selection.length === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加载部门下拉数据源
|
* 加载部门下拉数据源
|
||||||
*/
|
*/
|
||||||
async function loadDeptOptions() {
|
async function loadDeptOptions() {
|
||||||
const deptOptions: any[] = []
|
const deptOptions: any[] = [];
|
||||||
listDeptSelect().then(response => {
|
listSelectDepartments().then((response) => {
|
||||||
const rootDeptOption = {id: 0, label: '顶级部门', children: response.data}
|
const rootDeptOption = {
|
||||||
deptOptions.push(rootDeptOption)
|
id: 0,
|
||||||
state.deptOptions = deptOptions
|
label: "顶级部门",
|
||||||
})
|
children: response.data,
|
||||||
|
};
|
||||||
|
deptOptions.push(rootDeptOption);
|
||||||
|
state.deptOptions = deptOptions;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 表单重置
|
* 表单重置
|
||||||
**/
|
**/
|
||||||
function resetForm() {
|
function resetForm() {
|
||||||
state.formData = {
|
dataFormRef.value.resetFields();
|
||||||
id: undefined,
|
|
||||||
parentId: '',
|
|
||||||
name: '',
|
|
||||||
sort: 1,
|
|
||||||
status: 1
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加部门
|
* 添加部门
|
||||||
*/
|
*/
|
||||||
function handleAdd(row: any) {
|
function handleAdd(row: any) {
|
||||||
resetForm()
|
loadDeptOptions();
|
||||||
loadDeptOptions()
|
state.formData.parentId = row.id;
|
||||||
state.formData.parentId = row.id
|
|
||||||
state.dialog = {
|
state.dialog = {
|
||||||
title: '添加部门',
|
title: "添加部门",
|
||||||
visible: true
|
visible: true,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改部门
|
* 修改部门
|
||||||
*/
|
*/
|
||||||
async function handleUpdate(row: any) {
|
async function handleUpdate(row: any) {
|
||||||
await loadDeptOptions()
|
await loadDeptOptions();
|
||||||
const deptId = row.id || state.ids
|
const deptId = row.id || state.ids;
|
||||||
state.dialog = {
|
state.dialog = {
|
||||||
title: '修改部门',
|
title: "修改部门",
|
||||||
visible: true
|
visible: true,
|
||||||
}
|
};
|
||||||
getDeptDetail(deptId).then((response: any) => {
|
getDeptFormDetail(deptId).then((response: any) => {
|
||||||
state.formData = response.data
|
state.formData = response.data;
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门表单提交
|
* 部门表单提交
|
||||||
*/
|
*/
|
||||||
function submitForm() {
|
function submitForm() {
|
||||||
const dataForm = unref(dataFormRef)
|
const dataForm = unref(dataFormRef);
|
||||||
dataForm.validate((valid: any) => {
|
dataForm.validate((valid: any) => {
|
||||||
|
|
||||||
if (valid) {
|
if (valid) {
|
||||||
if (state.formData.id) {
|
if (state.formData.id) {
|
||||||
updateDept(state.formData.id, state.formData).then((res: any) => {
|
updateDept(state.formData.id, state.formData).then((res: any) => {
|
||||||
ElMessage.success('修改成功')
|
ElMessage.success("修改成功");
|
||||||
state.dialog.visible = false
|
state.dialog.visible = false;
|
||||||
handleQuery()
|
handleQuery();
|
||||||
})
|
});
|
||||||
} else {
|
} else {
|
||||||
addDept(state.formData).then(() => {
|
addDept(state.formData).then(() => {
|
||||||
ElMessage.success('新增成功')
|
ElMessage.success("新增成功");
|
||||||
state.dialog.visible = false
|
state.dialog.visible = false;
|
||||||
handleQuery()
|
handleQuery();
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -327,32 +321,34 @@ function submitForm() {
|
|||||||
* @param row
|
* @param row
|
||||||
*/
|
*/
|
||||||
function handleDelete(row: any) {
|
function handleDelete(row: any) {
|
||||||
const ids = [row.id || state.ids].join(',')
|
const ids = [row.id || state.ids].join(",");
|
||||||
ElMessageBox.confirm(`确认删除已选中的数据项?`, '警告', {
|
ElMessageBox.confirm(`确认删除已选中的数据项?`, "警告", {
|
||||||
confirmButtonText: '确定',
|
confirmButtonText: "确定",
|
||||||
cancelButtonText: '取消',
|
cancelButtonText: "取消",
|
||||||
type: 'warning'
|
type: "warning",
|
||||||
}).then(() => {
|
})
|
||||||
deleteDept(ids).then(() => {
|
.then(() => {
|
||||||
handleQuery()
|
deleteDept(ids)
|
||||||
ElMessage.success('删除成功')
|
.then(() => {
|
||||||
}).catch(() => {
|
handleQuery();
|
||||||
console.log(`删除失败`)
|
ElMessage.success("删除成功");
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
console.log(`删除失败`);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
}).catch(() =>
|
.catch(() => ElMessage.info("已取消删除"));
|
||||||
ElMessage.info('已取消删除')
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取消/关闭弹窗
|
* 取消/关闭弹窗
|
||||||
**/
|
**/
|
||||||
function cancel() {
|
function cancel() {
|
||||||
resetForm()
|
resetForm();
|
||||||
state.dialog.visible = false
|
state.dialog.visible = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
handleQuery()
|
handleQuery();
|
||||||
})
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user