Browse Source

Merge branch 'fixbug/533-huaguo-checktoken' into debug

jimmyyem 2 days ago
parent
commit
60ddee104f
3 changed files with 29 additions and 10 deletions
  1. 14 8
      crontask/contact_form.go
  2. 7 2
      internal/logic/token/check_token_logic.go
  3. 8 0
      internal/utils/timezone.go

+ 14 - 8
crontask/contact_form.go

@@ -158,15 +158,21 @@ func (l *CronTask) analyze() {
 		return
 	}
 
-	for _, u := range data {
-		if _, ok := contactFieldTemplates[u.BotID]; !ok {
-			c, _ := l.svcCtx.DB.ContactFieldTemplate.Query().Where(contactfieldtemplate.OrganizationID(u.OrganizationID)).First(l.ctx)
-			if c != nil {
-				contactFieldTemplates[u.BotID] = c.Template
-			} else {
-				contactFieldTemplates[u.BotID] = nil
-			}
+	wxs, err := l.svcCtx.DB.Wx.Query().All(l.ctx)
+	if err != nil {
+		return
+	}
+
+	for _, wx := range wxs {
+		c, _ := l.svcCtx.DB.ContactFieldTemplate.Query().Where(contactfieldtemplate.OrganizationID(wx.OrganizationID)).First(l.ctx)
+		if c != nil {
+			contactFieldTemplates[wx.Wxid] = c.Template
+		} else {
+			contactFieldTemplates[wx.Wxid] = nil
 		}
+	}
+
+	for _, u := range data {
 		if contactFieldTemplates[u.BotID] == nil {
 			continue
 		}

+ 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
+}