index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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="创建时间">
  16. <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
  17. range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
  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 slot-scope="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 slot-scope="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 slot-scope="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, syncCodegenFromSQL, createCodegenListFromSQL } 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. export default {
  96. name: "Codegen",
  97. components: { importTable },
  98. data() {
  99. return {
  100. // 遮罩层
  101. loading: true,
  102. // 唯一标识符
  103. uniqueId: "",
  104. // 选中表数组
  105. tableNames: [],
  106. // 显示搜索条件
  107. showSearch: true,
  108. // 总条数
  109. total: 0,
  110. // 表数据
  111. tableList: [],
  112. // 日期范围
  113. dateRange: "",
  114. // 查询参数
  115. queryParams: {
  116. pageNo: 1,
  117. pageSize: 10,
  118. tableName: undefined,
  119. tableComment: undefined
  120. },
  121. // 预览参数
  122. preview: {
  123. open: false,
  124. title: "代码预览",
  125. fileTree: [],
  126. data: {},
  127. activeName: "",
  128. },
  129. // 数据源列表
  130. dataSourceConfigs: [],
  131. };
  132. },
  133. created() {
  134. this.getList();
  135. // 加载数据源
  136. getDataSourceConfigList().then(response => {
  137. this.dataSourceConfigs = response.data;
  138. });
  139. },
  140. activated() {
  141. const time = this.$route.query.t;
  142. if (time != null && time !== this.uniqueId) {
  143. this.uniqueId = time;
  144. this.resetQuery();
  145. }
  146. },
  147. methods: {
  148. /** 查询表集合 */
  149. getList() {
  150. this.loading = true;
  151. getCodegenTablePage(this.addDateRange(this.queryParams, [
  152. this.dateRange[0] ? this.dateRange[0] + ' 00:00:00' : undefined,
  153. this.dateRange[1] ? this.dateRange[1] + ' 23:59:59' : undefined,
  154. ], 'CreateTime')).then(response => {
  155. this.tableList = response.data.list;
  156. this.total = response.data.total;
  157. this.loading = false;
  158. }
  159. );
  160. },
  161. /** 搜索按钮操作 */
  162. handleQuery() {
  163. this.queryParams.pageNo = 1;
  164. this.getList();
  165. },
  166. /** 生成代码操作 */
  167. handleGenTable(row) {
  168. downloadCodegen(row.id).then(response => {
  169. this.$download.zip(response, 'codegen-' + row.tableName + '.zip');
  170. })
  171. },
  172. /** 同步数据库操作 */
  173. handleSynchDb(row) {
  174. // 基于 DB 同步
  175. const tableName = row.tableName;
  176. this.$modal.confirm('确认要强制同步"' + tableName + '"表结构吗?').then(function() {
  177. return syncCodegenFromDB(row.id);
  178. }).then(() => {
  179. this.$modal.msgSuccess("同步成功");
  180. }).catch(() => {});
  181. },
  182. /** 打开导入表弹窗 */
  183. openImportTable() {
  184. this.$refs.import.show();
  185. },
  186. /** 重置按钮操作 */
  187. resetQuery() {
  188. this.dateRange = [];
  189. this.resetForm("queryForm");
  190. this.handleQuery();
  191. },
  192. /** 预览按钮 */
  193. handlePreview(row) {
  194. previewCodegen(row.id).then(response => {
  195. this.preview.data = response.data;
  196. let files = this.handleFiles(response.data);
  197. this.preview.fileTree = this.handleTree(files, "id", "parentId", "children",
  198. "/"); // "/" 为根节点
  199. // console.log(this.preview.fileTree)
  200. this.preview.activeName = response.data[0].filePath;
  201. this.preview.open = true;
  202. });
  203. },
  204. /** 高亮显示 */
  205. highlightedCode(item) {
  206. // const vmName = key.substring(key.lastIndexOf("/") + 1, key.indexOf(".vm"));
  207. // var language = vmName.substring(vmName.indexOf(".") + 1, vmName.length);
  208. var language = item.filePath.substring(item.filePath.lastIndexOf(".") + 1);
  209. const result = hljs.highlight(language, item.code || "", true);
  210. return result.value || '&nbsp;';
  211. },
  212. /** 复制代码成功 */
  213. clipboardSuccess(){
  214. this.$modal.msgSuccess("复制成功");
  215. },
  216. /** 生成 files 目录 **/
  217. handleFiles(datas) {
  218. let exists = {}; // key:file 的 id;value:true
  219. let files = [];
  220. // 遍历每个元素
  221. for (const data of datas) {
  222. let paths = data.filePath.split('/');
  223. let fullPath = ''; // 从头开始的路径,用于生成 id
  224. // 特殊处理 java 文件
  225. if (paths[paths.length - 1].indexOf('.java') >= 0) {
  226. let newPaths = [];
  227. for (let i = 0; i < paths.length; i++) {
  228. let path = paths[i];
  229. if (path !== 'java') {
  230. newPaths.push(path);
  231. continue;
  232. }
  233. newPaths.push(path);
  234. // 特殊处理中间的 package,进行合并
  235. let tmp = undefined;
  236. while (i < paths.length) {
  237. path = paths[i + 1];
  238. if (path === 'controller'
  239. || path === 'convert'
  240. || path === 'dal'
  241. || path === 'enums'
  242. || path === 'service'
  243. || path === 'vo' // 下面三个,主要是兜底。可能考虑到有人改了包结构
  244. || path === 'mysql'
  245. || path === 'dataobject') {
  246. break;
  247. }
  248. tmp = tmp ? tmp + '.' + path : path;
  249. i++;
  250. }
  251. if (tmp) {
  252. newPaths.push(tmp);
  253. }
  254. }
  255. paths = newPaths;
  256. }
  257. // 遍历每个 path, 拼接成树
  258. for (let i = 0; i < paths.length; i++) {
  259. // 已经添加到 files 中,则跳过
  260. let oldFullPath = fullPath;
  261. // 下面的 replaceAll 的原因,是因为上面包处理了,导致和 tabs 不匹配,所以 replaceAll 下
  262. fullPath = fullPath.length === 0 ? paths[i] : fullPath.replaceAll('.', '/') + '/' + paths[i];
  263. if (exists[fullPath]) {
  264. continue;
  265. }
  266. // 添加到 files 中
  267. exists[fullPath] = true;
  268. files.push({
  269. id: fullPath,
  270. label: paths[i],
  271. parentId: oldFullPath || '/' // "/" 为根节点
  272. });
  273. }
  274. }
  275. return files;
  276. },
  277. /** 节点单击事件 **/
  278. handleNodeClick(data, node) {
  279. if (node && !node.isLeaf) {
  280. return false;
  281. }
  282. // 判断,如果非子节点,不允许选中
  283. this.preview.activeName = data.id;
  284. },
  285. /** 修改按钮操作 */
  286. handleEditTable(row) {
  287. const tableId = row.id;
  288. const tableName = row.tableName || this.tableNames[0];
  289. const params = { pageNum: this.queryParams.pageNum };
  290. this.$tab.openPage("修改[" + tableName + "]生成配置", '/codegen/edit/' + tableId, params);
  291. },
  292. /** 删除按钮操作 */
  293. handleDelete(row) {
  294. const tableIds = row.id;
  295. this.$modal.confirm('是否确认删除表名称为"' + row.tableName + '"的数据项?').then(function() {
  296. return deleteCodegen(tableIds);
  297. }).then(() => {
  298. this.getList();
  299. this.$modal.msgSuccess("删除成功");
  300. }).catch(() => {});
  301. },
  302. // 数据源配置的名字
  303. dataSourceConfigNameFormat(row, column) {
  304. for (const config of this.dataSourceConfigs) {
  305. if (row.dataSourceConfigId === config.id) {
  306. return config.name;
  307. }
  308. }
  309. return '未知【' + row.leaderUserId + '】';
  310. },
  311. }
  312. };
  313. </script>