2023-02-11 00:44:00 +08:00
|
|
|
|
<template>
|
2023-03-27 14:06:30 +08:00
|
|
|
|
<Dialog
|
2023-04-02 23:52:56 +08:00
|
|
|
|
v-model="dialogVisible"
|
2023-03-27 14:06:30 +08:00
|
|
|
|
align-center
|
|
|
|
|
class="app-infra-codegen-preview-container"
|
2023-04-14 21:32:11 +08:00
|
|
|
|
title="代码预览"
|
|
|
|
|
width="80%"
|
2023-03-27 14:06:30 +08:00
|
|
|
|
>
|
2023-02-11 00:44:00 +08:00
|
|
|
|
<div class="flex">
|
2023-03-29 22:25:53 +08:00
|
|
|
|
<!-- 代码目录树 -->
|
|
|
|
|
<el-card
|
|
|
|
|
v-loading="loading"
|
2023-04-14 21:32:11 +08:00
|
|
|
|
:gutter="12"
|
|
|
|
|
class="w-1/3"
|
2023-03-29 22:25:53 +08:00
|
|
|
|
element-loading-text="生成文件目录中..."
|
2023-04-14 21:32:11 +08:00
|
|
|
|
shadow="hover"
|
2023-03-29 22:25:53 +08:00
|
|
|
|
>
|
2023-04-13 00:21:32 +08:00
|
|
|
|
<el-scrollbar height="calc(100vh - 88px - 40px)">
|
2023-02-11 00:44:00 +08:00
|
|
|
|
<el-tree
|
|
|
|
|
ref="treeRef"
|
|
|
|
|
:data="preview.fileTree"
|
|
|
|
|
:expand-on-click-node="false"
|
|
|
|
|
highlight-current
|
2023-09-22 17:49:11 +08:00
|
|
|
|
default-expand-all
|
2023-04-14 21:32:11 +08:00
|
|
|
|
node-key="id"
|
2023-02-11 00:44:00 +08:00
|
|
|
|
@node-click="handleNodeClick"
|
|
|
|
|
/>
|
|
|
|
|
</el-scrollbar>
|
|
|
|
|
</el-card>
|
2023-03-29 22:25:53 +08:00
|
|
|
|
<!-- 代码 -->
|
|
|
|
|
<el-card
|
|
|
|
|
v-loading="loading"
|
2023-04-14 21:32:11 +08:00
|
|
|
|
:gutter="12"
|
2023-09-22 17:49:11 +08:00
|
|
|
|
class="ml-3 w-2/3"
|
2023-03-29 22:25:53 +08:00
|
|
|
|
element-loading-text="加载代码中..."
|
2023-04-14 21:32:11 +08:00
|
|
|
|
shadow="hover"
|
2023-03-29 22:25:53 +08:00
|
|
|
|
>
|
2023-02-11 00:44:00 +08:00
|
|
|
|
<el-tabs v-model="preview.activeName">
|
|
|
|
|
<el-tab-pane
|
|
|
|
|
v-for="item in previewCodegen"
|
2023-04-14 21:32:11 +08:00
|
|
|
|
:key="item.filePath"
|
2023-02-11 00:44:00 +08:00
|
|
|
|
:label="item.filePath.substring(item.filePath.lastIndexOf('/') + 1)"
|
|
|
|
|
:name="item.filePath"
|
|
|
|
|
>
|
2023-04-14 21:32:11 +08:00
|
|
|
|
<el-button class="float-right" text type="primary" @click="copy(item.code)">
|
2023-03-27 14:06:30 +08:00
|
|
|
|
{{ t('common.copy') }}
|
|
|
|
|
</el-button>
|
2023-08-17 14:56:13 +08:00
|
|
|
|
<el-scrollbar height="600px">
|
2023-05-31 15:58:35 +08:00
|
|
|
|
<pre><code v-dompurify-html="highlightedCode(item)" class="hljs"></code></pre>
|
2023-08-17 14:56:13 +08:00
|
|
|
|
</el-scrollbar>
|
2023-02-11 00:44:00 +08:00
|
|
|
|
</el-tab-pane>
|
|
|
|
|
</el-tabs>
|
|
|
|
|
</el-card>
|
|
|
|
|
</div>
|
2023-03-27 14:06:30 +08:00
|
|
|
|
</Dialog>
|
2023-02-11 00:44:00 +08:00
|
|
|
|
</template>
|
2023-06-21 19:14:34 +08:00
|
|
|
|
<script lang="ts" setup>
|
2023-02-11 00:44:00 +08:00
|
|
|
|
import { useClipboard } from '@vueuse/core'
|
|
|
|
|
import { handleTree2 } from '@/utils/tree'
|
2023-03-27 14:06:30 +08:00
|
|
|
|
import * as CodegenApi from '@/api/infra/codegen'
|
2023-04-14 21:32:11 +08:00
|
|
|
|
|
2023-04-23 19:35:14 +08:00
|
|
|
|
import hljs from 'highlight.js' // 导入代码高亮文件
|
|
|
|
|
import 'highlight.js/styles/github.css' // 导入代码高亮样式
|
|
|
|
|
import java from 'highlight.js/lib/languages/java'
|
|
|
|
|
import xml from 'highlight.js/lib/languages/java'
|
|
|
|
|
import javascript from 'highlight.js/lib/languages/javascript'
|
|
|
|
|
import sql from 'highlight.js/lib/languages/sql'
|
|
|
|
|
import typescript from 'highlight.js/lib/languages/typescript'
|
|
|
|
|
|
2023-06-21 19:14:34 +08:00
|
|
|
|
defineOptions({ name: 'InfraCodegenPreviewCode' })
|
|
|
|
|
|
2023-02-11 00:44:00 +08:00
|
|
|
|
const { t } = useI18n() // 国际化
|
|
|
|
|
const message = useMessage() // 消息弹窗
|
2023-03-27 14:06:30 +08:00
|
|
|
|
|
2023-04-02 23:52:56 +08:00
|
|
|
|
const dialogVisible = ref(false) // 弹窗的是否展示
|
2023-03-29 22:25:53 +08:00
|
|
|
|
const loading = ref(false) // 加载中的状态
|
2023-02-11 00:44:00 +08:00
|
|
|
|
const preview = reactive({
|
2023-03-29 22:25:53 +08:00
|
|
|
|
fileTree: [], // 文件树
|
|
|
|
|
activeName: '' // 激活的文件名
|
2023-02-11 00:44:00 +08:00
|
|
|
|
})
|
2023-03-29 22:25:53 +08:00
|
|
|
|
const previewCodegen = ref<CodegenApi.CodegenPreviewVO[]>()
|
2023-03-27 14:06:30 +08:00
|
|
|
|
|
2023-03-29 22:25:53 +08:00
|
|
|
|
/** 点击文件 */
|
2023-02-11 00:44:00 +08:00
|
|
|
|
const handleNodeClick = async (data, node) => {
|
|
|
|
|
if (node && !node.isLeaf) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
preview.activeName = data.id
|
|
|
|
|
}
|
2023-03-29 22:25:53 +08:00
|
|
|
|
|
2023-02-11 00:44:00 +08:00
|
|
|
|
/** 生成 files 目录 **/
|
|
|
|
|
interface filesType {
|
|
|
|
|
id: string
|
|
|
|
|
label: string
|
|
|
|
|
parentId: string
|
|
|
|
|
}
|
2023-03-29 22:25:53 +08:00
|
|
|
|
|
|
|
|
|
/** 打开弹窗 */
|
|
|
|
|
const open = async (id: number) => {
|
2023-04-02 23:52:56 +08:00
|
|
|
|
dialogVisible.value = true
|
2023-03-29 22:25:53 +08:00
|
|
|
|
try {
|
|
|
|
|
loading.value = true
|
|
|
|
|
// 生成代码
|
|
|
|
|
const data = await CodegenApi.previewCodegen(id)
|
|
|
|
|
previewCodegen.value = data
|
|
|
|
|
// 处理文件
|
|
|
|
|
let file = handleFiles(data)
|
|
|
|
|
preview.fileTree = handleTree2(file, 'id', 'parentId', 'children', '/')
|
|
|
|
|
// 点击首个文件
|
|
|
|
|
preview.activeName = data[0].filePath
|
|
|
|
|
} finally {
|
|
|
|
|
loading.value = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
|
|
|
|
|
|
|
|
|
|
/** 处理文件 */
|
|
|
|
|
const handleFiles = (datas: CodegenApi.CodegenPreviewVO[]) => {
|
2023-02-11 00:44:00 +08:00
|
|
|
|
let exists = {} // key:file 的 id;value:true
|
|
|
|
|
let files: filesType[] = []
|
|
|
|
|
// 遍历每个元素
|
|
|
|
|
for (const data of datas) {
|
|
|
|
|
let paths = data.filePath.split('/')
|
|
|
|
|
let fullPath = '' // 从头开始的路径,用于生成 id
|
|
|
|
|
// 特殊处理 java 文件
|
|
|
|
|
if (paths[paths.length - 1].indexOf('.java') >= 0) {
|
|
|
|
|
let newPaths: string[] = []
|
|
|
|
|
for (let i = 0; i < paths.length; i++) {
|
|
|
|
|
let path = paths[i]
|
|
|
|
|
if (path !== 'java') {
|
|
|
|
|
newPaths.push(path)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
newPaths.push(path)
|
|
|
|
|
// 特殊处理中间的 package,进行合并
|
|
|
|
|
let tmp = ''
|
|
|
|
|
while (i < paths.length) {
|
|
|
|
|
path = paths[i + 1]
|
|
|
|
|
if (
|
|
|
|
|
path === 'controller' ||
|
|
|
|
|
path === 'convert' ||
|
|
|
|
|
path === 'dal' ||
|
|
|
|
|
path === 'enums' ||
|
|
|
|
|
path === 'service' ||
|
|
|
|
|
path === 'vo' || // 下面三个,主要是兜底。可能考虑到有人改了包结构
|
|
|
|
|
path === 'mysql' ||
|
|
|
|
|
path === 'dataobject'
|
|
|
|
|
) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
tmp = tmp ? tmp + '.' + path : path
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
if (tmp) {
|
|
|
|
|
newPaths.push(tmp)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
paths = newPaths
|
|
|
|
|
}
|
|
|
|
|
// 遍历每个 path, 拼接成树
|
|
|
|
|
for (let i = 0; i < paths.length; i++) {
|
|
|
|
|
// 已经添加到 files 中,则跳过
|
|
|
|
|
let oldFullPath = fullPath
|
|
|
|
|
// 下面的 replaceAll 的原因,是因为上面包处理了,导致和 tabs 不匹配,所以 replaceAll 下
|
|
|
|
|
fullPath = fullPath.length === 0 ? paths[i] : fullPath.replaceAll('.', '/') + '/' + paths[i]
|
|
|
|
|
if (exists[fullPath]) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// 添加到 files 中
|
|
|
|
|
exists[fullPath] = true
|
|
|
|
|
files.push({
|
|
|
|
|
id: fullPath,
|
|
|
|
|
label: paths[i],
|
|
|
|
|
parentId: oldFullPath || '/' // "/" 为根节点
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return files
|
|
|
|
|
}
|
2023-03-29 22:25:53 +08:00
|
|
|
|
|
2023-02-11 00:44:00 +08:00
|
|
|
|
/** 复制 **/
|
|
|
|
|
const copy = async (text: string) => {
|
|
|
|
|
const { copy, copied, isSupported } = useClipboard({ source: text })
|
|
|
|
|
if (!isSupported) {
|
|
|
|
|
message.error(t('common.copyError'))
|
2023-03-27 14:06:30 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
await copy()
|
|
|
|
|
if (unref(copied)) {
|
|
|
|
|
message.success(t('common.copySuccess'))
|
2023-02-11 00:44:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-15 21:36:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 代码高亮
|
|
|
|
|
*/
|
|
|
|
|
const highlightedCode = (item) => {
|
|
|
|
|
const language = item.filePath.substring(item.filePath.lastIndexOf('.') + 1)
|
|
|
|
|
const result = hljs.highlight(language, item.code || '', true)
|
|
|
|
|
return result.value || ' '
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 初始化 **/
|
|
|
|
|
onMounted(async () => {
|
|
|
|
|
// 注册代码高亮的各种语言
|
|
|
|
|
hljs.registerLanguage('java', java)
|
|
|
|
|
hljs.registerLanguage('xml', xml)
|
|
|
|
|
hljs.registerLanguage('html', xml)
|
|
|
|
|
hljs.registerLanguage('vue', xml)
|
|
|
|
|
hljs.registerLanguage('javascript', javascript)
|
|
|
|
|
hljs.registerLanguage('sql', sql)
|
|
|
|
|
hljs.registerLanguage('typescript', typescript)
|
|
|
|
|
})
|
2023-02-11 00:44:00 +08:00
|
|
|
|
</script>
|
2023-03-27 14:06:30 +08:00
|
|
|
|
<style lang="scss">
|
|
|
|
|
.app-infra-codegen-preview-container {
|
|
|
|
|
.el-scrollbar .el-scrollbar__wrap .el-scrollbar__view {
|
|
|
|
|
display: inline-block;
|
2023-08-04 21:33:00 +08:00
|
|
|
|
white-space: nowrap;
|
2023-03-27 14:06:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</style>
|