index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
  121. hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml"));
  122. hljs.registerLanguage("html", require("highlight.js/lib/languages/xml"));
  123. hljs.registerLanguage("vue", require("highlight.js/lib/languages/xml"));
  124. hljs.registerLanguage("javascript", require("highlight.js/lib/languages/javascript"));
  125. hljs.registerLanguage("sql", require("highlight.js/lib/languages/sql"));
  126. export default {
  127. name: "Codegen",
  128. components: { importTable },
  129. data() {
  130. return {
  131. // 遮罩层
  132. loading: true,
  133. // 唯一标识符
  134. uniqueId: "",
  135. // 选中表数组
  136. tableNames: [],
  137. // 显示搜索条件
  138. showSearch: true,
  139. // 总条数
  140. total: 0,
  141. // 表数据
  142. tableList: [],
  143. // 日期范围
  144. dateRange: "",
  145. // 查询参数
  146. queryParams: {
  147. pageNo: 1,
  148. pageSize: 10,
  149. tableName: undefined,
  150. tableComment: undefined
  151. },
  152. // 预览参数
  153. preview: {
  154. open: false,
  155. title: "代码预览",
  156. fileTree: [],
  157. data: {},
  158. activeName: "",
  159. },
  160. // 基于 SQL 导入
  161. importSQL: {
  162. open: false,
  163. title: "",
  164. form: {
  165. },
  166. rules: {
  167. sql: [{ required: true, message: "SQL 不能为空", trigger: "blur" }]
  168. }
  169. }
  170. };
  171. },
  172. created() {
  173. this.getList();
  174. },
  175. activated() {
  176. const time = this.$route.query.t;
  177. if (time != null && time !== this.uniqueId) {
  178. this.uniqueId = time;
  179. this.resetQuery();
  180. }
  181. },
  182. methods: {
  183. /** 查询表集合 */
  184. getList() {
  185. this.loading = true;
  186. getCodegenTablePage(this.addDateRange(this.queryParams, [
  187. this.dateRange[0] ? this.dateRange[0] + ' 00:00:00' : undefined,
  188. this.dateRange[1] ? this.dateRange[1] + ' 23:59:59' : undefined,
  189. ], 'CreateTime')).then(response => {
  190. this.tableList = response.data.list;
  191. this.total = response.data.total;
  192. this.loading = false;
  193. }
  194. );
  195. },
  196. /** 搜索按钮操作 */
  197. handleQuery() {
  198. this.queryParams.pageNo = 1;
  199. this.getList();
  200. },
  201. /** 生成代码操作 */
  202. handleGenTable(row) {
  203. downloadCodegen(row.id).then(response => {
  204. this.downloadZip(response, 'codegen-' + row.tableName + '.zip');
  205. })
  206. },
  207. /** 同步数据库操作 */
  208. handleSynchDb(row) {
  209. // 基于 SQL 同步
  210. if (row.importType === 2) {
  211. this.importSQL.open = true;
  212. this.importSQL.form.tableId = row.id;
  213. return;
  214. }
  215. // 基于 DB 同步
  216. const tableName = row.tableName;
  217. this.$confirm('确认要强制同步"' + tableName + '"表结构吗?', "警告", {
  218. confirmButtonText: "确定",
  219. cancelButtonText: "取消",
  220. type: "warning"
  221. }).then(function() {
  222. return syncCodegenFromDB(row.id);
  223. }).then(() => {
  224. this.msgSuccess("同步成功");
  225. })
  226. },
  227. /** 打开导入表弹窗 */
  228. openImportTable() {
  229. this.$refs.import.show();
  230. },
  231. /** 打开 SQL 导入的弹窗 **/
  232. openImportSQL() {
  233. this.importSQL.open = true;
  234. },
  235. /** 重置按钮操作 */
  236. resetQuery() {
  237. this.dateRange = [];
  238. this.resetForm("queryForm");
  239. this.handleQuery();
  240. },
  241. /** 预览按钮 */
  242. handlePreview(row) {
  243. previewCodegen(row.id).then(response => {
  244. this.preview.data = response.data;
  245. let files = this.handleFiles(response.data);
  246. console.log(files)
  247. this.preview.fileTree = this.handleTree(files, "id", "parentId", "children",
  248. "/"); // "/" 为根节点
  249. console.log(this.preview.fileTree)
  250. this.preview.activeName = response.data[0].filePath;
  251. this.preview.open = true;
  252. });
  253. },
  254. /** 高亮显示 */
  255. highlightedCode(item) {
  256. // const vmName = key.substring(key.lastIndexOf("/") + 1, key.indexOf(".vm"));
  257. // var language = vmName.substring(vmName.indexOf(".") + 1, vmName.length);
  258. var language = item.filePath.substring(item.filePath.lastIndexOf(".") + 1);
  259. const result = hljs.highlight(language, item.code || "", true);
  260. return result.value || '&nbsp;';
  261. },
  262. /** 生成 files 目录 **/
  263. handleFiles(datas) {
  264. let exists = {}; // key:file 的 id;value:true
  265. let files = [];
  266. // 遍历每个元素
  267. for (const data of datas) {
  268. let paths = data.filePath.split('/');
  269. let fullPath = ''; // 从头开始的路径,用于生成 id
  270. for (let i = 0; i < paths.length; i++) {
  271. // 已经添加大奥 files 中,则跳过
  272. let oldFullPath = fullPath;
  273. fullPath = fullPath.length === 0 ? paths[i] : fullPath + '/' + paths[i];
  274. if (exists[fullPath]) {
  275. continue;
  276. }
  277. // 添加到 files 中
  278. exists[fullPath] = true;
  279. files.push({
  280. id: fullPath,
  281. label: paths[i],
  282. parentId: oldFullPath || '/' // "/" 为根节点
  283. });
  284. }
  285. }
  286. return files;
  287. },
  288. /** 节点单击事件 **/
  289. handleNodeClick(data) {
  290. this.preview.activeName = data.id;
  291. },
  292. /** 修改按钮操作 */
  293. handleEditTable(row) {
  294. const tableId = row.id;
  295. this.$router.push("/codegen/edit/" + tableId);
  296. },
  297. /** 删除按钮操作 */
  298. handleDelete(row) {
  299. const tableIds = row.id;
  300. this.$confirm('是否确认删除表名称为"' + row.tableName + '"的数据项?', "警告", {
  301. confirmButtonText: "确定",
  302. cancelButtonText: "取消",
  303. type: "warning"
  304. }).then(function() {
  305. return deleteCodegen(tableIds);
  306. }).then(() => {
  307. this.getList();
  308. this.msgSuccess("删除成功");
  309. })
  310. },
  311. // 取消按钮
  312. cancel() {
  313. this.importSQL.open = false;
  314. this.reset();
  315. },
  316. // 表单重置
  317. reset() {
  318. this.importSQL.form = {
  319. tableId: undefined,
  320. sql: undefined,
  321. };
  322. this.resetForm("importSQLForm");
  323. },
  324. // 提交 import SQL 表单
  325. submitImportSQLForm() {
  326. this.$refs["importSQLForm"].validate(valid => {
  327. if (!valid) {
  328. return;
  329. }
  330. // 修改的提交
  331. let form = this.importSQL.form;
  332. if (form.tableId != null) {
  333. syncCodegenFromSQL(form.tableId, form.sql).then(response => {
  334. this.msgSuccess("同步成功");
  335. this.importSQL.open = false;
  336. this.getList();
  337. });
  338. return;
  339. }
  340. // 添加的提交
  341. createCodegenListFromSQL(form).then(response => {
  342. this.msgSuccess("导入成功");
  343. this.importSQL.open = false;
  344. this.getList();
  345. });
  346. });
  347. }
  348. }
  349. };
  350. </script>