perf: 分页

This commit is contained in:
pan 2024-03-22 15:58:10 +08:00
parent 4076650170
commit a2852db55b
3 changed files with 113 additions and 26 deletions

View File

@ -12,3 +12,18 @@ export const get = (tableName, type) => {
baseUrlApi("generator/") + tableName + "/" + type baseUrlApi("generator/") + tableName + "/" + type
); );
}; };
export const download = (name: String) => {
return http.request<Blob>(
"post",
baseUrlApi("generator/" + name + "/2"),
{},
{ responseType: "blob" }
);
};
export function sync(tables) {
return http.request<Blob>("post", baseUrlApi("generator/sync"), {
data: tables
});
}

View File

@ -1,6 +1,7 @@
import dayjs from "dayjs"; import dayjs from "dayjs";
import { message } from "@/utils/message"; import { message } from "@/utils/message";
import { CRUD } from "@/api/utils"; import { CRUD } from "@/api/utils";
import { download, sync } from "@/api/generator/generator";
import { reactive, ref, onMounted, toRaw } from "vue"; import { reactive, ref, onMounted, toRaw } from "vue";
import type { PaginationProps } from "@pureadmin/table"; import type { PaginationProps } from "@pureadmin/table";
import { isString, isEmpty } from "@pureadmin/utils"; import { isString, isEmpty } from "@pureadmin/utils";
@ -16,17 +17,24 @@ export function useRole() {
const form = reactive({ const form = reactive({
username: "", username: "",
tableName: "", tableName: "",
size: 999 size: 10,
page: 0
}); });
const dataList = ref([]); const dataList = ref([]);
const changeList = ref([]);
const loading = ref(true); const loading = ref(true);
const pagination = reactive<PaginationProps>({ const pagination = reactive<PaginationProps>({
total: 0, total: 0,
pageSize: 10, pageSize: 10,
pageSizes: [10, 20, 50],
currentPage: 1, currentPage: 1,
background: true background: true
}); });
const columns: TableColumnList = [ const columns: TableColumnList = [
{
type: "selection",
align: "left"
},
{ {
label: "表名", label: "表名",
prop: "tableName", prop: "tableName",
@ -62,15 +70,18 @@ export function useRole() {
]; ];
function handleSizeChange(val: number) { function handleSizeChange(val: number) {
console.log(`${val} items per page`); pagination.pageSize = val;
onSearch();
} }
function handleCurrentChange(val: number) { function handleCurrentChange(val: number) {
console.log(`current page: ${val}`); pagination.currentPage = val;
onSearch();
} }
function handleSelectionChange(val) { function handleSelectionChange(val) {
console.log("handleSelectionChange", val); console.log("handleSelectionChange", val.length);
changeList.value = val.map(person => person.tableName);
} }
function handleOffline(row) { function handleOffline(row) {
@ -80,8 +91,11 @@ export function useRole() {
async function onSearch() { async function onSearch() {
loading.value = true; loading.value = true;
const forms = toRaw(form);
forms.page = pagination.currentPage - 1;
forms.size = pagination.pageSize;
const { data } = await CRUD.get("generator/tables", { const { data } = await CRUD.get("generator/tables", {
params: toRaw(form) params: forms
}); });
dataList.value = data.content; dataList.value = data.content;
pagination.total = data.totalElements; pagination.total = data.totalElements;
@ -97,6 +111,36 @@ export function useRole() {
onSearch(); onSearch();
}; };
const downloadCode = name => {
download(name).then(data => {
const a = document.createElement("a");
const url = window.URL.createObjectURL(data); // 创建媒体流 url ,详细了解可自己查 URL.createObjectURL推荐 MDN
// 创建一个 a 标签,并设置其 href 属性和显示样式
a.href = url;
a.style.display = "none";
a.download = name + ".zip";
document.body.appendChild(a);
a.click();
// 点击 a 标签后,移除 a 标签
a.parentNode.removeChild(a);
// 释放对象 URL避免内存泄漏
window.URL.revokeObjectURL(url); // 删除创建的媒体流 url 对象
message("导出成功", {
type: "success"
});
});
};
const syncCode = () => {
sync(changeList.value).then(() => {
message("同步成功", {
type: "success"
});
onSearch();
});
};
onMounted(() => { onMounted(() => {
onSearch(); onSearch();
}); });
@ -107,12 +151,15 @@ export function useRole() {
columns, columns,
dataList, dataList,
pagination, pagination,
changeList,
onSearch, onSearch,
resetForm, resetForm,
handleOffline, handleOffline,
handleSizeChange, handleSizeChange,
handleCurrentChange, handleCurrentChange,
handleSelectionChange handleSelectionChange,
downloadCode,
syncCode
}; };
} }

View File

@ -4,7 +4,6 @@ import { useRole, useDetail } from "./hook";
import { PureTableBar } from "@/components/RePureTableBar"; import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks"; import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Plane from "@iconify-icons/ri/plane-line";
import Refresh from "@iconify-icons/ep/refresh"; import Refresh from "@iconify-icons/ep/refresh";
defineOptions({ defineOptions({
@ -19,12 +18,15 @@ const {
columns, columns,
dataList, dataList,
pagination, pagination,
changeList,
onSearch, onSearch,
resetForm, resetForm,
handleOffline, handleOffline,
handleSizeChange, handleSizeChange,
handleCurrentChange, handleCurrentChange,
handleSelectionChange handleSelectionChange,
downloadCode,
syncCode
} = useRole(); } = useRole();
</script> </script>
@ -59,24 +61,31 @@ const {
</el-form-item> </el-form-item>
</el-form> </el-form>
<PureTableBar <PureTableBar title="代码生成" :columns="columns" @refresh="onSearch">
title="在线用户(仅演示,操作后不生效)" <template #buttons>
:columns="columns" <el-button
@refresh="onSearch" type="success"
> :icon="useRenderIcon(Refresh)"
:disabled="changeList.length <= 0"
@click="syncCode"
>
同步
</el-button>
</template>
<template v-slot="{ size, dynamicColumns }"> <template v-slot="{ size, dynamicColumns }">
<pure-table <pure-table
adaptive
:adaptiveConfig="{ offsetBottom: 45 }"
align-whole="center" align-whole="center"
showOverflowTooltip showOverflowTooltip
table-layout="auto" table-layout="auto"
default-expand-all
stripe
:loading="loading" :loading="loading"
:size="size" :size="size"
adaptive
:adaptiveConfig="{ offsetBottom: 108 }"
:data="dataList" :data="dataList"
:columns="dynamicColumns" :columns="dynamicColumns"
:pagination="pagination" :pagination="pagination"
:paginationSmall="size === 'small' ? true : false"
:header-cell-style="{ :header-cell-style="{
background: 'var(--el-fill-color-light)', background: 'var(--el-fill-color-light)',
color: 'var(--el-text-color-primary)' color: 'var(--el-text-color-primary)'
@ -86,16 +95,32 @@ const {
@page-current-change="handleCurrentChange" @page-current-change="handleCurrentChange"
> >
<template #operation="{ row }"> <template #operation="{ row }">
<el-button <div :style="{ width: '100%', 'margin-left': '-15px' }">
class="reset-margin" <el-button
link class="reset-margin"
type="primary" link
:size="size" type="primary"
:icon="useRenderIcon(Plane)" :size="size"
@click="toDetail({ id: row.tableName }, 'query')" @click="toDetail({ id: row.tableName }, 'query')"
> >
预览 预览
</el-button> </el-button>
<el-button
class="reset-margin"
link
type="primary"
:size="size"
@click="downloadCode(row.tableName)"
>
下载
</el-button>
<el-button class="reset-margin" link type="primary" :size="size">
配置
</el-button>
<el-button class="reset-margin" link type="primary" :size="size">
生成
</el-button>
</div>
</template> </template>
</pure-table> </pure-table>
</template> </template>