index.vue 12 KB

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