wx_template_msg.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. //accessToken, err := models.GetWxAccessTokenByXzs()
  472. //if err != nil {
  473. // msg = "GetWxAccessToken Err:" + err.Error()
  474. // return
  475. //}
  476. //if accessToken == "" {
  477. // msg = "accessToken is empty"
  478. // return
  479. //}
  480. //var openIdList []*models.OpenIdList
  481. //openIdList = append(openIdList, item)
  482. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  483. //templateId := utils.TemplateIdByProductXzs
  484. //sendMap := make(map[string]interface{})
  485. //sendData := make(map[string]interface{})
  486. //sendMap["template_id"] = templateId
  487. //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId, "pagepath": "pageMy/reportDetail/reportDetail?id=" + strconv.Itoa(articleId) + "&IsSendWx=1"}
  488. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  489. //sendData["keyword1"] = map[string]interface{}{"value": keyWord1, "color": "#173177"}
  490. //sendData["keyword2"] = map[string]interface{}{"value": keyWord2, "color": "#173177"}
  491. //sendData["keyword3"] = map[string]interface{}{"value": keyWord3, "color": "#173177"}
  492. //sendData["keyword4"] = map[string]interface{}{"value": keyWord4, "color": "#173177"}
  493. //sendData["remark"] = map[string]interface{}{"value": "点击查看报告详情", "color": "#173177"}
  494. //sendMap["data"] = sendData
  495. //WxSendTemplateMsg(sendUrl, sendMap, openIdList, strconv.Itoa(articleId), utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD)
  496. openIdArr := make([]string, 0)
  497. openIdArr = append(openIdArr, item.OpenId)
  498. redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)
  499. sendInfo := new(SendWxTemplate)
  500. sendInfo.Keyword1 = keyword1
  501. sendInfo.Keyword2 = keyword2
  502. sendInfo.Keyword3 = keyword3
  503. sendInfo.Keyword4 = keyword4
  504. sendInfo.TemplateId = utils.TemplateIdByProductXzs
  505. sendInfo.RedirectUrl = redirectUrl
  506. sendInfo.RedirectTarget = 3
  507. sendInfo.Resource = strconv.Itoa(articleId)
  508. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  509. sendInfo.OpenIdArr = openIdArr
  510. err = PublicSendTemplateMsg(sendInfo)
  511. return
  512. }
  513. // 周日下午4点半,推送全部活动
  514. func SendActivityOnenIdWxTemplateMsg(first, keyword1, keyword2, keyword3, keyword4 string, openIdList []*models.OpenIdList) (err error) {
  515. defer func() {
  516. if err != nil {
  517. go utils.SendAlarmMsg(fmt.Sprint("发送周日下午4点半,推送全部活模版消息失败,"), 2)
  518. }
  519. }()
  520. //accessToken, err := models.GetWxAccessTokenByXzs()
  521. //if err != nil {
  522. // msg = "GetWxAccessToken Err:" + err.Error()
  523. // return
  524. //}
  525. //if accessToken == "" {
  526. // msg = "accessToken is empty"
  527. // return
  528. //}
  529. ////var openIdList []*models.OpenIdList
  530. ////openIdList = append(openIdList, item)
  531. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  532. //templateId := utils.WxMsgTemplateIdArticleUserRemind
  533. //sendMap := make(map[string]interface{})
  534. //sendData := make(map[string]interface{})
  535. //sendMap["template_id"] = templateId
  536. //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId, "pagepath": "pages/activity/activity"}
  537. ////sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId}
  538. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  539. //sendData["keyword1"] = map[string]interface{}{"value": keyWord1, "color": "#173177"}
  540. //sendData["keyword2"] = map[string]interface{}{"value": keyWord2, "color": "#173177"}
  541. //sendData["keyword3"] = map[string]interface{}{"value": keyWord3, "color": "#173177"}
  542. //sendData["keyword4"] = map[string]interface{}{"value": keyWord4, "color": "#173177"}
  543. //sendData["remark"] = map[string]interface{}{"value": "点击查看下周活动列表", "color": "#173177"}
  544. //sendMap["data"] = sendData
  545. //WxSendTemplateMsg(sendUrl, sendMap, openIdList, "", utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD)
  546. openIdArr := make([]string, 0)
  547. for _, v := range openIdList {
  548. openIdArr = append(openIdArr, v.OpenId)
  549. }
  550. redirectUrl := "pages/activity/activity"
  551. sendInfo := new(SendWxTemplate)
  552. sendInfo.Keyword1 = keyword1
  553. sendInfo.Keyword2 = keyword2
  554. sendInfo.Keyword3 = keyword3
  555. sendInfo.Keyword4 = keyword4
  556. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  557. sendInfo.RedirectUrl = redirectUrl
  558. sendInfo.RedirectTarget = 3
  559. //sendInfo.Resource =
  560. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  561. sendInfo.OpenIdArr = openIdArr
  562. err = PublicSendTemplateMsg(sendInfo)
  563. return
  564. }
  565. // 发送用户留言提醒
  566. // func SendCommentWxTemplateMsg(title, companyName, userName, sellerName, createTime, content string, openIdList []*models.OpenIdList, articleId int) (err error) {
  567. func SendCommentWxTemplateMsg(req models.AddCygxArticleCommentReq, user *models.WxUserItem, articleInfo *models.ArticleDetail, msgId int) (err error) {
  568. defer func() {
  569. if err != nil {
  570. go utils.SendAlarmMsg(fmt.Sprint("发送用户用户留言提醒失败,文章ID:", req.ArticleId, "Err:", err.Error()), 2)
  571. }
  572. }()
  573. companyItem, e := models.GetSellerDetailAllByCompanyId(user.CompanyId)
  574. if e != nil {
  575. err = errors.New("GetSellerDetailAllByCompanyId, Err: " + e.Error())
  576. return
  577. }
  578. var first string
  579. var keyword1 string
  580. var keyword2 string
  581. var keyword3 string
  582. var keyword4 string
  583. var remark string
  584. var redirectUrl string
  585. var mobile string
  586. first = "【" + articleInfo.Title + "】有新的留言,请及时处理"
  587. keyword1 = user.CompanyName + "——" + user.RealName + "(" + companyItem.SellerName + ")"
  588. keyword2 = "留言:" + articleInfo.Title
  589. keyword3 = time.Now().Format(utils.FormatDateTime)
  590. keyword4 = req.Content
  591. remark = "点击查看报告详情"
  592. if articleInfo.ArticleTypeId > 0 {
  593. //研选报告添加汪洋
  594. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic + "," + utils.ActSendMsgMobile
  595. } else {
  596. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic
  597. }
  598. openIdList, e := models.GetWxOpenIdByMobileList(mobile)
  599. if e != nil {
  600. err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error())
  601. return
  602. }
  603. openIdArr := make([]string, 0)
  604. for _, v := range openIdList {
  605. openIdArr = append(openIdArr, v.OpenId)
  606. }
  607. //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频
  608. if utils.RunMode != "release" {
  609. redirectUrl = utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleInfo.ArticleId)
  610. } else {
  611. redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=1"
  612. }
  613. sendInfo := new(SendWxTemplate)
  614. sendInfo.First = first
  615. sendInfo.Keyword1 = keyword1
  616. sendInfo.Keyword2 = keyword2
  617. sendInfo.Keyword3 = keyword3
  618. sendInfo.Keyword4 = keyword4
  619. sendInfo.Remark = remark
  620. sendInfo.TemplateId = utils.WxMsgTemplateIdAskMsgXzs
  621. sendInfo.RedirectUrl = redirectUrl
  622. sendInfo.RedirectTarget = 3
  623. sendInfo.Resource = strconv.Itoa(req.ArticleId)
  624. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_COMMENT
  625. sendInfo.OpenIdArr = openIdArr
  626. err = PublicSendTemplateMsg(sendInfo)
  627. if err != nil {
  628. return
  629. }
  630. return
  631. }
  632. // 微路演新增留言提问消息提醒
  633. func SendWxMsgWithMicroRoadshowAsk(req models.AddVideoCommnetReq, user *models.WxUserItem, resourceId, msgId int, title string, isResearch bool) (err error) {
  634. var msg string
  635. defer func() {
  636. if err != nil || msg != "" {
  637. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  638. go utils.SendAlarmMsg(fmt.Sprint("发送微路演新增留言提问消息提醒失败,用户名", req.Title, "ID:", req.SourceId), 2)
  639. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  640. }
  641. }()
  642. sourceId := req.SourceId
  643. companyItem, e := models.GetSellerDetailAllByCompanyId(user.CompanyId)
  644. if e != nil {
  645. return
  646. }
  647. if companyItem == nil {
  648. msg = "获取客户所属销售信息失败,UserID:" + strconv.Itoa(user.UserId)
  649. }
  650. var first string
  651. var keyword1 string
  652. var keyword2 string
  653. var keyword3 string
  654. var keyword4 string
  655. var remark string
  656. var mobile string
  657. first = req.Title + "有新的留言,请及时处理"
  658. keyword1 = user.CompanyName + "——" + user.RealName + "(" + companyItem.SellerName + ")"
  659. keyword3 = time.Now().Format(utils.FormatDateTime)
  660. keyword2 = "留言:" + title
  661. keyword4 = req.Content
  662. if isResearch {
  663. //研选报告添加汪洋
  664. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic + "," + utils.ActSendMsgMobile
  665. } else {
  666. mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic
  667. }
  668. openIdList, e := models.GetWxOpenIdByMobileList(mobile)
  669. if e != nil {
  670. err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error())
  671. return
  672. }
  673. openIdArr := make([]string, 0)
  674. for _, v := range openIdList {
  675. openIdArr = append(openIdArr, v.OpenId)
  676. }
  677. mapSource := make(map[int]string)
  678. mapSource[1] = "5"
  679. mapSource[2] = "4"
  680. mapSource[3] = "3"
  681. if mapSource[resourceId] == "" {
  682. err = errors.New("留言消息类型错误, resourceId: " + strconv.Itoa(resourceId))
  683. return
  684. }
  685. //SourceType int `description:"视频来源: 1-音频; 2-活动视频; 3-微路演视频 (不传默认为1)"`
  686. //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频
  687. redirectUrl := ""
  688. if utils.RunMode != "release" {
  689. if req.SourceType == 3 {
  690. redirectUrl = utils.WX_MSG_PATH_ACTIVITY_INDUSTRYR_EPORT + strconv.Itoa(resourceId)
  691. remark = "点击进入产业资源包详情页"
  692. } else {
  693. redirectUrl = utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(resourceId)
  694. remark = "点击查看活动详情"
  695. }
  696. } else {
  697. redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=" + mapSource[resourceId]
  698. }
  699. sendInfo := new(SendWxTemplate)
  700. sendInfo.First = first
  701. sendInfo.Keyword1 = keyword1
  702. sendInfo.Keyword2 = keyword2
  703. sendInfo.Keyword3 = keyword3
  704. sendInfo.Keyword4 = keyword4
  705. sendInfo.Remark = remark
  706. sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs
  707. sendInfo.RedirectUrl = redirectUrl
  708. sendInfo.RedirectTarget = 3
  709. sendInfo.Resource = strconv.Itoa(sourceId)
  710. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_COMMENT
  711. sendInfo.OpenIdArr = openIdArr
  712. err = PublicSendTemplateMsg(sendInfo)
  713. if err != nil {
  714. return
  715. }
  716. return
  717. }