refactor: 重构 API 层目录结构 & 样式模块化拆分
- API 层按业务域拆分(api/auth/, api/file/, api/system/*) - 类型定义从 types/api/ 移入对应 api 模块内(就近原则) - 公共类型统一为 api/common.ts,移除冗余类型重导出 - 样式文件按职责拆分(base/, layout/, vendors/),移除 common.scss - 移除失效的单元测试文件
This commit is contained in:
105
src/api/codegen/index.ts
Normal file
105
src/api/codegen/index.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import request from "@/utils/request";
|
||||
import type { GeneratorPreviewItem, TableQueryParams, TableItem, GenConfigForm } from "./types";
|
||||
|
||||
const GENERATOR_BASE_URL = "/api/v1/codegen";
|
||||
|
||||
const GeneratorAPI = {
|
||||
/** 获取数据表分页列表 */
|
||||
getTablePage(params: TableQueryParams) {
|
||||
return request<any, PageResult<TableItem>>({
|
||||
url: `${GENERATOR_BASE_URL}/table`,
|
||||
method: "get",
|
||||
params,
|
||||
});
|
||||
},
|
||||
|
||||
/** 获取代码生成配置 */
|
||||
getGenConfig(tableName: string) {
|
||||
return request<any, GenConfigForm>({
|
||||
url: `${GENERATOR_BASE_URL}/${tableName}/config`,
|
||||
method: "get",
|
||||
});
|
||||
},
|
||||
|
||||
/** 获取代码生成配置 */
|
||||
saveGenConfig(tableName: string, data: GenConfigForm) {
|
||||
return request({
|
||||
url: `${GENERATOR_BASE_URL}/${tableName}/config`,
|
||||
method: "post",
|
||||
data,
|
||||
});
|
||||
},
|
||||
|
||||
/** 获取代码生成预览数据 */
|
||||
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<any, GeneratorPreviewItem[]>({
|
||||
url: `${GENERATOR_BASE_URL}/${tableName}/preview`,
|
||||
method: "get",
|
||||
params: Object.keys(params).length ? params : undefined,
|
||||
});
|
||||
},
|
||||
|
||||
/** 重置代码生成配置 */
|
||||
resetGenConfig(tableName: string) {
|
||||
return request({
|
||||
url: `${GENERATOR_BASE_URL}/${tableName}/config`,
|
||||
method: "delete",
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 下载 ZIP 文件
|
||||
* @param url
|
||||
* @param fileName
|
||||
*/
|
||||
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,
|
||||
responseType: "blob",
|
||||
}).then((response) => {
|
||||
const contentDisposition = response?.headers?.["content-disposition"] as string | undefined;
|
||||
let fileName = `${tableName}.zip`;
|
||||
if (contentDisposition) {
|
||||
// content-disposition: attachment; filename=xxx.zip
|
||||
const match = /filename\*?=(?:UTF-8''|")?([^;"]+)/i.exec(contentDisposition);
|
||||
if (match?.[1]) {
|
||||
try {
|
||||
fileName = decodeURIComponent(match[1]);
|
||||
} catch {
|
||||
fileName = match[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const blob = new Blob([response.data], { type: "application/zip" });
|
||||
const a = document.createElement("a");
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export default GeneratorAPI;
|
||||
|
||||
// 重导出类型
|
||||
export * from "./types";
|
||||
Reference in New Issue
Block a user