feat : update actions gen

This commit is contained in:
zhangwenjian committed 2020-09-10 02:14:48 +08:00
1 parent aaba20d42a
commit fa07a57d22
14 files changed
+160 -112

No files matched your search

+81 -29
View File
@@ -30,7 +30,7 @@ func Preview(c *gin.Context) {
tools2.HasError(err, "", -1)
t5, err := template.ParseFiles("template/router.go.template")
tools2.HasError(err, "", -1)
t6, err := template.ParseFiles("template/dto.go.template")
t6, err := template.ParseFiles("template/v2/dto.go.template")
tools2.HasError(err, "", -1)
tab, _ := table.Get()
var b1 bytes.Buffer
@@ -68,7 +68,6 @@ func GenCode(c *gin.Context) {
routerfile := "template/routercheckrole.go.template"
oldTest := "// {{认证路由自动补充在此处请勿删除}}"
newText := "// {{认证路由自动补充在此处请勿删除}} \r\n register" + tab.ClassName + "Router(v1,authMiddleware)"
@@ -88,7 +87,7 @@ func GenCode(c *gin.Context) {
tools2.HasError(err, "", -1)
t5, err := template.ParseFiles("template/vue.go.template")
tools2.HasError(err, "", -1)
t6, err := template.ParseFiles("template/dto.go.template")
t6, err := template.ParseFiles("template/v2/dto.go.template")
tools2.HasError(err, "", -1)
_ = tools2.PathCreate("./apis/" + tab.ModuleName + "/")
@@ -136,7 +135,6 @@ func GenCode(c *gin.Context) {
app.OK(c, "", "Code generated successfully!")
}
func GenCodeV2(c *gin.Context) {
table := tools.SysTables{}
id, err := tools2.StringToInt(c.Param("tableId"))
@@ -144,14 +142,23 @@ func GenCodeV2(c *gin.Context) {
table.TableId = id
tab, _ := table.Get()
routerfile := "template/v1.2/routercheckrole.go.template"
if !tab.IsAuth {
routerfile = "template/v1.2/routernocheckrole.go.template"
if tab.IsActions {
ActionsGen(tab)
} else {
NOActionsGen(tab)
}
t1, err := template.ParseFiles("template/v1.2/model.go.template")
app.OK(c, "", "Code generated successfully!")
}
func NOActionsGen(tab tools.SysTables) {
routerfile := "template/routercheckrole.go.template"
if !tab.IsAuth {
routerfile = "template/routernocheckrole.go.template"
}
t1, err := template.ParseFiles("template/model.go.template")
tools2.HasError(err, "", -1)
t2, err := template.ParseFiles("template/api.go.template")
tools2.HasError(err, "", -1)
@@ -161,15 +168,15 @@ func GenCodeV2(c *gin.Context) {
tools2.HasError(err, "", -1)
t5, err := template.ParseFiles("template/vue.go.template")
tools2.HasError(err, "", -1)
t6, err := template.ParseFiles("template/v1.2/dto.go.template")
t6, err := template.ParseFiles("template/v2/dto.go.template")
tools2.HasError(err, "", -1)
_ = tools2.PathCreate("./app/admin/apis/" + tab.ModuleName + "/")
_ = tools2.PathCreate("./app/admin/models/")
_ = tools2.PathCreate("./app/admin/router/")
_ = tools2.PathCreate("./app/admin/dto/")
_ = tools2.PathCreate("./app/" + tab.PackageName + "/apis/" + tab.ModuleName + "/")
_ = tools2.PathCreate("./app/" + tab.PackageName + "/models/")
_ = tools2.PathCreate("./app/" + tab.PackageName + "/router/")
_ = tools2.PathCreate("./app/" + tab.PackageName + "/service/dto/")
_ = tools2.PathCreate(config.GenConfig.FrontPath + "/api/")
_ = tools2.PathCreate(config.GenConfig.FrontPath + "/views/" + tab.PackageName)
_ = tools2.PathCreate(config.GenConfig.FrontPath + "/views/" + tab.BusinessName)
var b1 bytes.Buffer
err = t1.Execute(&b1, tab)
@@ -183,15 +190,60 @@ func GenCodeV2(c *gin.Context) {
err = t5.Execute(&b5, tab)
var b6 bytes.Buffer
err = t6.Execute(&b6, tab)
tools2.FileCreate(b1, "./app/admin/models/"+tab.PackageName+".go")
tools2.FileCreate(b2, "./app/admin/apis/"+tab.ModuleName+"/"+tab.PackageName+".go")
tools2.FileCreate(b3, "./app/admin/router/"+tab.PackageName+".go")
tools2.FileCreate(b4, config.GenConfig.FrontPath+"/api/"+tab.PackageName+".js")
tools2.FileCreate(b5, config.GenConfig.FrontPath+"/views/"+tab.PackageName+"/index.vue")
tools2.FileCreate(b6, "./app/admin/service/dto/"+tab.PackageName+".go")
tools2.FileCreate(b1, "./app/"+tab.PackageName+"/models/"+tab.BusinessName+".go")
tools2.FileCreate(b2, "./app/"+tab.PackageName+"/apis/"+tab.ModuleName+"/"+tab.BusinessName+".go")
tools2.FileCreate(b3, "./app/"+tab.PackageName+"/router/"+tab.BusinessName+".go")
tools2.FileCreate(b4, config.GenConfig.FrontPath+"/api/"+tab.BusinessName+".js")
tools2.FileCreate(b5, config.GenConfig.FrontPath+"/views/"+tab.BusinessName+"/index.vue")
tools2.FileCreate(b6, "./app/"+tab.PackageName+"/service/dto/"+tab.BusinessName+".go")
}
app.OK(c, "", "Code generated successfully!")
func ActionsGen(tab tools.SysTables) {
routerfile := "template/v2/routercheckrole.go.template"
if !tab.IsAuth {
routerfile = "template/v2/routernocheckrole.go.template"
}
t1, err := template.ParseFiles("template/v2/model.go.template")
tools2.HasError(err, "", -1)
//t2, err := template.ParseFiles("template/v2/router.go.template")
//tools2.HasError(err, "", -1)
t3, err := template.ParseFiles(routerfile)
tools2.HasError(err, "", -1)
t4, err := template.ParseFiles("template/js.go.template")
tools2.HasError(err, "", -1)
t5, err := template.ParseFiles("template/vue.go.template")
tools2.HasError(err, "", -1)
t6, err := template.ParseFiles("template/v2/dto.go.template")
tools2.HasError(err, "", -1)
_ = tools2.PathCreate("./app/" + tab.PackageName + "/models/")
_ = tools2.PathCreate("./app/" + tab.PackageName + "/router/")
_ = tools2.PathCreate("./app/" + tab.PackageName + "/service/dto/")
_ = tools2.PathCreate(config.GenConfig.FrontPath + "/api/")
_ = tools2.PathCreate(config.GenConfig.FrontPath + "/views/" + tab.BusinessName)
var b1 bytes.Buffer
err = t1.Execute(&b1, tab)
//var b2 bytes.Buffer
//err = t2.Execute(&b2, tab)
var b3 bytes.Buffer
err = t3.Execute(&b3, tab)
var b4 bytes.Buffer
err = t4.Execute(&b4, tab)
var b5 bytes.Buffer
err = t5.Execute(&b5, tab)
var b6 bytes.Buffer
err = t6.Execute(&b6, tab)
tools2.FileCreate(b1, "./app/"+tab.PackageName+"/models/"+tab.BusinessName+".go")
//tools2.FileCreate(b2, "./app/"+tab.PackageName+"/router/"+tab.BusinessName+".go")
tools2.FileCreate(b3, "./app/"+tab.PackageName+"/router/"+tab.BusinessName+".go")
tools2.FileCreate(b4, config.GenConfig.FrontPath+"/api/"+tab.BusinessName+".js")
tools2.FileCreate(b5, config.GenConfig.FrontPath+"/views/"+tab.BusinessName+"/index.vue")
tools2.FileCreate(b6, "./app/"+tab.PackageName+"/service/dto/"+tab.BusinessName+".go")
}
func GenMenuAndApi(c *gin.Context) {
@@ -228,10 +280,10 @@ func GenMenuAndApi(c *gin.Context) {
Cmenu.Path = tab.TBName
Cmenu.MenuType = "C"
Cmenu.Action = "无"
Cmenu.Permission = tab.PackageName + ":" + tab.ModuleName + ":list"
Cmenu.Permission = tab.ModuleName + ":" + tab.BusinessName + ":list"
Cmenu.ParentId = Mmenu.MenuId
Cmenu.NoCache = false
Cmenu.Component = "/" + tab.ModuleName + "/index"
Cmenu.Component = "/" + tab.BusinessName + "/index"
Cmenu.Sort = 0
Cmenu.Visible = "0"
Cmenu.IsFrame = "0"
@@ -248,7 +300,7 @@ func GenMenuAndApi(c *gin.Context) {
MList.Path = tab.TBName
MList.MenuType = "F"
MList.Action = "无"
MList.Permission = tab.PackageName + ":" + tab.ModuleName + ":query"
MList.Permission = tab.ModuleName + ":" + tab.BusinessName + ":query"
MList.ParentId = Cmenu.MenuId
MList.NoCache = false
MList.Sort = 0
@@ -267,7 +319,7 @@ func GenMenuAndApi(c *gin.Context) {
MCreate.Path = tab.TBName
MCreate.MenuType = "F"
MCreate.Action = "无"
MCreate.Permission = tab.PackageName + ":" + tab.ModuleName + ":add"
MCreate.Permission = tab.ModuleName + ":" + tab.BusinessName + ":add"
MCreate.ParentId = Cmenu.MenuId
MCreate.NoCache = false
MCreate.Sort = 0
@@ -286,7 +338,7 @@ func GenMenuAndApi(c *gin.Context) {
MUpdate.Path = tab.TBName
MUpdate.MenuType = "F"
MUpdate.Action = "无"
MUpdate.Permission = tab.PackageName + ":" + tab.ModuleName + ":edit"
MUpdate.Permission = tab.ModuleName + ":" + tab.BusinessName + ":edit"
MUpdate.ParentId = Cmenu.MenuId
MUpdate.NoCache = false
MUpdate.Sort = 0
@@ -305,7 +357,7 @@ func GenMenuAndApi(c *gin.Context) {
MDelete.Path = tab.TBName
MDelete.MenuType = "F"
MDelete.Action = "无"
MDelete.Permission = tab.PackageName + ":" + tab.ModuleName + ":remove"
MDelete.Permission = tab.ModuleName + ":" + tab.BusinessName + ":remove"
MDelete.ParentId = Cmenu.MenuId
MDelete.NoCache = false
MDelete.Sort = 0
+1 -2
View File
@@ -5,7 +5,6 @@ import (
"go-admin/app/admin/middleware"
"go-admin/app/admin/middleware/handler"
"go-admin/common/global"
jwt "go-admin/pkg/jwtauth"
_ "go-admin/pkg/jwtauth"
"go-admin/tools"
config2 "go-admin/tools/config"
@@ -19,7 +18,7 @@ func InitRouter(r *gin.Engine) *gin.Engine {
middleware.InitMiddleware(r)
// the jwt middleware
var err error
authMiddleware, err = middleware.AuthInit()
authMiddleware, err := middleware.AuthInit()
tools.HasError(err, "JWT Init Error", 500)
// 注册系统路由
+1 -1
View File
@@ -65,7 +65,7 @@ func sysNoCheckRoleRouter(r *gin.RouterGroup) {
v1.GET("/monitor/server", monitor.ServerInfo)
v1.GET("/getCaptcha", system.GenerateCaptchaHandler)
v1.GET("/gen/preview/:tableId", Preview)
v1.GET("/gen/toproject/:tableId", GenCode)
v1.GET("/gen/toproject/:tableId", GenCodeV2)
v1.GET("/gen/todb/:tableId", GenMenuAndApi)
v1.GET("/gen/tabletree", GetSysTablesTree)
v1.GET("/menuTreeselect", system.GetMenuTreeelect)
+1
View File
@@ -2,6 +2,7 @@ package dto
import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/models"
"go-admin/common/dto"
models2 "go-admin/common/models"
+1 -2
View File
@@ -7,7 +7,6 @@ import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"go-admin/app/admin/router"
"go-admin/common/global"
mycasbin "go-admin/pkg/casbin"
"go-admin/pkg/logger"
@@ -22,6 +21,6 @@ func main() {
mycasbin.Setup()
logger.Setup()
global.GinEngine = gin.Default()
router.InitRouter()
//router.InitRouter()
log.Fatal(global.GinEngine.Run(":8000"))
}
+4 -3
View File
@@ -30,17 +30,18 @@ data: data
// 修改{{.ClassName}}
export function update{{.ClassName}}(data) {
return request({
url: '/api/v1/{{.ModuleName}}',
url: '/api/v1/{{.ModuleName}}/'+data.{{.PkJsonField}},
method: 'put',
data: data
})
}
// 删除{{.ClassName}}
export function del{{.ClassName}}({{.PkJsonField}}) {
export function del{{.ClassName}}({{.PkJsonField}},data) {
return request({
url: '/api/v1/{{.ModuleName}}/' + {{.PkJsonField}},
method: 'delete'
method: 'delete',
data: data
})
}
+7 -2
View File
@@ -1,8 +1,13 @@
package models
import (
orm "go-admin/global"
"go-admin/tools"
"strconv"
"go-admin/common/dto"
orm "go-admin/common/global"
"go-admin/common/models"
"go-admin/tools"
{{ range .Columns }}
{{- if eq .GoField "CreatedAt" -}}
{{- else if eq .GoField "UpdatedAt" -}}
@@ -5,18 +5,17 @@ import (
"go-admin/app/admin/models"
"go-admin/common/dto"
models2 "go-admin/common/models"
"net/http"
)
type {{.ClassName}}Search struct {
Pagination `search:"-"`
dto.Pagination `search:"-"`
{{ $tablename := .TBName -}}
{{ range .Columns -}}
{{$z := .IsQuery}}
{{- if ($z) -}}
{{.GoField}} {{.GoType}} `form:"{{.JsonField}}" search:"type:{{if eq .QueryType "EQ"}}exact{{ else if eq .QueryType "NE"}}iexact{{ else if eq .QueryType "LIKE"}}contains{{ else if eq .QueryType "GT"}}gt{{ else if eq .QueryType "GTE"}}gte{{ else if eq .QueryType "LT"}}lt{{ else if eq .QueryType "LTE"}}lte{{- end }};column:{{.ColumnName}};table:{{$tablename}}"` // {{.ColumnComment}}
{{end -}}
{{ end -}}
{{- end }}
}
@@ -75,29 +74,16 @@ func (s *{{.ClassName}}Control) GenerateM() (models2.ActiveRecord, error) {
{{- else }}
{{.GoField}}: s.{{.GoField}},
{{- end }}
{{- end }}
}, nil
}
func (s *{{.ClassName}}Control) GetId() interface{} {
return s.{{.PkColumn}}
return s.{{.PkGoField}}
}
type {{.ClassName}}ById struct {
Id int `uri:"id" validate:"required"`
Ids []int `json:"ids"`
}
func (s *{{.ClassName}}ById) Bind(ctx *gin.Context) error {
if ctx.Request.Method == http.MethodDelete {
err := ctx.Bind(s)
if err != nil {
return err
}
if len(s.Ids) > 0 {
return nil
}
}
return ctx.BindUri(s)
dto.ObjectById
}
func (s *{{.ClassName}}ById) Generate() dto.Control {
@@ -107,11 +93,4 @@ func (s *{{.ClassName}}ById) Generate() dto.Control {
func (s *{{.ClassName}}ById) GenerateM() (models2.ActiveRecord, error) {
return &models.{{.ClassName}}{}, nil
}
func (s *{{.ClassName}}ById) GetId() interface{} {
if len(s.Ids) > 0 {
return s.Ids
}
return s.Id
}
}
@@ -1,14 +1,17 @@
package models
import (
"strconv"
{{- if eq .IsActions true -}}
{{- else }}
"go-admin/common/dto"
orm "go-admin/common/global"
"go-admin/common/models"
"go-admin/tools"
"strconv"
orm "go-admin/common/global"
{{ end }}
"go-admin/common/models"
)
type SysJob struct {
type {{.ClassName}} struct {
{{ range .Columns -}}
{{$x := .Pk}}
{{- if ($x) }}
@@ -34,7 +37,7 @@ func (e *{{.ClassName}}) Generate() models.ActiveRecord {
}
func (e *{{.ClassName}}) GetId() interface{} {
return e.{{.PkColumn}}
return e.{{.PkGoField}}
}
func (e *{{.ClassName}}) SetCreateBy(createBy uint) {
@@ -3,7 +3,6 @@ package router
import (
"github.com/gin-gonic/gin"
"go-admin/apis/{{.ModuleName}}"
"go-admin/app/admin/middleware"
"go-admin/app/admin/models"
"go-admin/app/admin/service/dto"
@@ -26,7 +25,7 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJW
}))
r.GET("/:id", actions.PermissionAction(), actions.ViewAction(new(dto.{{.ClassName}}ById)))
r.POST("", actions.CreateAction(new(dto.{{.ClassName}}Control)))
r.PUT("", actions.PermissionAction(), actions.UpdateAction(new(dto.{{.ClassName}}Control)))
r.PUT("/:id", actions.PermissionAction(), actions.UpdateAction(new(dto.{{.ClassName}}Control)))
r.DELETE("/:id", actions.PermissionAction(), actions.DeleteAction(new(dto.{{.ClassName}}ById)))
}
}
@@ -3,7 +3,10 @@ package router
import (
"github.com/gin-gonic/gin"
"go-admin/apis/{{.ModuleName}}"
"go-admin/app/admin/middleware"
"go-admin/app/admin/models"
"go-admin/app/admin/service/dto"
"go-admin/common/actions"
)
// 无需认证的路由代码
@@ -17,7 +20,7 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup) {
}))
r.GET("/:id", actions.ViewAction(new(dto.{{.ClassName}}ById)))
r.POST("", actions.CreateAction(new(dto.{{.ClassName}}Control)))
r.PUT("", actions.UpdateAction(new(dto.{{.ClassName}}Control)))
r.PUT("/:id", actions.UpdateAction(new(dto.{{.ClassName}}Control)))
r.DELETE("/:id", actions.DeleteAction(new(dto.{{.ClassName}}ById)))
}
}
+6 -2
View File
@@ -215,7 +215,7 @@
</template>
<script>
import {add{{.ClassName}}, del{{.ClassName}}, get{{.ClassName}}, list{{.ClassName}}, update{{.ClassName}}} from '@/api/{{ .PackageName}}'
import {add{{.ClassName}}, del{{.ClassName}}, get{{.ClassName}}, list{{.ClassName}}, update{{.ClassName}}} from '@/api/{{ .BusinessName}}'
{{range .Columns}}
{{- if ne .FkTableName "" -}}
import {list{{.FkTableNameClass}} } from '@/api/{{ .FkTableNamePackage}}'
@@ -428,7 +428,11 @@
cancelButtonText: '取消',
type: 'warning'
}).then(function () {
return del{{.ClassName}}(Ids)
if (Ids.length > 1) {
return del{{.ClassName}}(Ids.shift(), { 'ids': Ids })
} else {
return del{{.ClassName}}(Ids, { 'ids': [] })
}
}).then(() => {
this.getList()
this.msgSuccess('删除成功')
+34 -34
View File
@@ -1,47 +1,47 @@
package test
import (
"go-admin/models/tools"
"os"
//"go-admin/models/tools"
//"os"
"testing"
"text/template"
//"text/template"
)
func TestGoModelTemplate(t *testing.T) {
t1, err := template.ParseFiles("model.go.template")
if err != nil {
t.Error(err)
}
table := tools.SysTables{}
table.TBName = "sys_tables"
tab, err := table.Get()
if err != nil {
t.Error(err)
}
file, err := os.Create("models/" + table.PackageName + ".go")
if err != nil {
t.Error(err)
}
defer file.Close()
_ = t1.Execute(file, tab)
//t1, err := template.ParseFiles("model.go.template")
//if err != nil {
// t.Error(err)
//}
//table := tools.SysTables{}
//table.TBName = "sys_tables"
//tab, err := table.Get()
//if err != nil {
// t.Error(err)
//}
//file, err := os.Create("models/" + table.PackageName + ".go")
//if err != nil {
// t.Error(err)
//}
//defer file.Close()
//
//_ = t1.Execute(file, tab)
t.Log("")
}
func TestGoApiTemplate(t *testing.T) {
t1, err := template.ParseFiles("api.go.template")
if err != nil {
t.Error(err)
}
table := tools.SysTables{}
table.TBName = "sys_tables"
tab, _ := table.Get()
file, err := os.Create("apis/" + table.PackageName + ".go")
if err != nil {
t.Error(err)
}
defer file.Close()
_ = t1.Execute(file, tab)
//t1, err := template.ParseFiles("api.go.template")
//if err != nil {
// t.Error(err)
//}
//table := tools.SysTables{}
//table.TBName = "sys_tables"
//tab, _ := table.Get()
//file, err := os.Create("apis/" + table.PackageName + ".go")
//if err != nil {
// t.Error(err)
//}
//defer file.Close()
//
//_ = t1.Execute(file, tab)
t.Log("")
}
+4 -1
View File
@@ -34,7 +34,10 @@ func FileCreate(content bytes.Buffer, name string) {
if err != nil {
log.Println(err)
}
file.WriteString(content.String())
_, err = file.WriteString(content.String())
if err != nil {
log.Println(err)
}
//for i := 0; i < len(content); i++ {
// //写入byte的slice数据
// file.Write(content)