wx_template_msg.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_task/models"
  6. "eta_gn/eta_task/services/alarm_msg"
  7. "eta_gn/eta_task/utils"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "strings"
  12. "time"
  13. )
  14. func SendWxMsgWithFrequency(first, keyword1, keyword2, remark string, openIdList []*models.OpenIdList) (err error) {
  15. var msg string
  16. defer func() {
  17. if err != nil {
  18. go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  19. utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
  20. }
  21. fmt.Println("line 21", err, msg)
  22. }()
  23. utils.FileLog.Info("services SendMsg")
  24. wxAppPath := ""
  25. openIdArr := make([]string, len(openIdList))
  26. for i, v := range openIdList {
  27. openIdArr[i] = v.OpenId
  28. }
  29. sendInfo := new(SendWxTemplate)
  30. sendInfo.WxAppId = utils.AdminWxAppId
  31. sendInfo.First = first
  32. sendInfo.Keyword1 = keyword1
  33. sendInfo.Keyword2 = keyword2
  34. sendInfo.RedirectUrl = wxAppPath
  35. sendInfo.RedirectTarget = 0
  36. sendInfo.TemplateId = utils.TemplateId
  37. sendInfo.Resource = wxAppPath
  38. sendInfo.OpenIdArr = openIdArr
  39. sendInfo.Remark = remark
  40. err = SendTemplateMsgV2(sendInfo)
  41. return
  42. }
  43. type SendWxTemplate struct {
  44. WxAppId string `description:"公众号appId"`
  45. First string `description:"模板消息first字段"`
  46. Keyword1 string `description:"模板消息keyword1字段"`
  47. Keyword2 string `description:"模板消息keyword2字段"`
  48. Keyword3 string `description:"模板消息keyword3字段"`
  49. Keyword4 string `description:"模板消息keyword4字段"`
  50. Productname string `description:"模板消息productname字段"`
  51. Date string `description:"模板消息date字段"`
  52. Remark string `description:"模板消息remark字段"`
  53. TemplateId string `description:"模板id"`
  54. RedirectUrl string `description:"跳转地址"`
  55. RedirectTarget int `description:"小程序跳转目标:1:弘则研报小程序,2:随手办公小程序"`
  56. Resource string `description:"资源唯一标识"`
  57. SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
  58. OpenIdArr []string `description:"消息接收者openid"`
  59. }
  60. // 推送模板消息
  61. func SendTemplateMsgV2(sendInfo *SendWxTemplate) (err error) {
  62. postData, err := json.Marshal(sendInfo)
  63. if err != nil {
  64. alarm_msg.SendAlarmMsg("SendTemplateMsg json.Marshal Err:"+err.Error(), 1)
  65. return err
  66. }
  67. body := ioutil.NopCloser(strings.NewReader(string(postData)))
  68. client := &http.Client{}
  69. req, err := http.NewRequest("POST", utils.SendWxTemplateMsgUrl, body)
  70. if err != nil {
  71. alarm_msg.SendAlarmMsg("SendTemplateMsg http.NewRequest Err:"+err.Error(), 1)
  72. return err
  73. }
  74. contentType := "application/json;charset=utf-8"
  75. req.Header.Set("Content-Type", contentType)
  76. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  77. resp, err := client.Do(req)
  78. if err != nil {
  79. fmt.Println("http client.Do Err:" + err.Error())
  80. return err
  81. }
  82. defer resp.Body.Close()
  83. b, err := ioutil.ReadAll(resp.Body)
  84. if err != nil {
  85. return err
  86. }
  87. result := new(models.BaseResponse)
  88. err = json.Unmarshal(b, &result)
  89. if err != nil {
  90. return err
  91. }
  92. if result.Ret != 200 {
  93. err = errors.New(string(b))
  94. return err
  95. }
  96. return
  97. }