sys_auto_code.go 2.3 KB
Newer Older
Mr.奇淼('s avatar
Mr.奇淼( 已提交
1
package v1
2 3 4

import (
	"fmt"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
5 6
	"gin-vue-admin/global/response"
	"gin-vue-admin/model"
7
	"gin-vue-admin/service"
8
	"github.com/gin-gonic/gin"
9
	"net/url"
10 11 12 13 14 15 16 17
	"os"
)

// @Tags SysApi
// @Summary 自动代码模板
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
18
// @Param data body model.AutoCodeStruct true "创建自动代码"
19 20 21
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /autoCode/createTemp [post]
func CreateTemp(c *gin.Context) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
22
	var a model.AutoCodeStruct
23
	_ = c.ShouldBindJSON(&a)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
	if a.AutoCreateApiToSql {
		apiList := [5]model.SysApi{
			{
				Path:        "/" + a.Abbreviation + "/" + "create" + a.StructName,
				Description: "新增" + a.StructName,
				ApiGroup:    a.Abbreviation,
				Method:      "POST",
			},
			{
				Path:        "/" + a.Abbreviation + "/" + "delete" + a.StructName,
				Description: "删除" + a.StructName,
				ApiGroup:    a.Abbreviation,
				Method:      "DELETE",
			},
			{
				Path:        "/" + a.Abbreviation + "/" + "update" + a.StructName,
				Description: "更新" + a.StructName,
				ApiGroup:    a.Abbreviation,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
42
				Method:      "PUT",
43 44 45 46 47 48 49 50 51 52
			},
			{
				Path:        "/" + a.Abbreviation + "/" + "find" + a.StructName,
				Description: "根据ID获取" + a.StructName,
				ApiGroup:    a.Abbreviation,
				Method:      "GET",
			},
			{
				Path:        "/" + a.Abbreviation + "/" + "get" + a.StructName + "List",
				Description: "获取" + a.StructName + "列表",
53
				ApiGroup:    a.Abbreviation,
54 55 56 57 58 59 60 61 62 63 64 65
				Method:      "GET",
			},
		}
		for _, v := range apiList {
			errC := service.CreateApi(v)
			if errC != nil {
				c.Writer.Header().Add("success", "false")
				c.Writer.Header().Add("msg", url.QueryEscape(fmt.Sprintf("自动化创建失败,%v,请自行清空垃圾数据", errC)))
				return
			}
		}
	}
66
	err := service.CreateTemp(a)
67
	if err != nil {
68
		response.FailWithMessage(fmt.Sprintf("创建失败,%v", err), c)
69 70 71 72 73 74 75 76 77
		os.Remove("./ginvueadmin.zip")
	} else {
		c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) //fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
		c.Writer.Header().Add("Content-Type", "application/json")
		c.Writer.Header().Add("success", "true")
		c.File("./ginvueadmin.zip")
		os.Remove("./ginvueadmin.zip")
	}
}