product_interior.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_clpt/models"
  6. "hongze/hongze_clpt/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) (itemResp []*models.ProductInteriorUrlResp) {
  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. material := "material/info"
  47. activity := "activity/detail"
  48. indepth := "indepth/info"
  49. var randStrStart = "start_cygx_{|}"
  50. var randStr = "start_cygx_{|}_end_cygx"
  51. urlMap := make(map[string]string)
  52. var hrefRegexp = regexp.MustCompile(utils.RegularUrl)
  53. match := hrefRegexp.FindAllString(body, -1)
  54. if match != nil {
  55. for _, v := range match {
  56. //过滤不相干的超链接
  57. if !strings.Contains(v, material) && !strings.Contains(v, activity) && !strings.Contains(v, indepth) {
  58. continue
  59. }
  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. sliceBody := strings.Split(body, randStr)
  68. var sliceBodyUrl []string
  69. for _, v := range sliceBody {
  70. sliceUrl := strings.Split(v, randStrStart)
  71. for _, url := range sliceUrl {
  72. if url == "" {
  73. continue
  74. }
  75. sliceBodyUrl = append(sliceBodyUrl, url)
  76. }
  77. }
  78. for _, url := range sliceBodyUrl {
  79. item := new(models.ProductInteriorUrlResp)
  80. item.Body = url
  81. if urlMap[url] == "" {
  82. item.Type = 1
  83. } else {
  84. urlSlice := strings.Split(url, "/")
  85. lenurlSlice := len(urlSlice)
  86. sourceId, _ := strconv.Atoi(urlSlice[lenurlSlice-1])
  87. item.SourceId = sourceId
  88. if strings.Contains(url, material) {
  89. item.Type = 2
  90. } else if strings.Contains(url, activity) {
  91. item.Type = 3
  92. } else if strings.Contains(url, indepth) {
  93. if lenurlSlice >= 2 {
  94. chartPermissionId, _ := strconv.Atoi(urlSlice[lenurlSlice-2])
  95. item.ChartPermissionId = chartPermissionId
  96. }
  97. item.Type = 4
  98. }
  99. }
  100. itemResp = append(itemResp, item)
  101. }
  102. return
  103. }
  104. func AddCygxProductInteriorHistory(user *models.WxUserItem, articleId int) (err error) {
  105. defer func() {
  106. if err != nil {
  107. go utils.SendAlarmMsg("产品内测用户浏览信息记录失败"+err.Error(), 2)
  108. }
  109. }()
  110. historyRecord := new(models.CygxProductInteriorHistory)
  111. historyRecord.UserId = user.UserId
  112. historyRecord.ProductInteriorId = articleId
  113. historyRecord.CreateTime = time.Now()
  114. historyRecord.Mobile = user.Mobile
  115. historyRecord.Email = user.Email
  116. historyRecord.CompanyId = user.CompanyId
  117. historyRecord.CompanyName = user.CompanyName
  118. historyRecord.RegisterPlatform = utils.REGISTER_PLATFORM
  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 GetCygxProductInteriorHistoryListPvMap(productInteriorIs []int) (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) + `) `
  146. pars = append(pars, productInteriorIs)
  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] = v.Pv
  154. }
  155. return
  156. }
  157. // 用户产品内测阅读,模板消息推送
  158. func ProductInteriorHistoryUserRmind(user *models.WxUserItem, productInteriorId int) (err error) {
  159. //30秒内阅读同一篇报告不做重复推送
  160. key := "CYGX_" + utils.CYGX_OBJ_PRODUCTINTERIOR + "_READ" + strconv.Itoa(productInteriorId) + "_" + strconv.Itoa(user.UserId)
  161. if utils.Rc.IsExist(key) {
  162. return
  163. }
  164. utils.Rc.Put(key, 1, 30*time.Second)
  165. defer func() {
  166. if err != nil {
  167. go utils.SendAlarmMsg(fmt.Sprint("用户产品内测阅读,模板消息推送,模板消息推送失败 ,ProductInteriorHistoryUserRmind"+err.Error(), "userId:", user.UserId, "productInteriorId:", productInteriorId), 2)
  168. }
  169. }()
  170. countUser, err := models.GetUserRemind(user.UserId)
  171. if err != nil {
  172. return err
  173. }
  174. if countUser == 0 {
  175. return err
  176. }
  177. var first string
  178. var keyword1 string
  179. var keyword2 string
  180. var keyword3 string
  181. var keyword4 string
  182. var remark string
  183. //获取销售手机号
  184. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  185. if err != nil && err.Error() != utils.ErrNoRow() {
  186. return err
  187. }
  188. if sellerItemQy != nil {
  189. sllerAndShareMobileArr, e := GetCompanySellerAndShareMobileByRai(user.CompanyId) //获取所属销售以及对应销售的手机号
  190. if e != nil {
  191. err = errors.New("GetCompanySellerAndShareMobileByRai, Err: " + e.Error())
  192. return
  193. }
  194. sllerAndShareMobiles := strings.Join(sllerAndShareMobileArr, ",")
  195. openIdList, e := models.GetWxOpenIdByMobileList(sllerAndShareMobiles)
  196. if e != nil {
  197. err = errors.New("GetSellerByAdminId, Err: " + e.Error())
  198. return
  199. }
  200. if len(openIdList) == 0 {
  201. return
  202. }
  203. detail, e := models.GetCygxProductInteriorDetail(productInteriorId)
  204. if e != nil {
  205. err = errors.New("GetCygxProductInteriorDetail, Err: " + e.Error())
  206. return
  207. }
  208. if detail == nil {
  209. return
  210. }
  211. keyword1 = detail.Title
  212. keyword2 = fmt.Sprint("互动:阅读报告,", user.RealName, "--", user.CompanyName)
  213. openIdArr := make([]string, 0)
  214. for _, v := range openIdList {
  215. openIdArr = append(openIdArr, v.OpenId)
  216. }
  217. redirectUrl := utils.WX_MSG_PATH_PRODUCTINTERIOR_DETAIL + strconv.Itoa(productInteriorId)
  218. sendInfo := new(SendWxTemplate)
  219. sendInfo.First = first
  220. sendInfo.Keyword1 = keyword1
  221. sendInfo.Keyword2 = keyword2
  222. sendInfo.Keyword3 = keyword3
  223. sendInfo.Keyword4 = keyword4
  224. sendInfo.Remark = remark
  225. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  226. sendInfo.RedirectUrl = redirectUrl
  227. sendInfo.RedirectTarget = 3
  228. sendInfo.Resource = strconv.Itoa(productInteriorId)
  229. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  230. sendInfo.OpenIdArr = openIdArr
  231. e = PublicSendTemplateMsg(sendInfo)
  232. if e != nil {
  233. err = errors.New("PublicSendTemplateMsg, Err: " + e.Error())
  234. return
  235. }
  236. }
  237. return
  238. }