From a1d7e81859f554f3a53680cc35f0f49bf1f77098 Mon Sep 17 00:00:00 2001
From: wwf <1971391498@qq.com>
Date: 星期四, 14 五月 2026 14:37:02 +0800
Subject: [PATCH] 导入项目

---
 src/views/infra/codegen/PreviewCode.vue |  222 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 222 insertions(+), 0 deletions(-)

diff --git a/src/views/infra/codegen/PreviewCode.vue b/src/views/infra/codegen/PreviewCode.vue
new file mode 100644
index 0000000..819fca6
--- /dev/null
+++ b/src/views/infra/codegen/PreviewCode.vue
@@ -0,0 +1,222 @@
+<template>
+  <Dialog
+    v-model="dialogVisible"
+    align-center
+    class="app-infra-codegen-preview-container"
+    title="浠g爜棰勮"
+    width="80%"
+  >
+    <div class="flex">
+      <!-- 浠g爜鐩綍鏍� -->
+      <el-card
+        v-loading="loading"
+        :gutter="12"
+        class="w-1/3"
+        element-loading-text="鐢熸垚鏂囦欢鐩綍涓�..."
+        shadow="hover"
+      >
+        <el-scrollbar height="calc(100vh - 88px - 40px)">
+          <el-tree
+            ref="treeRef"
+            :data="preview.fileTree"
+            :expand-on-click-node="false"
+            default-expand-all
+            highlight-current
+            node-key="id"
+            @node-click="handleNodeClick"
+          />
+        </el-scrollbar>
+      </el-card>
+      <!-- 浠g爜 -->
+      <el-card
+        v-loading="loading"
+        :gutter="12"
+        class="ml-3 w-2/3"
+        element-loading-text="鍔犺浇浠g爜涓�..."
+        shadow="hover"
+      >
+        <el-tabs v-model="preview.activeName">
+          <el-tab-pane
+            v-for="item in previewCodegen"
+            :key="item.filePath"
+            :label="item.filePath.substring(item.filePath.lastIndexOf('/') + 1)"
+            :name="item.filePath"
+          >
+            <el-button class="float-right" text type="primary" @click="copy(item.code)">
+              {{ t('common.copy') }}
+            </el-button>
+            <el-scrollbar height="600px">
+              <pre><code v-dompurify-html="highlightedCode(item)" class="hljs"></code></pre>
+            </el-scrollbar>
+          </el-tab-pane>
+        </el-tabs>
+      </el-card>
+    </div>
+  </Dialog>
+</template>
+<script lang="ts" setup>
+import { useClipboard } from '@vueuse/core'
+import { handleTree2 } from '@/utils/tree'
+import * as CodegenApi from '@/api/infra/codegen'
+
+import hljs from 'highlight.js' // 瀵煎叆浠g爜楂樹寒鏂囦欢
+import 'highlight.js/styles/github.css' // 瀵煎叆浠g爜楂樹寒鏍峰紡
+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'
+
+defineOptions({ name: 'InfraCodegenPreviewCode' })
+
+const { t } = useI18n() // 鍥介檯鍖�
+const message = useMessage() // 娑堟伅寮圭獥
+
+const dialogVisible = ref(false) // 寮圭獥鐨勬槸鍚﹀睍绀�
+const loading = ref(false) // 鍔犺浇涓殑鐘舵��
+const preview = reactive({
+  fileTree: [], // 鏂囦欢鏍�
+  activeName: '' // 婵�娲荤殑鏂囦欢鍚�
+})
+const previewCodegen = ref<CodegenApi.CodegenPreviewVO[]>()
+
+/** 鐐瑰嚮鏂囦欢 */
+const handleNodeClick = async (data, node) => {
+  if (node && !node.isLeaf) {
+    return false
+  }
+  preview.activeName = data.id
+}
+
+/** 鐢熸垚 files 鐩綍 **/
+interface filesType {
+  id: string
+  label: string
+  parentId: string
+}
+
+/** 鎵撳紑寮圭獥 */
+const open = async (id: number) => {
+  dialogVisible.value = true
+  try {
+    loading.value = true
+    // 鐢熸垚浠g爜
+    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[]) => {
+  let exists = {} // key锛歠ile 鐨� id锛泇alue锛歵rue
+  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
+}
+
+/** 澶嶅埗 **/
+const copy = async (text: string) => {
+  const { copy, copied, isSupported } = useClipboard({ legacy: true, source: text })
+  if (!isSupported) {
+    message.error(t('common.copyError'))
+    return
+  }
+  await copy()
+  if (unref(copied)) {
+    message.success(t('common.copySuccess'))
+  }
+}
+
+/**
+ * 浠g爜楂樹寒
+ */
+const highlightedCode = (item) => {
+  const language = item.filePath.substring(item.filePath.lastIndexOf('.') + 1)
+  const result = hljs.highlight(language, item.code || '', true)
+  return result.value || '&nbsp;'
+}
+
+/** 鍒濆鍖� **/
+onMounted(async () => {
+  // 娉ㄥ唽浠g爜楂樹寒鐨勫悇绉嶈瑷�
+  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)
+})
+</script>
+<style lang="scss">
+.app-infra-codegen-preview-container {
+  .el-scrollbar .el-scrollbar__wrap .el-scrollbar__view {
+    display: inline-block;
+    white-space: nowrap;
+  }
+}
+</style>

--
Gitblit v1.8.0