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