wx_template_msg.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_task/models"
  6. "eta/eta_task/services/alarm_msg"
  7. "eta/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. /*accessToken, err := models.GetWxAccessToken()
  25. if err != nil {
  26. msg = "GetWxAccessToken Err:" + err.Error()
  27. return
  28. }
  29. if accessToken == "" {
  30. msg = "accessToken is empty"
  31. return
  32. }
  33. sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  34. templateId := utils.TemplateId
  35. //if true{
  36. sendMap := make(map[string]interface{})
  37. sendData := make(map[string]interface{})
  38. sendMap["template_id"] = templateId
  39. //redirectUrl:="report.hzinsights.com/#/allindex/"+strconv.Itoa(v.ResearchReportId)+"/"+v.ReportType;
  40. //sendMap["url"]=redirectUrl
  41. sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  42. sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  43. sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  44. sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
  45. sendMap["data"] = sendData
  46. WxSendTemplateMsg(sendUrl, sendMap, openIdList)*/
  47. wxAppPath := ""
  48. openIdArr := make([]string, len(openIdList))
  49. for i, v := range openIdList {
  50. openIdArr[i] = v.OpenId
  51. }
  52. sendInfo := new(SendWxTemplate)
  53. sendInfo.WxAppId = utils.AdminWxAppId
  54. sendInfo.First = first
  55. sendInfo.Keyword1 = keyword1
  56. sendInfo.Keyword2 = keyword2
  57. sendInfo.RedirectUrl = wxAppPath
  58. sendInfo.RedirectTarget = 0
  59. sendInfo.TemplateId = utils.TemplateId
  60. sendInfo.Resource = wxAppPath
  61. sendInfo.OpenIdArr = openIdArr
  62. sendInfo.Remark = remark
  63. err = SendTemplateMsgV2(sendInfo)
  64. return
  65. }
  66. type SendWxTemplate struct {
  67. WxAppId string `description:"公众号appId"`
  68. First string `description:"模板消息first字段"`
  69. Keyword1 string `description:"模板消息keyword1字段"`
  70. Keyword2 string `description:"模板消息keyword2字段"`
  71. Keyword3 string `description:"模板消息keyword3字段"`
  72. Keyword4 string `description:"模板消息keyword4字段"`
  73. Productname string `description:"模板消息productname字段"`
  74. Date string `description:"模板消息date字段"`
  75. Remark string `description:"模板消息remark字段"`
  76. TemplateId string `description:"模板id"`
  77. RedirectUrl string `description:"跳转地址"`
  78. RedirectTarget int `description:"小程序跳转目标:1:弘则研报小程序,2:随手办公小程序"`
  79. Resource string `description:"资源唯一标识"`
  80. SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
  81. OpenIdArr []string `description:"消息接收者openid"`
  82. }
  83. // 推送模板消息
  84. func SendTemplateMsgV2(sendInfo *SendWxTemplate) (err error) {
  85. postData, err := json.Marshal(sendInfo)
  86. if err != nil {
  87. alarm_msg.SendAlarmMsg("SendTemplateMsg json.Marshal Err:"+err.Error(), 1)
  88. return err
  89. }
  90. body := ioutil.NopCloser(strings.NewReader(string(postData)))
  91. client := &http.Client{}
  92. req, err := http.NewRequest("POST", utils.SendWxTemplateMsgUrl, body)
  93. if err != nil {
  94. alarm_msg.SendAlarmMsg("SendTemplateMsg http.NewRequest Err:"+err.Error(), 1)
  95. return err
  96. }
  97. contentType := "application/json;charset=utf-8"
  98. req.Header.Set("Content-Type", contentType)
  99. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  100. resp, err := client.Do(req)
  101. if err != nil {
  102. fmt.Println("http client.Do Err:" + err.Error())
  103. return err
  104. }
  105. defer resp.Body.Close()
  106. b, err := ioutil.ReadAll(resp.Body)
  107. if err != nil {
  108. return err
  109. }
  110. result := new(models.BaseResponse)
  111. err = json.Unmarshal(b, &result)
  112. if err != nil {
  113. return err
  114. }
  115. if result.Ret != 200 {
  116. err = errors.New(string(b))
  117. return err
  118. }
  119. return
  120. }