From 75b529d7b0c0a7debe307a0133cae9732b816abb Mon Sep 17 00:00:00 2001 From: wenjianzhang Date: Thu, 11 Mar 2021 14:56:15 +0800 Subject: [PATCH] =?UTF-8?q?ormat=F0=9F=A5=9A=20=EF=BC=9A=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=A8=A1=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/apis/tools/gen.go | 7 ++- go.mod | 2 +- template/v4/dto.go.template | 18 ++---- template/v4/model.go.template | 4 +- template/v4/no_actions/apis.go.template | 57 ++++++++++++++++++- .../no_actions/router_check_role.go.template | 7 ++- .../router_no_check_role.go.template | 7 ++- 7 files changed, 78 insertions(+), 24 deletions(-) diff --git a/app/admin/apis/tools/gen.go b/app/admin/apis/tools/gen.go index 8048429a..93faddcc 100644 --- a/app/admin/apis/tools/gen.go +++ b/app/admin/apis/tools/gen.go @@ -33,6 +33,8 @@ func Preview(c *gin.Context) { pkg.HasError(err, "", -1) t6, err := template.ParseFiles("template/v4/dto.go.template") pkg.HasError(err, "", -1) + t7, err := template.ParseFiles("template/v4/no_actions/service.go.template") + pkg.HasError(err, "", -1) db, err := pkg.GetOrm(c) if err != nil { @@ -54,6 +56,8 @@ func Preview(c *gin.Context) { err = t5.Execute(&b5, tab) var b6 bytes.Buffer err = t6.Execute(&b6, tab) + var b7 bytes.Buffer + err = t7.Execute(&b7, tab) mp := make(map[string]interface{}) mp["template/model.go.template"] = b1.String() @@ -62,6 +66,7 @@ func Preview(c *gin.Context) { mp["template/vue.go.template"] = b4.String() mp["template/router.go.template"] = b5.String() mp["template/dto.go.template"] = b6.String() + mp["template/service.go.template"] = b7.String() var res app.Response res.Data = mp @@ -433,4 +438,4 @@ func GenMenuAndApi(c *gin.Context) { ADelete.MenuId, err = ADelete.Create(db) app.OK(c, "", "数据生成成功!") -} +} \ No newline at end of file diff --git a/go.mod b/go.mod index 3889a296..26f71d31 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/gin-gonic/gin v1.6.3 github.com/go-admin-team/go-admin-core v1.3.0-rc.0.0.20210309094332-92435b42cec0 github.com/go-admin-team/go-admin-core/plugins/logger/zap v0.0.0-20210309094332-92435b42cec0 // indirect - github.com/go-admin-team/go-admin-core/sdk v0.0.0-20210310135550-9ca1efa47cbf + github.com/go-admin-team/go-admin-core/sdk v1.3.0 github.com/google/uuid v1.2.0 github.com/mojocn/base64Captcha v1.3.1 github.com/mssola/user_agent v0.5.2 diff --git a/template/v4/dto.go.template b/template/v4/dto.go.template index 8f51bd39..aff1deb5 100644 --- a/template/v4/dto.go.template +++ b/template/v4/dto.go.template @@ -2,12 +2,11 @@ package dto import ( "github.com/gin-gonic/gin" - //"gorm.io/gorm" "go-admin/app/{{.PackageName}}/models" + common "go-admin/common/models" "go-admin/common/dto" - "go-admin/tools" - "github.com/go-admin-team/go-admin-core/sdk/pkg/logger" + "go-admin/common/apis" ) type {{.ClassName}}Search struct { @@ -17,7 +16,6 @@ type {{.ClassName}}Search struct { {{$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 }} } @@ -27,7 +25,7 @@ func (m *{{.ClassName}}Search) GetNeedSearch() interface{} { } func (m *{{.ClassName}}Search) Bind(ctx *gin.Context) error { - log := logger.GetRequestLogger(ctx) + log := apis.GetRequestLogger(ctx) err := ctx.ShouldBind(m) if err != nil { log.Debugf("ShouldBind error: %s", err.Error()) @@ -45,16 +43,14 @@ type {{.ClassName}}Control struct { {{- else if eq .GoField "DeletedAt" -}} {{- else if eq .GoField "CreateBy" -}} {{- else if eq .GoField "UpdateBy" -}} - {{- else }} - {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"` {{end -}} {{- end }} } func (s *{{.ClassName}}Control) Bind(ctx *gin.Context) error { - log := logger.GetRequestLogger(ctx) + log := apis.GetRequestLogger(ctx) err := ctx.ShouldBindUri(s) if err != nil { log.Debugf("ShouldBindUri error: %s", err.Error()) @@ -77,8 +73,6 @@ func (s *{{.ClassName}}Control) Generate() (*models.{{.ClassName}}, error) { {{- else if eq .GoField "UpdatedAt" -}} {{- else if eq .GoField "DeletedAt" -}} {{- else if eq .GoField "CreateBy" -}} - - {{- else if eq .GoField "UpdateBy" -}} {{- else }} {{.GoField}}: s.{{.GoField}}, @@ -105,7 +99,7 @@ func (s *{{.ClassName}}ById) GetId() interface{} { } func (s *{{.ClassName}}ById) Bind(ctx *gin.Context) error { - log := logger.GetRequestLogger(ctx) + log := apis.GetRequestLogger(ctx) err := ctx.ShouldBindUri(s) if err != nil { log.Debugf("ShouldBindUri error: %s", err.Error()) @@ -120,4 +114,4 @@ func (s *{{.ClassName}}ById) Bind(ctx *gin.Context) error { func (s *{{.ClassName}}ById) SetUpdateBy(id int) { -} +} \ No newline at end of file diff --git a/template/v4/model.go.template b/template/v4/model.go.template index 63ab38f1..1862b033 100644 --- a/template/v4/model.go.template +++ b/template/v4/model.go.template @@ -8,11 +8,9 @@ import ( type {{.ClassName}} struct { models.Model - models.ControlBy {{ range .Columns -}} {{$x := .Pk}} {{- if ($x) }} - {{- else if eq .GoField "CreatedAt" -}} {{- else if eq .GoField "UpdatedAt" -}} {{- else if eq .GoField "DeletedAt" -}} @@ -21,6 +19,8 @@ type {{.ClassName}} struct { {{- 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 { diff --git a/template/v4/no_actions/apis.go.template b/template/v4/no_actions/apis.go.template index 131c02b2..e0ff7c26 100644 --- a/template/v4/no_actions/apis.go.template +++ b/template/v4/no_actions/apis.go.template @@ -5,7 +5,6 @@ import ( "github.com/gin-gonic/gin" "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user" - "github.com/go-admin-team/go-admin-core/sdk/pkg" "go-admin/app/admin/models" "go-admin/app/admin/service" @@ -18,6 +17,22 @@ type {{.ClassName}} struct { apis.Api } +// @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} app.Response "{"code": 200, "data": [...]}" +// @Router /api/v1/{{.ModuleName}} [get] +// @Security Bearer +// Get{{.ClassName}}List 获取{{.TableComment}}列表 func (e *{{.ClassName}}) Get{{.ClassName}}List(c *gin.Context) { log := e.GetLogger(c) db, err := e.GetOrm(c) @@ -53,6 +68,15 @@ func (e *{{.ClassName}}) Get{{.ClassName}}List(c *gin.Context) { e.PageOK(c, list, int(count), d.GetPageIndex(), d.GetPageSize(), "查询成功") } +// @Summary 获取{{.TableComment}} +// @Description 获取{{.TableComment}} +// @Tags {{.TableComment}} +// @Param id path string false "id" +// @Success 200 {string} string "{"code": 200, "data": [...]}" +// @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}" +// @Router /api/v1/{{.ModuleName}} [get] +// @Security Bearer +// Get{{.ClassName}} 获取{{.TableComment}} func (e *{{.ClassName}}) Get{{.ClassName}}(c *gin.Context) { log := e.GetLogger(c) db, err := e.GetOrm(c) @@ -88,6 +112,16 @@ func (e *{{.ClassName}}) Get{{.ClassName}}(c *gin.Context) { e.OK(c, object, "查看成功") } +// @Summary 创建{{.TableComment}} +// @Description 创建{{.TableComment}} +// @Tags {{.TableComment}} +// @Accept application/json +// @Product application/json +// @Param data body dto.{{.ClassName}}Control true "data" +// @Success 200 {string} string "{"code": 200, "message": "添加成功"}" +// @Success 200 {string} string "{"code": -1, "message": "添加失败"}" +// @Router /api/v1/{{.ClassName}} [post] +// Insert{{.ClassName}} 创建{{.TableComment}} func (e *{{.ClassName}}) Insert{{.ClassName}}(c *gin.Context) { log := e.GetLogger(c) db, err := e.GetOrm(c) @@ -127,6 +161,16 @@ func (e *{{.ClassName}}) Insert{{.ClassName}}(c *gin.Context) { e.OK(c, object.GetId(), "创建成功") } +// @Summary 修改{{.TableComment}} +// @Description 修改{{.TableComment}} +// @Tags {{.TableComment}} +// @Accept application/json +// @Product application/json +// @Param data body dto.{{.ClassName}}Control true "body" +// @Success 200 {string} string "{"code": 200, "message": "修改成功"}" +// @Success 200 {string} string "{"code": -1, "message": "修改失败"}" +// @Router /api/v1/{{.ClassName}} [put] +// Update{{.ClassName}} 修改{{.TableComment}} func (e *{{.ClassName}}) Update{{.ClassName}}(c *gin.Context) { log := e.GetLogger(c) db, err := e.GetOrm(c) @@ -167,6 +211,15 @@ func (e *{{.ClassName}}) Update{{.ClassName}}(c *gin.Context) { e.OK(c, object.GetId(), "更新成功") } +// @Summary 删除{{.TableComment}} +// @Description 删除{{.TableComment}} +// @Tags {{.TableComment}} +// @Param id path int false "id" +// @Param ids body string false "ids" +// @Success 200 {string} string "{"code": 200, "message": "删除成功"}" +// @Success 200 {string} string "{"code": -1, "message": "删除失败"}" +// @Router /api/v1/{{.ClassName}}/{id} [delete] +// Delete{{.ClassName}} 删除{{.TableComment}} func (e *{{.ClassName}}) Delete{{.ClassName}}(c *gin.Context) { log := e.GetLogger(c) db, err := e.GetOrm(c) @@ -201,4 +254,4 @@ func (e *{{.ClassName}}) Delete{{.ClassName}}(c *gin.Context) { return } e.OK(c, control.GetId(), "删除成功") -} +} \ No newline at end of file diff --git a/template/v4/no_actions/router_check_role.go.template b/template/v4/no_actions/router_check_role.go.template index 6ac60c61..1ffd2663 100644 --- a/template/v4/no_actions/router_check_role.go.template +++ b/template/v4/no_actions/router_check_role.go.template @@ -2,9 +2,10 @@ package router import ( "github.com/gin-gonic/gin" - "go-admin/app/admin/apis/{{.ModuleName}}" - "go-admin/app/admin/middleware" jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth" + + "go-admin/app/admin/apis/{{.ModuleName}}" + "go-admin/common/middleware" ) func init() { @@ -22,4 +23,4 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJW r.PUT("/:id", api.Update{{.ClassName}}) r.DELETE("", api.Delete{{.ClassName}}) } -} +} \ No newline at end of file diff --git a/template/v4/no_actions/router_no_check_role.go.template b/template/v4/no_actions/router_no_check_role.go.template index 4764e3e6..20e02ec4 100644 --- a/template/v4/no_actions/router_no_check_role.go.template +++ b/template/v4/no_actions/router_no_check_role.go.template @@ -2,8 +2,9 @@ package router import ( "github.com/gin-gonic/gin" - "go-admin/app/admin/apis/{{.ModuleName}}" jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth" + + "go-admin/app/admin/apis/{{.ModuleName}}" ) func init() { @@ -15,10 +16,10 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJW api := &{{.ModuleName}}.{{.ClassName}}{} r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc()) { - r.GET("", api.Get{{.ClassName}}List) + r.GET("", api.Get{{.ClassName}}) r.GET("/:id", api.Get{{.ClassName}}) r.POST("", api.Insert{{.ClassName}}) r.PUT("/:id", api.Update{{.ClassName}}) r.DELETE("", api.Delete{{.ClassName}}) } -} +} \ No newline at end of file