index.vue 13 KB

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