product_interior.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/utils"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // GetProductInteriorUrl 处理产品内测中的连接并做跳转处理
  13. func GetProductInteriorUrl(url string, urlMap map[string]string) (itemResp *models.ProductInteriorUrlResp) {
  14. //2:文章详情 https://web.hzinsights.com/material/info/8436
  15. //3:活动详情 https://web.hzinsights.com/activity/detail/2701
  16. //4:产业详情 https://web.hzinsights.com/indepth/info/20/79
  17. item := new(models.ProductInteriorUrlResp)
  18. item.Body = url
  19. if urlMap[url] == "" {
  20. item.Type = 1
  21. } else {
  22. urlSlice := strings.Split(url, "/")
  23. lenurlSlice := len(urlSlice)
  24. sourceId, _ := strconv.Atoi(urlSlice[lenurlSlice-1])
  25. item.SourceId = sourceId
  26. if strings.Contains(url, "material/info") {
  27. item.Type = 2
  28. } else if strings.Contains(url, "activity/detail") {
  29. item.Type = 3
  30. } else if strings.Contains(url, "indepth/info") {
  31. if lenurlSlice >= 2 {
  32. chartPermissionId, _ := strconv.Atoi(urlSlice[lenurlSlice-2])
  33. item.ChartPermissionId = chartPermissionId
  34. }
  35. item.Type = 4
  36. }
  37. }
  38. itemResp = item
  39. return
  40. }
  41. // GetProductInteriorUrl 处理产品内测中的连接并做跳转处理
  42. func GetProductInteriorUrlBody(body string, user *models.WxUserItem) (itemResp []*models.ProductInteriorUrlResp, err error) {
  43. //2:文章详情 https://web.hzinsights.com/material/info/8436
  44. //3:活动详情 https://web.hzinsights.com/activity/detail/2701
  45. //4:产业详情 https://web.hzinsights.com/indepth/info/20/79
  46. // 用户权限
  47. authInfo, permissionArr, e := GetUserRaiPermissionInfo(user.UserId, user.CompanyId)
  48. if e != nil {
  49. err = errors.New("GetUserRaiPermissionInfo, Err: " + e.Error())
  50. return
  51. }
  52. material := "material/info" // 2:文章详情
  53. activity := "activity/detail" //3:活动详情
  54. indepth := "indepth/info" //4:产业详情
  55. ctivityvideo := "ctivity/video" //5:活动音频
  56. var randStrStart = "start_cygx_{|}"
  57. var randStr = "start_cygx_{|}_end_cygx"
  58. urlMap := make(map[string]string)
  59. var hrefRegexp = regexp.MustCompile(utils.RegularUrl)
  60. match := hrefRegexp.FindAllString(body, -1)
  61. if match != nil {
  62. for _, v := range match {
  63. //过滤不相干的超链接
  64. if !strings.Contains(v, material) && !strings.Contains(v, activity) && !strings.Contains(v, indepth) && !strings.Contains(v, ctivityvideo) {
  65. continue
  66. }
  67. body = strings.Replace(body, fmt.Sprint("href=\"", v, "\""), "", -1)
  68. body = strings.Replace(body, fmt.Sprint("<a >"), "", -1)
  69. body = strings.Replace(body, fmt.Sprint("</a>"), "", -1)
  70. body = strings.Replace(body, v, randStrStart+v+randStr, -1)
  71. urlMap[v] = v
  72. }
  73. }
  74. sliceBody := strings.Split(body, randStr)
  75. var sliceBodyUrl []string
  76. for _, v := range sliceBody {
  77. sliceUrl := strings.Split(v, randStrStart)
  78. for _, url := range sliceUrl {
  79. if url == "" {
  80. continue
  81. }
  82. sliceBodyUrl = append(sliceBodyUrl, url)
  83. }
  84. }
  85. for _, url := range sliceBodyUrl {
  86. item := new(models.ProductInteriorUrlResp)
  87. item.Body = url
  88. if urlMap[url] == "" {
  89. item.Type = 1
  90. } else {
  91. urlSlice := strings.Split(url, "/")
  92. lenurlSlice := len(urlSlice)
  93. sourceId, _ := strconv.Atoi(urlSlice[lenurlSlice-1])
  94. item.SourceId = sourceId
  95. if strings.Contains(url, material) {
  96. item.Type = 2
  97. } else if strings.Contains(url, activity) {
  98. item.Type = 3
  99. } else if strings.Contains(url, indepth) {
  100. if lenurlSlice >= 2 {
  101. chartPermissionId, _ := strconv.Atoi(urlSlice[lenurlSlice-2])
  102. item.ChartPermissionId = chartPermissionId
  103. }
  104. item.Type = 4
  105. } else if strings.Contains(url, ctivityvideo) {
  106. if lenurlSlice >= 2 {
  107. chartPermissionId, _ := strconv.Atoi(urlSlice[lenurlSlice-2])
  108. item.ChartPermissionId = chartPermissionId
  109. }
  110. item.Type = 5
  111. activityVideo, e := models.GetCygxActivityVideoReqByActivityId(sourceId)
  112. if e != nil {
  113. err = errors.New("GetCygxActivityVideoReqByActivityId, Err: " + e.Error())
  114. return
  115. }
  116. if activityVideo != nil {
  117. item.ActivityVideo = activityVideo
  118. }
  119. activityInfo, e := models.GetAddActivityInfoById(sourceId)
  120. if e != nil {
  121. err = errors.New("GetAddActivityInfoById, Err: " + e.Error())
  122. return
  123. }
  124. // 权限
  125. au := new(models.UserPermissionAuthInfo)
  126. au.SellerName = authInfo.SellerName
  127. au.SellerMobile = authInfo.SellerMobile
  128. au.HasPermission = authInfo.HasPermission
  129. au.OperationMode = authInfo.OperationMode
  130. if au.HasPermission == 1 {
  131. // 非宏观权限进一步判断是否有权限
  132. if activityInfo.ChartPermissionId != utils.HONG_GUAN_ID && !utils.InArrayByStr(permissionArr, activityInfo.ChartPermissionName) {
  133. au.HasPermission = 2
  134. }
  135. }
  136. // 无权限的弹框提示
  137. if au.HasPermission != 1 {
  138. if au.OperationMode == UserPermissionOperationModeCall {
  139. au.PopupMsg = UserPermissionPopupMsgCallMicroVideo
  140. } else {
  141. au.PopupMsg = UserPermissionPopupMsgApplyActivity
  142. }
  143. }
  144. item.AuthInfo = au
  145. }
  146. }
  147. itemResp = append(itemResp, item)
  148. }
  149. return
  150. }
  151. func AddCygxProductInteriorHistory(user *models.WxUserItem, articleId int) (err error) {
  152. cacheKey := fmt.Sprint("ProductInterior:", user.UserId, "articleId:", articleId)
  153. isExist := utils.Rc.IsExist(cacheKey)
  154. if isExist {
  155. return err
  156. }
  157. defer func() {
  158. if err != nil {
  159. go utils.SendAlarmMsg("产品内测用户浏览信息记录失败"+err.Error(), 2)
  160. }
  161. }()
  162. historyRecord := new(models.CygxProductInteriorHistory)
  163. historyRecord.UserId = user.UserId
  164. historyRecord.ProductInteriorId = articleId
  165. historyRecord.CreateTime = time.Now()
  166. historyRecord.Mobile = user.Mobile
  167. historyRecord.Email = user.Email
  168. historyRecord.CompanyId = user.CompanyId
  169. historyRecord.CompanyName = user.CompanyName
  170. sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  171. if err != nil && err.Error() != utils.ErrNoRow() {
  172. return
  173. }
  174. historyRecord.RealName = user.RealName
  175. if sellerItem != nil {
  176. historyRecord.SellerName = sellerItem.RealName
  177. }
  178. _, err = models.AddCygxProductInteriorHistory(historyRecord)
  179. ////10秒之内的阅读不重复记录
  180. setNX := utils.Rc.SetNX(cacheKey, articleId, time.Second*10)
  181. if !setNX {
  182. go utils.SendAlarmMsg("记录用户搜索关键词失败,设置Redis Key 过期时间失败:key"+cacheKey, 3)
  183. }
  184. return
  185. }
  186. // 获取产品内测的阅读数据
  187. func GetCygxProductInteriorHistoryListMap(productInteriorIs []int, user *models.WxUserItem) (mapPv map[int]int) {
  188. var err error
  189. defer func() {
  190. if err != nil {
  191. fmt.Println(err)
  192. go utils.SendAlarmMsg("获取产品内测的阅读数据,信息失败,Err:"+err.Error(), 3)
  193. }
  194. }()
  195. lenproductInteriorIs := len(productInteriorIs)
  196. if lenproductInteriorIs == 0 {
  197. return
  198. }
  199. var condition string
  200. var pars []interface{}
  201. condition = ` AND product_interior_id IN (` + utils.GetOrmInReplace(lenproductInteriorIs) + `) AND user_id = ?`
  202. pars = append(pars, productInteriorIs, user.UserId)
  203. list, err := models.GetCygxProductInteriorHistoryList(condition, pars)
  204. if err != nil {
  205. return
  206. }
  207. mapPv = make(map[int]int, 0)
  208. for _, v := range list {
  209. mapPv[v.ProductInteriorId]++
  210. }
  211. return
  212. }