index.vue 14 KB

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