refactor(generator): 重构代码生成功能,由指定路径生成模式调整为下载模式,更方便复杂场景

This commit is contained in:
2024-03-15 00:01:33 +08:00
parent e6fa9f60a9
commit 7792cd398f
3 changed files with 34 additions and 13 deletions

View File

@@ -57,7 +57,6 @@ export interface GenConfigRecord {
tableName: string; tableName: string;
moduleName: string; moduleName: string;
packageName: string; packageName: string;
frontendPath: string;
businessName: string; businessName: string;
author: string; author: string;
tablePrefix: string; tablePrefix: string;
@@ -89,5 +88,9 @@ export function preview(tableName: string) {
} }
export function generate(tableName: string) { export function generate(tableName: string) {
return axios.post(`${BASE_URL}/${tableName}`); return axios.request({
url: `${BASE_URL}/${tableName}`,
method: 'post',
responseType: 'blob',
});
} }

View File

@@ -46,6 +46,9 @@ axios.interceptors.response.use(
) { ) {
return response; return response;
} }
if (response.config.responseType === 'blob') {
return response;
}
const res = response.data; const res = response.data;
if (res.success) { if (res.success) {
return res; return res;

View File

@@ -206,7 +206,26 @@
*/ */
const handleGenerate = (tableName: string) => { const handleGenerate = (tableName: string) => {
generate(tableName).then((res) => { generate(tableName).then((res) => {
proxy.$message.success(res.msg); const contentDisposition = res.headers['content-disposition'];
const pattern = new RegExp('filename=([^;]+\\.[^\\.;]+);*');
const result = pattern.exec(contentDisposition) || '';
// 对名字进行解码
const fileName = window.decodeURI(result[1]);
// 创建下载的链接
const blob = new Blob([res.data]);
const downloadElement = document.createElement('a');
const href = window.URL.createObjectURL(blob);
downloadElement.style.display = 'none';
downloadElement.href = href;
// 下载后文件名
downloadElement.download = fileName;
document.body.appendChild(downloadElement);
// 点击下载
downloadElement.click();
// 下载完成,移除元素
document.body.removeChild(downloadElement);
// 释放掉 blob 对象
window.URL.revokeObjectURL(href);
}); });
}; };
@@ -256,6 +275,9 @@
<div class="app-container"> <div class="app-container">
<Breadcrumb :items="['menu.tool', 'menu.tool.generator.list']" /> <Breadcrumb :items="['menu.tool', 'menu.tool.generator.list']" />
<a-card class="general-card" :title="$t('menu.tool.generator.list')"> <a-card class="general-card" :title="$t('menu.tool.generator.list')">
<a-alert type="warning" style="margin-bottom: 15px">
默认已开启 Mock下载/导出类操作会出现错误例如下载的文件无法打开点击<a-link href="https://doc.charles7c.top/admin/other/faq.html#%E7%82%B9%E5%87%BB%E5%AF%BC%E5%87%BA%E6%8C%89%E9%92%AE%E6%8A%A5%E9%94%99">查看解决方法</a-link>
</a-alert>
<!-- 头部区域 --> <!-- 头部区域 -->
<div class="header"> <div class="header">
<!-- 搜索栏 --> <!-- 搜索栏 -->
@@ -266,7 +288,7 @@
v-model="queryParams.tableName" v-model="queryParams.tableName"
placeholder="输入表名称搜索" placeholder="输入表名称搜索"
allow-clear allow-clear
style="width: 150px" style="width: 230px"
@press-enter="handleQuery" @press-enter="handleQuery"
/> />
</a-form-item> </a-form-item>
@@ -283,7 +305,6 @@
</a-form> </a-form>
</div> </div>
</div> </div>
<!-- 列表区域 --> <!-- 列表区域 -->
<a-table <a-table
ref="tableRef" ref="tableRef"
@@ -503,12 +524,6 @@
placeholder="项目模块包名例如top.charles7c.continew.admin.system" placeholder="项目模块包名例如top.charles7c.continew.admin.system"
/> />
</a-form-item> </a-form-item>
<a-form-item label="前端路径" field="frontendPath">
<a-input
v-model="form.frontendPath"
placeholder="前端项目 views 目录地址"
/>
</a-form-item>
<a-form-item label="去表前缀" field="tablePrefix"> <a-form-item label="去表前缀" field="tablePrefix">
<a-input <a-input
v-model="form.tablePrefix" v-model="form.tablePrefix"
@@ -528,8 +543,8 @@
<!-- 生成预览区域 --> <!-- 生成预览区域 -->
<a-modal <a-modal
:body-style="{ :body-style="{
paddingTop: 0, paddingTop: 0,
}" }"
title="生成预览" title="生成预览"
:visible="previewVisible" :visible="previewVisible"
width="70%" width="70%"