product_interior.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package services
  2. import (
  3. "fmt"
  4. "hongze/hongze_cygx/models"
  5. "hongze/hongze_cygx/utils"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // GetProductInteriorUrl 处理产品内测中的连接并做跳转处理
  12. func GetProductInteriorUrl(url string, urlMap map[string]string) (itemResp *models.ProductInteriorUrlResp) {
  13. //2:文章详情 https://web.hzinsights.com/material/info/8436
  14. //3:活动详情 https://web.hzinsights.com/activity/detail/2701
  15. //4:产业详情 https://web.hzinsights.com/indepth/info/20/79
  16. item := new(models.ProductInteriorUrlResp)
  17. item.Body = url
  18. if urlMap[url] == "" {
  19. item.Type = 1
  20. } else {
  21. urlSlice := strings.Split(url, "/")
  22. lenurlSlice := len(urlSlice)
  23. sourceId, _ := strconv.Atoi(urlSlice[lenurlSlice-1])
  24. item.SourceId = sourceId
  25. if strings.Contains(url, "material/info") {
  26. item.Type = 2
  27. } else if strings.Contains(url, "activity/detail") {
  28. item.Type = 3
  29. } else if strings.Contains(url, "indepth/info") {
  30. if lenurlSlice >= 2 {
  31. chartPermissionId, _ := strconv.Atoi(urlSlice[lenurlSlice-2])
  32. item.ChartPermissionId = chartPermissionId
  33. }
  34. item.Type = 4
  35. }
  36. }
  37. itemResp = item
  38. return
  39. }
  40. // GetProductInteriorUrl 处理产品内测中的连接并做跳转处理
  41. func GetProductInteriorUrlBody(body string) (itemResp []*models.ProductInteriorUrlResp) {
  42. //2:文章详情 https://web.hzinsights.com/material/info/8436
  43. //3:活动详情 https://web.hzinsights.com/activity/detail/2701
  44. //4:产业详情 https://web.hzinsights.com/indepth/info/20/79
  45. material := "material/info"
  46. activity := "activity/detail"
  47. indepth := "indepth/info"
  48. var randStrStart = "start_cygx_{|}"
  49. var randStr = "start_cygx_{|}_end_cygx"
  50. urlMap := make(map[string]string)
  51. var hrefRegexp = regexp.MustCompile(utils.RegularUrl)
  52. match := hrefRegexp.FindAllString(body, -1)
  53. if match != nil {
  54. for _, v := range match {
  55. //过滤不相干的超链接
  56. if !strings.Contains(v, material) && !strings.Contains(v, activity) && !strings.Contains(v, indepth) {
  57. continue
  58. }
  59. fmt.Println(v)
  60. body = strings.Replace(body, fmt.Sprint("href=\"", v, "\""), "", -1)
  61. body = strings.Replace(body, fmt.Sprint("<a >"), "", -1)
  62. body = strings.Replace(body, fmt.Sprint("</a>"), "", -1)
  63. body = strings.Replace(body, v, randStrStart+v+randStr, -1)
  64. urlMap[v] = v
  65. }
  66. }
  67. fmt.Println(urlMap)
  68. sliceBody := strings.Split(body, randStr)
  69. var sliceBodyUrl []string
  70. for _, v := range sliceBody {
  71. sliceUrl := strings.Split(v, randStrStart)
  72. for _, url := range sliceUrl {
  73. if url == "" {
  74. continue
  75. }
  76. sliceBodyUrl = append(sliceBodyUrl, url)
  77. }
  78. }
  79. for _, url := range sliceBodyUrl {
  80. item := new(models.ProductInteriorUrlResp)
  81. item.Body = url
  82. if urlMap[url] == "" {
  83. item.Type = 1
  84. } else {
  85. urlSlice := strings.Split(url, "/")
  86. lenurlSlice := len(urlSlice)
  87. sourceId, _ := strconv.Atoi(urlSlice[lenurlSlice-1])
  88. item.SourceId = sourceId
  89. if strings.Contains(url, material) {
  90. item.Type = 2
  91. } else if strings.Contains(url, activity) {
  92. item.Type = 3
  93. } else if strings.Contains(url, indepth) {
  94. if lenurlSlice >= 2 {
  95. chartPermissionId, _ := strconv.Atoi(urlSlice[lenurlSlice-2])
  96. item.ChartPermissionId = chartPermissionId
  97. }
  98. item.Type = 4
  99. }
  100. }
  101. itemResp = append(itemResp, item)
  102. }
  103. return
  104. }
  105. func AddCygxProductInteriorHistory(user *models.WxUserItem, articleId int) (err error) {
  106. cacheKey := fmt.Sprint("ProductInterior:", user.UserId, "articleId:", articleId)
  107. isExist := utils.Rc.IsExist(cacheKey)
  108. if isExist {
  109. return err
  110. }
  111. defer func() {
  112. if err != nil {
  113. go utils.SendAlarmMsg("产品内测用户浏览信息记录失败"+err.Error(), 2)
  114. }
  115. }()
  116. historyRecord := new(models.CygxProductInteriorHistory)
  117. historyRecord.UserId = user.UserId
  118. historyRecord.ProductInteriorId = articleId
  119. historyRecord.CreateTime = time.Now()
  120. historyRecord.Mobile = user.Mobile
  121. historyRecord.Email = user.Email
  122. historyRecord.CompanyId = user.CompanyId
  123. historyRecord.CompanyName = user.CompanyName
  124. sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  125. if err != nil && err.Error() != utils.ErrNoRow() {
  126. return
  127. }
  128. historyRecord.RealName = user.RealName
  129. if sellerItem != nil {
  130. historyRecord.SellerName = sellerItem.RealName
  131. }
  132. _, err = models.AddCygxProductInteriorHistory(historyRecord)
  133. ////10秒之内的阅读不重复记录
  134. setNX := utils.Rc.SetNX(cacheKey, articleId, time.Second*10)
  135. if !setNX {
  136. go utils.SendAlarmMsg("记录用户搜索关键词失败,设置Redis Key 过期时间失败:key"+cacheKey, 3)
  137. }
  138. return
  139. }
  140. // 获取产品内测的阅读数据
  141. func GetCygxProductInteriorHistoryListMap(productInteriorIs []int, user *models.WxUserItem) (mapPv map[int]int) {
  142. var err error
  143. defer func() {
  144. if err != nil {
  145. fmt.Println(err)
  146. go utils.SendAlarmMsg("获取产品内测的阅读数据,信息失败,Err:"+err.Error(), 3)
  147. }
  148. }()
  149. lenproductInteriorIs := len(productInteriorIs)
  150. if lenproductInteriorIs == 0 {
  151. return
  152. }
  153. var condition string
  154. var pars []interface{}
  155. condition = ` AND product_interior_id IN (` + utils.GetOrmInReplace(lenproductInteriorIs) + `) AND user_id = ?`
  156. pars = append(pars, productInteriorIs, user.UserId)
  157. list, err := models.GetCygxProductInteriorHistoryList(condition, pars)
  158. if err != nil {
  159. return
  160. }
  161. mapPv = make(map[int]int, 0)
  162. for _, v := range list {
  163. mapPv[v.ProductInteriorId]++
  164. }
  165. return
  166. }