wx_template_msg.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "hongze/hongze_clpt/models"
  8. "hongze/hongze_clpt/utils"
  9. "io/ioutil"
  10. "net/http"
  11. "strconv"
  12. "time"
  13. )
  14. // 发送报告提问消息提醒
  15. func SendWxMsgWithAsk(name, askTime, askMsg, title string, openIdList []*models.OpenIdList, articleId int) (err error) {
  16. defer func() {
  17. if err != nil {
  18. go utils.SendAlarmMsg(fmt.Sprint("发送报告提问消息提醒模版消息失败,用户名", name, "文章ID:", articleId), 2)
  19. }
  20. }()
  21. var first string
  22. var keyword1 string
  23. var keyword2 string
  24. var keyword3 string
  25. var keyword4 string
  26. var remark string
  27. first = "研选报告有新的提问,请及时处理"
  28. keyword1 = name
  29. keyword3 = askTime
  30. keyword4 = askMsg
  31. remark = title
  32. openIdArr := make([]string, 0)
  33. for _, v := range openIdList {
  34. openIdArr = append(openIdArr, v.OpenId)
  35. }
  36. redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)
  37. sendInfo := new(SendWxTemplate)
  38. sendInfo.First = first
  39. sendInfo.Keyword1 = keyword1
  40. sendInfo.Keyword2 = keyword2
  41. sendInfo.Keyword3 = keyword3
  42. sendInfo.Keyword4 = keyword4
  43. sendInfo.Remark = remark
  44. sendInfo.TemplateId = utils.WxMsgTemplateIdAskMsgXzs
  45. sendInfo.RedirectUrl = redirectUrl
  46. sendInfo.RedirectTarget = 3
  47. sendInfo.Resource = strconv.Itoa(articleId)
  48. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  49. sendInfo.OpenIdArr = openIdArr
  50. err = PublicSendTemplateMsg(sendInfo)
  51. return
  52. }
  53. func WxSendTemplateMsg(sendUrl string, sendMap map[string]interface{}, items []*models.OpenIdList, resource string, sendType int) (err error) {
  54. for _, v := range items {
  55. sendMap["touser"] = v.OpenId
  56. data, err := json.Marshal(sendMap)
  57. if err != nil {
  58. fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
  59. return err
  60. }
  61. utils.FileLog.Info("One SendData:%s", string(data))
  62. fmt.Println(string(data))
  63. err = SendTemplateMsg(sendUrl, data, resource, sendType, v)
  64. if err != nil {
  65. fmt.Println("send err:", err.Error())
  66. return err
  67. }
  68. }
  69. return
  70. }
  71. func SendTemplateMsg(sendUrl string, data []byte, resource string, sendType int, dataItem *models.OpenIdList) (err error) {
  72. client := http.Client{}
  73. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(data))
  74. if err != nil {
  75. return
  76. }
  77. defer resp.Body.Close()
  78. body, _ := ioutil.ReadAll(resp.Body)
  79. utils.FileLog.Info("SendResult:%s", string(body))
  80. var templateResponse SendTemplateResponse
  81. err = json.Unmarshal(body, &templateResponse)
  82. fmt.Println(templateResponse.Errcode)
  83. fmt.Println(templateResponse.Errmsg)
  84. if err != nil {
  85. return err
  86. }
  87. //新增模板消息推送记录
  88. {
  89. tr := new(models.UserTemplateRecord)
  90. tr.UserId = dataItem.UserId
  91. tr.OpenId = dataItem.OpenId
  92. tr.Resource = resource
  93. tr.SendData = string(data)
  94. tr.Result = string(body)
  95. tr.CreateDate = time.Now().Format(utils.FormatDate)
  96. tr.CreateTime = time.Now().Format(utils.FormatDateTime)
  97. if templateResponse.Errcode == 0 {
  98. tr.SendStatus = 1
  99. } else {
  100. tr.SendStatus = 0
  101. }
  102. tr.SendType = sendType
  103. go func() {
  104. err = models.AddUserTemplateRecord(tr)
  105. if err != nil {
  106. utils.FileLog.Info(fmt.Sprintf("AddUserTemplateRecord Err:%s", err.Error()))
  107. }
  108. }()
  109. }
  110. return
  111. }
  112. // 活动带问提醒
  113. func SendActivityAskApplyTemplateMsgV2(keyword1, keyword2, keyword3, keyword4, remark string, itemOpenid *models.OpenIdList, itemAct *models.ActivityDetail, msgId int) (err error) {
  114. var msg string
  115. defer func() {
  116. if err != nil {
  117. go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  118. go utils.SendAlarmMsg("活动带问提醒发送模版消息失败"+remark, 2)
  119. }
  120. if msg != "" {
  121. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  122. }
  123. }()
  124. activityId := itemAct.ActivityId
  125. var redirectUrl string
  126. redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=2"
  127. openIdArr := make([]string, 0)
  128. openIdArr = append(openIdArr, itemOpenid.OpenId)
  129. sendInfo := new(SendWxTemplate)
  130. sendInfo.Keyword1 = keyword1
  131. sendInfo.Keyword2 = keyword2
  132. sendInfo.Keyword3 = keyword3
  133. sendInfo.Keyword4 = keyword4
  134. sendInfo.Remark = remark
  135. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
  136. sendInfo.RedirectUrl = redirectUrl
  137. sendInfo.RedirectTarget = 3
  138. sendInfo.Resource = strconv.Itoa(activityId)
  139. //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频
  140. if utils.RunMode != "release" {
  141. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_ADD
  142. } else {
  143. redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=2"
  144. }
  145. sendInfo.OpenIdArr = openIdArr
  146. err = PublicSendTemplateMsg(sendInfo)
  147. return
  148. }
  149. // 微路演新增留言提问消息提醒
  150. func SendWxMsgWithMicroRoadshowAsk(req models.AddVideoCommnetReq, user *models.WxUserItem, resourceId, msgId int, title string, isResearch bool) (err error) {
  151. var msg string
  152. defer func() {
  153. if err != nil || msg != "" {
  154. go utils.SendAlarmMsg(fmt.Sprint("发送微路演新增留言提问消息提醒失败,用户名", req.Title, "ID:", req.SourceId), 2)
  155. }
  156. }()
  157. companyItem, e := models.GetSellerDetailAllByCompanyId(user.CompanyId)
  158. if e != nil {
  159. return
  160. }
  161. if companyItem == nil {
  162. msg = "获取客户所属销售信息失败,UserID:" + strconv.Itoa(user.UserId)
  163. }
  164. var first string
  165. var keyword1 string
  166. var keyword2 string
  167. var keyword3 string
  168. var keyword4 string
  169. var remark string
  170. var mobile string
  171. first = req.Title + "有新的留言,请及时处理"
  172. keyword1 = user.CompanyName + "——" + user.RealName + "(" + companyItem.SellerName + ")"
  173. keyword3 = time.Now().Format(utils.FormatDateTime)
  174. keyword2 = title
  175. keyword4 = req.Content
  176. if isResearch {
  177. //研选报告添加汪洋
  178. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic + "," + utils.ActSendMsgMobile
  179. } else {
  180. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic
  181. }
  182. openIdList, e := models.GetWxOpenIdByMobileList(mobile)
  183. if e != nil {
  184. err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error())
  185. return
  186. }
  187. openIdArr := make([]string, 0)
  188. for _, v := range openIdList {
  189. openIdArr = append(openIdArr, v.OpenId)
  190. }
  191. mapSource := make(map[int]string)
  192. mapSource[1] = "5"
  193. mapSource[2] = "4"
  194. mapSource[3] = "3"
  195. if mapSource[resourceId] == "" {
  196. err = errors.New("留言消息类型错误, resourceId: " + strconv.Itoa(resourceId))
  197. return
  198. }
  199. //SourceType int `description:"视频来源: 1-音频; 2-活动视频; 3-微路演视频 (不传默认为1)"`
  200. //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频
  201. redirectUrl := ""
  202. if utils.RunMode != "release" {
  203. if req.SourceType == 3 {
  204. redirectUrl = utils.WX_MSG_PATH_ACTIVITY_INDUSTRYR_EPORT + strconv.Itoa(resourceId)
  205. remark = "点击进入产业资源包详情页"
  206. } else {
  207. redirectUrl = utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(resourceId)
  208. remark = "点击查看活动详情"
  209. }
  210. } else {
  211. redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=" + mapSource[resourceId]
  212. }
  213. sendInfo := new(SendWxTemplate)
  214. sendInfo.First = first
  215. sendInfo.Keyword1 = keyword1
  216. sendInfo.Keyword2 = keyword2
  217. sendInfo.Keyword3 = keyword3
  218. sendInfo.Keyword4 = keyword4
  219. sendInfo.Remark = remark
  220. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
  221. sendInfo.RedirectUrl = redirectUrl
  222. sendInfo.RedirectTarget = 3
  223. sendInfo.Resource = strconv.Itoa(req.SourceId)
  224. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_COMMENT
  225. sendInfo.OpenIdArr = openIdArr
  226. err = PublicSendTemplateMsg(sendInfo)
  227. if err != nil {
  228. return
  229. }
  230. return
  231. }
  232. // 发送用户留言提醒
  233. func SendCommentWxTemplateMsg(req models.AddCygxArticleCommentReq, user *models.WxUserItem, articleInfo *models.ArticleDetail, msgId int) (err error) {
  234. var msg string
  235. defer func() {
  236. if err != nil {
  237. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  238. go utils.SendAlarmMsg(fmt.Sprint("发送用户用户留言提醒失败,文章ID:", req.ArticleId), 2)
  239. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  240. }
  241. }()
  242. companyItem, e := models.GetSellerDetailAllByCompanyId(user.CompanyId)
  243. if e != nil {
  244. return
  245. }
  246. if companyItem == nil {
  247. msg = "获取客户所属销售信息失败,UserID:" + strconv.Itoa(user.UserId)
  248. }
  249. var first string
  250. var keyword1 string
  251. var keyword2 string
  252. var keyword3 string
  253. var keyword4 string
  254. var remark string
  255. var redirectUrl string
  256. var mobile string
  257. first = "【" + articleInfo.Title + "】有新的留言,请及时处理"
  258. keyword1 = user.CompanyName + "——" + user.RealName + "(" + companyItem.SellerName + ")"
  259. keyword2 = "留言:" + articleInfo.Title
  260. keyword3 = time.Now().Format(utils.FormatDateTime)
  261. keyword4 = req.Content
  262. remark = "点击查看报告详情"
  263. if articleInfo.ArticleTypeId > 0 {
  264. //研选报告添加汪洋
  265. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic + "," + utils.ActSendMsgMobile
  266. } else {
  267. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic
  268. }
  269. openIdList, e := models.GetWxOpenIdByMobileList(mobile)
  270. if e != nil {
  271. err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error())
  272. return
  273. }
  274. openIdArr := make([]string, 0)
  275. for _, v := range openIdList {
  276. openIdArr = append(openIdArr, v.OpenId)
  277. }
  278. //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频
  279. redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=1"
  280. sendInfo := new(SendWxTemplate)
  281. sendInfo.First = first
  282. sendInfo.Keyword1 = keyword1
  283. sendInfo.Keyword2 = keyword2
  284. sendInfo.Keyword3 = keyword3
  285. sendInfo.Keyword4 = keyword4
  286. sendInfo.Remark = remark
  287. sendInfo.TemplateId = utils.WxMsgTemplateIdAskMsgXzs
  288. sendInfo.RedirectUrl = redirectUrl
  289. sendInfo.RedirectTarget = 3
  290. sendInfo.Resource = strconv.Itoa(req.ArticleId)
  291. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_COMMENT
  292. sendInfo.OpenIdArr = openIdArr
  293. err = PublicSendTemplateMsg(sendInfo)
  294. if err != nil {
  295. return
  296. }
  297. return
  298. }
  299. // 发送用户操作活动消息提醒
  300. func SendWxMsgWithActivityUserRemind(keyword1, keyword2 string, openIdList []*models.OpenIdList, activityId int) (err error) {
  301. defer func() {
  302. if err != nil {
  303. go utils.SendAlarmMsg(fmt.Sprint("发送用户操作活动消息提醒,文章ID:", activityId), 2)
  304. }
  305. }()
  306. openIdArr := make([]string, 0)
  307. for _, v := range openIdList {
  308. openIdArr = append(openIdArr, v.OpenId)
  309. }
  310. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
  311. sendInfo := new(SendWxTemplate)
  312. sendInfo.Keyword1 = keyword1
  313. sendInfo.Keyword2 = keyword2
  314. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  315. sendInfo.RedirectUrl = redirectUrl
  316. sendInfo.RedirectTarget = 3
  317. sendInfo.Resource = strconv.Itoa(activityId)
  318. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  319. sendInfo.OpenIdArr = openIdArr
  320. err = PublicSendTemplateMsg(sendInfo)
  321. return
  322. }
  323. // 发送用户阅读报告消息提醒
  324. func SendWxMsgWithArticleUserRemind(keyword1, keyword2 string, openIdList []*models.OpenIdList, articleId int) (err error) {
  325. if len(openIdList) == 0 {
  326. return
  327. }
  328. defer func() {
  329. if err != nil {
  330. go utils.SendAlarmMsg(fmt.Sprint("发送报告提问消息提醒模版消息失败,文章ID:", articleId), 2)
  331. }
  332. }()
  333. openIdArr := make([]string, 0)
  334. for _, v := range openIdList {
  335. openIdArr = append(openIdArr, v.OpenId)
  336. }
  337. redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)
  338. sendInfo := new(SendWxTemplate)
  339. sendInfo.Keyword1 = keyword1
  340. sendInfo.Keyword2 = keyword2
  341. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  342. sendInfo.RedirectUrl = redirectUrl
  343. sendInfo.RedirectTarget = 3
  344. sendInfo.Resource = strconv.Itoa(articleId)
  345. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  346. sendInfo.OpenIdArr = openIdArr
  347. err = PublicSendTemplateMsg(sendInfo)
  348. return
  349. }
  350. // SendActivitieCancelSignTemplateMsg 取消活动给销售和内容组四人发模版消息
  351. func SendActivitieCancelSignTemplateMsg(user *models.WxUserItem, activityDetail *models.ActivityDetail) (err error) {
  352. defer func() {
  353. if err != nil {
  354. go utils.SendAlarmMsg(fmt.Sprint("取消活动给销售和内容组四人发模版消息Err:", err.Error(), "活动ID:", activityDetail.ActivityId, "手机号:", user.Mobile), 2)
  355. }
  356. }()
  357. //获取销售手机号
  358. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  359. if err != nil {
  360. return
  361. }
  362. configCode := utils.TPL_MSG_NEI_RONG_ZU
  363. cnf, e := models.GetConfigByCode(configCode)
  364. if e != nil {
  365. err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode)
  366. return
  367. }
  368. openIdList, e := models.GetWxOpenIdByMobileList(cnf.ConfigValue + "," + sellerItemQy.Mobile)
  369. if e != nil && e.Error() != utils.ErrNoRow() {
  370. err = errors.New("GetUserRecordListByMobile, Err: " + e.Error() + cnf.ConfigValue)
  371. return
  372. }
  373. keyword1 := user.RealName + "--" + user.CompanyName + "(" + sellerItemQy.RealName + ")"
  374. keyword2 := user.Mobile
  375. keyword3 := time.Now().Format(utils.FormatDateTime)
  376. keyword4 := "取消报名" + "【" + activityDetail.ActivityName + "】"
  377. openIdArr := make([]string, 0)
  378. for _, v := range openIdList {
  379. openIdArr = append(openIdArr, v.OpenId)
  380. }
  381. sendInfo := new(SendWxTemplate)
  382. sendInfo.Keyword1 = keyword1
  383. sendInfo.Keyword2 = keyword2
  384. sendInfo.Keyword3 = keyword3
  385. sendInfo.Keyword4 = keyword4
  386. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
  387. sendInfo.RedirectTarget = 3
  388. sendInfo.Resource = user.Mobile
  389. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY
  390. sendInfo.OpenIdArr = openIdArr
  391. err = PublicSendTemplateMsg(sendInfo)
  392. return
  393. }
  394. // SendNeiRongZuActivitieSignTemplateMsg 报名活动给销售和内容组四人发模版消息
  395. func SendNeiRongZuActivitieSignTemplateMsg(user *models.WxUserItem, activityDetail *models.ActivityDetail) (err error) {
  396. defer func() {
  397. if err != nil {
  398. go utils.SendAlarmMsg(fmt.Sprint("取消活动给销售和内容组四人发模版消息Err:", err.Error(), "活动ID:", activityDetail.ActivityId, "手机号:", user.Mobile), 2)
  399. }
  400. }()
  401. //获取销售手机号
  402. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  403. if err != nil && err.Error() != utils.ErrNoRow() {
  404. return
  405. }
  406. configCode := utils.TPL_MSG_NEI_RONG_ZU
  407. cnf, e := models.GetConfigByCode(configCode)
  408. if e != nil {
  409. err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode)
  410. return
  411. }
  412. openIdList, e := models.GetWxOpenIdByMobileList(cnf.ConfigValue + "," + sellerItemQy.Mobile)
  413. if e != nil && e.Error() != utils.ErrNoRow() {
  414. err = errors.New("GetUserRecordListByMobile, Err: " + e.Error() + cnf.ConfigValue)
  415. return err
  416. }
  417. keyword1 := user.RealName + "--" + user.CompanyName + "(" + sellerItemQy.RealName + ")"
  418. keyword2 := user.Mobile
  419. keyword3 := time.Now().Format(utils.FormatDateTime)
  420. keyword4 := "报名" + "【" + activityDetail.ActivityName + "】"
  421. openIdArr := make([]string, 0)
  422. for _, v := range openIdList {
  423. openIdArr = append(openIdArr, v.OpenId)
  424. }
  425. sendInfo := new(SendWxTemplate)
  426. sendInfo.Keyword1 = keyword1
  427. sendInfo.Keyword2 = keyword2
  428. sendInfo.Keyword3 = keyword3
  429. sendInfo.Keyword4 = keyword4
  430. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
  431. sendInfo.RedirectTarget = 3
  432. sendInfo.Resource = user.Mobile
  433. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY
  434. sendInfo.OpenIdArr = openIdArr
  435. err = PublicSendTemplateMsg(sendInfo)
  436. return
  437. }