refactor: 优化非 CRUD 代码可读性

This commit is contained in:
Ray.Hao
2026-05-26 19:53:23 +08:00
parent 2a538d7da7
commit bd4413b9f0
11 changed files with 89 additions and 52 deletions

View File

@@ -3,6 +3,18 @@ import type { GeneratorPreviewItem, TableQueryParams, TableItem, GenConfigForm }
const GENERATOR_BASE_URL = "/api/v1/codegen";
// 构建预览和下载接口的查询参数
const buildCodegenParams = (pageType?: "classic" | "curd", type?: "ts" | "js") => {
const params: Record<string, string> = {};
if (pageType) {
params.pageType = pageType;
}
if (type) {
params.type = type;
}
return Object.keys(params).length ? params : undefined;
};
const GeneratorAPI = {
/** 获取数据表分页列表 */
getTablePage(params: TableQueryParams) {
@@ -21,7 +33,7 @@ const GeneratorAPI = {
});
},
/** 获取代码生成配置 */
/** 保存代码生成配置 */
saveGenConfig(tableName: string, data: GenConfigForm) {
return request({
url: `${GENERATOR_BASE_URL}/${tableName}/config`,
@@ -32,17 +44,10 @@ const GeneratorAPI = {
/** 获取代码生成预览数据 */
getPreviewData(tableName: string, pageType?: "classic" | "curd", type?: "ts" | "js") {
const params: Record<string, string> = {};
if (pageType) {
params.pageType = pageType;
}
if (type) {
params.type = type;
}
return request<unknown, GeneratorPreviewItem[]>({
url: `${GENERATOR_BASE_URL}/${tableName}/preview`,
method: "get",
params: Object.keys(params).length ? params : undefined,
params: buildCodegenParams(pageType, type),
});
},
@@ -54,23 +59,12 @@ const GeneratorAPI = {
});
},
/**
* 下载 ZIP 文件
* @param url
* @param fileName
*/
/** 下载代码生成 ZIP 文件 */
download(tableName: string, pageType?: "classic" | "curd", type?: "ts" | "js") {
const params: Record<string, string> = {};
if (pageType) {
params.pageType = pageType;
}
if (type) {
params.type = type;
}
return request({
url: `${GENERATOR_BASE_URL}/${tableName}/download`,
method: "get",
params: Object.keys(params).length ? params : undefined,
params: buildCodegenParams(pageType, type),
responseType: "blob",
}).then((response) => {
const contentDisposition = response?.headers?.["content-disposition"] as string | undefined;
@@ -89,12 +83,12 @@ const GeneratorAPI = {
const blob = new Blob([response.data], { type: "application/zip" });
const a = document.createElement("a");
const url = window.URL.createObjectURL(blob);
a.href = url;
const downloadUrl = window.URL.createObjectURL(blob);
a.href = downloadUrl;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
window.URL.revokeObjectURL(downloadUrl);
});
},
};

View File

@@ -223,4 +223,3 @@ watch(
}
);
</script>
<style lang="scss" scoped></style>

View File

@@ -11,11 +11,11 @@ function createOnlineCountComposable() {
let unsubscribe: (() => void) | null = null;
// 处理在线人数变更消息
const handleOnlineCountMessage = (count: number) => {
if (count !== undefined && !isNaN(count)) {
onlineUserCount.value = count;
lastUpdateTime.value = Date.now();
}
if (!Number.isFinite(count) || count < 0) return;
onlineUserCount.value = count;
lastUpdateTime.value = Date.now();
};
const initialize = () => {

View File

@@ -184,7 +184,7 @@ function extractTopMenuPath(path: string): string {
function handleTopMenuSelect(menuPath: string) {
if (menuPath === activeTopMenuPath.value) return;
appStore.activeTopMenu(menuPath);
appStore.setActiveTopMenuPath(menuPath);
permissionStore.setMixLayoutSideMenus(menuPath);
navigateToFirstMenu(permissionStore.mixLayoutSideMenus);
}
@@ -215,7 +215,7 @@ watch(
const isTopMenuChanged = topMenuPath !== activeTopMenuPath.value;
if (isTopMenuChanged) {
appStore.activeTopMenu(topMenuPath);
appStore.setActiveTopMenuPath(topMenuPath);
}
// 切换布局(如左侧 -> 混合activeTopMenuPath 可能已是正确值,

View File

@@ -31,9 +31,9 @@ export function useLayout() {
appStore.toggleDevice(device);
if (isDesktop.value) {
appStore.openSideBar();
appStore.openSidebar();
} else {
appStore.closeSideBar();
appStore.closeSidebar();
}
});
@@ -82,7 +82,7 @@ export function useLayout() {
}
function closeSidebar() {
appStore.closeSideBar();
appStore.closeSidebar();
}
return {

View File

@@ -6,50 +6,104 @@ import { STORAGE_KEYS } from "@/constants";
import { defaults } from "@/settings";
export const useAppStore = defineStore("app", () => {
/**
* 当前设备类型
*/
const device = useStorage(STORAGE_KEYS.DEVICE, DeviceEnum.DESKTOP);
/**
* 组件默认尺寸
*/
const size = useStorage(STORAGE_KEYS.SIZE, defaults.size);
/**
* 当前语言
*/
const language = useStorage(STORAGE_KEYS.LANGUAGE, defaults.language);
/**
* 侧边栏持久化状态
*/
const sidebarStatus = useStorage(STORAGE_KEYS.SIDEBAR_STATUS, SidebarStatus.CLOSED);
/**
* 侧边栏显示状态
*/
const sidebar = reactive({
opened: sidebarStatus.value === SidebarStatus.OPENED,
withoutAnimation: false,
});
/**
* 当前激活的顶部菜单路径
*/
const activeTopMenuPath = useStorage(STORAGE_KEYS.ACTIVE_TOP_MENU_PATH, "");
/**
* 内容区是否全屏
*/
const contentFullscreen = ref(false);
/**
* Element Plus 当前语言包
*/
const locale = computed(() => (language?.value === "en" ? en : zhCn));
/**
* 切换侧边栏展开状态
*/
function toggleSidebar() {
sidebar.opened = !sidebar.opened;
sidebarStatus.value = sidebar.opened ? SidebarStatus.OPENED : SidebarStatus.CLOSED;
}
function closeSideBar() {
/**
* 关闭侧边栏
*/
function closeSidebar() {
sidebar.opened = false;
sidebarStatus.value = SidebarStatus.CLOSED;
}
function openSideBar() {
/**
* 打开侧边栏
*/
function openSidebar() {
sidebar.opened = true;
sidebarStatus.value = SidebarStatus.OPENED;
}
/**
* 切换设备类型
*/
function toggleDevice(val: string) {
device.value = val;
}
/**
* 切换组件尺寸
*/
function changeSize(val: string) {
size.value = val;
}
/**
* 切换语言
*/
function changeLanguage(val: string) {
language.value = val;
}
function activeTopMenu(val: string) {
activeTopMenuPath.value = val;
/**
* 设置顶部菜单激活路径
*/
function setActiveTopMenuPath(path: string) {
activeTopMenuPath.value = path;
}
/**
* 切换内容区全屏状态
*/
function toggleContentFullscreen() {
contentFullscreen.value = !contentFullscreen.value;
}
@@ -60,14 +114,14 @@ export const useAppStore = defineStore("app", () => {
language,
locale,
size,
activeTopMenu,
contentFullscreen,
toggleDevice,
changeSize,
changeLanguage,
toggleSidebar,
closeSideBar,
openSideBar,
closeSidebar,
openSidebar,
setActiveTopMenuPath,
toggleContentFullscreen,
activeTopMenuPath,
};

View File

@@ -12,5 +12,3 @@ import { useRoute } from "vue-router";
// 获取query参数
const query = useRoute().query.type as string;
</script>
<style lang="scss" scoped></style>

View File

@@ -623,5 +623,3 @@ const curd = {
},
};
</script>
<style scoped></style>

View File

@@ -1010,5 +1010,3 @@ onMounted(() => {
fetchPlanOptions();
});
</script>
<style scoped lang="scss"></style>

View File

@@ -495,5 +495,3 @@ onMounted(() => {
fetchData();
});
</script>
<style scoped lang="scss"></style>

View File

@@ -528,5 +528,3 @@ onMounted(() => {
handleQuery();
});
</script>
<style scoped lang="scss"></style>