mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-12-24 08:12:55 +08:00
更换为null_time,null_int
This commit is contained in:
122
server/core/null_int.go
Normal file
122
server/core/null_int.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// int类型别名,支持前端传递null,int,string类型
|
||||
// 忽略前端null值,接收""值时,返回0
|
||||
type NullInt struct {
|
||||
Int *int64
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func EncodeInt(value any) any {
|
||||
switch v := value.(type) {
|
||||
case map[string]any:
|
||||
if v["Int"] != nil {
|
||||
return v["Int"]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func DecodeInt(value any) (any, error) {
|
||||
switch v := value.(type) {
|
||||
case int:
|
||||
i := int64(v)
|
||||
return NullInt{Int: &i, Valid: true}, nil
|
||||
case int64:
|
||||
return NullInt{Int: &v, Valid: true}, nil
|
||||
case string:
|
||||
if v == "" {
|
||||
return NullInt{Int: nil, Valid: false}, nil
|
||||
}
|
||||
i, err := strconv.ParseInt(v, 10, 64)
|
||||
return NullInt{Int: &i, Valid: true}, err
|
||||
case nil:
|
||||
return NullInt{Int: nil, Valid: false}, nil
|
||||
case NullInt:
|
||||
return v, nil
|
||||
}
|
||||
return NullInt{Int: nil, Valid: false}, nil
|
||||
}
|
||||
|
||||
// gorm实现Scanner
|
||||
func (i *NullInt) Scan(value interface{}) error {
|
||||
i.Valid = false
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
v := value.(int64)
|
||||
i.Int, i.Valid = &v, true
|
||||
return nil
|
||||
}
|
||||
|
||||
// gorm实现 Valuer
|
||||
func (i NullInt) Value() (driver.Value, error) {
|
||||
if !i.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
v := i.Int
|
||||
if v == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return *v, nil
|
||||
}
|
||||
func (i NullInt) String() string {
|
||||
if i.Valid {
|
||||
return strconv.FormatInt(*i.Int, 10)
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// 实现json序列化接口
|
||||
func (i NullInt) MarshalJSON() ([]byte, error) {
|
||||
if i.Valid {
|
||||
return json.Marshal(i.Int)
|
||||
} else {
|
||||
return json.Marshal(nil)
|
||||
}
|
||||
}
|
||||
|
||||
// 实现json反序列化接口
|
||||
func (i *NullInt) UnmarshalJSON(data []byte) error {
|
||||
var x any
|
||||
if err := json.Unmarshal(data, &x); err != nil {
|
||||
return err
|
||||
}
|
||||
switch v := x.(type) {
|
||||
case int64:
|
||||
i.Int = &v
|
||||
i.Valid = true
|
||||
return nil
|
||||
case float64:
|
||||
i64 := int64(v)
|
||||
i.Int = &i64
|
||||
i.Valid = true
|
||||
return nil
|
||||
case string:
|
||||
if v == "" {
|
||||
i.Int = nil
|
||||
i.Valid = true
|
||||
return nil
|
||||
}
|
||||
num, err := strconv.ParseInt(v, 10, 64)
|
||||
if err == nil {
|
||||
i.Int = &num
|
||||
i.Valid = true
|
||||
} else {
|
||||
i.Valid = false
|
||||
}
|
||||
return err
|
||||
case nil:
|
||||
i.Valid = false
|
||||
default:
|
||||
i.Valid = false
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
117
server/core/null_time.go
Normal file
117
server/core/null_time.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
// const DateFormat = "2006-01-02"
|
||||
// const TimeFormat = "2006-01-02 15:04:05"
|
||||
|
||||
// NullTime 自定义时间格式
|
||||
type NullTime struct {
|
||||
Time *time.Time
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func (t *NullTime) IsZero() bool {
|
||||
return t.Valid
|
||||
}
|
||||
func (t *NullTime) UnmarshalJSON(bs []byte) error {
|
||||
var date string
|
||||
err := json.Unmarshal(bs, &date)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if date == "" {
|
||||
*t = NullTime{
|
||||
Time: nil,
|
||||
Valid: false,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
tt, _ := time.ParseInLocation(TimeFormat, date, time.Local)
|
||||
*t = NullTime{
|
||||
Time: &tt,
|
||||
Valid: true,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON 将NullTime类型的时间转化为JSON字符串格式
|
||||
// 返回转化后的JSON字符串和错误信息
|
||||
func (t NullTime) MarshalJSON() ([]byte, error) {
|
||||
if t.Valid {
|
||||
if t.Time == nil {
|
||||
return json.Marshal(nil)
|
||||
}
|
||||
tt := *t.Time
|
||||
tStr := tt.Format(TimeFormat)
|
||||
return json.Marshal(tStr)
|
||||
} else {
|
||||
return json.Marshal(nil)
|
||||
}
|
||||
}
|
||||
|
||||
// 写入数据库gorm调用
|
||||
func (t NullTime) Value() (driver.Value, error) {
|
||||
timeStr := t.String()
|
||||
if timeStr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
return timeStr, nil
|
||||
}
|
||||
|
||||
// 读取数据gorm调用
|
||||
func (t *NullTime) Scan(v any) error {
|
||||
// pt, err := time.ParseInLocation("2006-01-02 15:04:05", v.(time.Time).String(), time.Local)
|
||||
if pt, ok := v.(time.Time); ok {
|
||||
tt := pt.Format(TimeFormat)
|
||||
if tt == "0001-01-01 00:00:00" {
|
||||
*t = NullTime{
|
||||
Time: &pt,
|
||||
Valid: false,
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
*t = NullTime{
|
||||
Time: &pt,
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("cant convert %s to time", v)
|
||||
}
|
||||
|
||||
func (t NullTime) String() string {
|
||||
if !t.Valid {
|
||||
return ""
|
||||
}
|
||||
if t.Time == nil {
|
||||
return ""
|
||||
}
|
||||
tt := *t.Time
|
||||
return tt.Format(TimeFormat)
|
||||
}
|
||||
|
||||
func (NullTime) GormDBDataType(db *gorm.DB, field *schema.Field) string {
|
||||
// 使用 field.Tag、field.TagSettings 获取字段的 tag
|
||||
// 查看 https://github.com/go-gorm/gorm/blob/master/schema/field.go 获取全部的选项
|
||||
|
||||
// 根据不同的数据库驱动返回不同的数据类型
|
||||
// switch db.Dialector.Name() {
|
||||
// case "mysql", "sqlite":
|
||||
// return "JSON"
|
||||
// case "postgres":
|
||||
// return "JSONB"
|
||||
// }
|
||||
// return ""
|
||||
return "DATETIME"
|
||||
}
|
||||
@@ -1,11 +1,5 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"x_admin/core"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
||||
// PageResp 分页返回值
|
||||
type PageResp struct {
|
||||
Count int64 `json:"count"` // 总数
|
||||
@@ -15,10 +9,10 @@ type PageResp struct {
|
||||
}
|
||||
|
||||
// Copy 拷贝结构体
|
||||
func Copy(toValue interface{}, fromValue interface{}) interface{} {
|
||||
if err := copier.Copy(toValue, fromValue); err != nil {
|
||||
core.Logger.Errorf("Copy err: err=[%+v]", err)
|
||||
panic(SystemError)
|
||||
}
|
||||
return toValue
|
||||
}
|
||||
// func Copy(toValue interface{}, fromValue interface{}) interface{} {
|
||||
// if err := copier.Copy(toValue, fromValue); err != nil {
|
||||
// core.Logger.Errorf("Copy err: err=[%+v]", err)
|
||||
// panic(SystemError)
|
||||
// }
|
||||
// return toValue
|
||||
// }
|
||||
|
||||
@@ -38,7 +38,7 @@ var (
|
||||
LoginAccountError = RespType{code: 330, message: "登录账号或密码错误"}
|
||||
LoginDisableError = RespType{code: 331, message: "登录账号已被禁用了"}
|
||||
TokenEmpty = RespType{code: 332, message: "token参数为空"}
|
||||
TokenInvalid = RespType{code: 333, message: "token参数无效"}
|
||||
TokenInvalid = RespType{code: 333, message: "登录失效"}
|
||||
|
||||
NoPermission = RespType{code: 403, message: "无相关权限"}
|
||||
Request404Error = RespType{code: 404, message: "请求接口不存在"}
|
||||
|
||||
@@ -16,34 +16,19 @@ const TimeFormat = "2006-01-02 15:04:05"
|
||||
// TsTime 自定义时间格式
|
||||
type TsTime time.Time
|
||||
|
||||
// 通过时间字符串生成时间戳
|
||||
func ToUnix(date string) int64 {
|
||||
if date == "" {
|
||||
return 0
|
||||
}
|
||||
tt, _ := time.ParseInLocation(TimeFormat, date, time.Local)
|
||||
return tt.Unix()
|
||||
func (tst *TsTime) IsZero() bool {
|
||||
return tst.String() == "0001-01-01 00:00:00"
|
||||
}
|
||||
func ParseTimeToTsTime(date time.Time) TsTime {
|
||||
return TsTime(date)
|
||||
}
|
||||
func ParseStringToTsTime(date string) (TsTime, error) {
|
||||
t, e := time.Parse(TimeFormat, date)
|
||||
if e != nil {
|
||||
return NowTime(), e
|
||||
}
|
||||
return TsTime(t), nil
|
||||
}
|
||||
func NowTime() TsTime {
|
||||
return TsTime(time.Now())
|
||||
}
|
||||
|
||||
func (tst *TsTime) UnmarshalJSON(bs []byte) error {
|
||||
var date string
|
||||
err := json.Unmarshal(bs, &date)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if date == "" {
|
||||
*tst = TsTime(time.Time{})
|
||||
return nil
|
||||
}
|
||||
tt, _ := time.ParseInLocation(TimeFormat, date, time.Local)
|
||||
*tst = TsTime(tt)
|
||||
return nil
|
||||
@@ -53,7 +38,11 @@ func (tst *TsTime) UnmarshalJSON(bs []byte) error {
|
||||
// 返回转化后的JSON字符串和错误信息
|
||||
func (tst TsTime) MarshalJSON() ([]byte, error) {
|
||||
tt := time.Time(tst).Format(TimeFormat)
|
||||
return json.Marshal(tt)
|
||||
if tt == "0001-01-01 00:00:00" {
|
||||
return json.Marshal(nil)
|
||||
} else {
|
||||
return json.Marshal(tt)
|
||||
}
|
||||
}
|
||||
|
||||
// 写入数据库gorm调用
|
||||
Reference in New Issue
Block a user