ziwen 2 years ago
parent
commit
e2135372f3
2 changed files with 25 additions and 1 deletions
  1. 4 0
      models/page_history_record.go
  2. 21 1
      services/page_history_record.go

+ 4 - 0
models/page_history_record.go

@@ -45,3 +45,7 @@ func AddCygxPageHistoryRecord(item *CygxPageHistoryRecord) (lastId int64, err er
 	lastId, err = o.Insert(item)
 	return
 }
+
+type PageRouter struct {
+	PageRouter string
+}

+ 21 - 1
services/page_history_record.go

@@ -1,6 +1,7 @@
 package services
 
 import (
+	"encoding/json"
 	"fmt"
 	"github.com/beego/beego/v2/server/web/context"
 	"hongze/hongze_cygx/models"
@@ -25,8 +26,27 @@ func AddCygxPageHistoryRecord(user *models.WxUserItem, Ctx *context.Context)  {
 	}
 	if Ctx.Input.Method() == "POST" {
 		item.Parameter = string(Ctx.Input.RequestBody)
+		var pr models.PageRouter
+		err := json.Unmarshal(Ctx.Input.RequestBody, &pr)
+		if err != nil {
+			fmt.Println(err)
+			utils.FileLog.Info(err.Error())
+		}
+		item.PageRouter = pr.PageRouter
 	}
-	//fmt.Println("body:", string(Ctx.Input.RequestBody))
+	if strings.Contains(item.Router, "/api/article/detail") {
+		cacheKey := fmt.Sprint("uid:", user.UserId, "_Parameter:", item.Parameter)
+		isExist := utils.Rc.IsExist(cacheKey)
+		if !isExist {
+			setNX := utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*10)
+			if !setNX {
+				go utils.SendAlarmMsg("记录用户阅读文章,设置Redis Key 过期时间失败:key"+cacheKey, 3)
+			}
+			return
+		}
+	}
+
+
 	_, err := models.AddCygxPageHistoryRecord(item)
 	if err != nil {
 		fmt.Println(err)