index.vue 14 KB

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