Merge pull request #73 from cshaptx4869/patch-36

refactor: ♻️ cURD加强TS支持
This commit is contained in:
Ray Hao
2024-04-27 16:27:04 +08:00
committed by GitHub
9 changed files with 170 additions and 93 deletions

View File

@@ -12,7 +12,7 @@ export default defineMock([
username: "admin", username: "admin",
avatar: avatar:
"https://oss.youlai.tech/youlai-boot/2023/05/16/811270ef31f548af9cffc026dfc3777b.gif", "https://oss.youlai.tech/youlai-boot/2023/05/16/811270ef31f548af9cffc026dfc3777b.gif",
roles: ["ADMIN"], roles: ["ROOT"],
perms: [ perms: [
"sys:menu:delete", "sys:menu:delete",
"sys:dept:edit", "sys:dept:edit",

View File

@@ -74,9 +74,8 @@
<!-- 列表 --> <!-- 列表 -->
<el-table <el-table
v-loading="loading" v-loading="loading"
v-bind="contentConfig.table"
:data="pageData" :data="pageData"
:border="true"
:highlight-current-row="true"
@selection-change="handleSelectionChange" @selection-change="handleSelectionChange"
> >
<template v-for="col in contentConfig.cols" :key="col.prop"> <template v-for="col in contentConfig.cols" :key="col.prop">
@@ -182,36 +181,54 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, reactive } from "vue"; import { ref, reactive } from "vue";
import Pagination from "@/components/Pagination/index.vue";
import type { TableProps } from "element-plus";
// 对象类型
type IObject = Record<string, any>;
// 定义接收的属性 // 定义接收的属性
interface IOperatData { export interface IOperatData {
name: string; name: string;
row: any; row: any;
column: any; column: any;
$index: number; $index: number;
} }
export interface IContentConfig {
// 页面名称(参与组成权限标识,如sys:user:xxx)
pageName: string;
// table组件属性
table?: Omit<TableProps<any>, "data">;
// 列表的网络请求函数(需返回promise)
indexAction: (data: IObject) => Promise<IObject>;
// 删除的网络请求函数(需返回promise)
deleteAction: (id: string) => Promise<any>;
// 主键名(默认为id)
pk?: string;
// 表格工具栏(默认支持refresh,add,delete,export,也可自定义)
toolbar: (
| "refresh"
| "add"
| "delete"
| "export"
| {
auth?: string;
icon?: string;
name: string;
text: string;
}
)[];
// table组件列属性(额外的属性templet,operat,slotName)
cols: IObject[];
}
const props = defineProps<{ const props = defineProps<{
contentConfig: { contentConfig: IContentConfig;
// 页面名称(参与组成权限标识,如sys:user:xxx)
pageName: string;
// 列表的网络请求函数(需返回promise)
indexAction: (data: any) => Promise<any>;
// 删除的网络请求函数(需返回promise)
deleteAction: (data: any) => Promise<any>;
// 主键名(默认为id)
pk?: string;
// 表格工具栏(默认支持refresh,add,delete,export,也可自定义)
toolbar: any[];
// table组件列属性(额外的属性templet,operat)
cols: any[];
};
}>(); }>();
// 定义自定义事件 // 定义自定义事件
const emit = defineEmits<{ const emit = defineEmits<{
addClick: []; addClick: [];
exportClick: []; exportClick: [];
toolbarClick: [name: string]; toolbarClick: [name: string];
editClick: [row: any]; editClick: [row: IObject];
operatClick: [data: IOperatData]; operatClick: [data: IOperatData];
}>(); }>();
// 暴露的属性和方法 // 暴露的属性和方法
@@ -222,22 +239,22 @@ const pk = props.contentConfig.pk ?? "id";
// 加载状态 // 加载状态
const loading = ref(false); const loading = ref(false);
// 删除ID集合 用于批量删除 // 删除ID集合 用于批量删除
const removeIds = ref([]); const removeIds = ref<(number | string)[]>([]);
// 数据总数 // 数据总数
const total = ref(0); const total = ref(0);
// 列表数据 // 列表数据
const pageData = ref([]); const pageData = ref<IObject[]>([]);
// 每页条数 // 每页条数
const pageSize = 10; const pageSize = 10;
// 搜索参数 // 搜索参数
const queryParams = reactive<any>({ const queryParams = reactive<IObject>({
pageNum: 1, pageNum: 1,
pageSize: pageSize, pageSize: pageSize,
}); });
// 上一次搜索条件 // 上一次搜索条件
let lastFormData = {}; let lastFormData = {};
// 获取分页数据 // 获取分页数据
function fetchPageData(formData: any = {}, isRestart = false) { function fetchPageData(formData: IObject = {}, isRestart = false) {
loading.value = true; loading.value = true;
lastFormData = formData; lastFormData = formData;
if (isRestart) { if (isRestart) {
@@ -257,8 +274,8 @@ function fetchPageData(formData: any = {}, isRestart = false) {
fetchPageData(); fetchPageData();
// 行选中 // 行选中
function handleSelectionChange(selection: any) { function handleSelectionChange(selection: any[]) {
removeIds.value = selection.map((item: any) => item[pk]); removeIds.value = selection.map((item) => item[pk]);
} }
// 刷新 // 刷新
function handleRefresh() { function handleRefresh() {

View File

@@ -65,37 +65,40 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, reactive } from "vue"; import { ref, reactive } from "vue";
import { useThrottleFn } from "@vueuse/core"; import { useThrottleFn } from "@vueuse/core";
import type { FormRules, ElForm } from "element-plus"; import type { FormRules, DialogProps } from "element-plus";
// 对象类型
type IObject = Record<string, any>;
// 定义接收的属性 // 定义接收的属性
export interface IModalConfig {
// dialog组件属性
dialog: Partial<Omit<DialogProps, "modelValue">>;
// 页面名称
pageName?: string;
// 提交的网络请求函数(需返回promise)
formAction: (data: IObject) => Promise<any>;
// 表单项
formItems: Array<{
// 组件类型(如input,select,radio等)
type: string;
// 标签文本
label: string;
// 键名
prop: string;
// 组件属性
attrs?: IObject;
// 初始值
initialValue?: any;
// 可选项(适用于select,radio组件)
options?: { label: string; value: any }[];
// 插槽名(适用于组件类型为custom)
slotName?: string;
}>;
// 表单验证规则
formRules: FormRules;
}
const props = defineProps<{ const props = defineProps<{
modalConfig: { modalConfig: IModalConfig;
// dialog组件属性
dialog: any;
// 页面名称
pageName?: string;
// 提交的网络请求函数(需返回promise)
formAction: (data: any) => Promise<any>;
// 表单项
formItems: Array<{
// 组件类型(如input,select,radio等)
type: string;
// 标签文本
label: string;
// 键名
prop: string;
// 组件属性
attrs?: any;
// 初始值
initialValue?: any;
// 可选项(适用于select,radio组件)
options?: { label: string; value: any }[];
// 插槽名(适用于组件类型为custom)
slotName?: string;
}>;
// 表单验证规则
formRules: FormRules;
};
}>(); }>();
// 自定义事件 // 自定义事件
const emit = defineEmits<{ const emit = defineEmits<{
@@ -106,9 +109,9 @@ defineExpose({ setModalVisible });
const dialogVisible = ref(false); const dialogVisible = ref(false);
const formRef = ref<InstanceType<typeof ElForm>>(); const formRef = ref<InstanceType<typeof ElForm>>();
const formData = reactive<any>({}); const formData = reactive<IObject>({});
// 初始化 // 初始化
function setModalVisible(initData: any = {}) { function setModalVisible(initData: IObject = {}) {
dialogVisible.value = true; dialogVisible.value = true;
for (const item of props.modalConfig.formItems) { for (const item of props.modalConfig.formItems) {
formData[item.prop] = initData[item.prop] ?? item.initialValue ?? ""; formData[item.prop] = initData[item.prop] ?? item.initialValue ?? "";
@@ -116,7 +119,7 @@ function setModalVisible(initData: any = {}) {
} }
// 表单提交 // 表单提交
const handleSubmit = useThrottleFn(() => { const handleSubmit = useThrottleFn(() => {
formRef.value?.validate((valid: any) => { formRef.value?.validate((valid: boolean) => {
if (valid) { if (valid) {
props.modalConfig.formAction(formData).then(() => { props.modalConfig.formAction(formData).then(() => {
ElMessage.success( ElMessage.success(

View File

@@ -11,6 +11,13 @@
</template> </template>
</el-select> </el-select>
</template> </template>
<!-- TreeSelect 树形选择 -->
<template v-else-if="item.type === 'tree-select'">
<el-tree-select
v-model="queryParams[item.prop]"
v-bind="item.attrs"
/>
</template>
<!-- DatePicker 日期选择器 --> <!-- DatePicker 日期选择器 -->
<template v-else-if="item.type === 'date-picker'"> <template v-else-if="item.type === 'date-picker'">
<el-date-picker <el-date-picker
@@ -29,10 +36,10 @@
</el-form-item> </el-form-item>
</template> </template>
<el-form-item> <el-form-item>
<el-button type="primary" @click="handleQuery"> <el-button type="primary" icon="search" @click="handleQuery">
<i-ep-search />搜索 搜索
</el-button> </el-button>
<el-button @click="handleReset"><i-ep-refresh />重置</el-button> <el-button icon="refresh" @click="handleReset">重置</el-button>
</el-form-item> </el-form-item>
</el-form> </el-form>
</div> </div>
@@ -42,40 +49,43 @@
import { ref, reactive } from "vue"; import { ref, reactive } from "vue";
import type { ElForm } from "element-plus"; import type { ElForm } from "element-plus";
// 对象类型
type IObject = Record<string, any>;
// 定义接收的属性 // 定义接收的属性
export interface ISearchConfig {
// 页面名称(参与组成权限标识,如sys:user:xxx)
pageName: string;
// 表单项
formItems: Array<{
// 组件类型(如input,select等)
type: string;
// 标签文本
label: string;
// 键名
prop: string;
// 组件属性
attrs?: IObject;
// 初始值
initialValue?: any;
// 可选项(适用于select组件)
options?: { label: string; value: any }[];
}>;
}
interface IProps { interface IProps {
searchConfig: { searchConfig: ISearchConfig;
// 页面名称(参与组成权限标识,如sys:user:xxx)
pageName: string;
// 表单项
formItems: Array<{
// 组件类型(如input,select等)
type: string;
// 标签文本
label: string;
// 键名
prop: string;
// 组件属性
attrs?: any;
// 初始值
initialValue?: any;
// 可选项(适用于select组件)
options?: { label: string; value: any }[];
}>;
};
} }
const props = defineProps<IProps>(); const props = defineProps<IProps>();
// 自定义事件 // 自定义事件
const emit = defineEmits<{ const emit = defineEmits<{
queryClick: [queryParams: any]; queryClick: [queryParams: IObject];
resetClick: [queryParams: any]; resetClick: [queryParams: IObject];
}>(); }>();
// 暴露的属性和方法 // 暴露的属性和方法
defineExpose({ getQueryParams }); defineExpose({ getQueryParams });
const queryFormRef = ref<InstanceType<typeof ElForm>>(); const queryFormRef = ref<InstanceType<typeof ElForm>>();
// 搜索表单数据 // 搜索表单数据
const queryParams = reactive<any>({}); const queryParams = reactive<IObject>({});
for (const item of props.searchConfig.formItems) { for (const item of props.searchConfig.formItems) {
queryParams[item.prop] = item.initialValue ?? ""; queryParams[item.prop] = item.initialValue ?? "";
} }

View File

@@ -1,11 +1,14 @@
const modalConfig = { import type { IModalConfig } from "@/components/PageModal/index.vue";
const modalConfig: IModalConfig = {
pageName: "sys:user", pageName: "sys:user",
dialog: { dialog: {
title: "新增用户", title: "新增用户",
width: 800, width: 800,
"append-to-body": true, appendToBody: true,
draggable: true,
}, },
formAction: function (data: any) { formAction: function (data) {
console.log("add", data); console.log("add", data);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
resolve({ resolve({

View File

@@ -1,6 +1,12 @@
const contentConfig = { import type { IContentConfig } from "@/components/PageContent/index.vue";
const contentConfig: IContentConfig = {
pageName: "sys:user", pageName: "sys:user",
indexAction: function (data: any) { table: {
border: true,
highlightCurrentRow: true,
},
indexAction: function (data) {
console.log("index", data); console.log("index", data);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
@@ -44,7 +50,7 @@ const contentConfig = {
}, 800); }, 800);
}); });
}, },
deleteAction: function (id: string) { deleteAction: function (id) {
console.log("delete", id); console.log("delete", id);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
resolve({ resolve({
@@ -64,6 +70,7 @@ const contentConfig = {
name: "upload", name: "upload",
icon: "upload", icon: "upload",
text: "导入", text: "导入",
auth: "upload",
}, },
], ],
cols: [ cols: [
@@ -79,6 +86,7 @@ const contentConfig = {
align: "center", align: "center",
prop: "status", prop: "status",
templet: "custom", templet: "custom",
slotName: "status",
}, },
{ label: "创建时间", align: "center", prop: "createTime", width: 180 }, { label: "创建时间", align: "center", prop: "createTime", width: 180 },
{ {

View File

@@ -1,11 +1,13 @@
const modalConfig = { import type { IModalConfig } from "@/components/PageModal/index.vue";
const modalConfig: IModalConfig = {
pageName: "sys:user", pageName: "sys:user",
dialog: { dialog: {
title: "修改用户", title: "修改用户",
width: 800, width: 800,
"append-to-body": true, appendToBody: true,
}, },
formAction: function (data: any) { formAction: function (data) {
console.log("edit", data); console.log("edit", data);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
resolve({ resolve({

View File

@@ -1,4 +1,6 @@
const searchConfig = { import type { ISearchConfig } from "@/components/PageSearch/index.vue";
const searchConfig: ISearchConfig = {
pageName: "sys:user", pageName: "sys:user",
formItems: [ formItems: [
{ {
@@ -13,6 +15,37 @@ const searchConfig = {
}, },
}, },
}, },
{
type: "tree-select",
label: "部门",
prop: "deptId",
attrs: {
placeholder: "请选择",
data: [
{
value: 1,
label: "有来技术",
children: [
{
value: 2,
label: "研发部门",
},
{
value: 3,
label: "测试部门",
},
],
},
],
filterable: true,
"check-strictly": true,
"render-after-expand": false,
clearable: true,
style: {
width: "150px",
},
},
},
{ {
type: "select", type: "select",
label: "状态", label: "状态",

View File

@@ -48,6 +48,7 @@ import addModalConfig from "./config/add";
import editModalConfig from "./config/edit"; import editModalConfig from "./config/edit";
import type PageSearch from "@/components/PageSearch/index.vue"; import type PageSearch from "@/components/PageSearch/index.vue";
import type PageContent from "@/components/PageContent/index.vue"; import type PageContent from "@/components/PageContent/index.vue";
import type { IOperatData } from "@/components/PageContent/index.vue";
import type PageModal from "@/components/PageModal/index.vue"; import type PageModal from "@/components/PageModal/index.vue";
const searchRef = ref<InstanceType<typeof PageSearch>>(); const searchRef = ref<InstanceType<typeof PageSearch>>();
@@ -112,7 +113,7 @@ function handleEditClick(row: any) {
editModalRef.value?.setModalVisible(idMap[row.id]); editModalRef.value?.setModalVisible(idMap[row.id]);
} }
// 其他操作列 // 其他操作列
function handleOperatClick(data: any) { function handleOperatClick(data: IOperatData) {
console.log(data); console.log(data);
// 重置密码 // 重置密码
if (data.name === "reset_pwd") { if (data.name === "reset_pwd") {
@@ -135,7 +136,7 @@ function handleOperatClick(data: any) {
} }
// 表单提交 // 表单提交
function handleSubmitClick() { function handleSubmitClick() {
//刷新表数据 //刷新表数据
contentRef.value?.fetchPageData({}, true); contentRef.value?.fetchPageData({}, true);
} }
</script> </script>