diff --git a/marketing-api/model/dpa/product.go b/marketing-api/model/dpa/product.go index 1187d9d9..66ab70af 100644 --- a/marketing-api/model/dpa/product.go +++ b/marketing-api/model/dpa/product.go @@ -10,10 +10,10 @@ type Product struct { Title string `json:"title,omitempty"` // Description 商品标题 Description string `json:"description,omitempty"` - // OfflineTime 下线时间,格式"YYYY-MM-DD" - OfflineTime string `json:"offline_time,omitempty"` - // OnlineTime 上线时间,格式"YYYY-MM-DD" - OnlineTime string `json:"online_time,omitempty"` + // OfflineTime 下线时间,格式"YYYY-MM-DD" 或 格式为十位unix时间戳类型 + OfflineTime model.UnixTime `json:"offline_time,omitempty"` + // OnlineTime 上线时间,格式"YYYY-MM-DD" 或 格式为十位unix时间戳类型 + OnlineTime model.UnixTime `json:"online_time,omitempty"` // PlatformID 商品库ID PlatformID uint64 `json:"platform_id,omitempty"` // ProductID 商品ID diff --git a/marketing-api/model/dpa/product_for_update.go b/marketing-api/model/dpa/product_for_update.go index 6b652499..909a4e5c 100644 --- a/marketing-api/model/dpa/product_for_update.go +++ b/marketing-api/model/dpa/product_for_update.go @@ -1,7 +1,5 @@ package dpa -import "time" - // ProductForUpdate 商品详情 type ProductForUpdate struct { // Name 商品名称 @@ -87,15 +85,10 @@ type ProductForUpdate struct { } func CopyProductForUpdateFromProduct(src *Product, dist *ProductForUpdate) { - loc := time.Now().Location() dist.Name = src.Name dist.Title = src.Title dist.Description = src.Description - if src.OfflineTime != "" { - if t, err := time.ParseInLocation("2006-01-02", src.OfflineTime, loc); err == nil { - dist.OfflineTime = t.Unix() - } - } + dist.OfflineTime = src.OfflineTime.Value() dist.Status = src.Status dist.Stock = src.Stock dist.FirstCategory = src.FirstCategory diff --git a/marketing-api/model/types.go b/marketing-api/model/types.go index 6b72cef1..7ecb6822 100644 --- a/marketing-api/model/types.go +++ b/marketing-api/model/types.go @@ -1,6 +1,9 @@ package model -import "strconv" +import ( + "strconv" + "time" +) type AdVersion int @@ -263,3 +266,38 @@ func (i ReverseOnOffInt) String() string { } return "ON" } + +var timeFormats = []string{ + "2006-01-02 15:04:05", + "2006-01-02", +} + +// UnixTime support number/string in json +type UnixTime int64 + +func (ut UnixTime) Value() int64 { + return int64(ut) +} + +func (ut UnixTime) Time() time.Time { + return time.Unix(int64(ut), 0) +} + +func (ut *UnixTime) UnmarshalJSON(b []byte) (err error) { + if b[0] == '"' && b[len(b)-1] == '"' { + b = b[1 : len(b)-1] + } + if i, err := strconv.ParseUint(string(b), 10, 64); err == nil { + *ut = UnixTime(i) + } else { + loc := time.Now().Location() + str := string(b) + for _, fmt := range timeFormats { + if t, err := time.ParseInLocation(fmt, str, loc); err == nil { + *ut = UnixTime(t.Unix()) + break + } + } + } + return nil +}