index.vue 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. <template>
  2. <div class="app-container">
  3. <PageSearch
  4. ref="searchRef"
  5. :search-config="searchConfig"
  6. @query-click="handleQueryClick"
  7. @reset-click="handleResetClick"
  8. />
  9. <PageContent ref="contentRef" :content-config="contentConfig">
  10. <template #toolbar="{ toolbarRight, onToolbar, removeIds, cols }">
  11. <CrudToolbarLeft :remove-ids="removeIds">
  12. <el-row :gutter="10">
  13. <el-col :span="1.5">
  14. <el-button
  15. v-hasPerm="['module_generator:gencode:create']"
  16. type="primary"
  17. icon="Plus"
  18. @click="createTableVisible = true"
  19. >
  20. 创建
  21. </el-button>
  22. </el-col>
  23. <el-col :span="1.5">
  24. <el-button
  25. v-hasPerm="['module_generator:gencode:import']"
  26. type="success"
  27. icon="Upload"
  28. @click="handleImportClick"
  29. >
  30. 导入
  31. </el-button>
  32. </el-col>
  33. <el-col :span="1.5">
  34. <el-button
  35. v-hasPerm="['module_generator:gencode:delete']"
  36. type="danger"
  37. icon="Delete"
  38. :disabled="removeIds.length === 0"
  39. @click="handleDelete()"
  40. >
  41. 批量删除
  42. </el-button>
  43. </el-col>
  44. <el-col :span="1.5">
  45. <el-button
  46. v-hasPerm="['module_generator:gencode:operate']"
  47. type="warning"
  48. icon="Download"
  49. :disabled="removeIds.length === 0"
  50. @click="handleGenTable('0')"
  51. >
  52. 批量生成
  53. </el-button>
  54. </el-col>
  55. </el-row>
  56. </CrudToolbarLeft>
  57. <div class="data-table__toolbar--right">
  58. <CrudToolbarRight :buttons="toolbarRight" :cols="cols" :on-toolbar="onToolbar" />
  59. </div>
  60. </template>
  61. <template #table="{ data, loading: tableLoading, tableRef, onSelectionChange, pagination }">
  62. <div class="data-table__content">
  63. <el-table
  64. :ref="tableRef as any"
  65. v-loading="tableLoading"
  66. row-key="id"
  67. :data="data"
  68. height="100%"
  69. border
  70. stripe
  71. @selection-change="
  72. (s) => {
  73. handleTableSelectionChange(s as GenTableSchema[]);
  74. onSelectionChange(s);
  75. }
  76. "
  77. >
  78. <template #empty>
  79. <el-empty :image-size="80" description="暂无数据" />
  80. </template>
  81. <el-table-column
  82. v-if="contentCols.find((col) => col.prop === 'selection')?.show"
  83. type="selection"
  84. align="center"
  85. width="55"
  86. />
  87. <el-table-column
  88. v-if="contentCols.find((col) => col.prop === 'index')?.show"
  89. label="序号"
  90. type="index"
  91. min-width="30"
  92. align="center"
  93. fixed
  94. >
  95. <template #default="scope">
  96. <span>
  97. {{ (pagination.currentPage - 1) * pagination.pageSize + scope.$index + 1 }}
  98. </span>
  99. </template>
  100. </el-table-column>
  101. <el-table-column
  102. v-if="contentCols.find((col) => col.prop === 'table_name')?.show"
  103. label="表名称"
  104. prop="table_name"
  105. :show-overflow-tooltip="true"
  106. />
  107. <el-table-column
  108. v-if="contentCols.find((col) => col.prop === 'table_comment')?.show"
  109. label="表描述"
  110. prop="table_comment"
  111. :show-overflow-tooltip="true"
  112. />
  113. <el-table-column
  114. v-if="contentCols.find((col) => col.prop === 'class_name')?.show"
  115. label="实体"
  116. prop="class_name"
  117. :show-overflow-tooltip="true"
  118. />
  119. <el-table-column
  120. v-if="contentCols.find((col) => col.prop === 'created_time')?.show"
  121. label="创建时间"
  122. prop="created_time"
  123. />
  124. <el-table-column
  125. v-if="contentCols.find((col) => col.prop === 'updated_time')?.show"
  126. label="更新时间"
  127. prop="updated_time"
  128. />
  129. <el-table-column
  130. v-if="contentCols.find((col) => col.prop === 'operation')?.show"
  131. label="操作"
  132. align="center"
  133. min-width="120"
  134. class-name="small-padding fixed-width"
  135. >
  136. <template #default="scope">
  137. <el-button
  138. v-hasPerm="['module_generator:gencode:update']"
  139. link
  140. type="primary"
  141. :icon="MagicStick"
  142. @click="handlePreviewTable(scope.row)"
  143. >
  144. 代码生成
  145. </el-button>
  146. <el-button
  147. v-hasPerm="['module_generator:gencode:delete']"
  148. link
  149. type="danger"
  150. icon="Delete"
  151. @click="handleDelete(scope.row)"
  152. >
  153. 删除
  154. </el-button>
  155. <el-button
  156. v-hasPerm="['module_generator:db:sync']"
  157. link
  158. type="success"
  159. icon="Refresh"
  160. @click="handleSynchDb(scope.row)"
  161. >
  162. 同步
  163. </el-button>
  164. </template>
  165. </el-table-column>
  166. </el-table>
  167. </div>
  168. </template>
  169. </PageContent>
  170. <CreateTableDialog
  171. v-model="createTableVisible"
  172. :loading="loading"
  173. :link-from-gen="createTableLinkFromGen"
  174. @submit="handleCreateTableSubmit"
  175. />
  176. <ImportDbTableDialog
  177. ref="importDbDialogRef"
  178. v-model="importVisible"
  179. v-model:query="importQueryFormData"
  180. :data="dbTableList"
  181. :total="importTotal"
  182. :confirm-loading="importLoading"
  183. @query="handleImportQuery"
  184. @reset="handleImportReset"
  185. @confirm="handleImportTable"
  186. @fetch="getDbList"
  187. @selection-change="handleImportTableSelectionChange"
  188. />
  189. <GenCodeDrawer
  190. v-model="editVisible"
  191. v-model:preview-scope="previewScope"
  192. v-model:preview-types="previewTypes"
  193. v-model:code="code"
  194. :info="info"
  195. :rules="rules"
  196. :active-step="activeStep"
  197. :menu-options="menuOptions"
  198. :dict-options="dictOptions"
  199. :loading="loading"
  200. :next-step-loading="nextStepLoading"
  201. :preview-loading="previewLoading"
  202. :preview-type-options="previewTypeOptions"
  203. :filtered-tree-data="filteredTreeData"
  204. :cm-options="cmOptions"
  205. :bulk-set="bulkSet"
  206. @close="handleClose"
  207. @prev-step="prevStep"
  208. @next-step="nextStep"
  209. @gen-download="handleGenTable('0', info)"
  210. @gen-write="handleGenTable('1', info)"
  211. @clear-master-sub="clearMasterSub"
  212. @master-sub-blur="onMasterSubFieldBlur"
  213. @file-click="handleFileTreeNodeClick"
  214. @copy-code="handleCopyCode"
  215. />
  216. </div>
  217. </template>
  218. <script setup lang="ts">
  219. defineOptions({
  220. name: "GenCode",
  221. inheritAttrs: false,
  222. });
  223. import { ref, reactive, computed, onActivated, watch, nextTick, unref, provide } from "vue";
  224. import { useClipboard } from "@vueuse/core";
  225. import { useRoute } from "vue-router";
  226. import type { EditorConfiguration } from "codemirror";
  227. import type { CmComponentRef } from "codemirror-editor-vue3";
  228. import { ElMessage, ElMessageBox, type FormInstance } from "element-plus";
  229. import { MagicStick } from "@element-plus/icons-vue";
  230. import GencodeAPI, {
  231. type GenTableSchema,
  232. type DBTableSchema,
  233. type GenTablePageQuery,
  234. } from "@/api/module_generator/gencode";
  235. import MenuAPI, { MenuTable } from "@/api/module_system/menu";
  236. import DictAPI, { DictTable } from "@/api/module_system/dict";
  237. import { MenuTypeEnum } from "@/enums";
  238. import { useSettingsStore } from "@/store";
  239. import { ThemeMode } from "@/enums/settings/theme.enum";
  240. import CrudToolbarLeft from "@/components/CURD/CrudToolbarLeft.vue";
  241. import CrudToolbarRight from "@/components/CURD/CrudToolbarRight.vue";
  242. import PageSearch from "@/components/CURD/PageSearch.vue";
  243. import PageContent from "@/components/CURD/PageContent.vue";
  244. import CreateTableDialog, { type CreateTableSubmitMeta } from "./components/CreateTableDialog.vue";
  245. import GenCodeDrawer from "./components/GenCodeDrawer.vue";
  246. import ImportDbTableDialog from "./components/ImportDbTableDialog.vue";
  247. import { GENCODE_BASIC_FORM_KEY, GENCODE_CM_KEY } from "./gencodeInjectionKeys";
  248. import type { TreeNode } from "./types";
  249. import { useCrudList } from "@/components/CURD/useCrudList";
  250. import type { IContentConfig, ISearchConfig } from "@/components/CURD/types";
  251. // 表格列配置接口
  252. interface TableColumn {
  253. prop: string;
  254. label: string;
  255. show: boolean;
  256. minWidth?: string;
  257. formatter?: (row: any, column: any) => any;
  258. }
  259. // 文件数据接口
  260. interface FileData {
  261. path: string;
  262. file_name: string;
  263. content: string;
  264. full_path: string;
  265. }
  266. const { searchRef, contentRef, handleQueryClick, handleResetClick, refreshList } = useCrudList();
  267. // 组件引用(与子组件 inject 同步,供校验 / CodeMirror 主题)
  268. const cmRef = ref<CmComponentRef>();
  269. const basicInfo = ref<FormInstance>();
  270. const importDbDialogRef = ref<InstanceType<typeof ImportDbTableDialog>>();
  271. provide(GENCODE_BASIC_FORM_KEY, basicInfo);
  272. provide(GENCODE_CM_KEY, cmRef);
  273. // 状态管理
  274. const loading = ref(false);
  275. const nextStepLoading = ref(false);
  276. const uniqueId = ref("");
  277. const editVisible = ref(false);
  278. const activeStep = ref(0);
  279. // UI状态
  280. const createTableVisible = ref(false);
  281. const importVisible = ref(false);
  282. // 表单和列表数据
  283. const dbTableList = ref<DBTableSchema[]>([]);
  284. const ids = ref<number[]>([]);
  285. const tableNames = ref<string[]>([]);
  286. // 导入弹窗专用状态
  287. const importLoading = ref(false);
  288. const importTotal = ref<number>(0);
  289. const importQueryFormData = reactive<GenTablePageQuery>({
  290. page_no: 1,
  291. page_size: 10,
  292. table_name: undefined,
  293. table_comment: undefined,
  294. });
  295. // 下拉选项数据
  296. const dictOptions = ref<DictTable[]>([]);
  297. const menuOptions = ref<OptionType[]>([]);
  298. // 表格数据
  299. type TableItem = {
  300. table_name: string;
  301. table_comment: string;
  302. };
  303. const tables = ref<TableItem[]>([]);
  304. // 导入按钮点击事件
  305. async function handleImportClick() {
  306. importVisible.value = true;
  307. await getDbList();
  308. }
  309. // 预览相关数据
  310. const preview = reactive({
  311. open: false,
  312. title: "代码预览",
  313. data: {},
  314. active_name: "controller.py",
  315. });
  316. /** 预览接口加载中(第三步) */
  317. const previewLoading = ref(false);
  318. const previewScope = ref<"all" | "frontend" | "backend">("all");
  319. const previewTypeOptions = ["ts", "vue", "python"];
  320. const previewTypes = ref<string[]>([...previewTypeOptions]);
  321. const code = ref<string>("");
  322. const treeData = ref<TreeNode[]>([]);
  323. const searchConfig = reactive<ISearchConfig>({
  324. permPrefix: "module_generator:gencode",
  325. colon: true,
  326. isExpandable: false,
  327. showNumber: 3,
  328. form: { labelWidth: "auto" },
  329. searchButtonPerm: "module_generator:gencode:query",
  330. resetButtonPerm: "module_generator:gencode:query",
  331. formItems: [
  332. {
  333. prop: "table_name",
  334. label: "表名称",
  335. type: "input",
  336. attrs: { placeholder: "请输入表名称", clearable: true, style: { width: "200px" } },
  337. },
  338. {
  339. prop: "table_comment",
  340. label: "表描述",
  341. type: "input",
  342. attrs: { placeholder: "请输入表描述", clearable: true, style: { width: "200px" } },
  343. },
  344. ],
  345. });
  346. const contentCols = reactive<TableColumn[]>([
  347. { prop: "selection", label: "选择框", show: true },
  348. { prop: "index", label: "序号", show: true },
  349. { prop: "table_name", label: "表名称", show: true },
  350. { prop: "table_comment", label: "表描述", show: true },
  351. { prop: "class_name", label: "实体", show: true },
  352. { prop: "created_time", label: "创建时间", show: true },
  353. { prop: "updated_time", label: "更新时间", show: true },
  354. { prop: "operation", label: "操作", show: true },
  355. ]);
  356. const contentConfig = reactive<IContentConfig<GenTablePageQuery>>({
  357. permPrefix: "module_generator:gencode",
  358. cols: contentCols as IContentConfig["cols"],
  359. hideColumnFilter: false,
  360. toolbar: [],
  361. defaultToolbar: ["refresh", "filter"],
  362. pagination: {
  363. pageSize: 10,
  364. pageSizes: [10, 20, 30, 50],
  365. },
  366. request: { page_no: "page_no", page_size: "page_size" },
  367. indexAction: async (params) => {
  368. const res = await GencodeAPI.listTable(params as GenTablePageQuery);
  369. return {
  370. total: res.data.data?.total || 0,
  371. list: res.data.data?.items || res.data.data?.list || [],
  372. };
  373. },
  374. });
  375. const settingsStore = useSettingsStore();
  376. // 主题计算属性
  377. const codeTheme = computed(() => (settingsStore.theme === ThemeMode.DARK ? "dracula" : "default"));
  378. // 监听主题变化并更新CodeMirror实例
  379. watch(codeTheme, (newTheme) => {
  380. if (cmRef.value && cmRef.value.cminstance) {
  381. cmRef.value.cminstance.setOption("theme", newTheme);
  382. }
  383. });
  384. // CodeMirror配置
  385. const cmOptions: EditorConfiguration = {
  386. mode: "text/javascript",
  387. lineNumbers: true,
  388. smartIndent: true,
  389. indentUnit: 2,
  390. tabSize: 2,
  391. readOnly: false,
  392. theme: codeTheme.value,
  393. lineWrapping: true,
  394. autofocus: false,
  395. };
  396. // 工具函数
  397. const { copy } = useClipboard();
  398. // ===== 计算属性 =====
  399. // 过滤后的文件树数据
  400. const filteredTreeData = computed<TreeNode[]>(() => {
  401. if (!treeData.value.length) return [];
  402. // 基于原树按 scope/types 过滤叶子节点
  403. const match = (label: string, parentPath: string[]): boolean => {
  404. // scope 过滤:根据路径初步判断
  405. if (previewScope.value !== "all") {
  406. // 根据后端返回的格式,检查路径或文件名特征
  407. const isPythonBackend =
  408. parentPath.some((part) => part === "backend" || part === "python") || label.includes(".py");
  409. const isVueFrontend =
  410. parentPath.some((part) => part === "frontend" || part === "vue") ||
  411. label.includes(".vue") ||
  412. label.includes(".ts");
  413. if (previewScope.value === "backend" && !isPythonBackend) return false;
  414. if (previewScope.value === "frontend" && !isVueFrontend) return false;
  415. }
  416. // 类型过滤:根据文件内容特征判断类型
  417. if (label.endsWith(".py")) return previewTypes.value.includes("python");
  418. if (label.endsWith(".vue")) return previewTypes.value.includes("vue");
  419. if (label.endsWith(".ts")) return previewTypes.value.includes("ts");
  420. return true;
  421. };
  422. const cloneFilter = (node: TreeNode, parents: string[] = []): TreeNode | null => {
  423. if (!node.children || node.children.length === 0) {
  424. return match(node.label, parents) ? { ...node } : null;
  425. }
  426. const nextParents = [...parents, node.label];
  427. const children = (node.children || [])
  428. .map((c) => cloneFilter(c, nextParents))
  429. .filter(Boolean) as TreeNode[];
  430. if (!children.length) return null;
  431. return { label: node.label, children };
  432. };
  433. const filtered = treeData.value.map((n) => cloneFilter(n)).filter(Boolean) as TreeNode[];
  434. return filtered;
  435. });
  436. // ===== 功能函数 =====
  437. /** 一键复制代码 */
  438. const handleCopyCode = () => {
  439. const content = code.value;
  440. if (content) {
  441. copy(content);
  442. ElMessage.success("代码复制成功");
  443. } else {
  444. ElMessage.warning("没有可复制的代码");
  445. }
  446. };
  447. /** 文件树节点点击事件 */
  448. function handleFileTreeNodeClick(data: TreeNode): void {
  449. if (data && (!data.children || data.children.length === 0)) {
  450. code.value = data.content || "";
  451. void nextTick(() => applyPreviewEditorMode(data.label));
  452. }
  453. }
  454. /** 递归构建树形结构 */
  455. function buildTree(data: FileData[]): TreeNode {
  456. // 创建根节点
  457. const root: TreeNode = { label: "前后端代码", children: [] };
  458. data.forEach((item) => {
  459. // 将路径分成数组(确保使用正斜杠)
  460. const parts = item.path.split("/").filter((part) => part !== "");
  461. let currentNode = root;
  462. // 遍历路径部分,创建对应的文件夹节点
  463. parts.forEach((part) => {
  464. // 查找或创建当前部分的子节点
  465. let node = currentNode.children?.find((child) => child.label === part);
  466. if (!node) {
  467. node = { label: part, children: [] };
  468. currentNode.children?.push(node);
  469. }
  470. currentNode = node;
  471. });
  472. // 添加文件节点(保持原有目录树展示:文件节点仅显示文件名)
  473. currentNode.children?.push({
  474. label: item.file_name,
  475. full_path: item.full_path,
  476. content: item?.content,
  477. });
  478. });
  479. return root;
  480. }
  481. /** 深度优先取第一个文件节点 */
  482. function findFirstLeafInTree(nodes: TreeNode[]): TreeNode | null {
  483. for (const node of nodes) {
  484. if (!node.children || node.children.length === 0) {
  485. return node;
  486. }
  487. const leaf = findFirstLeafInTree(node.children);
  488. if (leaf) return leaf;
  489. }
  490. return null;
  491. }
  492. /** 按文件名切换预览区语法高亮 */
  493. function applyPreviewEditorMode(fileLabel: string) {
  494. const inst = cmRef.value?.cminstance;
  495. if (!inst) return;
  496. let mode = "text/javascript";
  497. if (fileLabel.endsWith(".py")) mode = "text/x-python";
  498. else if (fileLabel.endsWith(".vue")) mode = "text/html";
  499. else if (fileLabel.endsWith(".ts")) mode = "text/typescript";
  500. inst.setOption("mode", mode);
  501. }
  502. /** 获取生成预览 */
  503. async function handlePreview(row: GenTableSchema): Promise<void> {
  504. if (!row.id) {
  505. ElMessage.warning("无效的表ID");
  506. return;
  507. }
  508. previewLoading.value = true;
  509. try {
  510. const response = await GencodeAPI.previewTable(row.id!);
  511. const raw = response.data?.data;
  512. if (!raw || typeof raw !== "object" || Object.keys(raw).length === 0) {
  513. ElMessage.warning("预览内容为空,请先保存配置并检查字段与主子表设置");
  514. treeData.value = [];
  515. code.value = "";
  516. preview.data = {};
  517. return;
  518. }
  519. preview.data = raw;
  520. const filesData = Object.entries(raw).map(([key, content]) => {
  521. const pathParts = key.split("/");
  522. let fileName = pathParts.pop() || "";
  523. const path = pathParts.join("/");
  524. if (fileName.endsWith(".j2")) {
  525. fileName = fileName.substring(0, fileName.lastIndexOf(".j2"));
  526. }
  527. const contentStr = typeof content === "string" ? content : JSON.stringify(content);
  528. return {
  529. path,
  530. file_name: fileName,
  531. content: contentStr,
  532. full_path: key,
  533. } as FileData;
  534. });
  535. const treeRoot = buildTree(filesData);
  536. // 预览树仅展示生成文件树(不额外展示“上级目录:xxx”)
  537. treeData.value = [treeRoot];
  538. await nextTick();
  539. let firstLeaf: TreeNode | null = null;
  540. for (const r of filteredTreeData.value) {
  541. firstLeaf = findFirstLeafInTree([r]);
  542. if (firstLeaf) break;
  543. }
  544. if (!firstLeaf) {
  545. firstLeaf = findFirstLeafInTree(treeData.value);
  546. }
  547. code.value = firstLeaf?.content || "";
  548. await nextTick();
  549. if (firstLeaf?.label) {
  550. applyPreviewEditorMode(firstLeaf.label);
  551. }
  552. preview.open = true;
  553. preview.active_name = "model.py";
  554. } catch (error) {
  555. console.error("预览代码失败:", error);
  556. } finally {
  557. previewLoading.value = false;
  558. }
  559. }
  560. /** 表格行内生成代码操作 */
  561. async function handleGenTable(targetGenType: string, row?: GenTableSchema): Promise<void> {
  562. let tbNames: string | string[] = [];
  563. // 判断是单条还是批量操作
  564. if (row) {
  565. tbNames = [row.table_name || ""];
  566. } else if (tableNames.value.length > 0) {
  567. tbNames = tableNames.value;
  568. } else {
  569. ElMessage.error("请选择要生成的数据");
  570. return;
  571. }
  572. loading.value = true;
  573. try {
  574. if (targetGenType === "1") {
  575. if (!Array.isArray(tbNames) || tbNames.length !== 1 || !tbNames[0]) {
  576. ElMessage.error("自定义路径只能生成单表代码");
  577. loading.value = false;
  578. return;
  579. }
  580. if (row?.id) await confirmWritePaths(row.id);
  581. await GencodeAPI.genCodeToPath(tbNames[0]);
  582. ElMessage.success("已写入项目目录并创建菜单(若尚未存在)");
  583. } else {
  584. // ZIP压缩包下载
  585. const tableNamesArray = Array.isArray(tbNames) ? tbNames : [tbNames];
  586. const response = await GencodeAPI.batchGenCode(tableNamesArray);
  587. const raw = response.data as Blob;
  588. if (raw.size < 100 && raw.type.includes("json")) {
  589. const text = await raw.text();
  590. try {
  591. const json = JSON.parse(text) as { msg?: string };
  592. ElMessage.error(json.msg || "批量生成失败");
  593. return;
  594. } catch {
  595. /* 非 JSON 小文件仍尝试下载 */
  596. }
  597. }
  598. const blob = new Blob([raw], { type: "application/zip" });
  599. const url = URL.createObjectURL(blob);
  600. const link = document.createElement("a");
  601. link.href = url;
  602. link.download = "code.zip";
  603. link.click();
  604. URL.revokeObjectURL(url);
  605. ElMessage.success("已开始下载 code.zip");
  606. }
  607. } catch (error) {
  608. console.error("生成代码失败:", error);
  609. } finally {
  610. loading.value = false;
  611. }
  612. }
  613. function escapeHtml(s: string) {
  614. return s
  615. .replaceAll("&", "&amp;")
  616. .replaceAll("<", "&lt;")
  617. .replaceAll(">", "&gt;")
  618. .replaceAll('"', "&quot;")
  619. .replaceAll("'", "&#39;");
  620. }
  621. async function confirmWritePaths(tableId: number) {
  622. // 先保存当前抽屉配置,否则 preview 仍基于旧配置,回显会不准确
  623. await GencodeAPI.updateTable(info as GenTableSchema, tableId);
  624. const previewRes = await GencodeAPI.previewTable(tableId);
  625. const raw = previewRes.data?.data as Record<string, unknown> | undefined;
  626. const keys = raw && typeof raw === "object" ? Object.keys(raw) : [];
  627. const shown = keys.slice(0, 80);
  628. const more =
  629. keys.length > shown.length
  630. ? `<div style="margin-top:10px;padding:8px 12px;border-radius:6px;background:var(--el-fill-color-light);font-size:12px;color:var(--el-text-color-secondary);text-align:center">还有 <b style="color:var(--el-text-color-primary)">${keys.length - shown.length}</b> 个文件未列出</div>`
  631. : "";
  632. const listRows = shown
  633. .map((p, i) => {
  634. const bg = i % 2 === 0 ? "var(--el-fill-color-blank)" : "var(--el-fill-color-light)";
  635. return `<div class="gencode-write-path-row" style="padding:9px 14px;font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;font-size:12px;line-height:1.45;white-space:nowrap;color:var(--el-text-color-primary);background:${bg};border-bottom:1px solid var(--el-border-color-lighter)">${escapeHtml(p)}</div>`;
  636. })
  637. .join("");
  638. const listHtml = shown.length
  639. ? `<div class="gencode-write-path-list-wrap">${listRows}</div>${more}`
  640. : `<div style="padding:16px;border-radius:8px;background:var(--el-fill-color-light);color:var(--el-text-color-secondary);font-size:13px;text-align:center">未获取到预览路径,仍将继续写入。</div>`;
  641. const tipHtml = `<div style="margin-top:12px;padding-top:10px;border-top:1px solid var(--el-border-color-lighter);font-size:12px;line-height:1.5;color:var(--el-text-color-secondary)">与「代码预览」同源;路径为相对项目根的落盘位置。</div>`;
  642. await ElMessageBox.confirm(
  643. `<div class="gencode-write-confirm-body" style="font-family:var(--el-font-family);line-height:1.5;color:var(--el-text-color-primary)">
  644. <div style="margin-bottom:12px">
  645. <div style="font-size:15px;font-weight:600;letter-spacing:0.02em">将写入以下文件</div>
  646. <div style="margin-top:4px;font-size:12px;color:var(--el-text-color-secondary)">共 ${keys.length} 项 · 相对项目根目录</div>
  647. </div>
  648. ${listHtml}
  649. ${shown.length ? tipHtml : ""}
  650. </div>`,
  651. "写入本地确认",
  652. {
  653. confirmButtonText: "确认写入",
  654. cancelButtonText: "取消",
  655. type: "warning",
  656. dangerouslyUseHTMLString: true,
  657. customClass: "gencode-write-confirm-box",
  658. }
  659. );
  660. }
  661. /** 同步数据库操作 */
  662. async function handleSynchDb(row: GenTableSchema): Promise<void> {
  663. const tableName = row.table_name || "";
  664. if (!tableName) {
  665. ElMessage.error("表名不能为空");
  666. return;
  667. }
  668. const renderSummary = (p: any) => {
  669. const added = p.added?.length ?? 0;
  670. const removed = p.removed?.length ?? 0;
  671. const changed = p.changed?.length ?? 0;
  672. const unchanged = p.unchanged ?? 0;
  673. return { added, removed, changed, unchanged };
  674. };
  675. const renderHtml = (title: string, p: any) => {
  676. const s = renderSummary(p);
  677. const list = (xs: string[]) => (xs?.length ? xs.slice(0, 20).join(", ") : "无");
  678. return `
  679. <div style="line-height:1.6">
  680. <div style="font-weight:600;margin-bottom:6px">${title}</div>
  681. <div>新增:<b>${s.added}</b>;删除:<b>${s.removed}</b>;变更:<b>${s.changed}</b>;未变:${s.unchanged}</div>
  682. <div style="margin-top:6px">新增列:${list(p.added || [])}</div>
  683. <div>删除列:${list(p.removed || [])}</div>
  684. <div>变更列:${list((p.changed || []).map((c: any) => c.column_name))}</div>
  685. <div style="margin-top:8px;color:var(--el-text-color-secondary)">提示:同步会尽量保留你已配置的 dict/html/query 等生成项,仅以数据库结构为准更新元信息。</div>
  686. </div>
  687. `;
  688. };
  689. try {
  690. loading.value = true;
  691. const previewRes = await GencodeAPI.syncDbPreview(tableName);
  692. const preview = previewRes.data?.data as any;
  693. const mainHtml = renderHtml(`主表:${tableName}`, preview);
  694. const subHtml =
  695. preview?.sub_table_name && preview?.sub
  696. ? renderHtml(`子表:${preview.sub_table_name}`, preview.sub)
  697. : "";
  698. await ElMessageBox.confirm(`${mainHtml}${subHtml}`, "同步差异预览", {
  699. confirmButtonText: "确认同步",
  700. cancelButtonText: "取消",
  701. type: "warning",
  702. dangerouslyUseHTMLString: true,
  703. });
  704. await GencodeAPI.syncDb(tableName);
  705. ElMessage.success("表结构已同步到代码生成配置");
  706. refreshList();
  707. } catch (error) {
  708. if (error !== "cancel") console.error("同步表结构失败:", error);
  709. } finally {
  710. loading.value = false;
  711. }
  712. }
  713. /** 多选框选中数据 - 主表格 */
  714. function handleTableSelectionChange(selection: GenTableSchema[]): void {
  715. ids.value = selection.map((item) => item.id!);
  716. tableNames.value = selection.map((item) => item.table_name || "").filter(Boolean);
  717. }
  718. type ImportTableSelectionRow = { table_name: string; table_comment: string };
  719. /** 多选框选中数据 - 导入表格 */
  720. function handleImportTableSelectionChange(rows: ImportTableSelectionRow[]): void {
  721. tables.value = rows;
  722. }
  723. /** 代码生成「上级菜单」仅展示目录节点,便于挂到目录下生成新菜单(不选菜单/按钮作为父级) */
  724. const filterMenuTypes = (nodes: MenuTable[]) => {
  725. return nodes
  726. .filter((node) => node.type === MenuTypeEnum.CATALOG)
  727. .map((node: any): any => ({
  728. ...node,
  729. children: node.children ? filterMenuTypes(node.children) : [],
  730. }));
  731. };
  732. /** 代码生成专用:保留 route_path,便于实时推断分系统 module_xxx */
  733. function formatMenuTreeWithMeta(nodes: any[]): any[] {
  734. return nodes.map((node) => {
  735. const formattedNode: any = {
  736. value: node.id,
  737. label: node.name,
  738. disabled: node.status === false || String(node.status) === "false",
  739. route_path: node.route_path,
  740. };
  741. if (node.children && node.children.length > 0) {
  742. formattedNode.children = formatMenuTreeWithMeta(node.children);
  743. }
  744. return formattedNode;
  745. });
  746. }
  747. /** 表格行内「代码生成」:先打开抽屉再拉数据,避免接口慢时误以为点不动 */
  748. async function handlePreviewTable(row?: GenTableSchema): Promise<void> {
  749. const selectedTableId = row?.id ?? ids.value[0];
  750. if (selectedTableId === undefined || selectedTableId === null) {
  751. ElMessage.error("请选择要修改的数据");
  752. return;
  753. }
  754. // 先用“列表行数据”把基础信息回显出来(接口慢时不至于看到空表单/旧数据闪烁)
  755. Object.assign(info, {
  756. id: row?.id ?? selectedTableId,
  757. table_name: row?.table_name || info.table_name || "",
  758. table_comment: row?.table_comment ?? info.table_comment ?? "",
  759. class_name: row?.class_name ?? info.class_name ?? "",
  760. package_name: row?.package_name ?? info.package_name ?? "",
  761. module_name: row?.module_name ?? info.module_name ?? "",
  762. business_name: row?.business_name ?? info.business_name ?? "",
  763. function_name: row?.function_name ?? info.function_name ?? "",
  764. description: row?.description ?? info.description ?? "",
  765. parent_menu_id: row?.parent_menu_id ?? info.parent_menu_id ?? undefined,
  766. sub_table_name: row?.sub_table_name ?? info.sub_table_name ?? "",
  767. sub_table_fk_name: row?.sub_table_fk_name ?? info.sub_table_fk_name ?? "",
  768. } as Partial<GenTableSchema>);
  769. // 字段列表以 detail 接口为准,避免上一张表的 columns 残留
  770. info.columns = [];
  771. activeStep.value = 0;
  772. editVisible.value = true;
  773. try {
  774. await loadTableDetail(selectedTableId);
  775. } catch (e) {
  776. console.error("获取表详情失败:", e);
  777. ElMessage.error("获取表详情失败,请稍后重试");
  778. // 保持抽屉打开,便于重试或关闭;勿因接口失败整抽屉被关掉像「点不动」
  779. return;
  780. }
  781. try {
  782. const [menu_response, dict_response] = await Promise.all([
  783. MenuAPI.listMenu(),
  784. DictAPI.listDictType({ page_no: 1, page_size: 100 }),
  785. ]);
  786. // 使用代码生成专用格式化:保留 route_path 供「分系统」实时回显
  787. menuOptions.value = formatMenuTreeWithMeta(filterMenuTypes(menu_response.data.data));
  788. dictOptions.value = dict_response.data.data?.items || dict_response.data.data?.list || [];
  789. } catch (e) {
  790. console.error("菜单或字典加载失败:", e);
  791. ElMessage.warning("菜单或字典选项加载失败,部分下拉可能为空");
  792. }
  793. }
  794. /** 删除按钮操作 */
  795. async function handleDelete(row?: GenTableSchema): Promise<void> {
  796. const tableIds = row?.id ? [row.id] : ids.value;
  797. if (tableIds.length === 0) {
  798. ElMessage.error("请选择要删除的数据");
  799. return;
  800. }
  801. try {
  802. await ElMessageBox.confirm(`是否确认删除选中的${tableIds.length}条数据?`, "删除确认", {
  803. confirmButtonText: "确定",
  804. cancelButtonText: "取消",
  805. type: "warning",
  806. });
  807. await GencodeAPI.deleteTable(tableIds);
  808. refreshList();
  809. } catch (error) {
  810. if (error !== "cancel") {
  811. console.error("删除表数据失败:", error);
  812. }
  813. }
  814. }
  815. /** 创建表(由 CreateTableDialog 提交 SQL;表结构模式成功后可回写第三步主子表配置) */
  816. async function handleCreateTableSubmit(sql: string, meta?: CreateTableSubmitMeta): Promise<void> {
  817. if (!sql || sql.trim() === "") {
  818. ElMessage.error("请输入创建表SQL语句");
  819. return;
  820. }
  821. loading.value = true;
  822. try {
  823. await GencodeAPI.createTable(sql);
  824. createTableVisible.value = false;
  825. if (editVisible.value && activeStep.value === 2 && meta?.fromVisual && meta.visualSnapshot) {
  826. const v = meta.visualSnapshot;
  827. info.table_name = (v.mainTableName || "").trim();
  828. const mc = (v.mainComment || "").trim();
  829. if (mc) info.table_comment = mc;
  830. if (v.subEnabled) {
  831. info.sub_table_name = (v.subTableName || "").trim();
  832. info.sub_table_fk_name = (v.fkColumn || "").trim();
  833. } else {
  834. info.sub_table_name = "";
  835. info.sub_table_fk_name = "";
  836. }
  837. info.master_sub_hint = undefined;
  838. void nextTick(() => {
  839. basicInfo.value?.clearValidate?.(["table_name", "sub_table_name", "sub_table_fk_name"]);
  840. });
  841. }
  842. refreshList();
  843. importVisible.value = true;
  844. await getDbList();
  845. } catch (error) {
  846. console.error("创建表数据失败:", error);
  847. } finally {
  848. loading.value = false;
  849. }
  850. }
  851. /** 导入表操作 */
  852. async function handleImportTable(): Promise<void> {
  853. if (tables.value.length === 0) {
  854. ElMessage.error("请选择要导入的表");
  855. return;
  856. }
  857. importLoading.value = true;
  858. try {
  859. // 提取表名数组
  860. const tableNames = tables.value.map((table) => table.table_name || "");
  861. await GencodeAPI.importTable(tableNames);
  862. importVisible.value = false;
  863. refreshList();
  864. // 导入成功后自动打开代码生成抽屉
  865. if (tables.value.length === 1) {
  866. await nextTick();
  867. const list = (unref(contentRef.value?.pageData) ?? []) as GenTableSchema[];
  868. const importedTable = list.find((t) => t.table_name === tables.value[0].table_name);
  869. if (importedTable) {
  870. await handlePreviewTable(importedTable);
  871. }
  872. } else {
  873. // 导入了多个表,刷新列表
  874. ElMessage.success(`成功导入 ${tables.value.length} 个表`);
  875. }
  876. } catch (error) {
  877. console.error("导入表失败:", error);
  878. } finally {
  879. importLoading.value = false;
  880. }
  881. }
  882. /** 查询数据库表数据 */
  883. async function getDbList(): Promise<void> {
  884. importLoading.value = true;
  885. try {
  886. const res = await GencodeAPI.listDbTable(importQueryFormData);
  887. if (res.data && res.data.data) {
  888. dbTableList.value = res.data.data?.items || res.data.data?.list || [];
  889. importTotal.value = res.data.data.total;
  890. }
  891. } catch (error) {
  892. console.error("获取数据库表列表失败:", error);
  893. } finally {
  894. importLoading.value = false;
  895. }
  896. }
  897. /** 导入弹窗搜索按钮操作 */
  898. async function handleImportQuery(): Promise<void> {
  899. importQueryFormData.page_no = 1;
  900. await getDbList();
  901. }
  902. /** 导入弹窗重置按钮操作 */
  903. async function handleImportReset(): Promise<void> {
  904. importDbDialogRef.value?.resetQueryForm();
  905. await handleImportQuery();
  906. }
  907. // 路由和导航
  908. const route = useRoute();
  909. // ===== 生命周期和初始化 =====
  910. /** 页面激活时执行 */
  911. onActivated(async () => {
  912. const time = route.query.t;
  913. if (time != null && String(time) !== uniqueId.value) {
  914. uniqueId.value = String(time);
  915. const pageNo = Number(route.query.page_no || 1);
  916. await nextTick();
  917. if (contentRef.value) {
  918. contentRef.value.pagination.currentPage = pageNo;
  919. const q = searchRef.value?.getQueryParams() ?? {};
  920. const f = contentRef.value.getFilterParams?.() ?? {};
  921. contentRef.value.fetchPageData({ ...q, ...f }, false);
  922. }
  923. }
  924. });
  925. // 表单数据(后端返回字段可能含 null,这里做更宽松的承载,避免 TS 因类型收窄报错)
  926. const info = reactive<
  927. GenTableSchema & {
  928. sub_table_name?: string | null;
  929. sub_table_fk_name?: string | null;
  930. }
  931. >({
  932. id: undefined,
  933. table_name: "",
  934. table_comment: "",
  935. sub_table_name: "",
  936. sub_table_fk_name: "",
  937. class_name: "",
  938. package_name: "",
  939. module_name: "",
  940. business_name: "",
  941. function_name: "",
  942. description: "",
  943. parent_menu_id: undefined,
  944. pk_column: undefined,
  945. sub_table: undefined,
  946. columns: [],
  947. sub: false,
  948. master_sub_hint: undefined,
  949. });
  950. /** 代码生成抽屉第三步打开时,创建表弹窗从当前表单预填主/子表名(表结构模式) */
  951. const createTableLinkFromGen = computed(() => {
  952. if (!editVisible.value || activeStep.value !== 2) return null;
  953. return {
  954. table_name: info.table_name,
  955. table_comment: info.table_comment,
  956. sub_table_name: info.sub_table_name ?? undefined,
  957. sub_table_fk_name: info.sub_table_fk_name ?? undefined,
  958. };
  959. });
  960. /** 主子表两项同填或同空,且子表名不得与主表相同 */
  961. function validateMasterSubPair(_rule: unknown, _value: unknown, callback: (e?: Error) => void) {
  962. const sn = (info.sub_table_name || "").trim();
  963. const fk = (info.sub_table_fk_name || "").trim();
  964. if (Boolean(sn) !== Boolean(fk)) {
  965. callback(new Error("子表表名与外键列须同时填写或同时留空"));
  966. return;
  967. }
  968. if (sn && fk && sn === (info.table_name || "").trim()) {
  969. callback(new Error("子表表名不能与主表表名相同"));
  970. return;
  971. }
  972. callback();
  973. }
  974. function onMasterSubFieldBlur() {
  975. void nextTick(() => {
  976. basicInfo.value?.validateField("sub_table_name").catch(() => {});
  977. basicInfo.value?.validateField("sub_table_fk_name").catch(() => {});
  978. });
  979. }
  980. function clearMasterSub() {
  981. info.sub_table_name = "";
  982. info.sub_table_fk_name = "";
  983. info.master_sub_hint = undefined;
  984. info.sub = false;
  985. info.sub_table = undefined;
  986. void nextTick(() => {
  987. basicInfo.value?.clearValidate(["sub_table_name", "sub_table_fk_name"]);
  988. });
  989. }
  990. /** module_example 风格下业务名可空;模块名示例见 demo、gen_demo02 */
  991. function validateBusinessName(_rule: unknown, value: unknown, callback: (e?: Error) => void) {
  992. const pkg = (info.package_name || "").trim();
  993. const mod = (info.module_name || "").trim();
  994. const isExampleStyle = pkg.startsWith("module_") && Boolean(mod) && !mod.startsWith("module_");
  995. if (isExampleStyle) {
  996. callback();
  997. return;
  998. }
  999. if (value == null || !String(value).trim()) {
  1000. callback(new Error("业务名不能为空"));
  1001. return;
  1002. }
  1003. callback();
  1004. }
  1005. // 校验规则
  1006. const rules = {
  1007. table_name: [{ required: true, message: "表名称不能为空", trigger: "blur" }],
  1008. class_name: [{ required: true, message: "实体名称不能为空", trigger: "blur" }],
  1009. package_name: [{ required: true, message: "生成包路径不能为空", trigger: "blur" }],
  1010. module_name: [{ required: true, message: "生成模块名不能为空", trigger: "blur" }],
  1011. business_name: [{ validator: validateBusinessName, trigger: "blur" }],
  1012. function_name: [{ required: true, message: "生成功能名不能为空", trigger: "blur" }],
  1013. /** 与后端一致:可选;不选时写入本地会按包名自动建目录菜单 */
  1014. sub_table_name: [{ validator: validateMasterSubPair, trigger: "blur" }],
  1015. sub_table_fk_name: [{ validator: validateMasterSubPair, trigger: "blur" }],
  1016. };
  1017. // ===== 工具函数
  1018. /** 提交表单 - 保存配置(从基础配置进入字段配置时允许尚无列,便于先保存主表信息) */
  1019. async function submitForm(options?: { requireColumns?: boolean }) {
  1020. const requireColumns = options?.requireColumns !== false;
  1021. // 检查是否有表ID
  1022. if (!info.id) {
  1023. ElMessage.error("无效的表ID");
  1024. return;
  1025. }
  1026. try {
  1027. loading.value = true;
  1028. if (requireColumns && (!info.columns || info.columns.length === 0)) {
  1029. ElMessage.error("请配置字段信息");
  1030. return;
  1031. }
  1032. // 提交表单数据,确保columns是必需的,并且parent_menu_id总是被包含
  1033. const tableData = {
  1034. ...info,
  1035. parent_menu_id: info.parent_menu_id ?? null, // 将undefined转换为null,确保属性被传输
  1036. columns: info.columns || [], // 确保columns存在
  1037. };
  1038. delete (tableData as Record<string, unknown>).sub_table;
  1039. delete (tableData as Record<string, unknown>).sub;
  1040. delete (tableData as Record<string, unknown>).pk_column;
  1041. delete (tableData as Record<string, unknown>).master_sub_hint;
  1042. const savedColumns = info.columns;
  1043. const res = await GencodeAPI.updateTable(tableData as GenTableSchema, info.id || 0);
  1044. if (res.data?.data) {
  1045. Object.assign(info, res.data.data as GenTableSchema);
  1046. if (savedColumns && savedColumns.length > 0) {
  1047. info.columns = savedColumns;
  1048. }
  1049. }
  1050. return true;
  1051. } catch (error) {
  1052. console.error("保存表单失败:", error);
  1053. } finally {
  1054. loading.value = false;
  1055. }
  1056. }
  1057. // 下一步
  1058. async function nextStep(): Promise<void> {
  1059. if (activeStep.value < 3) {
  1060. nextStepLoading.value = true;
  1061. try {
  1062. // 在进入下一步前先保存当前配置
  1063. if (activeStep.value === 0) {
  1064. // 第一步:基础配置
  1065. const basicInfoValid = await basicInfo.value?.validate().catch(() => false);
  1066. if (!basicInfoValid) return;
  1067. } else if (activeStep.value === 1) {
  1068. // 第二步:字段配置
  1069. if (!info.columns || info.columns.length === 0) {
  1070. ElMessage.error("请配置字段信息");
  1071. return;
  1072. }
  1073. }
  1074. // 保存配置:从第 1 步离开时要求已配置列;从第 0 步进入字段配置时允许仅保存基础信息
  1075. const saved = await submitForm({ requireColumns: activeStep.value !== 0 });
  1076. if (!saved) return;
  1077. activeStep.value++;
  1078. // 当从字段配置进入预览步骤时,自动加载预览数据
  1079. if (activeStep.value === 2 && info.id) {
  1080. await handlePreview({ id: info.id, table_name: info.table_name } as GenTableSchema);
  1081. }
  1082. } finally {
  1083. nextStepLoading.value = false;
  1084. }
  1085. }
  1086. }
  1087. // 上一步
  1088. function prevStep(): void {
  1089. if (activeStep.value > 0) {
  1090. activeStep.value--;
  1091. }
  1092. }
  1093. // 批量设置字段属性
  1094. function bulkSet(field: string | string[], value: any): void {
  1095. if (!info.columns || !Array.isArray(info.columns)) return;
  1096. const fieldsToUpdate = Array.isArray(field) ? field : [field];
  1097. info.columns.forEach((column) => {
  1098. if (column && typeof column === "object") {
  1099. fieldsToUpdate.forEach((f) => {
  1100. (column as any)[f] = value;
  1101. });
  1102. }
  1103. });
  1104. }
  1105. function close(): void {
  1106. editVisible.value = false;
  1107. activeStep.value = 0; // 重置步骤
  1108. // 清除表单验证状态
  1109. setTimeout(() => {
  1110. basicInfo.value?.resetFields();
  1111. }, 300);
  1112. }
  1113. /** 处理抽屉关闭事件 */
  1114. function handleClose(): void {
  1115. close();
  1116. }
  1117. /** 加载表详情 */
  1118. async function loadTableDetail(id: number | string) {
  1119. try {
  1120. loading.value = true;
  1121. const response = await GencodeAPI.detailTable(Number(id));
  1122. if (response?.data?.data) {
  1123. const data = response.data.data;
  1124. // 填充表单数据
  1125. Object.assign(info, { ...data });
  1126. // 处理列数据
  1127. if (data && data.columns && Array.isArray(data.columns)) {
  1128. // 深拷贝确保数据独立性
  1129. info.columns = JSON.parse(JSON.stringify(data.columns));
  1130. // 设置列的选中状态
  1131. (info.columns ?? []).forEach((item: any) => {
  1132. item.select = true;
  1133. });
  1134. }
  1135. // 重置当前步骤为第一步
  1136. activeStep.value = 0;
  1137. }
  1138. } catch (error) {
  1139. console.error("获取表详情失败:", error);
  1140. throw error;
  1141. } finally {
  1142. loading.value = false;
  1143. }
  1144. }
  1145. </script>