1、初始化项目
This commit is contained in:
326
template/api_migrate.template
Normal file
326
template/api_migrate.template
Normal file
@ -0,0 +1,326 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg"
|
||||
|
||||
"go-admin/cmd/migrate/migration"
|
||||
common "go-admin/common/models"
|
||||
)
|
||||
|
||||
type Menu struct {
|
||||
MenuId int `json:"menuId" gorm:"primaryKey;autoIncrement"`
|
||||
MenuName string `json:"menuName" gorm:"size:128;"`
|
||||
Title string `json:"title" gorm:"size:128;"`
|
||||
Icon string `json:"icon" gorm:"size:128;"`
|
||||
Path string `json:"path" gorm:"size:128;"`
|
||||
Paths string `json:"paths" gorm:"size:128;"`
|
||||
MenuType string `json:"menuType" gorm:"size:1;"`
|
||||
Action string `json:"action" gorm:"size:16;"`
|
||||
Permission string `json:"permission" gorm:"size:255;"`
|
||||
ParentId int `json:"parentId" gorm:"size:11;"`
|
||||
NoCache bool `json:"noCache" gorm:"size:8;"`
|
||||
Breadcrumb string `json:"breadcrumb" gorm:"size:255;"`
|
||||
Component string `json:"component" gorm:"size:255;"`
|
||||
Sort int `json:"sort" gorm:"size:4;"`
|
||||
Visible string `json:"visible" gorm:"size:1;"`
|
||||
CreateBy string `json:"createBy" gorm:"size:128;"`
|
||||
UpdateBy string `json:"updateBy" gorm:"size:128;"`
|
||||
IsFrame string `json:"isFrame" gorm:"size:1;DEFAULT:0;"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt *time.Time `json:"deletedAt"`
|
||||
}
|
||||
|
||||
func (Menu) TableName() string {
|
||||
return "sys_menu"
|
||||
}
|
||||
|
||||
func init() {
|
||||
_, fileName, _, _ := runtime.Caller(0)
|
||||
migration.Migrate.SetVersion(migration.GetFilename(fileName), _{{.GenerateTime}}Test)
|
||||
}
|
||||
|
||||
func _{{.GenerateTime}}Test(db *gorm.DB, version string) error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
timeNow := pkg.GetCurrentTime()
|
||||
Mmenu := Menu{}
|
||||
Mmenu.MenuName = "{{.TBName}}Manage"
|
||||
Mmenu.Title = "{{.TableComment}}"
|
||||
Mmenu.Icon = "pass"
|
||||
Mmenu.Path = "/{{.TBName}}"
|
||||
Mmenu.MenuType = "M"
|
||||
Mmenu.Action = "无"
|
||||
Mmenu.ParentId = 0
|
||||
Mmenu.NoCache = false
|
||||
Mmenu.Component = "Layout"
|
||||
Mmenu.Sort = 0
|
||||
Mmenu.Visible = "0"
|
||||
Mmenu.IsFrame = "0"
|
||||
Mmenu.CreateBy = "1"
|
||||
Mmenu.UpdateBy = "1"
|
||||
Mmenu.CreatedAt = timeNow
|
||||
Mmenu.UpdatedAt = timeNow
|
||||
// Mmenu.MenuId, err = Mmenu.Create(db)
|
||||
err := tx.Create(&Mmenu).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
Cmenu := Menu{}
|
||||
Cmenu.MenuName = "{{.TBName}}"
|
||||
Cmenu.Title = "{{.TableComment}}"
|
||||
Cmenu.Icon = "pass"
|
||||
Cmenu.Path = "{{.TBName}}"
|
||||
Cmenu.MenuType = "C"
|
||||
Cmenu.Action = "无"
|
||||
Cmenu.Permission = "{{.PackageName}}:{{.BusinessName}}:list"
|
||||
Cmenu.ParentId = Mmenu.MenuId
|
||||
Cmenu.NoCache = false
|
||||
Cmenu.Component = "/{{.BusinessName}}/index"
|
||||
Cmenu.Sort = 0
|
||||
Cmenu.Visible = "0"
|
||||
Cmenu.IsFrame = "0"
|
||||
Cmenu.CreateBy = "1"
|
||||
Cmenu.UpdateBy = "1"
|
||||
Cmenu.CreatedAt = timeNow
|
||||
Cmenu.UpdatedAt = timeNow
|
||||
// Cmenu.MenuId, err = Cmenu.Create(db)
|
||||
err = tx.Create(&Cmenu).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
MList := Menu{}
|
||||
MList.MenuName = ""
|
||||
MList.Title = "分页获取{{.TableComment}}"
|
||||
MList.Icon = ""
|
||||
MList.Path = "{{.TBName}}"
|
||||
MList.MenuType = "F"
|
||||
MList.Action = "无"
|
||||
MList.Permission = "{{.PackageName}}:{{.BusinessName}}:query"
|
||||
MList.ParentId = Cmenu.MenuId
|
||||
MList.NoCache = false
|
||||
MList.Sort = 0
|
||||
MList.Visible = "0"
|
||||
MList.IsFrame = "0"
|
||||
MList.CreateBy = "1"
|
||||
MList.UpdateBy = "1"
|
||||
MList.CreatedAt = timeNow
|
||||
MList.UpdatedAt = timeNow
|
||||
// MList.MenuId, err = MList.Create(db)
|
||||
err = tx.Create(&MList).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
MCreate := Menu{}
|
||||
MCreate.MenuName = ""
|
||||
MCreate.Title = "创建{{.TableComment}}"
|
||||
MCreate.Icon = ""
|
||||
MCreate.Path = "{{.TBName}}"
|
||||
MCreate.MenuType = "F"
|
||||
MCreate.Action = "无"
|
||||
MCreate.Permission = "{{.PackageName}}:{{.BusinessName}}:add"
|
||||
MCreate.ParentId = Cmenu.MenuId
|
||||
MCreate.NoCache = false
|
||||
MCreate.Sort = 0
|
||||
MCreate.Visible = "0"
|
||||
MCreate.IsFrame = "0"
|
||||
MCreate.CreateBy = "1"
|
||||
MCreate.UpdateBy = "1"
|
||||
MCreate.CreatedAt = timeNow
|
||||
MCreate.UpdatedAt = timeNow
|
||||
// MCreate.MenuId, err = MCreate.Create(db)
|
||||
err = tx.Create(&MCreate).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
MUpdate := Menu{}
|
||||
MUpdate.MenuName = ""
|
||||
MUpdate.Title = "修改{{.TableComment}}"
|
||||
MUpdate.Icon = ""
|
||||
MUpdate.Path = "{{.TBName}}"
|
||||
MUpdate.MenuType = "F"
|
||||
MUpdate.Action = "无"
|
||||
MUpdate.Permission ="{{.PackageName}}:{{.BusinessName}}:edit"
|
||||
MUpdate.ParentId = Cmenu.MenuId
|
||||
MUpdate.NoCache = false
|
||||
MUpdate.Sort = 0
|
||||
MUpdate.Visible = "0"
|
||||
MUpdate.IsFrame = "0"
|
||||
MUpdate.CreateBy = "1"
|
||||
MUpdate.UpdateBy = "1"
|
||||
MUpdate.CreatedAt = timeNow
|
||||
MUpdate.UpdatedAt = timeNow
|
||||
// MUpdate.MenuId, err = MUpdate.Create(db)
|
||||
err = tx.Create(&MUpdate).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
MDelete := Menu{}
|
||||
MDelete.MenuName = ""
|
||||
MDelete.Title = "删除{{.TableComment}}"
|
||||
MDelete.Icon = ""
|
||||
MDelete.Path = "{{.TBName}}"
|
||||
MDelete.MenuType = "F"
|
||||
MDelete.Action = "无"
|
||||
MDelete.Permission = "{{.PackageName}}:{{.BusinessName}}:remove"
|
||||
MDelete.ParentId = Cmenu.MenuId
|
||||
MDelete.NoCache = false
|
||||
MDelete.Sort = 0
|
||||
MDelete.Visible = "0"
|
||||
MDelete.IsFrame = "0"
|
||||
MDelete.CreateBy = "1"
|
||||
MDelete.UpdateBy = "1"
|
||||
MDelete.CreatedAt = timeNow
|
||||
MDelete.UpdatedAt = timeNow
|
||||
// MDelete.MenuId, err = MDelete.Create(db)
|
||||
err = tx.Create(&MDelete).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var InterfaceId = 63
|
||||
Amenu := Menu{}
|
||||
Amenu.MenuName = "{{.TBName}}"
|
||||
Amenu.Title = "{{.TableComment}}"
|
||||
Amenu.Icon = "bug"
|
||||
Amenu.Path = "{{.TBName}}"
|
||||
Amenu.MenuType = "M"
|
||||
Amenu.Action = "无"
|
||||
Amenu.ParentId = InterfaceId
|
||||
Amenu.NoCache = false
|
||||
Amenu.Sort = 0
|
||||
Amenu.Visible = "1"
|
||||
Amenu.IsFrame = "0"
|
||||
Amenu.CreateBy = "1"
|
||||
Amenu.UpdateBy = "1"
|
||||
Amenu.CreatedAt = timeNow
|
||||
Amenu.UpdatedAt = timeNow
|
||||
// Amenu.MenuId, err = Amenu.Create(db)
|
||||
err = tx.Create(&Amenu).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
AList := Menu{}
|
||||
AList.MenuName = ""
|
||||
AList.Title = "分页获取{{.TableComment}}"
|
||||
AList.Icon = "bug"
|
||||
AList.Path = "/api/v1/{{.ModuleName}}"
|
||||
AList.MenuType = "A"
|
||||
AList.Action = "GET"
|
||||
AList.ParentId = Amenu.MenuId
|
||||
AList.NoCache = false
|
||||
AList.Sort = 0
|
||||
AList.Visible = "1"
|
||||
AList.IsFrame = "0"
|
||||
AList.CreateBy = "1"
|
||||
AList.UpdateBy = "1"
|
||||
AList.CreatedAt = timeNow
|
||||
AList.UpdatedAt = timeNow
|
||||
// AList.MenuId, err = AList.Create(db)
|
||||
err = tx.Create(&AList).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
AGet := Menu{}
|
||||
AGet.MenuName = ""
|
||||
AGet.Title = "根据id获取{{.TableComment}}"
|
||||
AGet.Icon = "bug"
|
||||
AGet.Path = "/api/v1/{{.ModuleName}}/:id"
|
||||
AGet.MenuType = "A"
|
||||
AGet.Action = "GET"
|
||||
AGet.ParentId = Amenu.MenuId
|
||||
AGet.NoCache = false
|
||||
AGet.Sort = 0
|
||||
AGet.Visible = "1"
|
||||
AGet.IsFrame = "0"
|
||||
AGet.CreateBy = "1"
|
||||
AGet.UpdateBy = "1"
|
||||
AGet.CreatedAt = timeNow
|
||||
AGet.UpdatedAt = timeNow
|
||||
// AGet.MenuId, err = AGet.Create(db)
|
||||
err = tx.Create(&AGet).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ACreate := Menu{}
|
||||
ACreate.MenuName = ""
|
||||
ACreate.Title = "创建{{.TableComment}}"
|
||||
ACreate.Icon = "bug"
|
||||
ACreate.Path = "/api/v1/{{.ModuleName}}"
|
||||
ACreate.MenuType = "A"
|
||||
ACreate.Action = "POST"
|
||||
ACreate.ParentId = Amenu.MenuId
|
||||
ACreate.NoCache = false
|
||||
ACreate.Sort = 0
|
||||
ACreate.Visible = "1"
|
||||
ACreate.IsFrame = "0"
|
||||
ACreate.CreateBy = "1"
|
||||
ACreate.UpdateBy = "1"
|
||||
ACreate.CreatedAt = timeNow
|
||||
ACreate.UpdatedAt = timeNow
|
||||
// ACreate.MenuId, err = ACreate.Create(db)
|
||||
err = tx.Create(&ACreate).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
AUpdate := Menu{}
|
||||
AUpdate.MenuName = ""
|
||||
AUpdate.Title = "修改{{.TableComment}}"
|
||||
AUpdate.Icon = "bug"
|
||||
AUpdate.Path = "/api/v1/{{.ModuleName}}/:id"
|
||||
AUpdate.MenuType = "A"
|
||||
AUpdate.Action = "PUT"
|
||||
AUpdate.ParentId = Amenu.MenuId
|
||||
AUpdate.NoCache = false
|
||||
AUpdate.Sort = 0
|
||||
AUpdate.Visible = "1"
|
||||
AUpdate.IsFrame = "0"
|
||||
AUpdate.CreateBy = "1"
|
||||
AUpdate.UpdateBy = "1"
|
||||
AUpdate.CreatedAt = timeNow
|
||||
AUpdate.UpdatedAt = timeNow
|
||||
// AUpdate.MenuId, err = AUpdate.Create(db)
|
||||
err = tx.Create(&AUpdate).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ADelete := Menu{}
|
||||
ADelete.MenuName = ""
|
||||
ADelete.Title = "删除{{.TableComment}}"
|
||||
ADelete.Icon = "bug"
|
||||
ADelete.Path = "/api/v1/{{.ModuleName}}"
|
||||
ADelete.MenuType = "A"
|
||||
ADelete.Action = "DELETE"
|
||||
ADelete.ParentId = Amenu.MenuId
|
||||
ADelete.NoCache = false
|
||||
ADelete.Sort = 0
|
||||
ADelete.Visible = "1"
|
||||
ADelete.IsFrame = "0"
|
||||
ADelete.CreateBy = "1"
|
||||
ADelete.UpdateBy = "1"
|
||||
ADelete.CreatedAt = timeNow
|
||||
ADelete.UpdatedAt = timeNow
|
||||
//ADelete.MenuId, err = ADelete.Create(db)
|
||||
err = tx.Create(&ADelete).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Create(&common.Migration{
|
||||
Version: version,
|
||||
}).Error
|
||||
})
|
||||
}
|
||||
8
template/cmd_api.template
Normal file
8
template/cmd_api.template
Normal file
@ -0,0 +1,8 @@
|
||||
package api
|
||||
|
||||
import "go-admin/app/{{.appName}}/router"
|
||||
|
||||
func init() {
|
||||
//注册路由 fixme 其他应用的路由,在本目录新建文件放在init方法
|
||||
AppRouters = append(AppRouters, router.InitRouter)
|
||||
}
|
||||
40
template/migrate.template
Normal file
40
template/migrate.template
Normal file
@ -0,0 +1,40 @@
|
||||
package {{.Package}}
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"runtime"
|
||||
|
||||
"go-admin/cmd/migrate/migration"
|
||||
common "go-admin/common/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
_, fileName, _, _ := runtime.Caller(0)
|
||||
migration.Migrate.SetVersion(migration.GetFilename(fileName), _{{.GenerateTime}}Test)
|
||||
}
|
||||
|
||||
func _{{.GenerateTime}}Test(db *gorm.DB, version string) error {
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
// TODO: 这里开始写入要变更的内容
|
||||
|
||||
// TODO: 例如 修改表字段 使用过程中请删除此段代码
|
||||
//err := tx.Migrator().RenameColumn(&models.SysConfig{}, "config_id", "id")
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
|
||||
// TODO: 例如 新增表结构 使用过程中请删除此段代码
|
||||
//err = tx.Debug().Migrator().AutoMigrate(
|
||||
// new(models.CasbinRule),
|
||||
// )
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
|
||||
|
||||
return tx.Create(&common.Migration{
|
||||
Version: version,
|
||||
}).Error
|
||||
})
|
||||
}
|
||||
75
template/router.template
Normal file
75
template/router.template
Normal file
@ -0,0 +1,75 @@
|
||||
package router
|
||||
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
_ "github.com/gin-gonic/gin"
|
||||
log "github.com/go-admin-team/go-admin-core/logger"
|
||||
"github.com/go-admin-team/go-admin-core/sdk"
|
||||
// "github.com/go-admin-team/go-admin-core/sdk/pkg"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
||||
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
||||
common "go-admin/common/middleware"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
routerNoCheckRole = make([]func(*gin.RouterGroup), 0)
|
||||
routerCheckRole = make([]func(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware), 0)
|
||||
)
|
||||
|
||||
// InitRouter 路由初始化
|
||||
func InitRouter() {
|
||||
var r *gin.Engine
|
||||
h := sdk.Runtime.GetEngine()
|
||||
if h == nil {
|
||||
h = gin.New()
|
||||
sdk.Runtime.SetEngine(h)
|
||||
}
|
||||
switch h.(type) {
|
||||
case *gin.Engine:
|
||||
r = h.(*gin.Engine)
|
||||
default:
|
||||
log.Fatal("not support other engine")
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
// the jwt middleware
|
||||
authMiddleware, err := common.AuthInit()
|
||||
if err != nil {
|
||||
log.Fatalf("JWT Init Error, %s", err.Error())
|
||||
}
|
||||
|
||||
// 注册业务路由
|
||||
InitBusinessRouter(r, authMiddleware)
|
||||
}
|
||||
|
||||
func InitBusinessRouter(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.Engine {
|
||||
|
||||
// 无需认证的路由
|
||||
noCheckRoleRouter(r)
|
||||
// 需要认证的路由
|
||||
checkRoleRouter(r, authMiddleware)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
// noCheckRoleRouter 无需认证的路由
|
||||
func noCheckRoleRouter(r *gin.Engine) {
|
||||
// 可根据业务需求来设置接口版本
|
||||
v := r.Group("/api/v1")
|
||||
|
||||
for _, f := range routerNoCheckRole {
|
||||
f(v)
|
||||
}
|
||||
}
|
||||
|
||||
// checkRoleRouter 需要认证的路由
|
||||
func checkRoleRouter(r *gin.Engine, authMiddleware *jwtauth.GinJWTMiddleware) {
|
||||
// 可根据业务需求来设置接口版本
|
||||
v := r.Group("/api/v1")
|
||||
|
||||
for _, f := range routerCheckRole {
|
||||
f(v, authMiddleware)
|
||||
}
|
||||
}
|
||||
31
template/v4/actions/router_check_role.go.template
Normal file
31
template/v4/actions/router_check_role.go.template
Normal file
@ -0,0 +1,31 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
||||
|
||||
"go-admin/app/{{.PackageName}}/models"
|
||||
"go-admin/app/{{.PackageName}}/service/dto"
|
||||
"go-admin/common/actions"
|
||||
"go-admin/common/middleware"
|
||||
)
|
||||
|
||||
func init() {
|
||||
routerCheckRole = append(routerCheckRole, register{{.ClassName}}Router)
|
||||
}
|
||||
|
||||
// 需认证的路由代码
|
||||
func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
||||
r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
|
||||
{
|
||||
model := &models.{{.ClassName}}{}
|
||||
r.GET("", actions.PermissionAction(), actions.IndexAction(model, new(dto.{{.ClassName}}Search), func() interface{} {
|
||||
list := make([]models.{{.ClassName}}, 0)
|
||||
return &list
|
||||
}))
|
||||
r.GET("/:id", actions.PermissionAction(), actions.ViewAction(new(dto.{{.ClassName}}ById), nil))
|
||||
r.POST("", actions.CreateAction(new(dto.{{.ClassName}}Control)))
|
||||
r.PUT("/:id", actions.PermissionAction(), actions.UpdateAction(new(dto.{{.ClassName}}Control)))
|
||||
r.DELETE("", actions.PermissionAction(), actions.DeleteAction(new(dto.{{.ClassName}}ById)))
|
||||
}
|
||||
}
|
||||
30
template/v4/actions/router_no_check_role.go.template
Normal file
30
template/v4/actions/router_no_check_role.go.template
Normal file
@ -0,0 +1,30 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"go-admin/app/{{.PackageName}}/middleware"
|
||||
"go-admin/app/{{.PackageName}}/models"
|
||||
"go-admin/app/{{.PackageName}}/service/dto"
|
||||
"go-admin/common/actions"
|
||||
)
|
||||
|
||||
func init() {
|
||||
routerNoCheckRole = append(routerNoCheckRole, register{{.ClassName}}Router)
|
||||
}
|
||||
|
||||
// 无需认证的路由代码
|
||||
func register{{.ClassName}}Router(v1 *gin.RouterGroup) {
|
||||
r := v1.Group("/{{.ModuleName}}")
|
||||
{
|
||||
model := &models.{{.ClassName}}{}
|
||||
r.GET("", actions.IndexAction(model, new(dto.{{.ClassName}}Search), func() interface{} {
|
||||
list := make([]models.{{.ClassName}}, 0)
|
||||
return &list
|
||||
}))
|
||||
r.GET("/:id", actions.ViewAction(new(dto.{{.ClassName}}ById), nil))
|
||||
r.POST("", actions.CreateAction(new(dto.{{.ClassName}}Control)))
|
||||
r.PUT("/:id", actions.UpdateAction(new(dto.{{.ClassName}}Control)))
|
||||
r.DELETE("", actions.DeleteAction(new(dto.{{.ClassName}}ById)))
|
||||
}
|
||||
}
|
||||
151
template/v4/dto.go.template
Normal file
151
template/v4/dto.go.template
Normal file
@ -0,0 +1,151 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
{{- $bb := false -}}
|
||||
{{- range .Columns -}}
|
||||
{{- $z := .IsQuery -}}
|
||||
{{- if ($z) -}}
|
||||
{{- if eq .GoType "time.Time" -}}{{- $bb = true -}}{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- range .Columns -}}
|
||||
{{- if eq .GoField "CreatedAt" -}}
|
||||
{{- else if eq .GoField "UpdatedAt" -}}
|
||||
{{- else if eq .GoField "DeletedAt" -}}
|
||||
{{- else -}}
|
||||
{{- if eq .GoType "time.Time" -}}{{- $bb = true -}}{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if eq $bb true }}
|
||||
"time"
|
||||
{{- end }}
|
||||
|
||||
"go-admin/app/{{.PackageName}}/models"
|
||||
"go-admin/common/dto"
|
||||
common "go-admin/common/models"
|
||||
)
|
||||
|
||||
type {{.ClassName}}GetPageReq struct {
|
||||
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}}" comment:"{{.ColumnComment}}"`
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{.ClassName}}Order
|
||||
}
|
||||
|
||||
type {{.ClassName}}Order struct {
|
||||
{{ $tablename := .TBName }}
|
||||
{{- range .Columns -}}
|
||||
{{.GoField}} string `form:"{{.JsonField}}Order" search:"type:order;column:{{.ColumnName}};table:{{$tablename}}"`
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
func (m *{{.ClassName}}GetPageReq) GetNeedSearch() interface{} {
|
||||
return *m
|
||||
}
|
||||
|
||||
type {{.ClassName}}InsertReq struct {
|
||||
{{- range .Columns -}}
|
||||
{{$x := .Pk}}
|
||||
{{- if ($x) }}
|
||||
{{.GoField}} {{.GoType}} `json:"-" comment:"{{.ColumnComment}}"` // {{.ColumnComment}}
|
||||
{{- else if eq .GoField "CreatedAt" -}}
|
||||
{{- else if eq .GoField "UpdatedAt" -}}
|
||||
{{- else if eq .GoField "DeletedAt" -}}
|
||||
{{- else if eq .GoField "CreateBy" -}}
|
||||
{{- else if eq .GoField "UpdateBy" -}}
|
||||
{{- else }}
|
||||
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"`
|
||||
{{- end -}}
|
||||
{{- end }}
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
func (s *{{.ClassName}}InsertReq) Generate(model *models.{{.ClassName}}) {
|
||||
{{- range .Columns -}}
|
||||
{{$x := .Pk}}
|
||||
{{- if ($x) }}
|
||||
if s.{{.GoField}} == 0 {
|
||||
model.Model = common.Model{ {{.GoField}}: s.{{.GoField}} }
|
||||
}
|
||||
{{- else if eq .GoField "CreatedAt" -}}
|
||||
{{- else if eq .GoField "UpdatedAt" -}}
|
||||
{{- else if eq .GoField "DeletedAt" -}}
|
||||
{{- else if eq .GoField "CreateBy"}}
|
||||
model.{{.GoField}} = s.{{.GoField}} // 添加这而,需要记录是被谁创建的
|
||||
{{- else if eq .GoField "UpdateBy" -}}
|
||||
{{- else }}
|
||||
model.{{.GoField}} = s.{{.GoField}}
|
||||
{{- end -}}
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
func (s *{{.ClassName}}InsertReq) GetId() interface{} {
|
||||
return s.{{.PkGoField}}
|
||||
}
|
||||
|
||||
type {{.ClassName}}UpdateReq struct {
|
||||
{{- range .Columns -}}
|
||||
{{$x := .Pk}}
|
||||
{{- if ($x) }}
|
||||
{{.GoField}} {{.GoType}} `uri:"{{.JsonField}}" comment:"{{.ColumnComment}}"` // {{.ColumnComment}}
|
||||
{{- else if eq .GoField "CreatedAt" -}}
|
||||
{{- else if eq .GoField "UpdatedAt" -}}
|
||||
{{- else if eq .GoField "DeletedAt" -}}
|
||||
{{- else if eq .GoField "CreateBy" -}}
|
||||
{{- else if eq .GoField "UpdateBy" -}}
|
||||
{{- else }}
|
||||
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"`
|
||||
{{- end -}}
|
||||
{{- end }}
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
func (s *{{.ClassName}}UpdateReq) Generate(model *models.{{.ClassName}}) {
|
||||
{{- range .Columns -}}
|
||||
{{$x := .Pk}}
|
||||
{{- if ($x) }}
|
||||
if s.{{.GoField}} == 0 {
|
||||
model.Model = common.Model{ {{.GoField}}: s.{{.GoField}} }
|
||||
}
|
||||
{{- else if eq .GoField "CreatedAt" -}}
|
||||
{{- else if eq .GoField "UpdatedAt" -}}
|
||||
{{- else if eq .GoField "DeletedAt" -}}
|
||||
{{- else if eq .GoField "CreateBy" -}}
|
||||
{{- else if eq .GoField "UpdateBy"}}
|
||||
model.{{.GoField}} = s.{{.GoField}} // 添加这而,需要记录是被谁更新的
|
||||
{{- else }}
|
||||
model.{{.GoField}} = s.{{.GoField}}
|
||||
{{- end -}}
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
func (s *{{.ClassName}}UpdateReq) GetId() interface{} {
|
||||
return s.{{.PkGoField}}
|
||||
}
|
||||
|
||||
// {{.ClassName}}GetReq 功能获取请求参数
|
||||
type {{.ClassName}}GetReq struct {
|
||||
{{- range .Columns -}}
|
||||
{{$x := .Pk}}
|
||||
{{- if ($x) }}
|
||||
{{.GoField}} {{.GoType}} `uri:"{{.JsonField}}"`
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
}
|
||||
func (s *{{.ClassName}}GetReq) GetId() interface{} {
|
||||
return s.{{.PkGoField}}
|
||||
}
|
||||
|
||||
// {{.ClassName}}DeleteReq 功能删除请求参数
|
||||
type {{.ClassName}}DeleteReq struct {
|
||||
Ids []int `json:"ids"`
|
||||
}
|
||||
|
||||
func (s *{{.ClassName}}DeleteReq) GetId() interface{} {
|
||||
return s.Ids
|
||||
}
|
||||
47
template/v4/js.go.template
Normal file
47
template/v4/js.go.template
Normal file
@ -0,0 +1,47 @@
|
||||
import request from '@/utils/request'
|
||||
|
||||
// 查询{{.ClassName}}列表
|
||||
export function list{{.ClassName}}(query) {
|
||||
return request({
|
||||
url: '/api/v1/{{.ModuleName}}',
|
||||
method: 'get',
|
||||
params: query
|
||||
})
|
||||
}
|
||||
|
||||
// 查询{{.ClassName}}详细
|
||||
export function get{{.ClassName}} ({{.PkJsonField}}) {
|
||||
return request({
|
||||
url: '/api/v1/{{.ModuleName}}/' + {{.PkJsonField}},
|
||||
method: 'get'
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// 新增{{.ClassName}}
|
||||
export function add{{.ClassName}}(data) {
|
||||
return request({
|
||||
url: '/api/v1/{{.ModuleName}}',
|
||||
method: 'post',
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
// 修改{{.ClassName}}
|
||||
export function update{{.ClassName}}(data) {
|
||||
return request({
|
||||
url: '/api/v1/{{.ModuleName}}/'+data.{{.PkJsonField}},
|
||||
method: 'put',
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
// 删除{{.ClassName}}
|
||||
export function del{{.ClassName}}(data) {
|
||||
return request({
|
||||
url: '/api/v1/{{.ModuleName}}',
|
||||
method: 'delete',
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
55
template/v4/model.go.template
Normal file
55
template/v4/model.go.template
Normal file
@ -0,0 +1,55 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
{{- $bb := false -}}
|
||||
{{- range .Columns -}}
|
||||
{{- $z := .IsQuery -}}
|
||||
{{- if ($z) -}}
|
||||
{{- if eq .GoType "time.Time" -}}{{- $bb = true -}}{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- range .Columns -}}
|
||||
{{- if eq .GoField "CreatedAt" -}}
|
||||
{{- else if eq .GoField "UpdatedAt" -}}
|
||||
{{- else if eq .GoField "DeletedAt" -}}
|
||||
{{- else -}}
|
||||
{{- if eq .GoType "time.Time" -}}{{- $bb = true -}}{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if eq $bb true }}
|
||||
"time"
|
||||
{{- end }}
|
||||
|
||||
"go-admin/common/models"
|
||||
|
||||
)
|
||||
|
||||
type {{.ClassName}} struct {
|
||||
models.Model
|
||||
{{ range .Columns -}}
|
||||
{{$x := .Pk}}
|
||||
{{- if ($x) }}
|
||||
{{- else if eq .GoField "CreatedAt" -}}
|
||||
{{- else if eq .GoField "UpdatedAt" -}}
|
||||
{{- else if eq .GoField "DeletedAt" -}}
|
||||
{{- else if eq .GoField "CreateBy" -}}
|
||||
{{- else if eq .GoField "UpdateBy" -}}
|
||||
{{- else }}
|
||||
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};comment:{{- if eq .ColumnComment "" -}}{{.GoField}}{{- else -}}{{.ColumnComment}}{{end -}}"` {{end -}}
|
||||
{{- end }}
|
||||
models.ModelTime
|
||||
models.ControlBy
|
||||
}
|
||||
|
||||
func ({{.ClassName}}) TableName() string {
|
||||
return "{{.TBName}}"
|
||||
}
|
||||
|
||||
func (e *{{.ClassName}}) Generate() models.ActiveRecord {
|
||||
o := *e
|
||||
return &o
|
||||
}
|
||||
|
||||
func (e *{{.ClassName}}) GetId() interface{} {
|
||||
return e.{{.PkGoField}}
|
||||
}
|
||||
198
template/v4/no_actions/apis.go.template
Normal file
198
template/v4/no_actions/apis.go.template
Normal file
@ -0,0 +1,198 @@
|
||||
package apis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/api"
|
||||
"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
|
||||
_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
|
||||
|
||||
"go-admin/app/{{.PackageName}}/models"
|
||||
"go-admin/app/{{.PackageName}}/service"
|
||||
"go-admin/app/{{.PackageName}}/service/dto"
|
||||
"go-admin/common/actions"
|
||||
)
|
||||
|
||||
type {{.ClassName}} struct {
|
||||
api.Api
|
||||
}
|
||||
|
||||
// GetPage 获取{{.TableComment}}列表
|
||||
// @Summary 获取{{.TableComment}}列表
|
||||
// @Description 获取{{.TableComment}}列表
|
||||
// @Tags {{.TableComment}}
|
||||
{{- $tablename := .TBName -}}
|
||||
{{- range .Columns -}}
|
||||
{{$z := .IsQuery}}
|
||||
{{- if ($z) }}
|
||||
// @Param {{.JsonField}} query {{.GoType}} false "{{.ColumnComment}}"
|
||||
{{- end -}}
|
||||
{{- end }}
|
||||
// @Param pageSize query int false "页条数"
|
||||
// @Param pageIndex query int false "页码"
|
||||
// @Success 200 {object} response.Response{data=response.Page{list=[]models.{{.ClassName}}}} "{"code": 200, "data": [...]}"
|
||||
// @Router /api/v1/{{.ModuleName}} [get]
|
||||
// @Security Bearer
|
||||
func (e {{.ClassName}}) GetPage(c *gin.Context) {
|
||||
req := dto.{{.ClassName}}GetPageReq{}
|
||||
s := service.{{.ClassName}}{}
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(&req).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
list := make([]models.{{.ClassName}}, 0)
|
||||
var count int64
|
||||
|
||||
err = s.GetPage(&req, p, &list, &count)
|
||||
if err != nil {
|
||||
e.Error(500, err, fmt.Sprintf("获取{{.TableComment}}失败,\r\n失败信息 %s", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
|
||||
}
|
||||
|
||||
// Get 获取{{.TableComment}}
|
||||
// @Summary 获取{{.TableComment}}
|
||||
// @Description 获取{{.TableComment}}
|
||||
// @Tags {{.TableComment}}
|
||||
// @Param id path int false "id"
|
||||
// @Success 200 {object} response.Response{data=models.{{.ClassName}}} "{"code": 200, "data": [...]}"
|
||||
// @Router /api/v1/{{.ModuleName}}/{id} [get]
|
||||
// @Security Bearer
|
||||
func (e {{.ClassName}}) Get(c *gin.Context) {
|
||||
req := dto.{{.ClassName}}GetReq{}
|
||||
s := service.{{.ClassName}}{}
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(&req).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
var object models.{{.ClassName}}
|
||||
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
err = s.Get(&req, p, &object)
|
||||
if err != nil {
|
||||
e.Error(500, err, fmt.Sprintf("获取{{.TableComment}}失败,\r\n失败信息 %s", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
e.OK( object, "查询成功")
|
||||
}
|
||||
|
||||
// Insert 创建{{.TableComment}}
|
||||
// @Summary 创建{{.TableComment}}
|
||||
// @Description 创建{{.TableComment}}
|
||||
// @Tags {{.TableComment}}
|
||||
// @Accept application/json
|
||||
// @Product application/json
|
||||
// @Param data body dto.{{.ClassName}}InsertReq true "data"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}"
|
||||
// @Router /api/v1/{{.ModuleName}} [post]
|
||||
// @Security Bearer
|
||||
func (e {{.ClassName}}) Insert(c *gin.Context) {
|
||||
req := dto.{{.ClassName}}InsertReq{}
|
||||
s := service.{{.ClassName}}{}
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(&req).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
// 设置创建人
|
||||
req.SetCreateBy(user.GetUserId(c))
|
||||
|
||||
err = s.Insert(&req)
|
||||
if err != nil {
|
||||
e.Error(500, err, fmt.Sprintf("创建{{.TableComment}}失败,\r\n失败信息 %s", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
e.OK(req.GetId(), "创建成功")
|
||||
}
|
||||
|
||||
// Update 修改{{.TableComment}}
|
||||
// @Summary 修改{{.TableComment}}
|
||||
// @Description 修改{{.TableComment}}
|
||||
// @Tags {{.TableComment}}
|
||||
// @Accept application/json
|
||||
// @Product application/json
|
||||
// @Param id path int true "id"
|
||||
// @Param data body dto.{{.ClassName}}UpdateReq true "body"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}"
|
||||
// @Router /api/v1/{{.ModuleName}}/{id} [put]
|
||||
// @Security Bearer
|
||||
func (e {{.ClassName}}) Update(c *gin.Context) {
|
||||
req := dto.{{.ClassName}}UpdateReq{}
|
||||
s := service.{{.ClassName}}{}
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(&req).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
req.SetUpdateBy(user.GetUserId(c))
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
|
||||
err = s.Update(&req, p)
|
||||
if err != nil {
|
||||
e.Error(500, err, fmt.Sprintf("修改{{.TableComment}}失败,\r\n失败信息 %s", err.Error()))
|
||||
return
|
||||
}
|
||||
e.OK( req.GetId(), "修改成功")
|
||||
}
|
||||
|
||||
// Delete 删除{{.TableComment}}
|
||||
// @Summary 删除{{.TableComment}}
|
||||
// @Description 删除{{.TableComment}}
|
||||
// @Tags {{.TableComment}}
|
||||
// @Param data body dto.{{.ClassName}}DeleteReq true "body"
|
||||
// @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}"
|
||||
// @Router /api/v1/{{.ModuleName}} [delete]
|
||||
// @Security Bearer
|
||||
func (e {{.ClassName}}) Delete(c *gin.Context) {
|
||||
s := service.{{.ClassName}}{}
|
||||
req := dto.{{.ClassName}}DeleteReq{}
|
||||
err := e.MakeContext(c).
|
||||
MakeOrm().
|
||||
Bind(&req).
|
||||
MakeService(&s.Service).
|
||||
Errors
|
||||
if err != nil {
|
||||
e.Logger.Error(err)
|
||||
e.Error(500, err, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// req.SetUpdateBy(user.GetUserId(c))
|
||||
p := actions.GetPermissionFromContext(c)
|
||||
|
||||
err = s.Remove(&req, p)
|
||||
if err != nil {
|
||||
e.Error(500, err, fmt.Sprintf("删除{{.TableComment}}失败,\r\n失败信息 %s", err.Error()))
|
||||
return
|
||||
}
|
||||
e.OK( req.GetId(), "删除成功")
|
||||
}
|
||||
27
template/v4/no_actions/router_check_role.go.template
Normal file
27
template/v4/no_actions/router_check_role.go.template
Normal file
@ -0,0 +1,27 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
||||
|
||||
"go-admin/app/{{.PackageName}}/apis"
|
||||
"go-admin/common/middleware"
|
||||
"go-admin/common/actions"
|
||||
)
|
||||
|
||||
func init() {
|
||||
routerCheckRole = append(routerCheckRole, register{{.ClassName}}Router)
|
||||
}
|
||||
|
||||
// register{{.ClassName}}Router
|
||||
func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
||||
api := apis.{{.ClassName}}{}
|
||||
r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
|
||||
{
|
||||
r.GET("", actions.PermissionAction(), api.GetPage)
|
||||
r.GET("/:id", actions.PermissionAction(), api.Get)
|
||||
r.POST("", api.Insert)
|
||||
r.PUT("/:id", actions.PermissionAction(), api.Update)
|
||||
r.DELETE("", api.Delete)
|
||||
}
|
||||
}
|
||||
25
template/v4/no_actions/router_no_check_role.go.template
Normal file
25
template/v4/no_actions/router_no_check_role.go.template
Normal file
@ -0,0 +1,25 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
|
||||
|
||||
"go-admin/app/{{.PackageName}}/apis"
|
||||
)
|
||||
|
||||
func init() {
|
||||
routerCheckRole = append(routerCheckRole, register{{.ClassName}}Router)
|
||||
}
|
||||
|
||||
// register{{.ClassName}}Router
|
||||
func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
||||
api := apis.{{.ClassName}}{}
|
||||
r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc())
|
||||
{
|
||||
r.GET("", api.GetPage)
|
||||
r.GET("/:id", api.Get)
|
||||
r.POST("", api.Insert)
|
||||
r.PUT("/:id", api.Update)
|
||||
r.DELETE("", api.Delete)
|
||||
}
|
||||
}
|
||||
109
template/v4/no_actions/service.go.template
Normal file
109
template/v4/no_actions/service.go.template
Normal file
@ -0,0 +1,109 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/go-admin-team/go-admin-core/sdk/service"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-admin/app/{{.PackageName}}/models"
|
||||
"go-admin/app/{{.PackageName}}/service/dto"
|
||||
"go-admin/common/actions"
|
||||
cDto "go-admin/common/dto"
|
||||
)
|
||||
|
||||
type {{.ClassName}} struct {
|
||||
service.Service
|
||||
}
|
||||
|
||||
// GetPage 获取{{.ClassName}}列表
|
||||
func (e *{{.ClassName}}) GetPage(c *dto.{{.ClassName}}GetPageReq, p *actions.DataPermission, list *[]models.{{.ClassName}}, count *int64) error {
|
||||
var err error
|
||||
var data models.{{.ClassName}}
|
||||
|
||||
err = e.Orm.Model(&data).
|
||||
Scopes(
|
||||
cDto.MakeCondition(c.GetNeedSearch()),
|
||||
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
|
||||
actions.Permission(data.TableName(), p),
|
||||
).
|
||||
Find(list).Limit(-1).Offset(-1).
|
||||
Count(count).Error
|
||||
if err != nil {
|
||||
e.Log.Errorf("{{.ClassName}}Service GetPage error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get 获取{{.ClassName}}对象
|
||||
func (e *{{.ClassName}}) Get(d *dto.{{.ClassName}}GetReq, p *actions.DataPermission, model *models.{{.ClassName}}) error {
|
||||
var data models.{{.ClassName}}
|
||||
|
||||
err := e.Orm.Model(&data).
|
||||
Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).
|
||||
First(model, d.GetId()).Error
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
err = errors.New("查看对象不存在或无权查看")
|
||||
e.Log.Errorf("Service Get{{.ClassName}} error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
e.Log.Errorf("db error:%s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Insert 创建{{.ClassName}}对象
|
||||
func (e *{{.ClassName}}) Insert(c *dto.{{.ClassName}}InsertReq) error {
|
||||
var err error
|
||||
var data models.{{.ClassName}}
|
||||
c.Generate(&data)
|
||||
err = e.Orm.Create(&data).Error
|
||||
if err != nil {
|
||||
e.Log.Errorf("{{.ClassName}}Service Insert error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update 修改{{.ClassName}}对象
|
||||
func (e *{{.ClassName}}) Update(c *dto.{{.ClassName}}UpdateReq, p *actions.DataPermission) error {
|
||||
var err error
|
||||
var data = models.{{.ClassName}}{}
|
||||
e.Orm.Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).First(&data, c.GetId())
|
||||
c.Generate(&data)
|
||||
|
||||
db := e.Orm.Save(&data)
|
||||
if err = db.Error; err != nil {
|
||||
e.Log.Errorf("{{.ClassName}}Service Save error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
if db.RowsAffected == 0 {
|
||||
return errors.New("无权更新该数据")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove 删除{{.ClassName}}
|
||||
func (e *{{.ClassName}}) Remove(d *dto.{{.ClassName}}DeleteReq, p *actions.DataPermission) error {
|
||||
var data models.{{.ClassName}}
|
||||
|
||||
db := e.Orm.Model(&data).
|
||||
Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).Delete(&data, d.GetId())
|
||||
if err := db.Error; err != nil {
|
||||
e.Log.Errorf("Service Remove{{.ClassName}} error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
if db.RowsAffected == 0 {
|
||||
return errors.New("无权删除该数据")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
485
template/v4/vue.go.template
Normal file
485
template/v4/vue.go.template
Normal file
@ -0,0 +1,485 @@
|
||||
{{$tableComment:=.TableComment}}
|
||||
<template>
|
||||
<BasicLayout>
|
||||
<template #wrapper>
|
||||
<el-card class="box-card">
|
||||
<el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
|
||||
{{range .Columns}}
|
||||
{{- $x := .IsQuery -}}
|
||||
{{- if (eq $x "1") -}}
|
||||
<el-form-item label="{{.ColumnComment}}" prop="{{.JsonField}}">
|
||||
{{- if ne .FkTableName "" -}}
|
||||
<el-select v-model="queryParams.{{.JsonField}}"
|
||||
placeholder="请选择" clearable size="small" {{if eq .IsEdit "false" -}} :disabled="isEdit" {{- end }}>
|
||||
<el-option
|
||||
v-for="dict in {{.JsonField}}Options"
|
||||
:key="dict.key"
|
||||
:label="dict.value"
|
||||
:value="dict.key"
|
||||
/>
|
||||
</el-select>
|
||||
{{- else -}}
|
||||
{{if eq .DictType "" -}}
|
||||
<el-input v-model="queryParams.{{.JsonField}}" placeholder="请输入{{.ColumnComment}}" clearable
|
||||
size="small" @keyup.enter.native="handleQuery"/>
|
||||
{{- else -}}
|
||||
<el-select v-model="queryParams.{{.JsonField}}"
|
||||
placeholder="{{$tableComment}}{{.ColumnComment}}" clearable size="small">
|
||||
<el-option
|
||||
v-for="dict in {{.JsonField}}Options"
|
||||
:key="dict.value"
|
||||
:label="dict.label"
|
||||
:value="dict.value"
|
||||
/>
|
||||
</el-select>
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
</el-form-item>
|
||||
{{end}}
|
||||
{{- end }}
|
||||
<el-form-item>
|
||||
<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
|
||||
<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
|
||||
<el-row :gutter="10" class="mb8">
|
||||
<el-col :span="1.5">
|
||||
<el-button
|
||||
v-permisaction="['{{.PackageName}}:{{.BusinessName}}:add']"
|
||||
type="primary"
|
||||
icon="el-icon-plus"
|
||||
size="mini"
|
||||
@click="handleAdd"
|
||||
>新增
|
||||
</el-button>
|
||||
</el-col>
|
||||
<el-col :span="1.5">
|
||||
<el-button
|
||||
v-permisaction="['{{.PackageName}}:{{.BusinessName}}:edit']"
|
||||
type="success"
|
||||
icon="el-icon-edit"
|
||||
size="mini"
|
||||
:disabled="single"
|
||||
@click="handleUpdate"
|
||||
>修改
|
||||
</el-button>
|
||||
</el-col>
|
||||
<el-col :span="1.5">
|
||||
<el-button
|
||||
v-permisaction="['{{.PackageName}}:{{.BusinessName}}:remove']"
|
||||
type="danger"
|
||||
icon="el-icon-delete"
|
||||
size="mini"
|
||||
:disabled="multiple"
|
||||
@click="handleDelete"
|
||||
>删除
|
||||
</el-button>
|
||||
</el-col>
|
||||
</el-row>
|
||||
|
||||
<el-table v-loading="loading" :data="{{.BusinessName}}List" @selection-change="handleSelectionChange">
|
||||
<el-table-column type="selection" width="55" align="center"/>
|
||||
{{- range .Columns -}}
|
||||
{{- $x := .IsList -}}
|
||||
{{- if (eq $x "1") }}
|
||||
{{- if ne .FkTableName "" -}}
|
||||
<el-table-column label="{{.ColumnComment}}" align="center" prop="{{.JsonField}}" :formatter="{{.JsonField}}Format" width="100">
|
||||
<template slot-scope="scope">
|
||||
{{ "{{" }} {{.JsonField}}Format(scope.row) {{"}}"}}
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
{{- else -}}
|
||||
{{- if ne .DictType "" -}}
|
||||
<el-table-column label="{{.ColumnComment}}" align="center" prop="{{.JsonField}}"
|
||||
:formatter="{{.JsonField}}Format" width="100">
|
||||
<template slot-scope="scope">
|
||||
{{ "{{" }} {{.JsonField}}Format(scope.row) {{"}}"}}
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
{{- end -}}
|
||||
{{- if eq .DictType "" -}}
|
||||
{{- if eq .HtmlType "datetime" -}}
|
||||
<el-table-column label="{{.ColumnComment}}" align="center" prop="{{.JsonField}}"
|
||||
:show-overflow-tooltip="true">
|
||||
<template slot-scope="scope">
|
||||
<span>{{ "{{" }} parseTime(scope.row.{{.JsonField}}) {{"}}"}}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
{{- else -}}
|
||||
<el-table-column label="{{.ColumnComment}}" align="center" prop="{{.JsonField}}"
|
||||
:show-overflow-tooltip="true"/>
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
|
||||
<template slot-scope="scope">
|
||||
<el-popconfirm
|
||||
class="delete-popconfirm"
|
||||
title="确认要修改吗?"
|
||||
confirm-button-text="修改"
|
||||
@confirm="handleUpdate(scope.row)"
|
||||
>
|
||||
<el-button
|
||||
slot="reference"
|
||||
v-permisaction="['{{.PackageName}}:{{.BusinessName}}:edit']"
|
||||
size="mini"
|
||||
type="text"
|
||||
icon="el-icon-edit"
|
||||
>修改
|
||||
</el-button>
|
||||
</el-popconfirm>
|
||||
<el-popconfirm
|
||||
class="delete-popconfirm"
|
||||
title="确认要删除吗?"
|
||||
confirm-button-text="删除"
|
||||
@confirm="handleDelete(scope.row)"
|
||||
>
|
||||
<el-button
|
||||
slot="reference"
|
||||
v-permisaction="['{{.PackageName}}:{{.BusinessName}}:remove']"
|
||||
size="mini"
|
||||
type="text"
|
||||
icon="el-icon-delete"
|
||||
>删除
|
||||
</el-button>
|
||||
</el-popconfirm>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
|
||||
<pagination
|
||||
v-show="total>0"
|
||||
:total="total"
|
||||
:page.sync="queryParams.pageIndex"
|
||||
:limit.sync="queryParams.pageSize"
|
||||
@pagination="getList"
|
||||
/>
|
||||
|
||||
<!-- 添加或修改对话框 -->
|
||||
<el-dialog :title="title" :visible.sync="open" width="500px">
|
||||
<el-form ref="form" :model="form" :rules="rules" label-width="80px">
|
||||
{{ range .Columns }}
|
||||
{{- $x := .IsInsert -}}
|
||||
{{- if (eq $x "1") -}}
|
||||
{{- if (.Pk) }}
|
||||
{{- else if eq .GoField "CreatedAt" -}}
|
||||
{{- else if eq .GoField "UpdatedAt" -}}
|
||||
{{- else if eq .GoField "DeletedAt" -}}
|
||||
{{- else if eq .GoField "UpdateBy" -}}
|
||||
{{- else if eq .GoField "CreateBy" -}}
|
||||
{{- else }}
|
||||
<el-form-item label="{{.ColumnComment}}" prop="{{.JsonField}}">
|
||||
{{ if eq "input" .HtmlType -}}
|
||||
<el-input v-model="form.{{.JsonField}}" placeholder="{{.ColumnComment}}"
|
||||
{{if eq .IsEdit "false" -}}:disabled="isEdit" {{- end}}/>
|
||||
{{- else if eq "select" .HtmlType -}}
|
||||
{{- if ne .FkTableName "" -}}
|
||||
<el-select v-model="form.{{.JsonField}}"
|
||||
placeholder="请选择" {{if eq .IsEdit "false" -}} :disabled="isEdit" {{- end }}>
|
||||
<el-option
|
||||
v-for="dict in {{.JsonField}}Options"
|
||||
:key="dict.key"
|
||||
:label="dict.value"
|
||||
:value="dict.key"
|
||||
/>
|
||||
</el-select>
|
||||
{{- else -}}
|
||||
<el-select v-model="form.{{.JsonField}}"
|
||||
placeholder="请选择" {{if eq .IsEdit "false" -}} :disabled="isEdit" {{- end }}>
|
||||
<el-option
|
||||
v-for="dict in {{.JsonField}}Options"
|
||||
:key="dict.value"
|
||||
:label="dict.label"
|
||||
:value="dict.value"
|
||||
/>
|
||||
</el-select>
|
||||
{{- end -}}
|
||||
{{- else if eq "radio" .HtmlType -}}
|
||||
<el-radio-group v-model="form.{{.JsonField}}">
|
||||
<el-radio
|
||||
v-for="dict in {{.JsonField}}Options"
|
||||
:key="dict.value"
|
||||
:label="dict.value"
|
||||
>{{"{{"}} dict.label {{"}}"}}</el-radio>
|
||||
</el-radio-group>
|
||||
{{- else if eq "file" .HtmlType -}}
|
||||
<el-input
|
||||
v-model="form.{{.JsonField}}"
|
||||
placeholder="图片"
|
||||
/>
|
||||
<el-button type="primary" @click="fileShow{{.GoField}}">选择文件</el-button>
|
||||
{{- else if eq "datetime" .HtmlType -}}
|
||||
<el-date-picker
|
||||
v-model="form.{{.JsonField}}"
|
||||
type="datetime"
|
||||
placeholder="选择日期">
|
||||
</el-date-picker>
|
||||
{{- else if eq "textarea" .HtmlType -}}
|
||||
<el-input
|
||||
v-model="form.{{.JsonField}}"
|
||||
type="textarea"
|
||||
:rows="2"
|
||||
placeholder="请输入内容">
|
||||
</el-input>
|
||||
{{- end }}
|
||||
</el-form-item>
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
</el-form>
|
||||
<div slot="footer" class="dialog-footer">
|
||||
<el-button type="primary" @click="submitForm">确 定</el-button>
|
||||
<el-button @click="cancel">取 消</el-button>
|
||||
</div>
|
||||
</el-dialog>
|
||||
</el-card>
|
||||
</template>
|
||||
</BasicLayout>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {add{{.ClassName}}, del{{.ClassName}}, get{{.ClassName}}, list{{.ClassName}}, update{{.ClassName}}} from '@/api/{{ .PackageName}}/{{ .MLTBName}}'
|
||||
{{ $package:=.PackageName }}
|
||||
{{range .Columns}}
|
||||
{{- if ne .FkTableName "" -}}
|
||||
import {list{{.FkTableNameClass}} } from '@/api/{{ $package }}/{{ .FkTableNamePackage}}'
|
||||
{{ end -}}
|
||||
{{- end -}}
|
||||
|
||||
export default {
|
||||
name: '{{.ClassName}}',
|
||||
components: {
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
// 遮罩层
|
||||
loading: true,
|
||||
// 选中数组
|
||||
ids: [],
|
||||
// 非单个禁用
|
||||
single: true,
|
||||
// 非多个禁用
|
||||
multiple: true,
|
||||
// 总条数
|
||||
total: 0,
|
||||
// 弹出层标题
|
||||
title: '',
|
||||
// 是否显示弹出层
|
||||
open: false,
|
||||
isEdit: false,
|
||||
// 类型数据字典
|
||||
typeOptions: [],
|
||||
{{.BusinessName}}List: [],
|
||||
{{range .Columns}}
|
||||
{{- if ne .DictType "" -}}
|
||||
{{.JsonField}}Options: [],
|
||||
{{- end -}}
|
||||
{{- end }}
|
||||
// 关系表类型
|
||||
{{range .Columns}}
|
||||
{{- if ne .FkTableName "" -}}
|
||||
{{.JsonField}}Options :[],
|
||||
{{ end -}}
|
||||
{{- end }}
|
||||
// 查询参数
|
||||
queryParams: {
|
||||
pageIndex: 1,
|
||||
pageSize: 10,
|
||||
{{ range .Columns }}
|
||||
{{- if (.IsQuery) -}}
|
||||
{{.JsonField}}:undefined,
|
||||
{{ end -}}
|
||||
{{- end }}
|
||||
},
|
||||
// 表单参数
|
||||
form: {
|
||||
},
|
||||
// 表单校验
|
||||
rules: {
|
||||
{{- range .Columns -}}
|
||||
{{- $x := .IsQuery -}}
|
||||
{{- if (eq $x "1") -}}
|
||||
{{.JsonField}}: [ {required: true, message: '{{.ColumnComment}}不能为空', trigger: 'blur'} ],
|
||||
{{ end }}
|
||||
{{- end -}}
|
||||
}
|
||||
}
|
||||
},
|
||||
created() {
|
||||
this.getList()
|
||||
{{range .Columns}}
|
||||
{{- if ne .DictType "" -}}
|
||||
this.getDicts('{{.DictType}}').then(response => {
|
||||
this.{{.JsonField}}Options = response.data
|
||||
})
|
||||
{{ end -}}
|
||||
{{- if ne .FkTableName "" -}}
|
||||
this.get{{.FkTableNameClass}}Items()
|
||||
{{ end -}}
|
||||
{{- end -}}
|
||||
},
|
||||
methods: {
|
||||
/** 查询参数列表 */
|
||||
getList() {
|
||||
this.loading = true
|
||||
list{{.ClassName}}(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
|
||||
this.{{.BusinessName}}List = response.data.list
|
||||
this.total = response.data.count
|
||||
this.loading = false
|
||||
}
|
||||
)
|
||||
},
|
||||
// 取消按钮
|
||||
cancel() {
|
||||
this.open = false
|
||||
this.reset()
|
||||
},
|
||||
// 表单重置
|
||||
reset() {
|
||||
this.form = {
|
||||
{{ range .Columns}}
|
||||
{{- $x := .IsInsert -}}
|
||||
{{- if (eq $x "1") -}}
|
||||
{{- if eq .GoField "CreatedAt" -}}
|
||||
{{- else if eq .GoField "UpdatedAt" -}}
|
||||
{{- else if eq .GoField "DeletedAt" -}}
|
||||
{{- else if eq .GoField "UpdateBy" -}}
|
||||
{{- else if eq .GoField "CreateBy" -}}
|
||||
{{- else }}
|
||||
{{.JsonField}}: undefined,
|
||||
{{- end }}
|
||||
{{- end -}}
|
||||
{{- end }}
|
||||
}
|
||||
this.resetForm('form')
|
||||
},
|
||||
getImgList: function() {
|
||||
this.form[this.fileIndex] = this.$refs['fileChoose'].resultList[0].fullUrl
|
||||
},
|
||||
fileClose: function() {
|
||||
this.fileOpen = false
|
||||
},
|
||||
{{range .Columns}}
|
||||
{{- if ne .DictType "" -}}
|
||||
{{.JsonField}}Format(row) {
|
||||
return this.selectDictLabel(this.{{.JsonField}}Options, row.{{.JsonField}})
|
||||
},
|
||||
{{ end -}}
|
||||
{{- if ne .FkTableName "" -}}
|
||||
{{.JsonField}}Format(row) {
|
||||
return this.selectItemsLabel(this.{{.JsonField}}Options, row.{{.JsonField}})
|
||||
},
|
||||
{{ end -}}
|
||||
{{- end -}}
|
||||
// 关系
|
||||
{{range .Columns}}
|
||||
{{- if ne .FkTableName "" -}}
|
||||
get{{.FkTableNameClass}}Items() {
|
||||
this.getItems(list{{.FkTableNameClass}}, undefined).then(res => {
|
||||
this.{{.JsonField}}Options = this.setItems(res, '{{.FkLabelId}}', '{{.FkLabelName}}')
|
||||
})
|
||||
},
|
||||
{{ end -}}
|
||||
{{- end -}}
|
||||
// 文件
|
||||
{{range .Columns}}
|
||||
{{- if eq .HtmlType "file" -}}
|
||||
fileShow{{.GoField}}: function() {
|
||||
this.fileOpen = true
|
||||
this.fileIndex = '{{.JsonField}}'
|
||||
},
|
||||
{{ end -}}
|
||||
{{- end -}}
|
||||
/** 搜索按钮操作 */
|
||||
handleQuery() {
|
||||
this.queryParams.pageIndex = 1
|
||||
this.getList()
|
||||
},
|
||||
/** 重置按钮操作 */
|
||||
resetQuery() {
|
||||
this.dateRange = []
|
||||
this.resetForm('queryForm')
|
||||
this.handleQuery()
|
||||
},
|
||||
/** 新增按钮操作 */
|
||||
handleAdd() {
|
||||
this.reset()
|
||||
this.open = true
|
||||
this.title = '添加{{.TableComment}}'
|
||||
this.isEdit = false
|
||||
},
|
||||
// 多选框选中数据
|
||||
handleSelectionChange(selection) {
|
||||
this.ids = selection.map(item => item.{{.PkJsonField}})
|
||||
this.single = selection.length !== 1
|
||||
this.multiple = !selection.length
|
||||
},
|
||||
/** 修改按钮操作 */
|
||||
handleUpdate(row) {
|
||||
this.reset()
|
||||
const {{.PkJsonField}} =
|
||||
row.{{.PkJsonField}} || this.ids
|
||||
get{{.ClassName}}({{.PkJsonField}}).then(response => {
|
||||
this.form = response.data
|
||||
this.open = true
|
||||
this.title = '修改{{.TableComment}}'
|
||||
this.isEdit = true
|
||||
})
|
||||
},
|
||||
/** 提交按钮 */
|
||||
submitForm: function () {
|
||||
this.$refs['form'].validate(valid => {
|
||||
if (valid) {
|
||||
if (this.form.{{.PkJsonField}} !== undefined) {
|
||||
update{{.ClassName}}(this.form).then(response => {
|
||||
if (response.code === 200) {
|
||||
this.msgSuccess(response.msg)
|
||||
this.open = false
|
||||
this.getList()
|
||||
} else {
|
||||
this.msgError(response.msg)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
add{{.ClassName}}(this.form).then(response => {
|
||||
if (response.code === 200) {
|
||||
this.msgSuccess(response.msg)
|
||||
this.open = false
|
||||
this.getList()
|
||||
} else {
|
||||
this.msgError(response.msg)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
/** 删除按钮操作 */
|
||||
handleDelete(row) {
|
||||
var Ids = (row.{{.PkJsonField}} && [row.{{.PkJsonField}}]) || this.ids
|
||||
|
||||
this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', '警告', {
|
||||
confirmButtonText: '确定',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
}).then(function () {
|
||||
return del{{.ClassName}}( { 'ids': Ids })
|
||||
}).then((response) => {
|
||||
if (response.code === 200) {
|
||||
this.msgSuccess(response.msg)
|
||||
this.open = false
|
||||
this.getList()
|
||||
} else {
|
||||
this.msgError(response.msg)
|
||||
}
|
||||
}).catch(function () {
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user