index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <template>
  2. <div class="app-container">
  3. <doc-alert title="代码生成" url="https://doc.iocoder.cn/new-feature/" />
  4. <doc-alert title="单元测试" url="https://doc.iocoder.cn/unit-test/" />
  5. <!-- 操作工作栏 -->
  6. <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
  7. <el-form-item label="表名称" prop="tableName">
  8. <el-input v-model="queryParams.tableName" placeholder="请输入表名称" clearable
  9. @keyup.enter.native="handleQuery"/>
  10. </el-form-item>
  11. <el-form-item label="表描述" prop="tableComment">
  12. <el-input v-model="queryParams.tableComment" placeholder="请输入表描述" clearable
  13. @keyup.enter.native="handleQuery"/>
  14. </el-form-item>
  15. <el-form-item label="创建时间" prop="createTime">
  16. <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
  17. range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
  18. </el-form-item>
  19. <el-form-item>
  20. <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
  21. <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
  22. </el-form-item>
  23. </el-form>
  24. <!-- 操作工作栏 -->
  25. <el-row :gutter="10" class="mb8">
  26. <el-col :span="1.5">
  27. <el-button type="info" plain icon="el-icon-upload" size="mini" @click="openImportTable"
  28. v-hasPermi="['infra:codegen:create']">导入</el-button>
  29. </el-col>
  30. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  31. </el-row>
  32. <!-- 列表 -->
  33. <el-table v-loading="loading" :data="tableList">
  34. <el-table-column label="数据源" align="center" :formatter="dataSourceConfigNameFormat"/>
  35. <el-table-column label="表名称" align="center" prop="tableName" width="200"/>
  36. <el-table-column label="表描述" align="center" prop="tableComment" :show-overflow-tooltip="true" width="120"/>
  37. <el-table-column label="实体" align="center" prop="className" width="200"/>
  38. <el-table-column label="创建时间" align="center" prop="createTime" width="180">
  39. <template v-slot="scope">
  40. <span>{{ parseTime(scope.row.createTime) }}</span>
  41. </template>
  42. </el-table-column>
  43. <el-table-column label="更新时间" align="center" prop="createTime" width="180">
  44. <template v-slot="scope">
  45. <span>{{ parseTime(scope.row.updateTime) }}</span>
  46. </template>
  47. </el-table-column>
  48. <el-table-column label="操作" align="center" width="300px" class-name="small-padding fixed-width">
  49. <template v-slot="scope">
  50. <el-button type="text" size="small" icon="el-icon-view" @click="handlePreview(scope.row)" v-hasPermi="['infra:codegen:preview']">预览</el-button>
  51. <el-button type="text" size="small" icon="el-icon-edit" @click="handleEditTable(scope.row)" v-hasPermi="['infra:codegen:update']">编辑</el-button>
  52. <el-button type="text" size="small" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['infra:codegen:delete']">删除</el-button>
  53. <el-button type="text" size="small" icon="el-icon-refresh" @click="handleSynchDb(scope.row)" v-hasPermi="['infra:codegen:update']">同步</el-button>
  54. <el-button type="text" size="small" icon="el-icon-download" @click="handleGenTable(scope.row)" v-hasPermi="['infra:codegen:download']">生成代码</el-button>
  55. </template>
  56. </el-table-column>
  57. </el-table>
  58. <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList"/>
  59. <!-- 预览界面 -->
  60. <el-dialog :title="preview.title" :visible.sync="preview.open" width="90%" top="5vh" append-to-body class="scrollbar">
  61. <el-row>
  62. <el-col :span="7">
  63. <el-tree :data="preview.fileTree" :expand-on-click-node="false" default-expand-all highlight-current
  64. @node-click="handleNodeClick"/>
  65. </el-col>
  66. <el-col :span="17">
  67. <el-tabs v-model="preview.activeName">
  68. <el-tab-pane v-for="item in preview.data" :label="item.filePath.substring(item.filePath.lastIndexOf('/') + 1)"
  69. :name="item.filePath" :key="item.filePath">
  70. <el-link :underline="false" icon="el-icon-document-copy" v-clipboard:copy="item.code" v-clipboard:success="clipboardSuccess" style="float:right">复制</el-link>
  71. <pre><code class="hljs" v-html="highlightedCode(item)"></code></pre>
  72. </el-tab-pane>
  73. </el-tabs>
  74. </el-col>
  75. </el-row>
  76. </el-dialog>
  77. <!-- 基于 DB 导入 -->
  78. <import-table ref="import" @ok="handleQuery" />
  79. </div>
  80. </template>
  81. <script>
  82. import { getCodegenTablePage, previewCodegen, downloadCodegen, deleteCodegen,
  83. syncCodegenFromDB } from "@/api/infra/codegen";
  84. import importTable from "./importTable";
  85. // 代码高亮插件
  86. import hljs from "highlight.js/lib/highlight";
  87. import "highlight.js/styles/github-gist.css";
  88. import {getDataSourceConfigList} from "@/api/infra/dataSourceConfig";
  89. hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
  90. hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml"));
  91. hljs.registerLanguage("html", require("highlight.js/lib/languages/xml"));
  92. hljs.registerLanguage("vue", require("highlight.js/lib/languages/xml"));
  93. hljs.registerLanguage("javascript", require("highlight.js/lib/languages/javascript"));
  94. hljs.registerLanguage("sql", require("highlight.js/lib/languages/sql"));
  95. hljs.registerLanguage("typescript", require("highlight.js/lib/languages/typescript"));
  96. export default {
  97. name: "Codegen",
  98. components: { importTable },
  99. data() {
  100. return {
  101. // 遮罩层
  102. loading: true,
  103. // 唯一标识符
  104. uniqueId: "",
  105. // 选中表数组
  106. tableNames: [],
  107. // 显示搜索条件
  108. showSearch: true,
  109. // 总条数
  110. total: 0,
  111. // 表数据
  112. tableList: [],
  113. // 日期范围
  114. dateRange: "",
  115. // 查询参数
  116. queryParams: {
  117. pageNo: 1,
  118. pageSize: 10,
  119. tableName: undefined,
  120. tableComment: undefined,
  121. createTime: []
  122. },
  123. // 预览参数
  124. preview: {
  125. open: false,
  126. title: "代码预览",
  127. fileTree: [],
  128. data: {},
  129. activeName: "",
  130. },
  131. // 数据源列表
  132. dataSourceConfigs: [],
  133. };
  134. },
  135. created() {
  136. this.getList();
  137. // 加载数据源
  138. getDataSourceConfigList().then(response => {
  139. this.dataSourceConfigs = response.data;
  140. });
  141. },
  142. activated() {
  143. const time = this.$route.query.t;
  144. if (time != null && time !== this.uniqueId) {
  145. this.uniqueId = time;
  146. this.resetQuery();
  147. }
  148. },
  149. methods: {
  150. /** 查询表集合 */
  151. getList() {
  152. this.loading = true;
  153. getCodegenTablePage(this.queryParams).then(response => {
  154. this.tableList = response.data.list;
  155. this.total = response.data.total;
  156. this.loading = false;
  157. }
  158. );
  159. },
  160. /** 搜索按钮操作 */
  161. handleQuery() {
  162. this.queryParams.pageNo = 1;
  163. this.getList();
  164. },
  165. /** 生成代码操作 */
  166. handleGenTable(row) {
  167. downloadCodegen(row.id).then(response => {
  168. this.$download.zip(response, 'codegen-' + row.tableName + '.zip');
  169. })
  170. },
  171. /** 同步数据库操作 */
  172. handleSynchDb(row) {
  173. // 基于 DB 同步
  174. const tableName = row.tableName;
  175. this.$modal.confirm('确认要强制同步"' + tableName + '"表结构吗?').then(function() {
  176. return syncCodegenFromDB(row.id);
  177. }).then(() => {
  178. this.$modal.msgSuccess("同步成功");
  179. }).catch(() => {});
  180. },
  181. /** 打开导入表弹窗 */
  182. openImportTable() {
  183. this.$refs.import.show();
  184. },
  185. /** 重置按钮操作 */
  186. resetQuery() {
  187. this.resetForm("queryForm");
  188. this.handleQuery();
  189. },
  190. /** 预览按钮 */
  191. handlePreview(row) {
  192. previewCodegen(row.id).then(response => {
  193. this.preview.data = response.data;
  194. let files = this.handleFiles(response.data);
  195. this.preview.fileTree = this.handleTree(files, "id", "parentId", "children",
  196. "/"); // "/" 为根节点
  197. // console.log(this.preview.fileTree)
  198. this.preview.activeName = response.data[0].filePath;
  199. this.preview.open = true;
  200. });
  201. },
  202. /** 高亮显示 */
  203. highlightedCode(item) {
  204. // const vmName = key.substring(key.lastIndexOf("/") + 1, key.indexOf(".vm"));
  205. // var language = vmName.substring(vmName.indexOf(".") + 1, vmName.length);
  206. var language = item.filePath.substring(item.filePath.lastIndexOf(".") + 1);
  207. const result = hljs.highlight(language, item.code || "", true);
  208. return result.value || '&nbsp;';
  209. },
  210. /** 复制代码成功 */
  211. clipboardSuccess() {
  212. this.$modal.msgSuccess("复制成功");
  213. },
  214. /** 生成 files 目录 **/
  215. handleFiles(datas) {
  216. let exists = {}; // key:file 的 id;value:true
  217. let files = [];
  218. // 遍历每个元素
  219. for (const data of datas) {
  220. let paths = data.filePath.split('/');
  221. let fullPath = ''; // 从头开始的路径,用于生成 id
  222. // 特殊处理 java 文件
  223. if (paths[paths.length - 1].indexOf('.java') >= 0) {
  224. let newPaths = [];
  225. for (let i = 0; i < paths.length; i++) {
  226. let path = paths[i];
  227. if (path !== 'java') {
  228. newPaths.push(path);
  229. continue;
  230. }
  231. newPaths.push(path);
  232. // 特殊处理中间的 package,进行合并
  233. let tmp = undefined;
  234. while (i < paths.length) {
  235. path = paths[i + 1];
  236. if (path === 'controller'
  237. || path === 'convert'
  238. || path === 'dal'
  239. || path === 'enums'
  240. || path === 'service'
  241. || path === 'vo' // 下面三个,主要是兜底。可能考虑到有人改了包结构
  242. || path === 'mysql'
  243. || path === 'dataobject') {
  244. break;
  245. }
  246. tmp = tmp ? tmp + '.' + path : path;
  247. i++;
  248. }
  249. if (tmp) {
  250. newPaths.push(tmp);
  251. }
  252. }
  253. paths = newPaths;
  254. }
  255. // 遍历每个 path, 拼接成树
  256. for (let i = 0; i < paths.length; i++) {
  257. // 已经添加到 files 中,则跳过
  258. let oldFullPath = fullPath;
  259. // 下面的 replaceAll 的原因,是因为上面包处理了,导致和 tabs 不匹配,所以 replaceAll 下
  260. fullPath = fullPath.length === 0 ? paths[i] : fullPath.replaceAll('.', '/') + '/' + paths[i];
  261. if (exists[fullPath]) {
  262. continue;
  263. }
  264. // 添加到 files 中
  265. exists[fullPath] = true;
  266. files.push({
  267. id: fullPath,
  268. label: paths[i],
  269. parentId: oldFullPath || '/' // "/" 为根节点
  270. });
  271. }
  272. }
  273. return files;
  274. },
  275. /** 节点单击事件 **/
  276. handleNodeClick(data, node) {
  277. if (node && !node.isLeaf) {
  278. return false;
  279. }
  280. // 判断,如果非子节点,不允许选中
  281. this.preview.activeName = data.id;
  282. },
  283. /** 修改按钮操作 */
  284. handleEditTable(row) {
  285. const tableId = row.id;
  286. const tableName = row.tableName || this.tableNames[0];
  287. const params = { pageNum: this.queryParams.pageNum };
  288. this.$tab.openPage("修改[" + tableName + "]生成配置", '/codegen/edit/' + tableId, params);
  289. },
  290. /** 删除按钮操作 */
  291. handleDelete(row) {
  292. const tableIds = row.id;
  293. this.$modal.confirm('是否确认删除表名称为"' + row.tableName + '"的数据项?').then(function() {
  294. return deleteCodegen(tableIds);
  295. }).then(() => {
  296. this.getList();
  297. this.$modal.msgSuccess("删除成功");
  298. }).catch(() => {});
  299. },
  300. // 数据源配置的名字
  301. dataSourceConfigNameFormat(row, column) {
  302. for (const config of this.dataSourceConfigs) {
  303. if (row.dataSourceConfigId === config.id) {
  304. return config.name;
  305. }
  306. }
  307. return '未知【' + row.leaderUserId + '】';
  308. },
  309. }
  310. };
  311. </script>