index.vue 13.1 KB
Newer Older
1
<template>
2
  <div>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
3
    <!-- 从数据库直接获取字段 -->
4
    <el-collapse v-model="activeNames">
5 6 7 8
      <el-collapse-item name="1">
        <template slot="title">
          <div :style="{fontSize:'16px',paddingLeft:'20px'}">
            点这里从现有数据库创建代码
9
            <i class="header-icon el-icon-thumb"></i>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
          </div>
        </template>
        <el-form ref="getTableForm" :inline="true" :model="dbform" label-width="120px">
          <el-form-item label="数据库名" prop="structName">
            <el-select @change="getTable" v-model="dbform.dbName" filterable placeholder="请选择数据库">
              <el-option
                v-for="item in dbOptions"
                :key="item.database"
                :label="item.database"
                :value="item.database"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="表名" prop="structName">
            <el-select
              v-model="dbform.tableName"
              :disabled="!dbform.dbName"
              filterable
              placeholder="请选择表"
            >
              <el-option
                v-for="item in tableOptions"
                :key="item.tableName"
                :label="item.tableName"
                :value="item.tableName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
39
            <el-button @click="getColumn" type="primary">使用此表创建</el-button>
40 41 42 43
          </el-form-item>
        </el-form>
      </el-collapse-item>
    </el-collapse>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
44 45

    <el-divider></el-divider>
46 47 48 49 50
    <!-- 初始版本自动化代码工具 -->
    <el-form ref="autoCodeForm" :rules="rules" :model="form" label-width="120px" :inline="true">
      <el-form-item label="Struct名称" prop="structName">
        <el-input v-model="form.structName" placeholder="首字母自动转换大写"></el-input>
      </el-form-item>
51 52 53
      <el-form-item label="tableName" prop="tableName">
        <el-input v-model="form.tableName" placeholder="指定表名(非必填)"></el-input>
      </el-form-item>
54 55 56
      <el-form-item label="Struct简称" prop="abbreviation">
        <el-input v-model="form.abbreviation" placeholder="简称会作为入参对象名和路由group"></el-input>
      </el-form-item>
57 58 59
      <el-form-item label="Struct中文名称" prop="description">
        <el-input v-model="form.description" placeholder="中文描述作为自动api描述"></el-input>
      </el-form-item>
60 61 62
      <el-form-item label="文件名称" prop="packageName">
        <el-input v-model="form.packageName"></el-input>
      </el-form-item>
63 64
      <el-form-item label="自动创建api">
        <el-checkbox v-model="form.autoCreateApiToSql"></el-checkbox>
65
      </el-form-item>
m0_50812349's avatar
m0_50812349 已提交
66 67 68
      <el-form-item label="自动移动文件">
        <el-checkbox v-model="form.autoMoveFile"></el-checkbox>
      </el-form-item>
69 70 71 72 73 74
    </el-form>
    <!-- 组件列表 -->
    <div class="button-box clearflex">
      <el-button @click="editAndAddField()" type="primary">新增Field</el-button>
    </div>
    <el-table :data="form.fields" border stripe>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
75 76 77 78 79
      <el-table-column type="index" label="序列" width="100"></el-table-column>
      <el-table-column prop="fieldName" label="Field名"></el-table-column>
      <el-table-column prop="fieldDesc" label="中文名"></el-table-column>
      <el-table-column prop="fieldJson" label="FieldJson"></el-table-column>
      <el-table-column prop="fieldType" label="Field数据类型" width="130"></el-table-column>
80 81
      <el-table-column prop="dataType" label="数据库字段类型" width="130"></el-table-column>
      <el-table-column prop="dataTypeLong" label="数据库字段长度" width="130"></el-table-column>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
82 83 84
      <el-table-column prop="columnName" label="数据库字段" width="130"></el-table-column>
      <el-table-column prop="comment" label="数据库字段描述" width="130"></el-table-column>
      <el-table-column prop="fieldSearchType" label="搜索条件" width="130"></el-table-column>
85
      <el-table-column prop="dictType" label="字典" width="130"></el-table-column>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
86
      <el-table-column label="操作" width="300">
87
        <template slot-scope="scope">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="editAndAddField(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            :disabled="scope.$index == 0"
            @click="moveUpField(scope.$index)"
          >上移</el-button>
          <el-button
            size="mini"
            type="text"
            :disabled="(scope.$index + 1) == form.fields.length"
            @click="moveDownField(scope.$index)"
          >下移</el-button>
106 107 108 109 110 111
          <el-popover placement="top" v-model="scope.row.visible">
            <p>确定删除吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="scope.row.visible = false">取消</el-button>
              <el-button type="primary" size="mini" @click="deleteField(scope.$index)">确定</el-button>
            </div>
LoeYueng's avatar
LoeYueng 已提交
112
            <el-button size="mini" type="danger" icon="el-icon-delete" slot="reference">删除</el-button>
113 114 115 116
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
117
    <el-tag type="danger">id , created_at , updated_at , deleted_at 会自动生成请勿重复创建</el-tag>
118 119
    <!-- 组件列表 -->
    <div class="button-box clearflex">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
120
      <el-button @click="enterForm" type="primary">生成代码</el-button>
121
    </div>
122 123
    <!-- 组件弹窗 -->
    <el-dialog title="组件内容" :visible.sync="dialogFlag">
124
      <FieldDialog v-if="dialogFlag" :dialogMiddle="dialogMiddle" ref="fieldDialog" />
125 126 127 128 129 130
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="enterDialog">确 定</el-button>
      </div>
    </el-dialog>
  </div>
131 132
</template>
<script>
133
const fieldTemplate = {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
134 135 136
  fieldName: "",
  fieldDesc: "",
  fieldType: "",
137
  dataType: "",
Mr.奇淼('s avatar
Mr.奇淼( 已提交
138 139
  fieldJson: "",
  columnName: "",
140
  dataTypeLong: "",
Mr.奇淼('s avatar
Mr.奇淼( 已提交
141
  comment: "",
142 143
  fieldSearchType: "",
  dictType:""
Mr.奇淼('s avatar
Mr.奇淼( 已提交
144 145 146
};

import FieldDialog from "@/view/systemTools/autoCode/component/fieldDialog.vue";
147
import { toUpperCase, toHump } from "@/utils/stringFun.js";
148
import { createTemp, getDB, getTable, getColumn } from "@/api/autoCode.js";
149
import { getDict } from "@/utils/dictionary";
150 151

export default {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
152
  name: "autoCode",
153 154
  data() {
    return {
155
      activeNames: [""],
Mr.奇淼('s avatar
Mr.奇淼( 已提交
156 157 158 159 160 161 162 163
      dbform: {
        dbName: "",
        tableName: ""
      },
      dbOptions: [],
      tableOptions: [],
      addFlag: "",
      fdMap: {},
164
      form: {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
165
        structName: "",
166
        tableName: "",
Mr.奇淼('s avatar
Mr.奇淼( 已提交
167 168 169
        packageName: "",
        abbreviation: "",
        description: "",
170
        autoCreateApiToSql: false,
m0_50812349's avatar
m0_50812349 已提交
171
        autoMoveFile: false,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
172
        fields: []
173 174
      },
      rules: {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        structName: [
          { required: true, message: "请输入结构体名称", trigger: "blur" }
        ],
        abbreviation: [
          { required: true, message: "请输入结构体简称", trigger: "blur" }
        ],
        description: [
          { required: true, message: "请输入结构体描述", trigger: "blur" }
        ],
        packageName: [
          {
            required: true,
            message: "文件名称:sys_xxxx_xxxx",
            trigger: "blur"
          }
        ]
191 192 193
      },
      dialogMiddle: {},
      bk: {},
Mr.奇淼('s avatar
Mr.奇淼( 已提交
194 195
      dialogFlag: false
    };
196 197
  },
  components: {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
198
    FieldDialog
199 200 201
  },
  methods: {
    editAndAddField(item) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
202
      this.dialogFlag = true;
203
      if (item) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
204 205 206
        this.addFlag = "edit";
        this.bk = JSON.parse(JSON.stringify(item));
        this.dialogMiddle = item;
207
      } else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
208 209
        this.addFlag = "add";
        this.dialogMiddle = JSON.parse(JSON.stringify(fieldTemplate));
210 211 212 213
      }
    },
    moveUpField(index) {
      if (index == 0) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
214
        return;
215
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
216 217 218
      const oldUpField = this.form.fields[index - 1];
      this.form.fields.splice(index - 1, 1);
      this.form.fields.splice(index, 0, oldUpField);
219 220
    },
    moveDownField(index) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
221
      const fCount = this.form.fields.length;
222
      if (index == fCount - 1) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
223
        return;
224
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
225 226 227
      const oldDownField = this.form.fields[index + 1];
      this.form.fields.splice(index + 1, 1);
      this.form.fields.splice(index, 0, oldDownField);
228 229
    },
    enterDialog() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
230
      this.$refs.fieldDialog.$refs.fieldDialogFrom.validate(valid => {
231
        if (valid) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
232 233 234 235 236
          this.dialogMiddle.fieldName = toUpperCase(
            this.dialogMiddle.fieldName
          );
          if (this.addFlag == "add") {
            this.form.fields.push(this.dialogMiddle);
237
          }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
238
          this.dialogFlag = false;
239
        } else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
240
          return false;
241
        }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
242
      });
243
    },
244
    closeDialog() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
245 246
      if (this.addFlag == "edit") {
        this.dialogMiddle = this.bk;
247
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
248
      this.dialogFlag = false;
249
    },
250
    deleteField(index) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
251
      this.form.fields.splice(index, 1);
252 253 254 255
    },
    async enterForm() {
      if (this.form.fields.length <= 0) {
        this.$message({
Mr.奇淼('s avatar
Mr.奇淼( 已提交
256 257 258 259
          type: "error",
          message: "请填写至少一个field"
        });
        return false;
260
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
261 262 263
      if (
        this.form.fields.some(item => item.fieldName == this.form.structName)
      ) {
264
        this.$message({
Mr.奇淼('s avatar
Mr.奇淼( 已提交
265 266 267 268
          type: "error",
          message: "存在与结构体同名的字段"
        });
        return false;
269
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
270
      this.$refs.autoCodeForm.validate(async valid => {
271
        if (valid) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
272
          this.form.structName = toUpperCase(this.form.structName);
273 274
          if (this.form.structName == this.form.abbreviation) {
            this.$message({
Mr.奇淼('s avatar
Mr.奇淼( 已提交
275 276 277 278
              type: "error",
              message: "structName和struct简称不能相同"
            });
            return false;
279
          }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
280
          const data = await createTemp(this.form);
281 282
          if(data.headers?.success == "false"){
            return
Mr.奇淼('s avatar
Mr.奇淼( 已提交
283 284 285 286 287 288 289 290 291 292 293
          }else if(data.code == 0){
            this.$message({
              type: "success",
              message: "自动化代码创建成功,并已自动迁移"
            })
            return
          }else{
             this.$message({
              type: "success",
              message: "自动化代码创建成功,正在下载"
            })
294
          }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
295 296 297
          const blob = new Blob([data]);
          const fileName = "ginvueadmin.zip";
          if ("download" in document.createElement("a")) {
298
            // 不是IE浏览器
Mr.奇淼('s avatar
Mr.奇淼( 已提交
299 300 301 302 303 304 305 306 307
            let url = window.URL.createObjectURL(blob);
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            link.setAttribute("download", fileName);
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link); // 下载完成移除元素
            window.URL.revokeObjectURL(url); // 释放掉blob对象
308
          } else {
309
            // IE 10+
Mr.奇淼('s avatar
Mr.奇淼( 已提交
310
            window.navigator.msSaveBlob(blob, fileName);
311
          }
312
        } else {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
313
          return false;
314
        }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
315 316 317 318 319 320 321
      });
    },
    async getDb() {
      const res = await getDB();
      if (res.code == 0) {
        this.dbOptions = res.data.dbs;
      }
322
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
323 324 325 326 327
    async getTable() {
      const res = await getTable({ dbName: this.dbform.dbName });
      if (res.code == 0) {
        this.tableOptions = res.data.tables;
      }
328
      this.dbform.tableName = "";
Mr.奇淼('s avatar
Mr.奇淼( 已提交
329
    },
330
    async getColumn() {
331
      const gormModelList = ["id", "created_at", "updated_at", "deleted_at"];
332
      const res = await getColumn(this.dbform);
333 334 335 336 337 338 339 340 341
      if (res.code == 0) {
        const tbHump = toHump(this.dbform.tableName);
        this.form.structName = toUpperCase(tbHump);
        this.form.tableName = this.dbform.tableName;
        this.form.packageName = tbHump;
        this.form.abbreviation = tbHump;
        this.form.description = tbHump + "";
        this.form.autoCreateApiToSql = true;
        this.form.fields = [];
342 343 344 345
        res.data.columns &&
          res.data.columns.map(item => {
            if (!gormModelList.some(gormfd => gormfd == item.columnName)) {
              const fbHump = toHump(item.columnName);
346 347
              this.form.fields.push({
                fieldName: toUpperCase(fbHump),
348
                fieldDesc: item.columnComment || fbHump + "字段",
349 350 351 352
                fieldType: this.fdMap[item.dataType],
                dataType: item.dataType,
                fieldJson: fbHump,
                dataTypeLong: item.dataTypeLong,
353 354
                columnName: item.columnName,
                comment: item.columnComment,
355 356
                fieldSearchType: "",
                dictType:""
357 358 359 360
              });
            }
          });
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
361 362
    },
    async setFdMap() {
m0_50812349's avatar
m0_50812349 已提交
363 364
      const fdTypes= ["string", "int", "bool", "float64", "time.Time"];
      fdTypes.map(async fdtype => {
365 366 367 368 369
        const res = await getDict(fdtype);
        res.map(item => {
          this.fdMap[item.label] = fdtype;
        });
      });
Mr.奇淼('s avatar
Mr.奇淼( 已提交
370
    }
371
  },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
372 373
  created() {
    this.getDb();
374
    this.setFdMap();
Mr.奇淼('s avatar
Mr.奇淼( 已提交
375 376
  }
};
377 378 379 380 381 382 383 384
</script>
<style scope lang="scss">
.button-box {
  padding: 10px 20px;
  .el-button {
    float: right;
  }
}
385
</style>