✨ init
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const layout = "2006-01-02 15:04:05"
|
||||
|
||||
type MyTime time.Time
|
||||
|
||||
func GetNowTime() MyTime {
|
||||
return MyTime(time.Now())
|
||||
}
|
||||
|
||||
func (t *MyTime) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == "null" {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
//前端接收的时间字符串
|
||||
str := string(data)
|
||||
//去除接收的str收尾多余的"
|
||||
timeStr := strings.Trim(str, "\"")
|
||||
t1, err := time.Parse(layout, timeStr)
|
||||
*t = MyTime(t1)
|
||||
return err
|
||||
}
|
||||
|
||||
func (t MyTime) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%v\"", time.Time(t).Format(layout))
|
||||
return []byte(formatted), nil
|
||||
}
|
||||
|
||||
func (t MyTime) Value() (driver.Value, error) {
|
||||
// MyTime 转换成 time.Time 类型
|
||||
tTime := time.Time(t)
|
||||
return tTime.Format(layout), nil
|
||||
}
|
||||
|
||||
func (t *MyTime) Scan(v interface{}) error {
|
||||
switch vt := v.(type) {
|
||||
case time.Time:
|
||||
// 字符串转成 time.Time 类型
|
||||
*t = MyTime(vt)
|
||||
default:
|
||||
return errors.New("类型处理错误")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *MyTime) String() string {
|
||||
return fmt.Sprintf("hhh:%s", time.Time(*t).String())
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/gin-contrib/requestid"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Resp struct {
|
||||
RequestId string `json:"requestId"`
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func NewResp(requestId string, code int, msg string, data interface{}) *Resp {
|
||||
return &Resp{RequestId: requestId, Code: code, Msg: msg, Data: data}
|
||||
}
|
||||
|
||||
// Result 响应
|
||||
func Result(code int, msg string, data interface{}, ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, Resp{
|
||||
RequestId: requestid.Get(ctx),
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// Success 成功返回
|
||||
func Success(data interface{}, ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, Resp{
|
||||
RequestId: requestid.Get(ctx),
|
||||
Code: 200,
|
||||
Msg: "SUCCESS",
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// Fail 失败返回
|
||||
func Fail(code int, msg string, ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, Resp{
|
||||
RequestId: requestid.Get(ctx),
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
Data: nil,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type Token struct {
|
||||
Token string `json:"token"`
|
||||
Expired time.Time `json:"expired"`
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package models
|
||||
|
||||
// UserReq 请求对象
|
||||
type UserReq struct {
|
||||
Username string `bson:"username" json:"username" binding:"required"`
|
||||
Password string `bson:"password" json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
// UserInfo 用户对象
|
||||
type UserInfo struct {
|
||||
ID uint `gorm:"primary_key"`
|
||||
Username string `gorm:"comment:用户名;" bson:"username" json:"username" binding:"required"`
|
||||
Password string `gorm:"comment:密码;" bson:"password" json:"password" binding:"required"`
|
||||
CreateAt MyTime `gorm:"comment:创建时间;type:datetime;" bson:"createAt" json:"createAt"`
|
||||
UpdateAt MyTime `gorm:"comment:更新时间;type:datetime;" bson:"updateAt" json:"updateAt"`
|
||||
}
|
||||
Reference in New Issue
Block a user