product_interior.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. defer func() {
  107. if err != nil {
  108. go utils.SendAlarmMsg("产品内测用户浏览信息记录失败"+err.Error(), 2)
  109. }
  110. }()
  111. historyRecord := new(models.CygxProductInteriorHistory)
  112. historyRecord.UserId = user.UserId
  113. historyRecord.ProductInteriorId = articleId
  114. historyRecord.CreateTime = time.Now()
  115. historyRecord.Mobile = user.Mobile
  116. historyRecord.Email = user.Email
  117. historyRecord.CompanyId = user.CompanyId
  118. historyRecord.CompanyName = user.CompanyName
  119. sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  120. if err != nil && err.Error() != utils.ErrNoRow() {
  121. return
  122. }
  123. historyRecord.RealName = user.RealName
  124. if sellerItem != nil {
  125. historyRecord.SellerName = sellerItem.RealName
  126. }
  127. _, err = models.AddCygxProductInteriorHistory(historyRecord)
  128. return
  129. }
  130. // 获取产品内测的阅读数据
  131. func GetCygxProductInteriorHistoryListMap(productInteriorIs []int, user *models.WxUserItem) (mapPv map[int]int) {
  132. var err error
  133. defer func() {
  134. if err != nil {
  135. fmt.Println(err)
  136. go utils.SendAlarmMsg("获取产品内测的阅读数据,信息失败,Err:"+err.Error(), 3)
  137. }
  138. }()
  139. lenproductInteriorIs := len(productInteriorIs)
  140. if lenproductInteriorIs == 0 {
  141. return
  142. }
  143. var condition string
  144. var pars []interface{}
  145. condition = ` AND product_interior_id IN (` + utils.GetOrmInReplace(lenproductInteriorIs) + `) AND user_id = ?`
  146. pars = append(pars, productInteriorIs, user.UserId)
  147. list, err := models.GetCygxProductInteriorHistoryList(condition, pars)
  148. if err != nil {
  149. return
  150. }
  151. mapPv = make(map[int]int, 0)
  152. for _, v := range list {
  153. mapPv[v.ProductInteriorId]++
  154. }
  155. return
  156. }