page_history_record.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/server/web/context"
  6. "hongze/hongze_mfyx/models"
  7. "hongze/hongze_mfyx/utils"
  8. "strings"
  9. "time"
  10. )
  11. func AddCygxPageHistoryRecord(user *models.WxUserItem, Ctx *context.Context) {
  12. item := new(models.CygxPageHistoryRecord)
  13. item.UserId = user.UserId
  14. item.CreateTime = time.Now()
  15. item.Mobile = user.Mobile
  16. item.Email = user.Email
  17. item.CompanyId = user.CompanyId
  18. item.CompanyName = user.CompanyName
  19. item.Router = Ctx.Request.RequestURI
  20. item.PageRouter = Ctx.Input.Query("PageRouter")
  21. if user.InviteCompany != "" {
  22. item.Router += "&From=" + user.InviteCompany
  23. }
  24. item.RegisterPlatform = utils.REGISTER_PLATFORM
  25. index := strings.Index(item.Router, "?")
  26. if index != -1 {
  27. item.Parameter = item.Router[index+1:]
  28. }
  29. if Ctx.Input.Method() == "POST" && string(Ctx.Input.RequestBody) != "" {
  30. item.Parameter = string(Ctx.Input.RequestBody)
  31. var pr models.PageRouter
  32. err := json.Unmarshal(Ctx.Input.RequestBody, &pr)
  33. if err != nil {
  34. fmt.Println(err)
  35. utils.FileLog.Info(err.Error())
  36. }
  37. item.PageRouter = pr.PageRouter
  38. }
  39. if strings.Contains(item.Router, "/api/article/detail") {
  40. //cacheKey := fmt.Sprint("uid:", user.UserId, "_Parameter:", item.Parameter)
  41. //isExist := utils.Rc.IsExist(cacheKey)
  42. //if !isExist {
  43. // setNX := utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*10)
  44. // if !setNX {
  45. // go utils.SendAlarmMsg("记录用户阅读文章,设置Redis Key 过期时间失败:key"+cacheKey, 3)
  46. // }
  47. // return
  48. //}
  49. sliceParameter := strings.Split(item.Parameter, "&PageRouter=")
  50. cacheKey := fmt.Sprint("uid:", user.UserId, "_Parameter:", sliceParameter[0])
  51. isExist := utils.Rc.IsExist(cacheKey)
  52. if isExist {
  53. return
  54. }
  55. setNX := utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*10)
  56. if !setNX {
  57. if !setNX {
  58. go utils.SendAlarmMsg("记录用户阅读文章,设置Redis Key 过期时间失败:key"+cacheKey, 3)
  59. }
  60. }
  61. }
  62. _, err := models.AddCygxPageHistoryRecord(item)
  63. if err != nil {
  64. fmt.Println(err)
  65. utils.FileLog.Info(err.Error())
  66. return
  67. }
  68. }