Browse Source

增加了前端api模板

pixel 5 years ago
parent
commit
6fbb6f235d

+ 84 - 0
QMPlusServer/autoCode/fe/autocode/api/api.js

@@ -0,0 +1,84 @@
+import service from '@/utils/request'
+
+// @Tags Test
+// @Summary 创建Test
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body autocode.Test true "创建Test"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
+// @Router /t/createTest [post]
+export const createTest = (data) => {
+     return service({
+         url: "/t/createTest",
+         method: 'post',
+         data
+     })
+ }
+
+
+// @Tags Test
+// @Summary 删除Test
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body autocode.Test true "删除Test"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
+// @Router /t/deleteTest [post]
+ export const deleteTest = (data) => {
+     return service({
+         url: "/t/deleteTest",
+         method: 'post',
+         data
+     })
+ }
+
+// @Tags Test
+// @Summary 更新Test
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body autocode.Test true "更新Test"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
+// @Router /t/updateTest [post]
+ export const updateTest = (data) => {
+     return service({
+         url: "/t/updateTest",
+         method: 'post',
+         data
+     })
+ }
+
+
+// @Tags Test
+// @Summary 用id查询Test
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body autocode.Test true "用id查询Test"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
+// @Router /t/findTest [post]
+ export const findTest = (data) => {
+     return service({
+         url: "/t/findTest",
+         method: 'post',
+         data
+     })
+ }
+
+
+// @Tags Test
+// @Summary 分页获取Test列表
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body modelInterface.PageInfo true "分页获取Test列表"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
+// @Router /t/getTestList [post]
+ export const getTestList = (data) => {
+     return service({
+         url: "/t/getTestList",
+         method: 'post',
+         data
+     })
+ }

+ 118 - 0
QMPlusServer/autoCode/te/autocode/api/api.go

@@ -0,0 +1,118 @@
+package api
+
+import (
+	"fmt"
+	"gin-vue-admin/controller/servers"
+	"gin-vue-admin/model/modelInterface"
+	// 请自行引入model路径
+	"github.com/gin-gonic/gin"
+)
+
+
+// @Tags Test
+// @Summary 创建Test
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body autocode.Test true "创建Test"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
+// @Router /t/createTest [post]
+func CreateTest(c *gin.Context) {
+	var t autocode.Test
+	_ = c.ShouldBindJSON(&t)
+	err := t.CreateTest()
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("创建失败:%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "创建成功", gin.H{})
+	}
+}
+
+
+// @Tags Test
+// @Summary 删除Test
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body autocode.Test true "删除Test"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
+// @Router /t/deleteTest [post]
+func DeleteTest(c *gin.Context) {
+	var t autocode.Test
+	_ = c.ShouldBindJSON(&t)
+	err := t.DeleteTest()
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("删除失败:%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "创建成功", gin.H{})
+	}
+}
+
+
+// @Tags Test
+// @Summary 更新Test
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body autocode.Test true "更新Test"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
+// @Router /t/updateTest [post]
+func UpdateTest(c *gin.Context) {
+	var t autocode.Test
+	_ = c.ShouldBindJSON(&t)
+	err,ret := t.UpdateTest()
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("更新失败:%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "更新成功", gin.H{
+		    "ret":ret,
+		})
+	}
+}
+
+
+// @Tags Test
+// @Summary 用id查询Test
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body autocode.Test true "用id查询Test"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
+// @Router /t/findTest [post]
+func FindTest(c *gin.Context) {
+	var t autocode.Test
+	_ = c.ShouldBindJSON(&t)
+	err,ret := t.FindById()
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("查询失败:%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "查询成功", gin.H{
+		    "ret":ret,
+		})
+	}
+}
+
+
+// @Tags Test
+// @Summary 分页获取Test列表
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body modelInterface.PageInfo true "分页获取Test列表"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
+// @Router /t/getTestList [post]
+func GetTestList(c *gin.Context) {
+	var pageInfo modelInterface.PageInfo
+	_ = c.ShouldBindJSON(&pageInfo)
+	err, list, total := new(autocode.Test).GetInfoList(pageInfo)
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "获取数据成功", gin.H{
+			"autocodeList": list,
+			"total":    total,
+			"page":     pageInfo.Page,
+			"pageSize": pageInfo.PageSize,
+		})
+	}
+}

+ 52 - 0
QMPlusServer/autoCode/te/autocode/model/model.go

@@ -0,0 +1,52 @@
+// 自动生成模板Test
+package autocode
+
+import (
+	"gin-vue-admin/controller/servers"
+	"gin-vue-admin/init/qmsql"
+	"gin-vue-admin/model/modelInterface"
+	"github.com/jinzhu/gorm"
+)
+
+type Test struct {
+      gorm.Model 
+      TestComponent  string `json:"testComponent"`    
+      TestBigComponent  int `json:"testBigComponent"`    
+}
+
+// 创建Test
+func (t *Test)CreateTest()(err error){
+        err = qmsql.DEFAULTDB.Create(t).Error
+        return err
+}
+
+// 删除Test
+func (t *Test)DeleteTest()(err error){
+        err = qmsql.DEFAULTDB.Delete(t).Error
+        return err
+}
+
+// 更新Test
+func (t *Test)UpdateTest()(err error, ret Test){
+        err = qmsql.DEFAULTDB.Save(t).Error
+        return err, *t
+}
+
+// 根据ID查看单条Test
+func (t *Test)FindById()(err error,ret Test){
+    err = qmsql.DEFAULTDB.Where("id = ?",t.ID).First(&ret).Error
+    return err,ret
+}
+
+// 分页获取Test
+func (t *Test)GetInfoList(info modelInterface.PageInfo)(err error, list interface{}, total int){
+    	// 封装分页方法 调用即可 传入 当前的结构体和分页信息
+    	err, db, total := servers.PagingServer(t, info)
+    	if err != nil {
+    		return
+    	} else {
+    		var reTestList []Test
+    		err = db.Find(&reTestList).Error
+    		return err, reTestList, total
+    	}
+}

+ 18 - 0
QMPlusServer/autoCode/te/autocode/router/router.go

@@ -0,0 +1,18 @@
+package router
+
+import (
+	"gin-vue-admin/controller/api"
+	"gin-vue-admin/middleware"
+	"github.com/gin-gonic/gin"
+)
+
+func InitTestRouter(Router *gin.RouterGroup) {
+	TestRouter := Router.Group("t").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
+	{
+		TestRouter.POST("createTest", api.CreateTest)     // 新建Test
+		TestRouter.POST("deleteTest", api.DeleteTest)   //删除Test
+		TestRouter.POST("updateTest", api.UpdateTest)   //更新Test
+		TestRouter.POST("findTest ", api.FindTest)           // 根据ID获取Test
+		TestRouter.POST("getTestList", api.GetTestList) //获取Test列表
+	}
+}

+ 51 - 31
QMPlusServer/model/autoCodeModel/autoCode.go

@@ -2,6 +2,7 @@ package autoCodeModel
 
 import (
 	"fmt"
+	"gin-vue-admin/tools"
 	"html/template"
 	"os"
 )
@@ -31,8 +32,13 @@ type Dictionary struct {
 }
 
 func Temp() {
-	tmpl, err := template.ParseFiles("../../tpl/te/struct.go.tpl")
-	fmt.Println(tmpl, err)
+	modelTmpl, modelTplErr := template.ParseFiles("../../tpl/te/model.go.tpl")
+	apiTmpl, apiTplErr := template.ParseFiles("../../tpl/te/api.go.tpl")
+	routerTmpl, routerTplErr := template.ParseFiles("../../tpl/te/router.go.tpl")
+	feapiTmpl, feapiTplErr := template.ParseFiles("../../tpl/fe/api.js.tpl")
+
+	fmt.Println(modelTplErr,apiTplErr,routerTplErr,feapiTplErr)
+
 	a1 := Component{
 		ComponentName:       "TestComponent",
 		ComponentType:       "string",
@@ -59,37 +65,51 @@ func Temp() {
 		Abbreviation: "t",
 		Components:   []Component{a1, a2},
 	}
+	_autoCode := "../../autoCode/"
+	_te := "../../autoCode/te/"
+	_dir := "../../autoCode/te/" + a.PackageName
+	_modeldir := "../../autoCode/te/" + a.PackageName+"/model"
+	_apidir := "../../autoCode/te/" + a.PackageName+"/api"
+	_routerdir := "../../autoCode/te/" + a.PackageName+"/router"
+	_fe := "../../autoCode/fe/"
+	_fe_dir:="../../autoCode/fe/" + a.PackageName
+	_fe_apidir:="../../autoCode/fe/" + a.PackageName+"/api"
+	mkerr := createDir(_autoCode,_te,_dir,_modeldir,_apidir,_routerdir,_fe,_fe_dir,_fe_apidir)
+	fmt.Print(mkerr)
+	model, _ := os.OpenFile("../../autoCode/te/"+a.PackageName+"/model/model.go", os.O_CREATE|os.O_WRONLY, 0755)
+	api, _ := os.OpenFile("../../autoCode/te/"+a.PackageName+"/api/api.go", os.O_CREATE|os.O_WRONLY, 0755)
+	router, _ := os.OpenFile("../../autoCode/te/"+a.PackageName+"/router/router.go", os.O_CREATE|os.O_WRONLY, 0755)
+	feapi ,_ := os.OpenFile("../../autoCode/fe/"+a.PackageName+"/api/api.js", os.O_CREATE|os.O_WRONLY, 0755)
+	modelErr := modelTmpl.Execute(model, a)
+	apiErr := apiTmpl.Execute(api, a)
+	routerErr := routerTmpl.Execute(router, a)
+	feapiErr := feapiTmpl.Execute(feapi, a)
 
-	_dir := "../" + a.PackageName
-	exist, err := pathExists(_dir)
-	if err != nil {
-		//log.L.Info(fmt.Sprintf("get dir error![%v]\n", err))
-		return
-	}
-	if exist {
-		//log.L.Info(fmt.Sprintf("has dir![%v]\n"+_dir))
-	} else {
-		//log.L.Info(fmt.Sprintf("no dir![%v]\n"+_dir))
-		// 创建文件夹
-		err := os.Mkdir(_dir, os.ModePerm)
+	fmt.Println(modelErr,apiErr,routerErr,feapiErr)
+}
+
+
+
+//批量创建文件夹
+func createDir(dirs ...string)(err error){
+	for _,v:=range dirs{
+		exist, err := tools.PathExists(v)
 		if err != nil {
-			//log.L.Error(fmt.Sprintf("mkdir error![%v]\n",err))
+			//log.L.Info(fmt.Sprintf("get dir error![%v]\n", err))
+			return err
+		}
+		if exist {
+			//log.L.Info(fmt.Sprintf("has dir![%v]\n"+_dir))
 		} else {
-			//log.L.Info("mkdir success!\n")
+			//log.L.Info(fmt.Sprintf("no dir![%v]\n"+_dir))
+			// 创建文件夹
+			err = os.Mkdir(v, os.ModePerm)
+			if err != nil {
+				//log.L.Error(fmt.Sprintf("mkdir error![%v]\n",err))
+			} else {
+				//log.L.Info("mkdir success!\n")
+			}
 		}
 	}
-	file, err := os.OpenFile("../"+a.PackageName+"/struct.go", os.O_CREATE|os.O_WRONLY, 0755)
-	err = tmpl.Execute(file, a)
-}
-
-// 判断文件夹是否存在
-func pathExists(path string) (bool, error) {
-	_, err := os.Stat(path)
-	if err == nil {
-		return true, nil
-	}
-	if os.IsNotExist(err) {
-		return false, nil
-	}
-	return false, err
-}
+	return err
+}

+ 84 - 0
QMPlusServer/tpl/fe/api.js.tpl

@@ -0,0 +1,84 @@
+import service from '@/utils/request'
+
+// @Tags {{.StructName}}
+// @Summary 创建{{.StructName}}
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body {{.PackageName}}.{{.StructName}} true "创建{{.StructName}}"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
+// @Router /{{.Abbreviation}}/create{{.StructName}} [post]
+export const create{{.StructName}} = (data) => {
+     return service({
+         url: "/{{.Abbreviation}}/create{{.StructName}}",
+         method: 'post',
+         data
+     })
+ }
+
+
+// @Tags {{.StructName}}
+// @Summary 删除{{.StructName}}
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body {{.PackageName}}.{{.StructName}} true "删除{{.StructName}}"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
+// @Router /{{.Abbreviation}}/delete{{.StructName}} [post]
+ export const delete{{.StructName}} = (data) => {
+     return service({
+         url: "/{{.Abbreviation}}/delete{{.StructName}}",
+         method: 'post',
+         data
+     })
+ }
+
+// @Tags {{.StructName}}
+// @Summary 更新{{.StructName}}
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body {{.PackageName}}.{{.StructName}} true "更新{{.StructName}}"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
+// @Router /{{.Abbreviation}}/update{{.StructName}} [post]
+ export const update{{.StructName}} = (data) => {
+     return service({
+         url: "/{{.Abbreviation}}/update{{.StructName}}",
+         method: 'post',
+         data
+     })
+ }
+
+
+// @Tags {{.StructName}}
+// @Summary 用id查询{{.StructName}}
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body {{.PackageName}}.{{.StructName}} true "用id查询{{.StructName}}"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
+// @Router /{{.Abbreviation}}/find{{.StructName}} [post]
+ export const find{{.StructName}} = (data) => {
+     return service({
+         url: "/{{.Abbreviation}}/find{{.StructName}}",
+         method: 'post',
+         data
+     })
+ }
+
+
+// @Tags {{.StructName}}
+// @Summary 分页获取{{.StructName}}列表
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body modelInterface.PageInfo true "分页获取{{.StructName}}列表"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
+// @Router /{{.Abbreviation}}/get{{.StructName}}List [post]
+ export const get{{.StructName}}List = (data) => {
+     return service({
+         url: "/{{.Abbreviation}}/get{{.StructName}}List",
+         method: 'post',
+         data
+     })
+ }

+ 90 - 1
QMPlusServer/tpl/te/api.go.tpl

@@ -4,7 +4,7 @@ import (
 	"fmt"
 	"gin-vue-admin/controller/servers"
 	"gin-vue-admin/model/modelInterface"
-	"gin-vue-admin/model/sysModel"
+	// 请自行引入model路径
 	"github.com/gin-gonic/gin"
 )
 
@@ -27,3 +27,92 @@ func Create{{.StructName}}(c *gin.Context) {
 		servers.ReportFormat(c, true, "创建成功", gin.H{})
 	}
 }
+
+
+// @Tags {{.StructName}}
+// @Summary 删除{{.StructName}}
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body {{.PackageName}}.{{.StructName}} true "删除{{.StructName}}"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
+// @Router /{{.Abbreviation}}/delete{{.StructName}} [post]
+func Delete{{.StructName}}(c *gin.Context) {
+	var {{.Abbreviation}} {{.PackageName}}.{{.StructName}}
+	_ = c.ShouldBindJSON(&{{.Abbreviation}})
+	err := {{.Abbreviation}}.Delete{{.StructName}}()
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("删除失败:%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "创建成功", gin.H{})
+	}
+}
+
+
+// @Tags {{.StructName}}
+// @Summary 更新{{.StructName}}
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body {{.PackageName}}.{{.StructName}} true "更新{{.StructName}}"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
+// @Router /{{.Abbreviation}}/update{{.StructName}} [post]
+func Update{{.StructName}}(c *gin.Context) {
+	var {{.Abbreviation}} {{.PackageName}}.{{.StructName}}
+	_ = c.ShouldBindJSON(&{{.Abbreviation}})
+	err,re{{.Abbreviation}} := {{.Abbreviation}}.Update{{.StructName}}()
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("更新失败:%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "更新成功", gin.H{
+		    "re{{.Abbreviation}}":re{{.Abbreviation}},
+		})
+	}
+}
+
+
+// @Tags {{.StructName}}
+// @Summary 用id查询{{.StructName}}
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body {{.PackageName}}.{{.StructName}} true "用id查询{{.StructName}}"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
+// @Router /{{.Abbreviation}}/find{{.StructName}} [post]
+func Find{{.StructName}}(c *gin.Context) {
+	var {{.Abbreviation}} {{.PackageName}}.{{.StructName}}
+	_ = c.ShouldBindJSON(&{{.Abbreviation}})
+	err,re{{.Abbreviation}} := {{.Abbreviation}}.FindById()
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("查询失败:%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "查询成功", gin.H{
+		    "re{{.Abbreviation}}":re{{.Abbreviation}},
+		})
+	}
+}
+
+
+// @Tags {{.StructName}}
+// @Summary 分页获取{{.StructName}}列表
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body modelInterface.PageInfo true "分页获取{{.StructName}}列表"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
+// @Router /{{.Abbreviation}}/get{{.StructName}}List [post]
+func Get{{.StructName}}List(c *gin.Context) {
+	var pageInfo modelInterface.PageInfo
+	_ = c.ShouldBindJSON(&pageInfo)
+	err, list, total := new({{.PackageName}}.{{.StructName}}).GetInfoList(pageInfo)
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "获取数据成功", gin.H{
+			"{{.PackageName}}List": list,
+			"total":    total,
+			"page":     pageInfo.Page,
+			"pageSize": pageInfo.PageSize,
+		})
+	}
+}

+ 18 - 0
QMPlusServer/tpl/te/router.go.tpl

@@ -0,0 +1,18 @@
+package router
+
+import (
+	"gin-vue-admin/controller/api"
+	"gin-vue-admin/middleware"
+	"github.com/gin-gonic/gin"
+)
+
+func Init{{.StructName}}Router(Router *gin.RouterGroup) {
+	{{.StructName}}Router := Router.Group("{{.Abbreviation}}").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
+	{
+		{{.StructName}}Router.POST("create{{.StructName}}", api.Create{{.StructName}})     // 新建{{.StructName}}
+		{{.StructName}}Router.POST("delete{{.StructName}}", api.Delete{{.StructName}})   //删除{{.StructName}}
+		{{.StructName}}Router.POST("update{{.StructName}}", api.Update{{.StructName}})   //更新{{.StructName}}
+		{{.StructName}}Router.POST("find{{.StructName}} ", api.Find{{.StructName}})           // 根据ID获取{{.StructName}}
+		{{.StructName}}Router.POST("get{{.StructName}}List", api.Get{{.StructName}}List) //获取{{.StructName}}列表
+	}
+}