wx_template_msg.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "hongze/hongze_cygx/models"
  8. "hongze/hongze_cygx/utils"
  9. "io/ioutil"
  10. "net/http"
  11. "strconv"
  12. "time"
  13. )
  14. // 活动预开始模版消息通知
  15. func SendWxMsgWithFrequency(keyword2, activityName, reserveResults, activityTime, activityAddress, remark string, openIdList []*models.OpenIdList, activityId int) (err error) {
  16. if len(openIdList) == 0 {
  17. return
  18. }
  19. var msg string
  20. defer func() {
  21. if err != nil {
  22. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  23. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  24. }
  25. fmt.Println("line 21", err, msg)
  26. }()
  27. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(activityId)
  28. sendInfo := new(SendWxTemplate)
  29. sendInfo.Keyword1 = activityName
  30. sendInfo.Keyword2 = keyword2
  31. openIdArr := make([]string, 0)
  32. for _, v := range openIdList {
  33. openIdArr = append(openIdArr, v.OpenId)
  34. }
  35. sendInfo.Remark = remark
  36. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  37. sendInfo.RedirectUrl = redirectUrl
  38. sendInfo.RedirectTarget = 3
  39. sendInfo.Resource = strconv.Itoa(activityId)
  40. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_ADD
  41. sendInfo.OpenIdArr = openIdArr
  42. err = PublicSendTemplateMsg(sendInfo)
  43. return
  44. }
  45. // 活动预开始模版消息通知(类目模版消息,暂时单独推送)
  46. func SendWxMsgWithFrequencyBycategory(keyWord1, keyWord2 string, openIdList []*models.OpenIdList, activityId int) (err error) {
  47. if len(openIdList) == 0 {
  48. return
  49. }
  50. var msg string
  51. defer func() {
  52. if err != nil {
  53. go utils.SendAlarmMsg(fmt.Sprint("活动预开始模版消息通知消息失败SendWxMsgWithFrequencyBycategory ,activityId:", activityId, msg+";Err:"+err.Error()), 2)
  54. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  55. }
  56. }()
  57. accessToken, err := models.GetWxAccessTokenByXzs()
  58. if err != nil {
  59. msg = "GetWxAccessToken Err:" + err.Error()
  60. return
  61. }
  62. if accessToken == "" {
  63. msg = "accessToken is empty"
  64. return
  65. }
  66. keyWord1 = utils.TruncateActivityNameString(keyWord1)
  67. //keyword1 := keyWord2
  68. //keyword1 := keyWord2
  69. sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  70. templateId := utils.WxMsgTemplateIdActivityChangeApplyXzs
  71. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(activityId)
  72. sendMap := make(map[string]interface{})
  73. sendData := make(map[string]interface{})
  74. sendMap["template_id"] = templateId
  75. sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId, "pagepath": redirectUrl}
  76. sendData["thing18"] = map[string]interface{}{"value": keyWord1, "color": "#173177"}
  77. sendData["time23"] = map[string]interface{}{"value": keyWord2, "color": "#173177"}
  78. sendMap["data"] = sendData
  79. WxSendTemplateMsg(sendUrl, sendMap, openIdList, strconv.Itoa(activityId), utils.TEMPLATE_MSG_CYGX_ACTIVITY_ADD)
  80. return
  81. }
  82. // 专项调研活动预开始消息通知
  83. // func SendWxMsgWithFrequencySpecial(first, activityName, reserveResults, activityTime, activityAddress, remark string, openIdList []*models.OpenIdList, activityId int) (err error) {
  84. func SendWxMsgWithFrequencySpecial(first, keyword1, keyword2, keyword3, keyword4, remark string, openIdList []*models.OpenIdList, activityId int) (err error) {
  85. if len(openIdList) == 0 {
  86. return
  87. }
  88. var msg string
  89. defer func() {
  90. if err != nil {
  91. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  92. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  93. }
  94. }()
  95. openIdArr := make([]string, 0)
  96. for _, v := range openIdList {
  97. openIdArr = append(openIdArr, v.OpenId)
  98. }
  99. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
  100. sendInfo := new(SendWxTemplate)
  101. sendInfo.First = first
  102. sendInfo.Keyword1 = keyword1
  103. sendInfo.Keyword2 = keyword2
  104. sendInfo.Keyword3 = keyword3
  105. sendInfo.Keyword4 = keyword4
  106. sendInfo.Remark = remark
  107. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityApplyXzs
  108. sendInfo.RedirectUrl = redirectUrl
  109. sendInfo.RedirectTarget = 3
  110. sendInfo.Resource = strconv.Itoa(activityId)
  111. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_ADD
  112. sendInfo.OpenIdArr = openIdArr
  113. err = PublicSendTemplateMsg(sendInfo)
  114. return
  115. }
  116. // 发送报告提问消息提醒
  117. func SendWxMsgWithAsk(name, askTime, askMsg, title string, openIdList []*models.OpenIdList, articleId int) (err error) {
  118. if len(openIdList) == 0 {
  119. return
  120. }
  121. var msg string
  122. defer func() {
  123. if err != nil {
  124. //go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  125. go utils.SendAlarmMsg(fmt.Sprint("发送报告提问消息提醒模版消息失败,SendWxMsgWithAsk 用户名", name, "文章ID:", articleId, "Err:"+err.Error()), 2)
  126. //utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  127. }
  128. fmt.Println("line 21", err, msg)
  129. }()
  130. var first string
  131. var keyword1 string
  132. var keyword2 string
  133. var keyword3 string
  134. var keyword4 string
  135. var remark string
  136. first = "研选报告有新的提问,请及时处理"
  137. keyword1 = name
  138. keyword3 = askTime
  139. keyword2 = ""
  140. keyword4 = askMsg
  141. remark = title
  142. openIdArr := make([]string, 0)
  143. for _, v := range openIdList {
  144. openIdArr = append(openIdArr, v.OpenId)
  145. }
  146. redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)
  147. sendInfo := new(SendWxTemplate)
  148. sendInfo.First = first
  149. sendInfo.Keyword1 = keyword1
  150. sendInfo.Keyword2 = keyword2
  151. sendInfo.Keyword3 = keyword3
  152. sendInfo.Keyword4 = keyword4
  153. sendInfo.Remark = remark
  154. sendInfo.TemplateId = utils.WxMsgTemplateIdAskMsgXzs
  155. sendInfo.RedirectUrl = redirectUrl
  156. sendInfo.RedirectTarget = 3
  157. sendInfo.Resource = strconv.Itoa(articleId)
  158. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  159. sendInfo.OpenIdArr = openIdArr
  160. err = PublicSendTemplateMsg(sendInfo)
  161. if err != nil {
  162. return
  163. }
  164. return
  165. }
  166. // 到期提醒模板消息
  167. func SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark string, openIdList []*models.OpenIdList) (err error) {
  168. var msg string
  169. defer func() {
  170. if err != nil {
  171. fmt.Println(err)
  172. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  173. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  174. }
  175. }()
  176. accessToken, err := models.GetWxAccessToken()
  177. if err != nil {
  178. msg = "GetWxAccessToken Err:" + err.Error()
  179. return
  180. }
  181. if accessToken == "" {
  182. msg = "accessToken is empty"
  183. return
  184. }
  185. sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  186. sendMap := make(map[string]interface{})
  187. sendData := make(map[string]interface{})
  188. sendMap["template_id"] = utils.WxMsgTemplateIdActivityChange
  189. sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  190. sendData["productname"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  191. sendData["date"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  192. sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  193. sendMap["data"] = sendData
  194. fmt.Println(accessToken)
  195. fmt.Println(sendUrl)
  196. //WxSendTemplateMsg(sendUrl, sendMap, openIdList)
  197. return
  198. }
  199. // 发送报告提问消息提醒
  200. func SendWxMsgWithArticleClassToAdmin(keyword1, keyword2, keyword3, keyword4 string, openIdList []*models.OpenIdList, articleId int) (err error) {
  201. if len(openIdList) == 0 {
  202. return
  203. }
  204. defer func() {
  205. if err != nil {
  206. go utils.SendAlarmMsg(fmt.Sprint("发送报告提问消息提醒模版消息失败,SendWxMsgWithArticleClassToAdmin 文章ID:", articleId, "Err:"+err.Error()), 2)
  207. }
  208. }()
  209. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  210. //templateId := utils.TemplateIdByProductXzs
  211. //sendMap := make(map[string]interface{})
  212. //sendData := make(map[string]interface{})
  213. //sendMap["template_id"] = templateId
  214. //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId, "pagepath": "pageMy/reportDetail/reportDetail?id=" + strconv.Itoa(articleId)}
  215. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  216. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  217. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  218. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  219. //sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"}
  220. //sendMap["data"] = sendData
  221. //WxSendTemplateMsg(sendUrl, sendMap, openIdList, strconv.Itoa(articleId), utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD)
  222. openIdArr := make([]string, 0)
  223. for _, v := range openIdList {
  224. openIdArr = append(openIdArr, v.OpenId)
  225. }
  226. redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)
  227. sendInfo := new(SendWxTemplate)
  228. sendInfo.Keyword1 = keyword1
  229. sendInfo.Keyword2 = keyword2
  230. sendInfo.Keyword3 = keyword3
  231. sendInfo.Keyword4 = keyword4
  232. sendInfo.TemplateId = utils.TemplateIdByProductXzs
  233. sendInfo.RedirectUrl = redirectUrl
  234. sendInfo.RedirectTarget = 3
  235. sendInfo.Resource = strconv.Itoa(articleId)
  236. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  237. sendInfo.OpenIdArr = openIdArr
  238. err = PublicSendTemplateMsg(sendInfo)
  239. return
  240. }
  241. // 发送用户阅读报告消息提醒
  242. func SendWxMsgWithArticleUserRemind(keyword1, keyword2 string, openIdList []*models.OpenIdList, articleId int) (err error) {
  243. if len(openIdList) == 0 {
  244. return
  245. }
  246. defer func() {
  247. if err != nil {
  248. go utils.SendAlarmMsg(fmt.Sprint("发送报告提问消息提醒模版消息失败,SendWxMsgWithArticleUserRemind 文章ID:", articleId, "Err:"+err.Error()), 2)
  249. }
  250. }()
  251. openIdArr := make([]string, 0)
  252. for _, v := range openIdList {
  253. openIdArr = append(openIdArr, v.OpenId)
  254. }
  255. redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)
  256. sendInfo := new(SendWxTemplate)
  257. sendInfo.Keyword1 = keyword1
  258. sendInfo.Keyword2 = keyword2
  259. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  260. sendInfo.RedirectUrl = redirectUrl
  261. sendInfo.RedirectTarget = 3
  262. sendInfo.Resource = strconv.Itoa(articleId)
  263. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  264. sendInfo.OpenIdArr = openIdArr
  265. return
  266. }
  267. // 发送用户操作活动消息提醒
  268. func SendWxMsgWithActivityUserRemind(keyword1, keyword2 string, openIdList []*models.OpenIdList, activityId int) (err error) {
  269. if len(openIdList) == 0 {
  270. return
  271. }
  272. defer func() {
  273. if err != nil {
  274. go utils.SendAlarmMsg(fmt.Sprint("发送用户操作活动消息提醒,文章ID:", activityId), 2)
  275. }
  276. }()
  277. openIdArr := make([]string, 0)
  278. for _, v := range openIdList {
  279. openIdArr = append(openIdArr, v.OpenId)
  280. }
  281. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
  282. sendInfo := new(SendWxTemplate)
  283. sendInfo.Keyword1 = keyword1
  284. sendInfo.Keyword2 = keyword2
  285. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  286. sendInfo.RedirectUrl = redirectUrl
  287. sendInfo.RedirectTarget = 3
  288. sendInfo.Resource = strconv.Itoa(activityId)
  289. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  290. sendInfo.OpenIdArr = openIdArr
  291. err = PublicSendTemplateMsg(sendInfo)
  292. return
  293. }
  294. // 活动带问提醒
  295. func SendActivityAskApplyTemplateMsgV2(keyword1, keyword2, keyword3, keyword4, remark string, itemOpenid *models.OpenIdList, itemAct *models.ActivityDetail, msgId int) (err error) {
  296. var msg string
  297. defer func() {
  298. if err != nil {
  299. go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  300. go utils.SendAlarmMsg("活动带问提醒发送模版消息失败"+remark, 2)
  301. }
  302. if msg != "" {
  303. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  304. }
  305. }()
  306. activityId := itemAct.ActivityId
  307. var redirectUrl string
  308. //redirectUrl = utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(activityId)
  309. //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频
  310. //if utils.RunMode != "release" {
  311. //
  312. //} else {
  313. //
  314. //}
  315. redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=2"
  316. openIdArr := make([]string, 0)
  317. openIdArr = append(openIdArr, itemOpenid.OpenId)
  318. sendInfo := new(SendWxTemplate)
  319. sendInfo.Keyword1 = keyword1
  320. sendInfo.Keyword2 = keyword2
  321. sendInfo.Keyword3 = keyword3
  322. sendInfo.Keyword4 = keyword4
  323. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_ADD
  324. sendInfo.Remark = remark
  325. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
  326. sendInfo.RedirectUrl = redirectUrl
  327. sendInfo.RedirectTarget = 3
  328. sendInfo.Resource = strconv.Itoa(activityId)
  329. sendInfo.OpenIdArr = openIdArr
  330. err = PublicSendTemplateMsg(sendInfo)
  331. return
  332. }
  333. // 专项产业调研模板消息推送
  334. //func SendSpecialTemplateMsg(applyName, applyTime, mobile, activityName, resource string, itemOpenid *models.OpenIdList) (err error) {
  335. // var msg string
  336. // defer func() {
  337. // if err != nil {
  338. // go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  339. // go utils.SendAlarmMsg("专项产业调研模板消息推送消息失败"+activityName, 2)
  340. // }
  341. // if msg != "" {
  342. // utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  343. // }
  344. // }()
  345. // var accessToken string
  346. // if utils.RunMode == "release" {
  347. // accessToken, err = models.GetWxAccessTokenByXzs()
  348. // if err != nil {
  349. // msg = "GetWxAccessToken Err:" + err.Error()
  350. // return
  351. // }
  352. // if accessToken == "" {
  353. // msg = "accessToken is empty"
  354. // return
  355. // }
  356. // } else {
  357. // accessToken, err = models.GetWxAccessToken()
  358. // if err != nil {
  359. // msg = "GetWxAccessToken Err:" + err.Error()
  360. // return
  361. // }
  362. // if accessToken == "" {
  363. // msg = "accessToken is empty"
  364. // return
  365. // }
  366. // }
  367. // sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  368. // sendMap := make(map[string]interface{})
  369. // sendData := make(map[string]interface{})
  370. // var first string
  371. // if resource == "sale" {
  372. // first = "有客户对专项调研感兴趣"
  373. // } else {
  374. // first = "有5家公司预报名专项调研"
  375. // }
  376. //
  377. // keyword1 := applyName
  378. // keyword2 := mobile
  379. // keyword3 := applyTime
  380. // keyword4 := activityName
  381. //
  382. // fontColor := "#D9001B"
  383. // sendData["first"] = map[string]interface{}{"value": first, "color": fontColor}
  384. // sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": fontColor}
  385. // sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": fontColor}
  386. // sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": fontColor}
  387. // sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": fontColor}
  388. // if utils.RunMode == "release" {
  389. // sendMap["template_id"] = utils.WxMsgTemplateIdApplyXzs
  390. // } else {
  391. // sendMap["template_id"] = utils.WxMsgTemplateIdApply
  392. // }
  393. // sendMap["data"] = sendData
  394. // sendTemplateMsg(sendUrl, sendMap, itemOpenid, mobile, utils.TEMPLATE_MSG_CYGX_ACTIVITY_ADD)
  395. // return
  396. //}
  397. //弘则投资:
  398. //Y59n_AHg-RLCKaz293geW76KDHpGL1qOnE7eF_lxelY 活动预约通知
  399. //CB7bOl7f3viMG4s1uhRo7WM0Jbx3WvodKuIZ8A_z8fM 项目状态变更通知
  400. //
  401. //
  402. //弘则研究:
  403. //MwZ5wxfd0O1Yt0Pkf6OkfWP4USQzZbiEo5SkZ26735s 活动预约通知
  404. //dYg6iHooRq74PyCXmw_Ns7qdJZmbtLoKS2p2FKeaXl0 项目状态变更通知
  405. func WxSendTemplateMsg(sendUrl string, sendMap map[string]interface{}, items []*models.OpenIdList, resource string, sendType int) (err error) {
  406. for _, v := range items {
  407. sendMap["touser"] = v.OpenId
  408. data, err := json.Marshal(sendMap)
  409. if err != nil {
  410. fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
  411. return err
  412. }
  413. utils.FileLog.Info("One SendData:%s", string(data))
  414. fmt.Println(string(data))
  415. err = SendTemplateMsg(sendUrl, data, resource, sendType, v)
  416. if err != nil {
  417. fmt.Println("send err:", err.Error())
  418. return err
  419. }
  420. }
  421. return
  422. }
  423. func SendTemplateMsg(sendUrl string, data []byte, resource string, sendType int, dataItem *models.OpenIdList) (err error) {
  424. client := http.Client{}
  425. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(data))
  426. if err != nil {
  427. return
  428. }
  429. defer resp.Body.Close()
  430. body, _ := ioutil.ReadAll(resp.Body)
  431. utils.FileLog.Info("SendResult:%s", string(body))
  432. var templateResponse models.SendTemplateResponse
  433. err = json.Unmarshal(body, &templateResponse)
  434. fmt.Println(templateResponse.Errcode)
  435. fmt.Println(templateResponse.Errmsg)
  436. if err != nil {
  437. return err
  438. }
  439. //新增模板消息推送记录
  440. {
  441. tr := new(models.UserTemplateRecord)
  442. tr.UserId = dataItem.UserId
  443. tr.OpenId = dataItem.OpenId
  444. tr.Resource = resource
  445. tr.SendData = string(data)
  446. tr.Result = string(body)
  447. tr.CreateDate = time.Now().Format(utils.FormatDate)
  448. tr.CreateTime = time.Now().Format(utils.FormatDateTime)
  449. if templateResponse.Errcode == 0 {
  450. tr.SendStatus = 1
  451. } else {
  452. tr.SendStatus = 0
  453. }
  454. tr.SendType = sendType
  455. go func() {
  456. err = models.AddUserTemplateRecord(tr)
  457. if err != nil {
  458. utils.FileLog.Info(fmt.Sprintf("AddUserTemplateRecord Err:%s", err.Error()))
  459. }
  460. }()
  461. }
  462. return
  463. }
  464. // 发送报告更新的消息给预约的用户
  465. func SendWxMsgWithArticleUpdate(first, keyword1, keyword2, keyword3, keyword4 string, item *models.OpenIdList, articleId int) (err error) {
  466. defer func() {
  467. if err != nil {
  468. go utils.SendAlarmMsg(fmt.Sprint("发送报告提问消息提醒模版消息失败,SendWxMsgWithArticleUpdate 文章ID:", articleId, "Err:"+err.Error()), 2)
  469. }
  470. }()
  471. openIdArr := make([]string, 0)
  472. openIdArr = append(openIdArr, item.OpenId)
  473. redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)
  474. sendInfo := new(SendWxTemplate)
  475. sendInfo.Keyword1 = keyword1
  476. sendInfo.Keyword2 = keyword2
  477. sendInfo.Keyword3 = keyword3
  478. sendInfo.Keyword4 = keyword4
  479. sendInfo.TemplateId = utils.TemplateIdByProductXzs
  480. sendInfo.RedirectUrl = redirectUrl
  481. sendInfo.RedirectTarget = 3
  482. sendInfo.Resource = strconv.Itoa(articleId)
  483. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  484. sendInfo.OpenIdArr = openIdArr
  485. err = PublicSendTemplateMsg(sendInfo)
  486. return
  487. }
  488. func SendWxMsgWithArticleUpdateList(keyword1, keyword2, keyword3, keyword4 string, openIdArr []string, articleId int) (err error) {
  489. defer func() {
  490. if err != nil {
  491. go utils.SendAlarmMsg(fmt.Sprint("发送报告提问消息提醒模版消息失败,SendWxMsgWithArticleUpdate 文章ID:", articleId, "Err:"+err.Error()), 2)
  492. }
  493. }()
  494. redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)
  495. sendInfo := new(SendWxTemplate)
  496. sendInfo.Keyword1 = keyword1
  497. sendInfo.Keyword2 = keyword2
  498. sendInfo.Keyword3 = keyword3
  499. sendInfo.Keyword4 = keyword4
  500. sendInfo.TemplateId = utils.TemplateIdByProductXzs
  501. sendInfo.RedirectUrl = redirectUrl
  502. sendInfo.RedirectTarget = 3
  503. sendInfo.Resource = strconv.Itoa(articleId)
  504. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  505. sendInfo.OpenIdArr = openIdArr
  506. err = PublicSendTemplateMsg(sendInfo)
  507. return
  508. }
  509. // 周日下午4点半,推送全部活动
  510. func SendActivityOnenIdWxTemplateMsg(first, keyword1, keyword2, keyword3, keyword4 string, openIdList []*models.OpenIdList) (err error) {
  511. defer func() {
  512. if err != nil {
  513. go utils.SendAlarmMsg(fmt.Sprint("发送周日下午4点半,推送全部活模版消息失败,"), 2)
  514. }
  515. }()
  516. //accessToken, err := models.GetWxAccessTokenByXzs()
  517. //if err != nil {
  518. // msg = "GetWxAccessToken Err:" + err.Error()
  519. // return
  520. //}
  521. //if accessToken == "" {
  522. // msg = "accessToken is empty"
  523. // return
  524. //}
  525. ////var openIdList []*models.OpenIdList
  526. ////openIdList = append(openIdList, item)
  527. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  528. //templateId := utils.WxMsgTemplateIdArticleUserRemind
  529. //sendMap := make(map[string]interface{})
  530. //sendData := make(map[string]interface{})
  531. //sendMap["template_id"] = templateId
  532. //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId, "pagepath": "pages/activity/activity"}
  533. ////sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId}
  534. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  535. //sendData["keyword1"] = map[string]interface{}{"value": keyWord1, "color": "#173177"}
  536. //sendData["keyword2"] = map[string]interface{}{"value": keyWord2, "color": "#173177"}
  537. //sendData["keyword3"] = map[string]interface{}{"value": keyWord3, "color": "#173177"}
  538. //sendData["keyword4"] = map[string]interface{}{"value": keyWord4, "color": "#173177"}
  539. //sendData["remark"] = map[string]interface{}{"value": "点击查看下周活动列表", "color": "#173177"}
  540. //sendMap["data"] = sendData
  541. //WxSendTemplateMsg(sendUrl, sendMap, openIdList, "", utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD)
  542. openIdArr := make([]string, 0)
  543. for _, v := range openIdList {
  544. openIdArr = append(openIdArr, v.OpenId)
  545. }
  546. redirectUrl := "pages/activity/activity"
  547. sendInfo := new(SendWxTemplate)
  548. sendInfo.Keyword1 = keyword1
  549. sendInfo.Keyword2 = keyword2
  550. sendInfo.Keyword3 = keyword3
  551. sendInfo.Keyword4 = keyword4
  552. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  553. sendInfo.RedirectUrl = redirectUrl
  554. sendInfo.RedirectTarget = 3
  555. //sendInfo.Resource =
  556. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  557. sendInfo.OpenIdArr = openIdArr
  558. err = PublicSendTemplateMsg(sendInfo)
  559. return
  560. }
  561. // 发送用户留言提醒
  562. // func SendCommentWxTemplateMsg(title, companyName, userName, sellerName, createTime, content string, openIdList []*models.OpenIdList, articleId int) (err error) {
  563. func SendCommentWxTemplateMsg(req models.AddCygxArticleCommentReq, user *models.WxUserItem, articleInfo *models.ArticleDetail, msgId int) (err error) {
  564. defer func() {
  565. if err != nil {
  566. go utils.SendAlarmMsg(fmt.Sprint("发送用户用户留言提醒失败,文章ID:", req.ArticleId, "Err:", err.Error()), 2)
  567. }
  568. }()
  569. companyItem, e := models.GetSellerDetailAllByCompanyId(user.CompanyId)
  570. if e != nil {
  571. err = errors.New("GetSellerDetailAllByCompanyId, Err: " + e.Error())
  572. return
  573. }
  574. var first string
  575. var keyword1 string
  576. var keyword2 string
  577. var keyword3 string
  578. var keyword4 string
  579. var remark string
  580. var redirectUrl string
  581. var mobile string
  582. first = "【" + articleInfo.Title + "】有新的留言,请及时处理"
  583. keyword1 = user.CompanyName + "——" + user.RealName + "(" + companyItem.SellerName + ")"
  584. keyword2 = "留言:" + articleInfo.Title
  585. keyword3 = time.Now().Format(utils.FormatDateTime)
  586. keyword4 = req.Content
  587. remark = "点击查看报告详情"
  588. if articleInfo.ArticleTypeId > 0 {
  589. //研选报告添加汪洋
  590. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic + "," + utils.ActSendMsgMobile
  591. } else {
  592. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic
  593. }
  594. openIdList, e := models.GetWxOpenIdByMobileList(mobile)
  595. if e != nil {
  596. err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error())
  597. return
  598. }
  599. openIdArr := make([]string, 0)
  600. for _, v := range openIdList {
  601. openIdArr = append(openIdArr, v.OpenId)
  602. }
  603. //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频
  604. if utils.RunMode != "release" {
  605. redirectUrl = utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleInfo.ArticleId)
  606. } else {
  607. redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=1"
  608. }
  609. sendInfo := new(SendWxTemplate)
  610. sendInfo.First = first
  611. sendInfo.Keyword1 = keyword1
  612. sendInfo.Keyword2 = keyword2
  613. sendInfo.Keyword3 = keyword3
  614. sendInfo.Keyword4 = keyword4
  615. sendInfo.Remark = remark
  616. sendInfo.TemplateId = utils.WxMsgTemplateIdAskMsgXzs
  617. sendInfo.RedirectUrl = redirectUrl
  618. sendInfo.RedirectTarget = 3
  619. sendInfo.Resource = strconv.Itoa(req.ArticleId)
  620. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_COMMENT
  621. sendInfo.OpenIdArr = openIdArr
  622. err = PublicSendTemplateMsg(sendInfo)
  623. if err != nil {
  624. return
  625. }
  626. return
  627. }
  628. // 微路演新增留言提问消息提醒
  629. func SendWxMsgWithMicroRoadshowAsk(req models.AddVideoCommnetReq, user *models.WxUserItem, resourceId, msgId int, title string, isResearch bool) (err error) {
  630. var msg string
  631. defer func() {
  632. if err != nil || msg != "" {
  633. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  634. go utils.SendAlarmMsg(fmt.Sprint("发送微路演新增留言提问消息提醒失败,用户名", req.Title, "ID:", req.SourceId), 2)
  635. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  636. }
  637. }()
  638. sourceId := req.SourceId
  639. companyItem, e := models.GetSellerDetailAllByCompanyId(user.CompanyId)
  640. if e != nil {
  641. return
  642. }
  643. if companyItem == nil {
  644. msg = "获取客户所属销售信息失败,UserID:" + strconv.Itoa(user.UserId)
  645. }
  646. var first string
  647. var keyword1 string
  648. var keyword2 string
  649. var keyword3 string
  650. var keyword4 string
  651. var remark string
  652. var mobile string
  653. first = req.Title + "有新的留言,请及时处理"
  654. keyword1 = user.CompanyName + "——" + user.RealName + "(" + companyItem.SellerName + ")"
  655. keyword3 = time.Now().Format(utils.FormatDateTime)
  656. keyword2 = "留言:" + title
  657. keyword4 = req.Content
  658. if isResearch {
  659. //研选报告添加汪洋
  660. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic + "," + utils.ActSendMsgMobile
  661. } else {
  662. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic
  663. }
  664. openIdList, e := models.GetWxOpenIdByMobileList(mobile)
  665. if e != nil {
  666. err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error())
  667. return
  668. }
  669. openIdArr := make([]string, 0)
  670. for _, v := range openIdList {
  671. openIdArr = append(openIdArr, v.OpenId)
  672. }
  673. mapSource := make(map[int]string)
  674. mapSource[1] = "5"
  675. mapSource[2] = "4"
  676. mapSource[3] = "3"
  677. if mapSource[resourceId] == "" {
  678. err = errors.New("留言消息类型错误, resourceId: " + strconv.Itoa(resourceId))
  679. return
  680. }
  681. //SourceType int `description:"视频来源: 1-音频; 2-活动视频; 3-微路演视频 (不传默认为1)"`
  682. //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频
  683. redirectUrl := ""
  684. if utils.RunMode != "release" {
  685. if req.SourceType == 3 {
  686. redirectUrl = utils.WX_MSG_PATH_ACTIVITY_INDUSTRYR_EPORT + strconv.Itoa(resourceId)
  687. remark = "点击进入产业资源包详情页"
  688. } else {
  689. redirectUrl = utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(resourceId)
  690. remark = "点击查看活动详情"
  691. }
  692. } else {
  693. redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=" + mapSource[resourceId]
  694. }
  695. sendInfo := new(SendWxTemplate)
  696. sendInfo.First = first
  697. sendInfo.Keyword1 = keyword1
  698. sendInfo.Keyword2 = keyword2
  699. sendInfo.Keyword3 = keyword3
  700. sendInfo.Keyword4 = keyword4
  701. sendInfo.Remark = remark
  702. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
  703. sendInfo.RedirectUrl = redirectUrl
  704. sendInfo.RedirectTarget = 3
  705. sendInfo.Resource = strconv.Itoa(sourceId)
  706. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_COMMENT
  707. sendInfo.OpenIdArr = openIdArr
  708. err = PublicSendTemplateMsg(sendInfo)
  709. if err != nil {
  710. return
  711. }
  712. return
  713. }