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,135 @@
# _*_ coding : UTF-8 _*_
# @Time : {{ current_time }}
# @UpdateTime : {{ current_time }}
# @Author : {{ author }}
# @File : {{ table_name }}.py
# @Comment : 本程序用于生成{{ table_comment }}增删改查接口
from datetime import datetime
from typing import Optional
from fastapi import APIRouter, Depends, Path, Request, Query
from fastapi.responses import JSONResponse
from annotation.auth import Auth
from annotation.log import Log
from config.constant import BusinessType
from controller.login import LoginController
from models import {{ class_name }}
from schemas.common import BaseResponse, DeleteListParams
from schemas.{{ name }} import Add{{ class_name }}Params, Update{{ class_name }}Params, Get{{ class_name }}InfoResponse, Get{{ class_name }}ListResponse
from utils.response import Response
{{ table_name }}API= APIRouter(
prefix="{{ prefix }}",
dependencies=[Depends(LoginController.get_current_user)],
)
@{{ table_name }}API.post("/add", response_class=JSONResponse, response_model=BaseResponse, summary="新增{{ description }}")
@Log(title="新增{{ description }}", business_type=BusinessType.INSERT)
@Auth(permission_list=["{{ name }}:btn:add"])
async def add_{{ name }}(request: Request, params: Add{{ class_name }}Params):
if await {{ class_name }}.get_or_none(
{% for column in columns if column.is_insert %}
{{ column.python_name }} = params.{{ column.python_name }},
{% endfor %}
del_flag=1
):
return Response.error(msg="{{ description }}已存在!")
{{ name }} = await {{ class_name }}.create(
{% for column in columns if column.is_insert %}
{{ column.python_name }} = params.{{ column.python_name }},
{% endfor %}
)
if {{ name }}:
return Response.success(msg="新增成功!")
else:
return Response.error(msg="新增失败")
@{{ table_name }}API.delete("/delete/{id}", response_class=JSONResponse, response_model=BaseResponse, summary="删除{{ description }}")
@{{ table_name }}API.post("/delete/{id}", response_class=JSONResponse, response_model=BaseResponse, summary="删除{{ description }}")
@Log(title="删除{{ description }}", business_type=BusinessType.DELETE)
@Auth(permission_list=["{{ name }}:btn:delete"])
async def delete_{{ name }}(request: Request, id: str = Path(description="{{ description }}ID")):
if {{ name }} := await {{ class_name }}.get_or_none(id=id, del_flag=1):
{{ name }}.del_flag = 0
await {{ name }}.save()
return Response.success(msg="删除成功")
else:
return Response.error(msg="{{ description }}不存在!")
@{{ table_name }}API.delete("/deleteList", response_class=JSONResponse, response_model=BaseResponse, summary="批量删除{{ description }}")
@{{ table_name }}API.post("/deleteList", response_class=JSONResponse, response_model=BaseResponse, summary="批量删除{{ description }}")
@Log(title="批量删除{{ description }}", business_type=BusinessType.DELETE)
@Auth(permission_list=["{{ name }}:btn:delete"])
async def delete_{{ name }}_list(request: Request, params: DeleteListParams):
for id in set(params.ids):
if {{ name }} := await {{ class_name }}.get_or_none(id=id, del_flag=1):
{{ name }}.del_flag = 0
await {{ name }}.save()
return Response.success(msg="删除成功")
@{{ table_name }}API.put("/update/{id}", response_class=JSONResponse, response_model=BaseResponse, summary="修改{{ description }}")
@{{ table_name }}API.post("/update/{id}", response_class=JSONResponse, response_model=BaseResponse, summary="修改{{ description }}")
@Log(title="修改{{ description }}", business_type=BusinessType.UPDATE)
@Auth(permission_list=["{{ name }}:btn:update"])
async def update_{{ name }}(request: Request, params: Update{{ class_name }}Params, id: str = Path(description="{{ description }}ID")):
if {{ name }} := await {{ class_name }}.get_or_none(id=id, del_flag=1):
{% for column in columns if column.is_edit %}
{{ name }}.{{ column.python_name }} = params.{{ column.python_name }},
{% endfor %}
await {{ name }}.save()
return Response.success(msg="修改成功")
else:
return Response.error(msg="{{ description }}不存在")
@{{ table_name }}API.get("/info/{id}", response_class=JSONResponse, response_model=Get{{ class_name }}InfoResponse, summary="获取{{ description }}信息")
@Log(title="获取{{ description }}信息", business_type=BusinessType.SELECT)
@Auth(permission_list=["{{ name }}:btn:info"])
async def get_{{ name }}_info(request: Request, id: str = Path(description="{{ description }}ID")):
if {{ name }} := await {{ class_name }}.get_or_none(id=id, del_flag=1):
data = {
{% for column in columns if column.is_list %}
"{{ column.python_name }}":{{ name }}.{{ column.python_name }},
{% endfor %}
}
return Response.success(data=data)
else:
return Response.error(msg="{{ description }}不存在")
@{{ table_name }}API.get("/list", response_class=JSONResponse, response_model=Get{{ class_name }}ListResponse, summary="获取{{ description }}列表")
@Log(title="获取{{ description }}列表", business_type=BusinessType.SELECT)
@Auth(permission_list=["{{ name }}:btn:list"])
async def get_{{ name }}_list(
request: Request,
page: int = Query(default=1, description="当前页码"),
pageSize: int = Query(default=10, description="每页数量"),
{% for column in columns if column.is_query %}
{{ column.python_name }}: Optional[str] = Query(default=None, description="{{ column.column_comment }}"),
{% endfor %}
):
filterArgs={
{% for column in columns if column.is_query %}
"{{ column.python_name }}{{ column.query_way }}": {{ column.python_name }},
{% endfor %}
}
filterArgs = {k: v for k, v in filterArgs.items() if v is not None}
total = await {{ class_name }}.filter(**filterArgs, del_flag=1).count()
data = await {{ class_name }}.filter(**filterArgs, del_flag=1).offset((page - 1) * pageSize).limit(pageSize).values(
{% for column in columns if column.is_list %}
{{ column.python_name }} = "{{ column.python_name }}",
{% endfor %}
)
return Response.success(data={
"total": total,
"result": data,
"page": page,
"pageSize": pageSize,
})

View File

@@ -0,0 +1,44 @@
# _*_ coding : UTF-8 _*_
# @Time : {{ current_time }}
# @UpdateTime : {{ current_time }}
# @Author : {{ author }}
# @File : {{ table_name }}.py
# @Comment : 本程序用于{{ table_comment }}模型
from tortoise import fields
from models.common import BaseModel
class {{ class_name }}(BaseModel):
"""
{{ table_comment }}模型
"""
{% for column in columns %}
{%- set params = [] %}
{%- if column.max_length is not none %}{% set params = params + ["max_length=" ~ column.max_length] %}{% endif %}
{%- if column.is_nullable %}{% set params = params + ["null=True"] %}{% endif %}
{%- if column.is_unique %}{% set params = params + ["unique=True"] %}{% endif %}
{%- if column.default is not none %}{% set params = params + ["default=" ~ column.default] %}{% endif %}
{%- if column.column_comment %}{% set params = params + ['description="' ~ column.column_comment ~ '"'] %}{% endif %}
{%- if column.column_name %}{% set params = params + ['source_field="' ~ column.column_name ~ '"'] %}{% endif %}
{{ column.python_name }} = fields.{{ column.field_type }}({{ params | join(", ") }})
"""
{{ column.column_comment }}。
{%- if column.max_length is not none %}
- 最大长度为 {{ column.max_length }} 个字符
{%- endif %}
- 映射到数据库字段 {{ column.column_name }}
{%- if column.is_nullable %}
- 可为空
{%- endif %}
{%- if column.default is not none %}
- 默认值:{{ column.default }}
{%- endif %}
"""
{% endfor %}
class Meta:
table = "{{ table_name }}"
table_description = "{{ table_comment }}"

View File

@@ -0,0 +1,51 @@
# _*_ coding : UTF-8 _*_
# @Time : {{ current_time }}
# @UpdateTime : {{ current_time }}
# @Author : {{ author }}
# @File : {{ table_name }}.py
# @Comment : 本程序用于生成{{ table_comment }}参数和响应模型
from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field, ConfigDict
from pydantic.alias_generators import to_snake
from schemas.common import BaseResponse, ListQueryResult
class {{ class_name }}Info(BaseModel):
"""{{ description }}信息"""
model_config = ConfigDict(alias_generator=to_snake, populate_by_name=True)
{% for column in columns %}
{{ column.python_name }}: {% if not column.is_required %}Optional[{% endif %}{{ column.python_type }}{% if not column.is_required %}]{% endif %} = Field(
{% if column.default is not none %}default={{ column.default }}, {% endif %}title="{{ column.column_comment }}"
)
{% endfor %}
class Add{{ class_name }}Params(BaseModel):
"""新增{{ description }}参数"""
model_config = ConfigDict(alias_generator=to_snake, populate_by_name=True)
{% for column in columns if column.is_insert %}
{{ column.python_name }}: {% if not column.is_required %}Optional[{% endif %}{{ column.python_type }}{% if not column.is_required %}]{% endif %} = Field(
{% if column.default is not none %}default={{ column.default }}, {% endif %}title="{{ column.column_comment }}"
)
{% endfor %}
class Update{{ class_name }}Params(BaseModel):
"""更新{{ description }}参数"""
model_config = ConfigDict(alias_generator=to_snake, populate_by_name=True)
{% for column in columns if column.is_edit %}
{{ column.python_name }}: {% if not column.is_required %}Optional[{% endif %}{{ column.python_type }}{% if not column.is_required %}]{% endif %} = Field(
{% if column.default is not none %}default={{ column.default }}, {% endif %}title="{{ column.column_comment }}"
)
{% endfor %}
class Get{{ class_name }}InfoResponse(BaseResponse):
"""获取{{ description }}信息响应"""
data: {{ class_name }}Info = Field(None, title="{{ table_comment }}信息")
class Get{{ class_name }}InfoListResult(ListQueryResult):
"""获取{{ description }}信息列表响应结果"""
result: List[{{ class_name }}Info] = Field(None, title="{{ table_comment }}信息列表")
class Get{{ class_name }}InfoListResponse(BaseResponse):
"""获取{{ description }}信息列表响应"""
data: Get{{ class_name }}InfoListResult = Field(None, title="{{ table_comment }}信息列表")

14
templates/sql.jinja Normal file
View File

@@ -0,0 +1,14 @@
-- {{ description }}权限
-- 按钮权限
-- 添加
INSERT INTO `permission` VALUES ('{{ uuid4() }}', 1, '', '{{ now() }}', '', '{{ now() }}', 3, '{{ permission_id }}', 'buttons:Add', '', '', '', 1, '', '', '', '', '', '', '{{ name }}:btn:add', '', 1, 0, 0, 0, 1, 0, 0);
-- 删除
INSERT INTO `permission` VALUES ('{{ uuid4() }}', 1, '', '{{ now() }}', '', '{{ now() }}', 3, '{{ permission_id }}', 'buttons:Delete', '', '', '', 2, '', '', '', '', '', '', '{{ name }}:btn:delete', '', 1, 0, 0, 0, 1, 0, 0);
-- 修改
INSERT INTO `permission` VALUES ('{{ uuid4() }}', 1, '', '{{ now() }}', '', '{{ now() }}', 3, '{{ permission_id }}', 'buttons:Update', '', '', '', 3, '', '', '', '', '', '', '{{ name }}:btn:update', '', 1, 0, 0, 0, 1, 0, 0);
-- 详情
INSERT INTO `permission` VALUES ('{{ uuid4() }}', 1, '', '{{ now() }}', '', '{{ now() }}', 3, '{{ permission_id }}', 'buttons:Details', '', '', '', 4, '', '', '', '', '', '', '{{ name }}:btn:info', '', 1, 0, 0, 0, 1, 0, 0);
-- 数据列表
INSERT INTO `permission` VALUES ('{{ uuid4() }}', 1, '', '{{ now() }}', '', '{{ now() }}', 3, '{{ permission_id }}', 'buttons:DataList', '', '', '', 5, '', '', '', '', '', '', '{{ name }}:btn:list', '', 1, 0, 0, 0, 1, 0, 0);

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 %}
}

View File

@@ -0,0 +1,95 @@
<template>
<el-form ref="ruleFormRef" :model="newFormInline" label-width="82px">
<el-row :gutter="30">
[% for column in columns if column.is_insert or column.is_edit %]
<re-col :value="24" :xm="24" :sm="24">
<el-form-item label="[[ column.column_comment ]]" prop="[[ column.python_name ]]">
[% if column.show_type == "input" %]
<el-input
v-model="newFormInline.[[ column.python_name ]]"
placeholder="请输入[[ column.column_comment ]]~"
clearable
/>
[% elif column.show_type == "textarea" %]
<el-input
v-model="newFormInline.[[ column.python_name ]]"
type="textarea"
placeholder="请输入[[ column.column_comment ]]~"
clearable
/>
[% elif column.show_type == "select" %]
<el-select
v-model="newFormInline.[[ column.python_name ]]"
placeholder="请选择[[ column.column_comment ]]~"
filterable
clearable
>
<el-option
v-for="item in options.[[ column.python_name ]]"
:key="item.value"
:label="item.label"
:value="item.value" />
</el-select>
[% elif column.show_type == "radio" %]
<el-radio-group v-model="newFormInline.[[ column.python_name ]]">
<el-radio
v-for="item in options.[[ column.python_name ]]"
:key="item.value"
:label="item.value"
>{{ item.label }}
</el-radio>
</el-radio-group>
[% elif column.show_type == "checkbox" %]
<el-checkbox-group v-model="newFormInline.[[ column.python_name ]]">
<el-checkbox
v-for="item in options.[[ column.python_name ]]"
:key="item.value"
:label="item.value">
{{ item.label }}
</el-checkbox>
</el-checkbox-group>
[% elif column.show_type == "datetime" %]
<el-date-picker
v-model="newFormInline.[[ column.python_name ]]"
type="datetime"
placeholder="请选择[[ column.column_comment ]]~"
format="YYYY-MM-DD HH:mm:ss"
value-format="x" />
[% endif %]
</el-form-item>
</re-col>
[% endfor %]
</el-row>
</el-form>
</template>
<script setup lang="ts">
import { ref } from "vue";
import ReCol from "@/components/ReCol";
import type { FormRules } from "element-plus";
import { [[ class_name ]]Info } from "types/[[ name ]]";
interface FormItemProps {
[% for column in columns if column.is_list %]
/** [[ column.column_comment ]] */
[[ column.python_name ]]: [[ column.typescript_type ]];
[% endfor %]
}
interface FormProps {
formInline: FormItemProps;
}
const props = withDefaults(defineProps<FormProps>(), {
formInline: () => ({
[% for column in columns if column.is_list %]
/** [[ column.column_comment ]] */
[[ column.python_name ]]: "",
[% endfor %]
})
});
const newFormInline = ref<ConfigInfo>(props.formInline);
/** 自定义表单规则校验 */
const formRules = reactive<FormRules>({
[% for column in columns if column.is_insert or column.is_edit %]
[[ column.python_name ]]: [{ required: [[ 'true' if column.is_required else 'false' ]], message: "请输入[[ column.column_comment ]]~", trigger: "blur" }],
[% endfor %]
});
defineExpose({ newFormInline });
</script>

View File

@@ -0,0 +1,237 @@
<template>
<div class="main">
<el-form
ref="formRef"
:inline="true"
:model="form"
class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
>
[% for column in columns if column.is_query %]
<el-form-item label="[[ column.column_comment ]]" prop="[[ column.python_name ]]">
[% if column.show_type == "input" %]
<el-input
v-model="form.[[ column.python_name ]]"
placeholder="请输入[[ column.column_comment ]]~"
class="!w-[200px]"
clearable
/>
[% elif column.show_type == "textarea" %]
<el-input
v-model="form.[[ column.python_name ]]"
type="textarea"
placeholder="请输入[[ column.column_comment ]]~"
class="!w-[200px]"
clearable
/>
[% elif column.show_type == "select" %]
<el-select
v-model="form.[[ column.python_name ]]"
placeholder="请选择[[ column.column_comment ]]~"
class="!w-[200px]"
clearable
>
<el-option
v-for="item in options.[[ column.python_name ]]"
:key="item.value"
:label="item.label"
:value="item.value" />
</el-select>
[% elif column.show_type == "radio" %]
<el-radio-group v-model="form.[[ column.python_name ]]">
<el-radio
v-for="item in options.[[ column.python_name ]]"
:key="item.value"
:label="item.value"
>{{ item.label }}
</el-radio>
</el-radio-group>
[% elif column.show_type == "checkbox" %]
<el-checkbox-group v-model="form.[[ column.python_name ]]">
<el-checkbox
v-for="item in options.[[ column.python_name ]]"
:key="item.value"
:label="item.value">
{{ item.label }}
</el-checkbox>
</el-checkbox-group>
[% elif column.show_type == "datetime" %]
<el-date-picker
v-model="form.[[ column.python_name ]]"
type="datetime"
placeholder="请选择[[ column.column_comment ]]~"
format="YYYY-MM-DD HH:mm:ss"
value-format="x" />
[% endif %]
</el-form-item>
[% endfor %]
<el-form-item>
<el-button
type="primary"
:icon="useRenderIcon('ri:search-line')"
:loading="loading"
@click="onSearch"
>
{{ t("buttons:Search") }}
</el-button>
<el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
{{ t("buttons:Reset") }}
</el-button>
</el-form-item>
</el-form>
<PureTableBar title="{{ description }}管理" :columns="columns" @refresh="onSearch">
<template #buttons>
<el-button
v-if="hasAuth('[[ name ]]:btn:add')"
type="primary"
:icon="useRenderIcon(AddFill)"
@click="openDialog('新增')"
>
{{ t("buttons:Add") }}
</el-button>
</template>
<template v-slot="{ size, dynamicColumns }">
<div
v-if="selectedNum > 0"
v-motion-fade
class="bg-[var(--el-fill-color-light)] w-full h-[46px] mb-2 pl-4 flex items-center"
>
<div class="flex-auto">
<span
style="font-size: var(--el-font-size-base)"
class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
>
已选 {{ selectedNum }} 项
</span>
<el-button type="primary" text @click="onSelectionCancel">
{{ t("buttons:Deselect") }}
</el-button>
</div>
<el-popconfirm
v-if="hasAuth('[[ name ]]:btn:delete')"
title="是否确认删除?"
@confirm="onbatchDel"
>
<template #reference>
<el-button type="danger" text class="mr-1">
{{ t("buttons:DeleteInBatches") }}
</el-button>
</template>
</el-popconfirm>
</div>
<pure-table
ref="tableRef"
row-key="id"
adaptive
border
stripe
:adaptiveConfig="{ offsetBottom: 45 }"
align-whole="center"
table-layout="auto"
:loading="loading"
:size="size"
:data="dataList"
:columns="dynamicColumns"
:pagination="pagination"
:paginationSmall="size === 'small' ? true : false"
:header-cell-style="{
background: 'var(--el-fill-color-light)',
color: 'var(--el-text-color-primary)'
}"
@selection-change="handleSelectionChange"
@page-size-change="handleSizeChange"
@page-current-change="handleCurrentChange"
>
<template #operation="{ row }">
<el-button
class="reset-margin"
link
type="primary"
:size="size"
:disabled="!hasAuth('[[ name ]]:btn:update')"
:icon="useRenderIcon(EditPen)"
@click="openDialog('修改', row)"
>
{{ t("buttons:Update") }}
</el-button>
<el-popconfirm
:title="`是否确认删除配置名为 ${row.name} 的这条数据`"
@confirm="handleDelete(row)"
>
<template #reference>
<el-button
class="reset-margin"
link
:disabled="!hasAuth('[[ name ]]:btn:delete')"
type="danger"
:size="size"
:icon="useRenderIcon(Delete)"
>
{{ t("buttons:Delete") }}
</el-button>
</template>
</el-popconfirm>
</template>
</pure-table>
</template>
</PureTableBar>
</div>
</template>
<script setup lang="ts">
defineOptions({
name: "{{ class_name }}Index"
});
import { ref } from "vue";
import { use[[ class_name ]] } from "./utils/hook";
import { useI18n } from "vue-i18n";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";
const { t } = useI18n();
import { hasAuth } from "@/utils/auth";
/**
* 表格Ref
*/
const tableRef = ref();
const formRef = ref();
const {
form,
dataList,
loading,
pagination,
columns,
selectedNum,
onSearch,
openDialog,
resetForm,
handleDelete,
handleSizeChange,
handleCurrentChange,
handleSelectionChange,
onSelectionCancel,
onbatchDel
} = use[[ class_name ]](tableRef);
</script>
<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
margin: 0;
}
:deep(.el-button:focus-visible) {
outline: none;
}
.main-content {
margin: 24px 24px 0 !important;
}
.search-form {
:deep(.el-form-item) {
margin-bottom: 12px;
}
}
</style>