Skip to content

Commit

Permalink
feat(generator): 源项目内生成代码 (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
luoqiz authored Jan 12, 2025
1 parent 8c55504 commit 14cc54d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/apis/code/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ export function genPreview(tableNames: Array<string>) {
}

/** @desc 生成代码 */
export function generate(tableNames: Array<string>) {
export function downloadCode(tableNames: Array<string>) {
return http.requestNative({
url: `${BASE_URL}/${tableNames}`,
url: `${BASE_URL}/${tableNames}/download`,
method: 'post',
responseType: 'blob',
})
}

/** @desc 生成代码 */
export function generateCode(tableNames: Array<string>) {
return http.requestNative({
url: `${BASE_URL}/${tableNames}`,
method: 'post',
})
}

/** @desc 查询字典列表 */
export function listFieldConfigDict() {
return http.get<LabelValueState[]>(`${BASE_URL}/dict`)
Expand Down
8 changes: 7 additions & 1 deletion src/views/code/generator/GenPreviewModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<template #title>
{{ previewTableNames.length === 1 ? `生成 ${previewTableNames[0]} 表预览` : '批量生成预览' }}
<a-link v-permission="['code:generator:generate']" style="margin-left: 10px" icon @click="onDownload">下载源码</a-link>
<a-link v-permission="['code:generator:generate']" style="margin-left: 10px" icon @click="onGenerator">生成源码</a-link>
</template>
<div class="preview-content">
<a-layout :has-sider="true">
Expand Down Expand Up @@ -71,7 +72,7 @@ import { Message, type TreeNodeData } from '@arco-design/web-vue'
import { useClipboard } from '@vueuse/core'
import { type GeneratePreviewResp, genPreview } from '@/apis/code/generator'
const emit = defineEmits<{ (e: 'generate', previewTableNames: string[]): void }>()
const emit = defineEmits([ 'download','generate'])
const { copy, copied } = useClipboard()
const genPreviewList = ref<GeneratePreviewResp[]>([])
Expand Down Expand Up @@ -128,6 +129,11 @@ const assembleTree = (genPreview: GeneratePreviewResp) => {
// 下载
const onDownload = () => {
emit('download', [previewTableNames.value])
}
// 下载
const onGenerator = () => {
emit('generate', [previewTableNames.value])
}
// 校验文件类型
Expand Down
18 changes: 14 additions & 4 deletions src/views/code/generator/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@
</GiTable>

<GenConfigDrawer ref="GenConfigDrawerRef" @save-success="search" />
<GenPreviewModal ref="GenPreviewModalRef" @generate="onGenerate" />
<GenPreviewModal ref="GenPreviewModalRef" @generate="onGenerate" @download="onDownload" />
</div>
</template>

<script setup lang="ts">
import { Message } from '@arco-design/web-vue'
import GenConfigDrawer from './GenConfigDrawer.vue'
import { generate, listGenConfig } from '@/apis/code/generator'
import { downloadCode, generateCode, listGenConfig } from '@/apis/code/generator'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useTable } from '@/hooks'
import { isMobile } from '@/utils'
Expand Down Expand Up @@ -125,8 +126,8 @@ const onPreview = (tableNames: Array<string>) => {
}
// 生成
const onGenerate = async (tableNames: Array<string>) => {
const res = await generate(tableNames)
const onDownload = async (tableNames: Array<string>) => {
const res = await downloadCode(tableNames)
const contentDisposition = res.headers['content-disposition']
const pattern = /filename=([^;]+\.[^.;]+);*/
const result = pattern.exec(contentDisposition) || ''
Expand All @@ -148,6 +149,15 @@ const onGenerate = async (tableNames: Array<string>) => {
// 释放掉 blob 对象
window.URL.revokeObjectURL(href)
}
// 生成
const onGenerate = async (tableNames: Array<string>) => {
const res = await generateCode(tableNames)
if(res.code === 0){
Message.success('代码生成成功')
}
}
</script>

<style scoped lang="scss"></style>

0 comments on commit 14cc54d

Please sign in to comment.