index.vue 15.3 KB
Newer Older
sunhongwei's avatar
sunhongwei committed
1 2
<template>
  <div class="app-container">
Marcus's avatar
Marcus committed
3
    <doc-alert :title="$t('代码生成')" url="https://doc.iocoder.cn/new-feature/" />
sunhongwei's avatar
sunhongwei committed
4 5
    <!-- 操作工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
Marcus's avatar
Marcus committed
6 7
      <el-form-item :label="$t('表名称')" prop="tableName">
        <el-input v-model="queryParams.tableName" :placeholder="$t('请输入表名称')" clearable
sunhongwei's avatar
sunhongwei committed
8 9
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
Marcus's avatar
Marcus committed
10 11
      <el-form-item :label="$t('表描述')" prop="tableComment">
        <el-input v-model="queryParams.tableComment" :placeholder="$t('请输入表描述')" clearable
sunhongwei's avatar
sunhongwei committed
12 13
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
Marcus's avatar
Marcus committed
14
      <el-form-item :label="$t('创建时间')">
sunhongwei's avatar
sunhongwei committed
15 16 17 18
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item>
Marcus's avatar
Marcus committed
19 20
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('重置') }}</el-button>
sunhongwei's avatar
sunhongwei committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
      </el-form-item>
    </el-form>

    <!-- 操作工作栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload" size="mini" @click="openImportTable"
                   v-hasPermi="['infra:codegen:create']">基于 DB 导入</el-button>
        <el-button type="info" plain icon="el-icon-upload" size="mini" @click="openImportSQL"
                   v-hasPermi="['infra:codegen:create']">基于 SQL 导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="tableList">
Marcus's avatar
Marcus committed
37 38 39 40
      <el-table-column :label="$t('表名称')" align="center" prop="tableName" :show-overflow-tooltip="true" width="200"/>
      <el-table-column :label="$t('表描述')" align="center" prop="tableComment" :show-overflow-tooltip="true" width="120"/>
      <el-table-column :label="$t('实体')" align="center" prop="className" :show-overflow-tooltip="true" width="200"/>
      <el-table-column :label="$t('创建时间')" align="center" prop="createTime" width="160">
sunhongwei's avatar
sunhongwei committed
41 42 43 44
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
Marcus's avatar
Marcus committed
45
      <el-table-column :label="$t('更新时间')" align="center" prop="createTime" width="160">
sunhongwei's avatar
sunhongwei committed
46 47 48 49
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
Marcus's avatar
Marcus committed
50
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
sunhongwei's avatar
sunhongwei committed
51
        <template slot-scope="scope">
Marcus's avatar
Marcus committed
52 53 54 55 56 57
          <el-button type="text" size="small" icon="el-icon-view" @click="handlePreview(scope.row)" v-hasPermi="['infra:codegen:preview']">{{ $t('预览') }}</el-button>
          <el-button type="text" size="small" icon="el-icon-edit" @click="handleEditTable(scope.row)" v-hasPermi="['infra:codegen:update']">{{ $t('编辑') }}</el-button>
          <el-button type="text" size="small" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['infra:codegen:delete']">{{ $t('删除') }}</el-button>
          <el-button type="text" size="small" icon="el-icon-refresh" @click="handleSynchDb(scope.row)" v-hasPermi="['infra:codegen:update']">{{ $t('同步') }}</el-button>
          <el-button type="text" size="small" icon="el-icon-download" @click="handleGenTable(scope.row)" v-hasPermi="['infra:codegen:download']">{{ $t('生成代码') }}</el-button>
          <el-button type="text" size="small" icon="el-icon-download" @click="generateValidateCode(scope.row)">{{ $t('生成验证配置') }}</el-button>
sunhongwei's avatar
sunhongwei committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 预览界面 -->
    <el-dialog :title="preview.title" :visible.sync="preview.open" width="90%" top="5vh" append-to-body class="scrollbar">
      <el-row>
        <el-col :span="7">
          <el-tree :data="preview.fileTree" :expand-on-click-node="false" default-expand-all highlight-current
                   @node-click="handleNodeClick"/>
        </el-col>
        <el-col :span="17">
          <el-tabs v-model="preview.activeName">
            <el-tab-pane v-for="item in preview.data" :label="item.filePath.substring(item.filePath.lastIndexOf('/') + 1)"
                         :name="item.filePath" :key="item.filePath">
Marcus's avatar
Marcus committed
74
              <el-link :underline="false" icon="el-icon-document-copy" v-clipboard:copy="item.code" v-clipboard:success="clipboardSuccess" style="float:right">{{ $t('复制') }}</el-link>
sunhongwei's avatar
sunhongwei committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
              <pre><code class="hljs" v-html="highlightedCode(item)"></code></pre>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </el-dialog>

    <!-- 基于 DB 导入 -->
    <import-table ref="import" @ok="handleQuery" />

    <!-- 基于 SQL 导入 -->
    <el-dialog :title="importSQL.title" :visible.sync="importSQL.open" width="800px" append-to-body>
      <el-form ref="importSQLForm" :model="importSQL.form" :rules="importSQL.rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="建表 SQL 语句" prop="sql">
              <el-input v-model="importSQL.form.sql" type="textarea" rows="30" style="width: 650px;" placeholder="请输入建 SQL 语句" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
Marcus's avatar
Marcus committed
97 98
        <el-button type="primary" @click="submitImportSQLForm">{{ $t('确 定') }}</el-button>
        <el-button @click="cancel">{{ $t('取 消') }}</el-button>
sunhongwei's avatar
sunhongwei committed
99 100 101 102 103 104
      </div>
    </el-dialog>
  </div>
</template>

<script>
105
import {getCodegenDetail, getCodegenTablePage, previewCodegen, downloadCodegen, deleteCodegen,
sunhongwei's avatar
sunhongwei committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  syncCodegenFromDB, syncCodegenFromSQL, createCodegenListFromSQL } from "@/api/infra/codegen";

import importTable from "./importTable";
// 代码高亮插件
import hljs from "highlight.js/lib/highlight";
import "highlight.js/styles/github-gist.css";
hljs.registerLanguage("java", require("highlight.js/lib/languages/java"));
hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml"));
hljs.registerLanguage("html", require("highlight.js/lib/languages/xml"));
hljs.registerLanguage("vue", require("highlight.js/lib/languages/xml"));
hljs.registerLanguage("javascript", require("highlight.js/lib/languages/javascript"));
hljs.registerLanguage("sql", require("highlight.js/lib/languages/sql"));

export default {
  name: "Codegen",
  components: { importTable },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 唯一标识符
      uniqueId: "",
      // 选中表数组
      tableNames: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表数据
      tableList: [],
      // 日期范围
      dateRange: "",
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        tableName: undefined,
        tableComment: undefined
      },
      // 预览参数
      preview: {
        open: false,
Marcus's avatar
Marcus committed
148
        title: this.$t("代码预览"),
sunhongwei's avatar
sunhongwei committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
        fileTree: [],
        data: {},
        activeName: "",
      },
      // 基于 SQL 导入
      importSQL: {
        open: false,
        title: "",
        form: {

        },
        rules: {
          sql: [{ required: true, message: "SQL 不能为空", trigger: "blur" }]
        }
      }
    };
  },
  created() {
    this.getList();
  },
  activated() {
    const time = this.$route.query.t;
    if (time != null && time !== this.uniqueId) {
      this.uniqueId = time;
      this.resetQuery();
    }
  },
  methods: {
    /** 查询表集合 */
    getList() {
      this.loading = true;
      getCodegenTablePage(this.addDateRange(this.queryParams, [
        this.dateRange[0] ? this.dateRange[0] + ' 00:00:00' : undefined,
        this.dateRange[1] ? this.dateRange[1] + ' 23:59:59' : undefined,
      ], 'CreateTime')).then(response => {
            this.tableList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 生成代码操作 */
    handleGenTable(row) {
      downloadCodegen(row.id).then(response => {
        this.$download.zip(response, 'codegen-' + row.tableName + '.zip');
      })
    },
    /** 同步数据库操作 */
    handleSynchDb(row) {
      // 基于 SQL 同步
      if (row.importType === 2) {
        this.importSQL.open = true;
        this.importSQL.form.tableId = row.id;
        return;
      }
      // 基于 DB 同步
      const tableName = row.tableName;
      this.$modal.confirm('确认要强制同步"' + tableName + '"表结构吗?').then(function() {
          return syncCodegenFromDB(row.id);
      }).then(() => {
          this.$modal.msgSuccess("同步成功");
      }).catch(() => {});
    },
    /** 打开导入表弹窗 */
    openImportTable() {
      this.$refs.import.show();
    },
    /** 打开 SQL 导入的弹窗 **/
    openImportSQL() {
      this.importSQL.open = true;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 预览按钮 */
    handlePreview(row) {
      previewCodegen(row.id).then(response => {
        this.preview.data = response.data;
        let files = this.handleFiles(response.data);
        this.preview.fileTree = this.handleTree(files, "id", "parentId", "children",
            "/"); // "/" 为根节点
        // console.log(this.preview.fileTree)
        this.preview.activeName = response.data[0].filePath;
        this.preview.open = true;
      });
    },
    /** 高亮显示 */
    highlightedCode(item) {
      // const vmName = key.substring(key.lastIndexOf("/") + 1, key.indexOf(".vm"));
      // var language = vmName.substring(vmName.indexOf(".") + 1, vmName.length);
      var language = item.filePath.substring(item.filePath.lastIndexOf(".") + 1);
      const result = hljs.highlight(language, item.code || "", true);
      return result.value || '&nbsp;';
    },
    /** 复制代码成功 */
    clipboardSuccess(){
      this.$modal.msgSuccess("复制成功");
    },
    /** 生成 files 目录 **/
    handleFiles(datas) {
      let exists = {}; // key:file 的 id;value:true
      let files = [];
      // 遍历每个元素
      for (const data of datas) {
        let paths = data.filePath.split('/');
        let fullPath = ''; // 从头开始的路径,用于生成 id
        // 特殊处理 java 文件
        if (paths[paths.length - 1].indexOf('.java') >= 0) {
          let newPaths = [];
          for (let i = 0; i < paths.length; i++) {
            let path = paths[i];
            if (path !== 'java') {
              newPaths.push(path);
              continue;
            }
            newPaths.push(path);
            // 特殊处理中间的 package,进行合并
            let tmp = undefined;
            while (i < paths.length) {
              path = paths[i + 1];
              if (path === 'controller'
                || path === 'convert'
                || path === 'dal'
                || path === 'enums'
                || path === 'service'
                || path === 'vo' // 下面三个,主要是兜底。可能考虑到有人改了包结构
                || path === 'mysql'
                || path === 'dataobject') {
                break;
              }
              tmp = tmp ? tmp + '.' + path : path;
              i++;
            }
            if (tmp) {
              newPaths.push(tmp);
            }
          }
          paths = newPaths;
        }
        // 遍历每个 path, 拼接成树
        for (let i = 0; i < paths.length; i++) {
          // 已经添加到 files 中,则跳过
          let oldFullPath = fullPath;
          // 下面的 replaceAll 的原因,是因为上面包处理了,导致和 tabs 不匹配,所以 replaceAll 下
          fullPath = fullPath.length === 0 ? paths[i] : fullPath.replaceAll('.', '/') + '/' + paths[i];
          if (exists[fullPath]) {
            continue;
          }
          // 添加到 files 中
          exists[fullPath] = true;
          files.push({
            id: fullPath,
            label: paths[i],
            parentId: oldFullPath || '/'  // "/" 为根节点
          });
        }
      }
      return files;
    },
    /** 节点单击事件 **/
    handleNodeClick(data, node) {
      if (node && !node.isLeaf) {
        return false;
      }
      // 判断,如果非子节点,不允许选中
      this.preview.activeName = data.id;
    },
    /** 修改按钮操作 */
    handleEditTable(row) {
      const tableId = row.id;
      this.$router.push("/codegen/edit/" + tableId);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tableIds = row.id;
      this.$modal.confirm('是否确认删除表名称为"' + row.tableName + '"的数据项?').then(function() {
          return deleteCodegen(tableIds);
      }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.importSQL.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.importSQL.form = {
        tableId: undefined,
        sql: undefined,
      };
      this.resetForm("importSQLForm");
    },
    // 提交 import SQL 表单
    submitImportSQLForm() {
      this.$refs["importSQLForm"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        let form = this.importSQL.form;
        if (form.tableId != null) {
          syncCodegenFromSQL(form.tableId, form.sql).then(response => {
            this.$modal.msgSuccess("同步成功");
            this.importSQL.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createCodegenListFromSQL(form).then(response => {
          this.$modal.msgSuccess("导入成功");
          this.importSQL.open = false;
          this.getList();
        });
      });
375 376 377 378 379 380 381 382 383
    },
    generateValidateCode(row){
      let l = this.$loading()
      getCodegenDetail(row.id).then(res => {
        let rules = {}
        res.data.columns.forEach(item => {
          let rule = {trigger: 'blur'}
          if(!item.nullable){
            rule.required = true
Marcus's avatar
Marcus committed
384
            rule.message = this.$t('不能为空')
385 386 387 388 389 390 391 392 393 394 395 396
          }
          if(['int', 'tinyint', 'bigint'].indexOf(item.columnType)){
            rule.type = 'integer'
          }
          if(item.columnType.indexOf('decimal') === 0){
            rule.type = 'decimal'
          }
          if(item.columnType == 'datetime'){
            rule.type = 'date'
          }
          rules[item.javaField] = [rule]
        })
Marcus's avatar
Marcus committed
397
        console.log(row.tableComment + this.$t('校验规则'), JSON.stringify(rules))
398 399 400 401 402 403
        this.$message('规则已生成,打开控制台查看')
      })
      .finally(res => {
        l.close()
      })
    },
sunhongwei's avatar
sunhongwei committed
404 405 406
  }
};
</script>