Browse Source

fix:修改时间判断

jimmyyem 2 days ago
parent
commit
e33e14a62d
2 changed files with 15 additions and 2 deletions
  1. 7 2
      internal/logic/token/check_token_logic.go
  2. 8 0
      internal/utils/timezone.go

+ 7 - 2
internal/logic/token/check_token_logic.go

@@ -10,6 +10,7 @@ import (
 	"wechat-api/ent"
 	"wechat-api/ent/agent"
 	"wechat-api/ent/token"
+	"wechat-api/internal/utils"
 	"wechat-api/internal/utils/dberrorhandler"
 
 	"wechat-api/internal/svc"
@@ -36,7 +37,6 @@ func (l *CheckTokenLogic) CheckToken(req *types.CheckTokenReq) (resp *types.Chec
 		valid bool
 		sign  string
 	)
-	timestamp := time.Now().Unix()
 
 	tokenItem, err := l.svcCtx.DB.Token.Query().Where(token.TokenEQ(*req.Token)).Limit(1).Only(l.ctx)
 	if err != nil && !ent.IsNotFound(err) {
@@ -48,9 +48,14 @@ func (l *CheckTokenLogic) CheckToken(req *types.CheckTokenReq) (resp *types.Chec
 	var customAgentKey string
 	var openaiBase string
 	var openaiKey string
+
+	loc, _ := utils.GetTimezone()
+	expireAt := tokenItem.ExpireAt.In(loc).Unix()
+	timestamp := time.Now().In(loc).Unix()
+
 	if tokenItem == nil { // 判断Token是否存在
 		valid = false
-	} else if tokenItem.ExpireAt.Unix() > timestamp { // 判断Token是否过期
+	} else if expireAt > timestamp { // 判断Token是否过期
 		if tokenItem.MAC == "" { // 判断MAC是否存在,如果不存咋则说明是第一次激活该Token
 			valid = true
 			err = l.svcCtx.DB.Token.UpdateOneID(tokenItem.ID).SetNotNilMAC(req.Mac).Exec(l.ctx)

+ 8 - 0
internal/utils/timezone.go

@@ -0,0 +1,8 @@
+package utils
+
+import "time"
+
+func GetTimezone() (*time.Location, error) {
+	loc, err := time.LoadLocation("Asia/Shanghai")
+	return loc, err
+}