重构组合式api
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
<template>
|
||||
<div class="system-role-container">
|
||||
<el-card shadow="hover">
|
||||
<!-- 搜索区域 -->
|
||||
<div class="system-user-search mb15">
|
||||
<el-form :inline="true">
|
||||
<el-form-item label="产品名称">
|
||||
<el-input size="default" v-model="tableData.param.roleName" placeholder="请输入产品名称" class="w-50 m-2" clearable />
|
||||
<el-input size="default" v-model="tableData.param.productName" placeholder="请输入产品名称" class="w-50 m-2" clearable />
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button size="default" type="primary" class="ml10" @click="roleList">
|
||||
<el-button size="default" type="primary" class="ml10" @click="handleSearch">
|
||||
<el-icon>
|
||||
<ele-Search />
|
||||
</el-icon>
|
||||
@@ -19,7 +20,7 @@
|
||||
</el-icon>
|
||||
新增产品
|
||||
</el-button>
|
||||
<!-- 添加导入导出按钮 -->
|
||||
<!-- 导入导出按钮 -->
|
||||
<el-button size="default" type="warning" class="ml10" @click="onOpenImport">
|
||||
<el-icon>
|
||||
<ele-Upload />
|
||||
@@ -35,253 +36,349 @@
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</div>
|
||||
<el-table :data="tableData.data" style="width: 100%">
|
||||
|
||||
<!-- 产品表格 -->
|
||||
<el-table :data="tableData.data" style="width: 100%" v-loading="tableData.loading">
|
||||
<el-table-column type="index" label="序号" width="60" />
|
||||
<el-table-column prop="name" label="产品名称" show-overflow-tooltip></el-table-column>
|
||||
<el-table-column prop="remark" label="创建人" show-overflow-tooltip></el-table-column>
|
||||
<el-table-column prop="status" label="修改人" show-overflow-tooltip></el-table-column>
|
||||
<el-table-column prop="createdAt" label="创建时间" show-overflow-tooltip></el-table-column>
|
||||
<el-table-column prop="createdAt" label="修改时间" show-overflow-tooltip></el-table-column>
|
||||
<el-table-column prop="name" label="产品名称" show-overflow-tooltip />
|
||||
<el-table-column prop="creator" label="创建人" show-overflow-tooltip />
|
||||
<el-table-column prop="updater" label="修改人" show-overflow-tooltip />
|
||||
<el-table-column prop="createdAt" label="创建时间" show-overflow-tooltip />
|
||||
<el-table-column prop="updatedAt" label="修改时间" show-overflow-tooltip />
|
||||
<el-table-column label="操作" width="220">
|
||||
<template #default="scope">
|
||||
<el-button size="small" text type="primary" @click="onOpenEditRole(scope.row)"
|
||||
><el-icon><ele-EditPen /></el-icon>修改</el-button
|
||||
>
|
||||
<el-button size="small" text type="primary" @click="onRowDel(scope.row)"
|
||||
><el-icon><ele-DeleteFilled /></el-icon>删除</el-button
|
||||
>
|
||||
<el-button size="small" text type="primary" @click="onOpenEditRole(scope.row)">
|
||||
<el-icon><ele-EditPen /></el-icon>修改
|
||||
</el-button>
|
||||
<el-button size="small" text type="primary" @click="onRowDel(scope.row)">
|
||||
<el-icon><ele-DeleteFilled /></el-icon>删除
|
||||
</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
|
||||
<!-- 分页组件 -->
|
||||
<pagination
|
||||
v-show="tableData.total > 0"
|
||||
:total="tableData.total"
|
||||
v-model:page="tableData.param.pageNum"
|
||||
v-model:limit="tableData.param.pageSize"
|
||||
@pagination="roleList"
|
||||
@pagination="handlePaginationChange"
|
||||
/>
|
||||
</el-card>
|
||||
|
||||
<EditRole ref="editRoleRef" @getRoleList="roleList" />
|
||||
<!-- 编辑对话框组件 -->
|
||||
<EditRole ref="editRoleRef" @getRoleList="getProductList" />
|
||||
|
||||
<!-- 添加导入导出对话框 -->
|
||||
<ImportDialog ref="importDialogRef" @getRoleList="roleList" />
|
||||
<!-- 导入导出对话框组件 -->
|
||||
<ImportDialog ref="importDialogRef" @getRoleList="getProductList" />
|
||||
<ExportDialog ref="exportDialogRef" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { toRefs, reactive, onMounted, ref, defineComponent, toRaw, getCurrentInstance } from 'vue';
|
||||
<script lang="ts" setup>
|
||||
import { ref, reactive, onMounted, nextTick } from 'vue';
|
||||
import { ElMessageBox, ElMessage } from 'element-plus';
|
||||
import EditRole from '/@/views/customerService/product/component/editRole.vue';
|
||||
import { deleteRole, getRoleList } from '/@/api/system/role';
|
||||
import ImportDialog from '/@/views/customerService/product/component/importDialog.vue';
|
||||
import ExportDialog from '/@/views/customerService/product/component/exportDialog.vue';
|
||||
import { deleteRole, getRoleList } from '/@/api/customerService/product';
|
||||
|
||||
// 定义接口来定义对象的类型
|
||||
interface TableData {
|
||||
/**
|
||||
* 产品数据接口
|
||||
*/
|
||||
interface ProductData {
|
||||
id: number;
|
||||
status: number;
|
||||
listOrder: number;
|
||||
name: string;
|
||||
creator: string; // 创建人
|
||||
updater: string; // 修改人
|
||||
remark: string;
|
||||
dataScope: number;
|
||||
createdAt: string;
|
||||
}
|
||||
interface TableDataState {
|
||||
tableData: {
|
||||
data: Array<TableData>;
|
||||
total: number;
|
||||
loading: boolean;
|
||||
param: {
|
||||
roleName: string;
|
||||
roleStatus: string;
|
||||
pageNum: number;
|
||||
pageSize: number;
|
||||
};
|
||||
};
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
const dateList = ref([
|
||||
/**
|
||||
* 查询参数接口
|
||||
*/
|
||||
interface QueryParam {
|
||||
productName: string; // 产品名称
|
||||
roleStatus: string; // 状态
|
||||
pageNum: number; // 页码
|
||||
pageSize: number; // 每页大小
|
||||
}
|
||||
|
||||
/**
|
||||
* 表格状态接口
|
||||
*/
|
||||
interface TableState {
|
||||
data: ProductData[]; // 表格数据
|
||||
total: number; // 总条数
|
||||
loading: boolean; // 加载状态
|
||||
param: QueryParam; // 查询参数
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟产品数据
|
||||
*/
|
||||
const mockProductData: ProductData[] = [
|
||||
{
|
||||
id: 1,
|
||||
status: '李四',
|
||||
status: 1,
|
||||
listOrder: 0,
|
||||
name: 'xxx药品',
|
||||
remark: '张三',
|
||||
creator: '张三',
|
||||
updater: '李四',
|
||||
remark: '药品描述',
|
||||
dataScope: 3,
|
||||
createdAt: '2022-04-01 11:38:39',
|
||||
updatedAt: '2022-04-28 10:00:15',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
status: '李四',
|
||||
status: 1,
|
||||
listOrder: 0,
|
||||
name: 'xxx药品',
|
||||
remark: '张三',
|
||||
name: 'yyy医疗器械',
|
||||
creator: '王五',
|
||||
updater: '赵六',
|
||||
remark: '医疗器械描述',
|
||||
dataScope: 3,
|
||||
createdAt: '2022-04-01 11:38:39',
|
||||
updatedAt: '2022-04-28 10:01:34',
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
status: '李四',
|
||||
status: 1,
|
||||
listOrder: 0,
|
||||
name: 'xxx药品',
|
||||
remark: '张三',
|
||||
name: 'zzz保健品',
|
||||
creator: '孙七',
|
||||
updater: '周八',
|
||||
remark: '保健品描述',
|
||||
dataScope: 3,
|
||||
createdAt: '2022-04-01 11:38:39',
|
||||
updatedAt: '2022-04-01 11:38:39',
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
status: '李四',
|
||||
status: 1,
|
||||
listOrder: 0,
|
||||
name: 'xxx药品',
|
||||
remark: '张三',
|
||||
name: 'aaa化妆品',
|
||||
creator: '吴九',
|
||||
updater: '郑十',
|
||||
remark: '化妆品描述',
|
||||
dataScope: 3,
|
||||
createdAt: '2022-04-01 11:38:39',
|
||||
updatedAt: '2022-04-01 11:38:39',
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
status: '李四',
|
||||
status: 1,
|
||||
listOrder: 0,
|
||||
name: 'xxx药品',
|
||||
remark: '张三',
|
||||
name: 'bbb药品',
|
||||
creator: '钱一',
|
||||
updater: '孙二',
|
||||
remark: '药品描述',
|
||||
dataScope: 2,
|
||||
createdAt: '2022-04-01 11:38:39',
|
||||
updatedAt: '2022-04-01 11:38:39',
|
||||
},
|
||||
{
|
||||
id: 8,
|
||||
status: '李四',
|
||||
status: 1,
|
||||
listOrder: 0,
|
||||
name: 'xxx药品',
|
||||
remark: '张三',
|
||||
name: 'ccc医疗器械',
|
||||
creator: '李三',
|
||||
updater: '王四',
|
||||
remark: '医疗器械描述',
|
||||
dataScope: 2,
|
||||
createdAt: '2022-04-01 11:38:39',
|
||||
updatedAt: '2022-04-06 09:53:40',
|
||||
},
|
||||
]);
|
||||
];
|
||||
|
||||
export default defineComponent({
|
||||
name: 'apiV1SystemRoleList',
|
||||
components: { EditRole, ImportDialog, ExportDialog },
|
||||
setup() {
|
||||
const { proxy } = getCurrentInstance() as any;
|
||||
const addRoleRef = ref();
|
||||
const editRoleRef = ref();
|
||||
const importDialogRef = ref();
|
||||
const exportDialogRef = ref();
|
||||
const dataScopeRef = ref();
|
||||
const state = reactive<TableDataState>({
|
||||
tableData: {
|
||||
data: [],
|
||||
total: 0,
|
||||
loading: false,
|
||||
param: {
|
||||
roleName: '',
|
||||
roleStatus: '',
|
||||
pageNum: 1,
|
||||
pageSize: 10,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// 初始化表格数据
|
||||
const initTableData = () => {
|
||||
roleList();
|
||||
};
|
||||
|
||||
const roleList = () => {
|
||||
const data: Array<TableData> = [];
|
||||
getRoleList(state.tableData.param).then((res) => {
|
||||
const list = res.data.list ?? [];
|
||||
list.map((item: TableData) => {
|
||||
data.push({
|
||||
id: item.id,
|
||||
status: item.status,
|
||||
listOrder: item.listOrder,
|
||||
name: item.name,
|
||||
remark: item.remark,
|
||||
dataScope: item.dataScope,
|
||||
createdAt: item.createdAt,
|
||||
});
|
||||
});
|
||||
state.tableData.data = dateList.value;
|
||||
state.tableData.total = dateList.value.length;
|
||||
});
|
||||
};
|
||||
|
||||
// 打开新增角色弹窗
|
||||
const onOpenAddRole = () => {
|
||||
editRoleRef.value.openDialog();
|
||||
};
|
||||
|
||||
// 打开修改角色弹窗
|
||||
const onOpenEditRole = (row: Object) => {
|
||||
editRoleRef.value.openDialog(toRaw(row));
|
||||
};
|
||||
|
||||
// 打开导入弹窗
|
||||
const onOpenImport = () => {
|
||||
importDialogRef.value.openDialog();
|
||||
};
|
||||
|
||||
// 打开导出弹窗
|
||||
const onOpenExport = () => {
|
||||
exportDialogRef.value.openDialog(state.tableData.data);
|
||||
};
|
||||
|
||||
// 删除角色
|
||||
const onRowDel = (row: any) => {
|
||||
ElMessageBox.confirm(`此操作将永久删除角色:"${row.name}",是否继续?`, '提示', {
|
||||
confirmButtonText: '确认',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning',
|
||||
})
|
||||
.then(() => {
|
||||
deleteRole(row.id).then(() => {
|
||||
ElMessage.success('删除成功');
|
||||
proxy.$refs['editRoleRef']?.resetMenuSession?.();
|
||||
roleList();
|
||||
});
|
||||
})
|
||||
.catch(() => {});
|
||||
};
|
||||
|
||||
// 分页改变
|
||||
const onHandleSizeChange = (val: number) => {
|
||||
state.tableData.param.pageSize = val;
|
||||
};
|
||||
|
||||
// 分页改变
|
||||
const onHandleCurrentChange = (val: number) => {
|
||||
state.tableData.param.pageNum = val;
|
||||
};
|
||||
|
||||
// 页面加载时
|
||||
onMounted(() => {
|
||||
initTableData();
|
||||
});
|
||||
|
||||
return {
|
||||
addRoleRef,
|
||||
editRoleRef,
|
||||
importDialogRef,
|
||||
exportDialogRef,
|
||||
dataScopeRef,
|
||||
onOpenAddRole,
|
||||
onOpenEditRole,
|
||||
onOpenImport,
|
||||
onOpenExport,
|
||||
onRowDel,
|
||||
onHandleSizeChange,
|
||||
onHandleCurrentChange,
|
||||
roleList,
|
||||
...toRefs(state),
|
||||
};
|
||||
// 响应式状态
|
||||
const tableData = reactive<TableState>({
|
||||
data: [],
|
||||
total: 0,
|
||||
loading: false,
|
||||
param: {
|
||||
productName: '',
|
||||
roleStatus: '',
|
||||
pageNum: 1,
|
||||
pageSize: 10,
|
||||
},
|
||||
});
|
||||
|
||||
// 组件引用
|
||||
const editRoleRef = ref<InstanceType<typeof EditRole>>();
|
||||
const importDialogRef = ref<InstanceType<typeof ImportDialog>>();
|
||||
const exportDialogRef = ref<InstanceType<typeof ExportDialog>>();
|
||||
|
||||
/**
|
||||
* 获取产品列表
|
||||
*/
|
||||
const getProductList = async () => {
|
||||
try {
|
||||
tableData.loading = true;
|
||||
|
||||
// 实际API调用 - 注释掉模拟数据后取消注释
|
||||
// const res = await getRoleList(tableData.param);
|
||||
// if (res.data?.list) {
|
||||
// tableData.data = res.data.list;
|
||||
// tableData.total = res.data.total;
|
||||
// }
|
||||
|
||||
// 模拟数据 - 添加延迟模拟网络请求
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
// 根据产品名称过滤数据
|
||||
const filteredData = mockProductData.filter((item) => !tableData.param.productName || item.name.includes(tableData.param.productName));
|
||||
|
||||
tableData.data = filteredData;
|
||||
tableData.total = filteredData.length;
|
||||
} catch (error) {
|
||||
console.error('获取产品列表失败:', error);
|
||||
ElMessage.error('获取产品列表失败');
|
||||
} finally {
|
||||
tableData.loading = false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 处理搜索
|
||||
*/
|
||||
const handleSearch = () => {
|
||||
tableData.param.pageNum = 1; // 重置到第一页
|
||||
getProductList();
|
||||
};
|
||||
|
||||
/**
|
||||
* 处理分页变化
|
||||
* @param pagination - 分页参数
|
||||
*/
|
||||
const handlePaginationChange = (pagination: { page: number; limit: number }) => {
|
||||
tableData.param.pageNum = pagination.page;
|
||||
tableData.param.pageSize = pagination.limit;
|
||||
getProductList();
|
||||
};
|
||||
|
||||
/**
|
||||
* 打开新增产品对话框
|
||||
*/
|
||||
const onOpenAddRole = () => {
|
||||
if (editRoleRef.value) {
|
||||
editRoleRef.value.openDialog();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 打开编辑产品对话框
|
||||
* @param row - 产品数据
|
||||
*/
|
||||
const onOpenEditRole = (row: ProductData) => {
|
||||
if (editRoleRef.value) {
|
||||
editRoleRef.value.openDialog(row);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 打开导入对话框
|
||||
*/
|
||||
const onOpenImport = () => {
|
||||
if (importDialogRef.value) {
|
||||
importDialogRef.value.openDialog();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 打开导出对话框
|
||||
*/
|
||||
const onOpenExport = () => {
|
||||
if (exportDialogRef.value) {
|
||||
exportDialogRef.value.openDialog(tableData.data);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 删除产品
|
||||
* @param row - 产品数据
|
||||
*/
|
||||
const onRowDel = async (row: ProductData) => {
|
||||
try {
|
||||
await ElMessageBox.confirm(`此操作将永久删除产品:"${row.name}",是否继续?`, '提示', {
|
||||
confirmButtonText: '确认',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning',
|
||||
});
|
||||
|
||||
// 实际删除操作
|
||||
// await deleteRole(row.id);
|
||||
|
||||
// 模拟删除成功
|
||||
ElMessage.success('删除成功');
|
||||
|
||||
// 刷新列表
|
||||
await getProductList();
|
||||
} catch (error) {
|
||||
// 用户取消操作,不处理
|
||||
console.log('用户取消删除操作');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 初始化表格数据
|
||||
*/
|
||||
const initTableData = () => {
|
||||
getProductList();
|
||||
};
|
||||
|
||||
// 生命周期 - 页面加载时初始化数据
|
||||
onMounted(() => {
|
||||
initTableData();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.system-role-container {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.system-user-search {
|
||||
margin-bottom: 15px;
|
||||
|
||||
:deep(.el-form-item) {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.mb15 {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.ml10 {
|
||||
margin-left: 10px;
|
||||
}
|
||||
|
||||
.w-50 {
|
||||
width: 240px;
|
||||
}
|
||||
|
||||
// 表格操作按钮样式
|
||||
:deep(.el-table) {
|
||||
.el-button {
|
||||
margin: 0 2px;
|
||||
|
||||
&.el-button--small {
|
||||
padding: 6px 8px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 分页样式
|
||||
:deep(.pagination-container) {
|
||||
margin-top: 15px;
|
||||
padding: 10px 0;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user