product_interior.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_mfyx/models"
  6. "hongze/hongze_mfyx/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] = v.Pv
  210. }
  211. return
  212. }
  213. // 获取产品内测的阅读数据
  214. func GetCygxProductInteriorHistoryListPvMap(productInteriorIs []int) (mapPv map[int]int) {
  215. var err error
  216. defer func() {
  217. if err != nil {
  218. fmt.Println(err)
  219. go utils.SendAlarmMsg("获取产品内测的阅读数据,信息失败,Err:"+err.Error(), 3)
  220. }
  221. }()
  222. lenproductInteriorIs := len(productInteriorIs)
  223. if lenproductInteriorIs == 0 {
  224. return
  225. }
  226. var condition string
  227. var pars []interface{}
  228. condition = ` AND product_interior_id IN (` + utils.GetOrmInReplace(lenproductInteriorIs) + `) `
  229. pars = append(pars, productInteriorIs)
  230. list, err := models.GetCygxProductInteriorHistoryList(condition, pars)
  231. if err != nil {
  232. return
  233. }
  234. mapPv = make(map[int]int, 0)
  235. for _, v := range list {
  236. mapPv[v.ProductInteriorId] = v.Pv
  237. }
  238. return
  239. }
  240. // 用户产品内测阅读,模板消息推送
  241. func ProductInteriorHistoryUserRmind(user *models.WxUserItem, productInteriorId int) (err error) {
  242. //30秒内阅读同一篇报告不做重复推送
  243. key := "CYGX_" + utils.CYGX_OBJ_PRODUCTINTERIOR + "_READ" + strconv.Itoa(productInteriorId) + "_" + strconv.Itoa(user.UserId)
  244. if utils.Rc.IsExist(key) {
  245. return
  246. }
  247. utils.Rc.Put(key, 1, 30*time.Second)
  248. defer func() {
  249. if err != nil {
  250. go utils.SendAlarmMsg(fmt.Sprint("用户产品内测阅读,模板消息推送,模板消息推送失败 ,ProductInteriorHistoryUserRmind"+err.Error(), "userId:", user.UserId, "productInteriorId:", productInteriorId), 2)
  251. }
  252. }()
  253. countUser, err := models.GetUserRemind(user.UserId)
  254. if err != nil {
  255. return err
  256. }
  257. if countUser == 0 {
  258. return err
  259. }
  260. var first string
  261. var keyword1 string
  262. var keyword2 string
  263. var keyword3 string
  264. var keyword4 string
  265. var remark string
  266. //获取销售手机号
  267. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  268. if err != nil && err.Error() != utils.ErrNoRow() {
  269. return err
  270. }
  271. if sellerItemQy != nil {
  272. sllerAndShareMobileArr, e := GetCompanySellerAndShareMobileByRai(user.CompanyId) //获取所属销售以及对应销售的手机号
  273. if e != nil {
  274. err = errors.New("GetCompanySellerAndShareMobileByRai, Err: " + e.Error())
  275. return
  276. }
  277. sllerAndShareMobiles := strings.Join(sllerAndShareMobileArr, ",")
  278. openIdList, e := models.GetWxOpenIdByMobileList(sllerAndShareMobiles)
  279. if e != nil {
  280. err = errors.New("GetSellerByAdminId, Err: " + e.Error())
  281. return
  282. }
  283. if len(openIdList) == 0 {
  284. return
  285. }
  286. detail, e := models.GetCygxProductInteriorDetail(productInteriorId)
  287. if e != nil {
  288. err = errors.New("GetCygxProductInteriorDetail, Err: " + e.Error())
  289. return
  290. }
  291. if detail == nil {
  292. return
  293. }
  294. keyword1 = detail.Title
  295. keyword2 = fmt.Sprint("互动:阅读报告,", user.RealName, "--", user.CompanyName)
  296. openIdArr := make([]string, 0)
  297. for _, v := range openIdList {
  298. openIdArr = append(openIdArr, v.OpenId)
  299. }
  300. redirectUrl := utils.WX_MSG_PATH_PRODUCTINTERIOR_DETAIL + strconv.Itoa(productInteriorId)
  301. sendInfo := new(SendWxTemplate)
  302. sendInfo.First = first
  303. sendInfo.Keyword1 = keyword1
  304. sendInfo.Keyword2 = keyword2
  305. sendInfo.Keyword3 = keyword3
  306. sendInfo.Keyword4 = keyword4
  307. sendInfo.Remark = remark
  308. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  309. sendInfo.RedirectUrl = redirectUrl
  310. sendInfo.RedirectTarget = 3
  311. sendInfo.Resource = strconv.Itoa(productInteriorId)
  312. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  313. sendInfo.OpenIdArr = openIdArr
  314. e = PublicSendTemplateMsg(sendInfo)
  315. if e != nil {
  316. err = errors.New("PublicSendTemplateMsg, Err: " + e.Error())
  317. return
  318. }
  319. }
  320. return
  321. }