rts-sim-testing-service/dto/common.go

91 lines
1.9 KiB
Go
Raw Permalink Normal View History

package dto
import (
"encoding/json"
"fmt"
"time"
)
2023-07-14 16:28:47 +08:00
type TokenRespDto struct {
Code int `json:"code"`
Token string `json:"token"`
Expire string `json:"expire"`
}
type ErrorDto struct {
Code int `json:"code"`
2023-09-04 11:12:57 +08:00
Tip string `json:"title"`
Message string `json:"detail"`
2023-07-14 16:28:47 +08:00
}
type OrderItem struct {
Column string `form:"column" json:"column"`
Asc bool `form:"asc" json:"asc"`
}
type PageQueryDto struct {
2023-07-14 16:28:47 +08:00
// 页码
Page int `form:"current" json:"page" binding:"required" example:"1"`
2023-07-14 16:28:47 +08:00
// 页面行数
Size int `form:"size" json:"size" binding:"required" example:"10"`
// 排序项
Orders []OrderItem `form:"orders" json:"orders"`
}
type PageDto[T any] struct {
Total int `form:"total" json:"total"`
PageQueryDto
Records []T `form:"records" json:"records"`
}
func (p *PageDto[T]) MarshalJSON() ([]byte, error) {
if p.Records == nil {
p.Records = make([]T, 0)
}
return json.Marshal(*p)
}
type JsonTime time.Time
func (jt *JsonTime) MarshalJSON() ([]byte, error) {
format := time.Time(*jt).Format(time.DateTime)
return json.Marshal(format)
}
func (jt *JsonTime) UnmarshalJSON(data []byte) error {
if data[0] == '"' {
data = data[1 : len(data)-1]
}
str := string(data)
parse, err := time.Parse(time.DateTime, str)
if err != nil {
2023-09-04 11:12:57 +08:00
panic(ErrorDto{Code: ArgumentParseError, Message: fmt.Sprintf("时间参数格式化出错:\n%s", err.Error())})
}
*jt = JsonTime(parse)
return nil
}
func (jt *JsonTime) String() string {
return time.Time(*jt).Format(time.DateTime)
}
// 数据库分页偏移
func (p *PageQueryDto) Offset() int {
if p.Page > 0 {
return (p.Page - 1) * p.Size
}
return 0
}
func (p *PageQueryDto) Default() {
p.Page = 1
p.Size = 10
}
type LoginDto struct {
2023-07-14 16:28:47 +08:00
// 账号
Account string `form:"account" json:"account" binding:"required" example:"17791995809"`
// 加密密码
Password string `form:"password" json:"password" binding:"required" example:"95129dbaace576e46f41a629e6f35d5c"`
}