feat: 添加代码生成功能

This commit is contained in:
2025-03-04 00:54:33 +08:00
parent c790233aee
commit bd13f1cfdc
16 changed files with 1758 additions and 1 deletions

View File

@@ -0,0 +1,42 @@
import { http } from "@/utils/http";
import type {
{{ class_name }}Info,
Get{{ class_name }}ListParams,
Add{{ class_name }}Params,
Update{{ class_name }}Params,
} from "types/{{ name }}";
import { filterEmptyObject } from "./utils";
/** 添加{{ description }}数据 */
export const postAdd{{ class_name }}API = (data: Add{{ class_name }}Params) => {
return http.request<null>("post", "/api{{ prefix }}/add", { data });
};
/** 删除{{ description }}数据 */
export const delete{{ class_name }}API = (id: string) => {
return http.request<null>("delete", `/api{{ prefix }}/delete/${id}`);
};
/** 批量删除{{ description }}数据 */
export const delete{{ class_name }}ListAPI = (ids: string[]) => {
return http.request<null>("delete", "/api{{ prefix }}/delete", {
data: { ids },
});
};
/** 修改{{ description }}数据 */
export const putUpdate{{ class_name }}API = (data: Update{{ class_name }}Params, id: string) => {
return http.request<null>("put", `/api{{ prefix }}/update/${id}`, { data });
};
/** 获取{{ description }}信息 */
export const get{{ class_name }}InfoAPI = (id: string) => {
return http.request<{{ class_name }}Info>("get", `/api{{ prefix }}/info/${id}`);
};
/** 获取{{ description }}列表 */
export const get{{ class_name }}ListAPI = (params: Get{{ class_name }}ListParams) => {
return http.request<QueryListResult<{{ class_name }}Info>>("get", "/api{{ prefix }}/list", {
params: filterEmptyObject(params),
});
};

View File

@@ -0,0 +1,284 @@
import dayjs from "dayjs";
import editForm from "../components/form.vue";
import { message } from "@/utils/message";
import { type Ref, ref, reactive, onMounted, h, toRaw } from "vue";
import type { {{ class_name }}Info } from "types/{{ name }}";
import type { PaginationProps } from "@pureadmin/table";
import { addDialog } from "@/components/ReDialog";
import {
delete{{ class_name }}API,
delete{{ class_name }}ListAPI,
get{{ class_name }}ListAPI,
postAdd{{ class_name }}API,
putUpdate{{ class_name }}API
} from "@/api/{{ name }}";
import { getKeyList } from "@pureadmin/utils";
export const use{{ class_name }} = (tableRef: Ref) => {
/**
* 查询表单
*/
const form = reactive({
{% for column in columns if column.is_query %}
/** {{ column.column_comment }} */
{{ column.column_name }}: "",
{% endfor %}
});
/**
* 表单Ref
*/
const formRef = ref(null);
/**
* 数据列表
*/
const dataList = ref<{{ class_name }}Info[]>([]);
/**
* 加载状态
*/
const loading = ref(true);
/**
* 已选数量
*/
const selectedNum = ref<number>(0);
/**
* 分页参数
*/
const pagination = reactive<PaginationProps>({
total: 0,
pageSize: 10,
currentPage: 1,
background: true,
pageSizes: [10, 20, 30, 40, 50]
});
/**
* 表格列设置
*/
const columns: TableColumnList = [
{
label: "勾选列", // 如果需要表格多选此处label必须设置
type: "selection",
fixed: "left",
reserveSelection: true // 数据刷新后保留选项
},
{% for column in columns if column.is_list %}
{
label: "{{ column.column_comment }}",
prop: "{{ column.column_name }}",
hide: {{ "true" if column.is_hide else "false" }}{% if column.python_type == "datetime" %},
formatter: ({ {{ column.column_name }} }) =>
dayjs({{ column.column_name }}).format("YYYY-MM-DD HH:mm:ss"){% endif %}
},
{% endfor %}
{
label: "操作",
fixed: "right",
width: 220,
slot: "operation"
}
];
/**
* 初次查询
*/
const onSearch = async () => {
loading.value = true;
const res = await get{{ class_name }}ListAPI({
page: pagination.currentPage,
pageSize: pagination.pageSize,
...toRaw(form)
});
if (res.success) {
dataList.value = res.data.result;
pagination.total = res.data.total;
pagination.currentPage = res.data.page;
pagination.pageSize = res.data.pageSize;
}
message(res.msg, {
type: res.success ? "success" : "error"
});
loading.value = false;
};
/**
* 重置表单
* @param formEl 表单ref
* @returns
*/
const resetForm = async (formEl: any) => {
if (!formEl) return;
formEl.resetFields();
await onSearch();
};
/**
* 处理删除
* @param row
*/
const handleDelete = async (row: {{ class_name }}Info) => {
const res = await delete{{ class_name }}API(row.id);
if (res.success) {
onSearch();
}
message(res.msg, {
type: res.success ? "success" : "error"
});
};
/**
* 处理每页数量变化
*/
const handleSizeChange = async (val: number) => {
loading.value = true;
const res = await get{{ class_name }}ListAPI({
page: pagination.currentPage,
pageSize: val,
...toRaw(form)
});
if (res.success) {
dataList.value = res.data.result;
pagination.total = res.data.total;
pagination.currentPage = res.data.page;
pagination.pageSize = res.data.pageSize;
}
message(res.msg, {
type: res.success ? "success" : "error"
});
loading.value = false;
};
/**
* 处理页码变化
* @param val
*/
const handleCurrentChange = async (val: number) => {
loading.value = true;
const res = await get{{ class_name }}ListAPI({
page: val,
pageSize: pagination.pageSize,
...toRaw(form)
});
if (res.success) {
dataList.value = res.data.result;
pagination.total = res.data.total;
pagination.currentPage = res.data.page;
pagination.pageSize = res.data.pageSize;
}
message(res.msg, {
type: res.success ? "success" : "error"
});
loading.value = false;
};
/** 当CheckBox选择项发生变化时会触发该事件 */
const handleSelectionChange = async (val: any) => {
selectedNum.value = val.length;
// 重置表格高度
tableRef.value.setAdaptive();
};
/** 取消选择 */
const onSelectionCancel = async () => {
selectedNum.value = 0;
// 用于多选表格,清空用户的选择
tableRef.value.getTableRef().clearSelection();
};
/**
* 批量删除
*/
const onbatchDel = async () => {
// 返回当前选中的行
const curSelected = tableRef.value.getTableRef().getSelectionRows();
const res = await delete{{ class_name }}ListAPI({
ids: getKeyList(curSelected, "id")
});
if (res.success) {
message(res.msg, {
type: "success"
});
tableRef.value.getTableRef().clearSelection();
onSearch();
} else {
message(res.msg, { type: "error", duration: 5000 });
}
};
const openDialog = async (title = "新增", row?: {{ class_name }}Info) => {
addDialog({
title: `${title}配置`,
props: {
formInline: {
/** 方式 */
title:title,
{% for column in columns if column.is_list %}
/** {{ column.column_comment }} */
{{ column.python_name }}: row?.{{ column.python_name }} ?? "",
{% endfor %}
}
},
width: "45%",
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () =>
h(editForm, {
formInline: {
/** 方式 */
title:title,
{% for column in columns if column.is_list %}
/** {{ column.column_comment }} */
{{ column.python_name }}: row?.{{ column.python_name }} ?? "",
{% endfor %}
},
ref: formRef
}),
beforeSure: async (done, {}) => {
const FormData = formRef.value.newFormInline;
if (title === "新增") {
let addForm = {
{% for column in columns if column.is_insert %}
/** {{ column.column_comment }} */
{{ column.python_name }}: FormData.{{ column.python_name }} ?? "",
{% endfor %}
};
const res = await postAdd{{ class_name }}PI(addForm);
if (res.success) {
done();
await onSearch();
}
message(res.msg, { type: res.success ? "success" : "error" });
} else {
let updateForm = {
{% for column in columns if column.is_update %}
/** {{ column.column_comment }} */
{{ column.python_name }}: FormData.{{ column.python_name }} ?? "",
{% endfor %}
};
const res = await putUpdate{{ class_name }}API(updateForm, row.id);
if (res.success) {
done();
await onSearch();
}
message(res.msg, { type: res.success ? "success" : "error" });
}
}
});
};
/**
* 页面加载执行
*/
onMounted(async () => {
await onSearch();
});
return {
form,
dataList,
loading,
pagination,
columns,
selectedNum,
openDialog,
onSearch,
resetForm,
handleDelete,
handleSizeChange,
handleCurrentChange,
handleSelectionChange,
onSelectionCancel,
onbatchDel
};
};

View File

@@ -0,0 +1,37 @@
/** {{ description }}信息 */
export interface {{ class_name }}Info {
{% for column in columns if column.is_list %}
/** {{ column.column_comment }} */
{{ column.python_name }}: {{ column.typescript_type }};
{% endfor %}
}
/** 获取{{ description }}列表参数 */
export interface Get{{ class_name }}ListParams {
/** 当前页 */
page: number;
/** 每页数量 */
pageSize: number;
{% for column in columns if column.is_query %}
/** {{ column.column_comment }} */
{{ column.python_name }}?: string;
{% endfor %}
}
/** 添加{{ description }}数据参数 */
export interface Add{{ class_name }}Params {
{% for column in columns if column.is_insert %}
/** {{ column.column_comment }} */
{{ column.python_name }}{% if not column.is_required %}?{% endif %}: {{ column.typescript_type }};
{% endfor %}
}
/** 更新{{ description }}数据参数 */
export interface Update{{ class_name }}Params {
{% for column in columns if column.is_edit %}
/** {{ column.column_comment }} */
{{ column.python_name }}{% if not column.is_required %}?{% endif %}: {{ column.typescript_type }};
{% endfor %}
}