From b45895605de24177464678b1dc1313366be7a180 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Sat, 17 Jun 2023 18:16:08 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E7=94=A8=E4=B8=8A=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E7=9A=84timezone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/cp_m/anniversary.go | 13 ++++++++----- route/cp_r/anniversary.go | 17 +++++++++++++++-- route/cp_r/space.go | 17 ++++++++++++----- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/domain/model/cp_m/anniversary.go b/domain/model/cp_m/anniversary.go index a85da08..002f958 100644 --- a/domain/model/cp_m/anniversary.go +++ b/domain/model/cp_m/anniversary.go @@ -140,7 +140,7 @@ func GetNeedRemindCpAnniversary(model *domain.Model) []CpAnniversary { } now := time.Now().Unix() for i, v := range rows { - ts := CalcNextAnniversary(v.Timestamp) + ts := CalcNextAnniversary(v.Timestamp, time.Local) if now > ts { res = append(res, rows[i]) } @@ -149,7 +149,7 @@ func GetNeedRemindCpAnniversary(model *domain.Model) []CpAnniversary { } // 获取cp当天需要提醒的纪念日 -func GetUserTodayCpAnniversary(model *domain.Model, cpId mysql.ID) []CpAnniversary { +func GetUserTodayCpAnniversary(model *domain.Model, cpId mysql.ID, tz *time.Location) []CpAnniversary { var rows, res []CpAnniversary if err := model.DB().Model(CpAnniversary{}). Where("`timestamp` > 0"). @@ -161,7 +161,7 @@ func GetUserTodayCpAnniversary(model *domain.Model, cpId mysql.ID) []CpAnniversa } now := time.Now().Unix() for i, v := range rows { - ts := CalcNextAnniversary(v.Timestamp) + ts := CalcNextAnniversary(v.Timestamp, tz) if now > ts { res = append(res, rows[i]) } @@ -174,11 +174,14 @@ func UpdateCpAnniversaryReminded(model *domain.Model, id mysql.ID) error { } // 计算下一个纪念日 -func CalcNextAnniversary(timestamp int64) int64 { +func CalcNextAnniversary(timestamp int64, tz *time.Location) int64 { + if tz == nil { + tz = time.Local + } now := time.Now() birthday := time.Unix(timestamp, 0) // 计算今年的生日日期 - thisYearBirthday := time.Date(now.Year(), birthday.Month(), birthday.Day(), 0, 0, 0, 0, time.Local) + thisYearBirthday := time.Date(now.Year(), birthday.Month(), birthday.Day(), 0, 0, 0, 0, tz) // 如果今年的生日还未到,则生日日期为今年的生日日期;否则为明年的生日日期 var next time.Time diff --git a/route/cp_r/anniversary.go b/route/cp_r/anniversary.go index f0c23c8..2ad3e99 100644 --- a/route/cp_r/anniversary.go +++ b/route/cp_r/anniversary.go @@ -1,6 +1,7 @@ package cp_r import ( + "git.hilo.cn/hilo-common/_const/enum/timezone_e" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mycontext" "github.com/gin-gonic/gin" @@ -13,6 +14,7 @@ import ( "hilo-user/req" "hilo-user/resp" "strconv" + "time" ) type PostPutAnniversaryReq struct { @@ -92,8 +94,19 @@ func PageAnniversary(c *gin.Context) (*mycontext.MyContext, error) { if err != nil { return myCtx, err } - var response = make([]cp_cv.CvCpAnniversary, 0) model := domain.CreateModelContext(myCtx) + loc, err := time.LoadLocation(c.GetHeader(mycontext.TIMEZONE)) + if err != nil { + user, err := user_m.GetUser(model, userId) + if err != nil { + return myCtx, err + } + if user.Language == "ar" { + loc = timezone_e.KSATimezoneLoc + } + } + + var response = make([]cp_cv.CvCpAnniversary, 0) cpRelation, exits := cp_m.GetCpRelation(model, userId) if exits { userIds := []uint64{cpRelation.UserId1, cpRelation.UserId2} @@ -122,7 +135,7 @@ func PageAnniversary(c *gin.Context) (*mycontext.MyContext, error) { for _, v := range anniversary { timestamp := v.Timestamp if v.Type == cp_e.AnniversaryItemTypeAnniversary && timestamp > 0 { - timestamp = cp_m.CalcNextAnniversary(timestamp) + timestamp = cp_m.CalcNextAnniversary(timestamp, loc) } // 客户端只认识0 1 Type := v.Type diff --git a/route/cp_r/space.go b/route/cp_r/space.go index ac0ed4a..1be7cc6 100644 --- a/route/cp_r/space.go +++ b/route/cp_r/space.go @@ -1,6 +1,7 @@ package cp_r import ( + "git.hilo.cn/hilo-common/_const/enum/timezone_e" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/resource/mysql" @@ -30,15 +31,21 @@ func CpSpace(c *gin.Context) (*mycontext.MyContext, error) { if err != nil { return myContext, err } - externalId := c.Query("externalId") - if len(externalId) <= 0 { - return myContext, bizerr.InvalidParameter - } var model = domain.CreateModelContext(myContext) userInfo, err := user_m.GetUser(model, userId) if err != nil { return myContext, err } + loc, err := time.LoadLocation(c.GetHeader(mycontext.TIMEZONE)) + if err != nil { + if userInfo.Language == "ar" { + loc = timezone_e.KSATimezoneLoc + } + } + externalId := c.Query("externalId") + if len(externalId) <= 0 { + return myContext, bizerr.InvalidParameter + } targetUserInfo, err := user_m.GetUserByExtId(model, externalId) if err != nil { return myContext, err @@ -130,7 +137,7 @@ func CpSpace(c *gin.Context) (*mycontext.MyContext, error) { } // 需要提醒的纪念日 if exists && (userId == cpRelation.UserId1 || userId == cpRelation.UserId2) { - anniversary := cp_m.GetUserTodayCpAnniversary(model, cpRelation.Id) + anniversary := cp_m.GetUserTodayCpAnniversary(model, cpRelation.Id, loc) for _, a := range anniversary { response.CpAnniversary = append(response.CpAnniversary, cp_cv.CvCpAnniversary{ Id: a.ID, -- 2.22.0