This commit is contained in:
parent
59a04bcb53
commit
6acc371b4e
@ -54,14 +54,17 @@ func (c *ItemController) Detail(id uint) mvc.Result {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param type path string true "商品类型"
|
||||
// @Param getListByTypeParam query validate.GetListByTypeParam true "分页信息"
|
||||
// @Param pagingParam query validate.PagingParam true "分页信息"
|
||||
// @Success 200 {object} []model.ItemModel "{["id":1],["id":2]}"
|
||||
// @Failure 400 {object} map[string]interface{} "{"msg": "错误信息","code":0}"
|
||||
// @Router /api/item/list/type/{type} [get]
|
||||
func (c *ItemController) GetListByType(ttype int) mvc.Result {
|
||||
getListByTypeParam := new(validate.GetListByTypeParam)
|
||||
e := validate.ReadJSON(c.Ctx, getListByTypeParam)
|
||||
items, e := c.Service.Item.GetItems(ttype, getListByTypeParam.Page, getListByTypeParam.PageSize)
|
||||
pagingParam := new(validate.PagingParam)
|
||||
e := validate.ReadQuery(c.Ctx, pagingParam)
|
||||
if e.Error() {
|
||||
return e.Response()
|
||||
}
|
||||
items, e := c.Service.Item.GetItems(ttype, pagingParam.Page, pagingParam.PageSize)
|
||||
if e.Error() {
|
||||
return e.Response()
|
||||
}
|
||||
@ -99,7 +102,12 @@ func (c *ItemController) GetTypes() mvc.Result {
|
||||
// @Router /api/item/search [get]
|
||||
func (c *ItemController) Search() mvc.Result {
|
||||
keyword := c.Ctx.URLParam("keyword")
|
||||
items, e := c.Service.Item.Search(keyword)
|
||||
pagineParam := new(validate.PagingParam)
|
||||
e := validate.ReadQuery(c.Ctx, pagineParam)
|
||||
if e.Error() {
|
||||
return e.Response()
|
||||
}
|
||||
items, e := c.Service.Item.Search(keyword, pagingParam.Page, pagingParam.PageSize)
|
||||
if e.Error() {
|
||||
return e.Response()
|
||||
}
|
||||
|
11
docs/docs.go
11
docs/docs.go
@ -326,20 +326,21 @@ const docTemplate = `{
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"minimum": 0,
|
||||
"type": "integer",
|
||||
"default": 1,
|
||||
"example": 1,
|
||||
"description": "@description 页码",
|
||||
"name": "page",
|
||||
"in": "query",
|
||||
"required": true
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"maximum": 50,
|
||||
"type": "integer",
|
||||
"default": 10,
|
||||
"example": 10,
|
||||
"description": "@description 每页数量",
|
||||
"name": "pageSize",
|
||||
"in": "query",
|
||||
"required": true
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
|
@ -320,20 +320,21 @@
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"minimum": 0,
|
||||
"type": "integer",
|
||||
"default": 1,
|
||||
"example": 1,
|
||||
"description": "@description 页码",
|
||||
"name": "page",
|
||||
"in": "query",
|
||||
"required": true
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"maximum": 50,
|
||||
"type": "integer",
|
||||
"default": 10,
|
||||
"example": 10,
|
||||
"description": "@description 每页数量",
|
||||
"name": "pageSize",
|
||||
"in": "query",
|
||||
"required": true
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
|
@ -363,17 +363,18 @@ paths:
|
||||
name: type
|
||||
required: true
|
||||
type: string
|
||||
- description: '@description 页码'
|
||||
- default: 1
|
||||
example: 1
|
||||
in: query
|
||||
minimum: 0
|
||||
name: page
|
||||
required: true
|
||||
type: integer
|
||||
- description: '@description 每页数量'
|
||||
- default: 10
|
||||
description: '@description 每页数量'
|
||||
example: 10
|
||||
in: query
|
||||
maximum: 50
|
||||
name: pageSize
|
||||
required: true
|
||||
type: integer
|
||||
produces:
|
||||
- application/json
|
||||
|
1
go.mod
1
go.mod
@ -26,6 +26,7 @@ require (
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/llgcode/draw2d v0.0.0-20240627062922-0ed1ff131195 // indirect
|
||||
github.com/mcuadros/go-defaults v1.2.0 // indirect
|
||||
github.com/oschwald/maxminddb-golang v1.13.0 // indirect
|
||||
github.com/redis/go-redis/v9 v9.7.0 // indirect
|
||||
golang.org/x/image v0.18.0 // indirect
|
||||
|
3
go.sum
3
go.sum
@ -139,6 +139,8 @@ github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxec
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mcuadros/go-defaults v1.2.0 h1:FODb8WSf0uGaY8elWJAkoLL0Ri6AlZ1bFlenk56oZtc=
|
||||
github.com/mcuadros/go-defaults v1.2.0/go.mod h1:WEZtHEVIGYVDqkKSWBdWKUVdRyKlMfulPaGDWIVeCWY=
|
||||
github.com/mediocregopher/radix/v3 v3.8.1 h1:rOkHflVuulFKlwsLY01/M2cM2tWCjDoETcMqKbAWu1M=
|
||||
github.com/mediocregopher/radix/v3 v3.8.1/go.mod h1:8FL3F6UQRXHXIBSPUs5h0RybMF8i4n7wVopoX3x7Bv8=
|
||||
github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk=
|
||||
@ -270,6 +272,7 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
|
||||
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
|
||||
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
|
@ -51,9 +51,9 @@ func (s *ItemService) GetItems(ttype int, page int, page_size int) (items []mode
|
||||
return
|
||||
}
|
||||
|
||||
func (s *ItemService) Search(keyword string) (items []model.ItemModel, e *ecode.Ecode) {
|
||||
func (s *ItemService) Search(keyword string, page int, page_size int) (items []model.ItemModel, e *ecode.Ecode) {
|
||||
e = ecode.OK()
|
||||
model.DB.Where("itemname like ?", "%"+keyword+"%").Where("state!=?", model.ItemStateDelete).Find(&items)
|
||||
model.DB.Where("state!=?", model.ItemStateDelete).Where("itemname like ?", "%"+keyword+"%").Offset((page - 1) * page_size).Limit(page_size).Find(&items)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,11 @@
|
||||
package validate
|
||||
|
||||
import "github.com/mcuadros/go-defaults"
|
||||
|
||||
type CreateTypeParam struct {
|
||||
Name string `json:"name" validate:"required,max=20"`
|
||||
}
|
||||
|
||||
func (p *CreateTypeParam) Default() {
|
||||
defaults.SetDefaults(p)
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package validate
|
||||
|
||||
import "github.com/mcuadros/go-defaults"
|
||||
|
||||
type CreateItemParam struct {
|
||||
Itemname string `json:"itemname" validate:"required,max=12" example:"女大自用iPhone18"`
|
||||
// @description 商品价格,单位为分
|
||||
@ -15,9 +17,18 @@ type DeleteItemParam struct {
|
||||
Id uint `json:"id" validate:"required" example:"1"`
|
||||
}
|
||||
|
||||
type GetListByTypeParam struct {
|
||||
// @description 页码
|
||||
Page int `json:"page" validate:"required" example:"1"`
|
||||
type PagingParam struct {
|
||||
Page int `json:"page" validate:"min=0" example:"1" default:"1"`
|
||||
// @description 每页数量
|
||||
PageSize int `json:"pageSize" validate:"required" example:"10"`
|
||||
PageSize int `json:"pageSize" validate:"max=50" example:"10" default:"10"`
|
||||
}
|
||||
|
||||
func (p *CreateItemParam) Default() {
|
||||
defaults.SetDefaults(p)
|
||||
}
|
||||
func (p *DeleteItemParam) Default() {
|
||||
defaults.SetDefaults(p)
|
||||
}
|
||||
func (p *PagingParam) Default() {
|
||||
defaults.SetDefaults(p)
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package validate
|
||||
|
||||
import "github.com/mcuadros/go-defaults"
|
||||
|
||||
type LoginParam struct {
|
||||
Username string `json:"username" validate:"required,max=12" example:"wzjian"`
|
||||
Password string `json:"password" validate:"required,min=6" example:"123456"`
|
||||
@ -14,3 +16,10 @@ type SignupParam struct {
|
||||
CaptchaKey string `json:"captcha_key" validate:"required,len=8" example:"devonlyy"`
|
||||
CaptchaData string `json:"captcha_data" validate:"required,len=4" example:"DEV1"`
|
||||
}
|
||||
|
||||
func (p *LoginParam) Default() {
|
||||
defaults.SetDefaults(p)
|
||||
}
|
||||
func (p *SignupParam) Default() {
|
||||
defaults.SetDefaults(p)
|
||||
}
|
||||
|
@ -12,6 +12,10 @@ import (
|
||||
var validate *validator.Validate
|
||||
var trans ut.Translator
|
||||
|
||||
type ParamInterface interface {
|
||||
Default()
|
||||
}
|
||||
|
||||
func InitValidator() {
|
||||
validate = validator.New()
|
||||
eng := zh.New()
|
||||
@ -21,6 +25,7 @@ func InitValidator() {
|
||||
addTranslation("min", "{0} 长度必须至少为 {1} 个字符")
|
||||
addTranslation("max", "{0} 长度不能超过 {1} 个字符")
|
||||
addTranslation("len", "{0} 长度应该为 {1} 个字符")
|
||||
addTranslation("required", "{0} 不能为空")
|
||||
}
|
||||
|
||||
func addTranslation(tag string, tr string) {
|
||||
@ -36,7 +41,7 @@ func GetValidator() *validator.Validate {
|
||||
return validate
|
||||
}
|
||||
|
||||
func ReadJSON(ctx iris.Context, result interface{}) *ecode.Ecode {
|
||||
func ReadJSON(ctx iris.Context, result ParamInterface) *ecode.Ecode {
|
||||
ctx.ReadBody(result)
|
||||
err := GetValidator().Struct(result)
|
||||
if err != nil {
|
||||
@ -48,5 +53,23 @@ func ReadJSON(ctx iris.Context, result interface{}) *ecode.Ecode {
|
||||
}
|
||||
return ecode.Set(255, firstValue)
|
||||
}
|
||||
// 填充默认值
|
||||
result.Default()
|
||||
return ecode.OK()
|
||||
}
|
||||
|
||||
func ReadQuery(ctx iris.Context, result ParamInterface) *ecode.Ecode {
|
||||
ctx.ReadQuery(result)
|
||||
err := GetValidator().Struct(result)
|
||||
if err != nil {
|
||||
validateErr := err.(validator.ValidationErrors)
|
||||
firstValue := ""
|
||||
for _, v := range validateErr.Translate(trans) {
|
||||
firstValue = v
|
||||
break // 退出循环,确保只获取第一个元素
|
||||
}
|
||||
return ecode.Set(255, firstValue)
|
||||
}
|
||||
result.Default()
|
||||
return ecode.OK()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user