wechat_send_msg.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/company"
  7. "eta_gn/eta_api/services/alarm_msg"
  8. "eta_gn/eta_api/services/third"
  9. "eta_gn/eta_api/utils"
  10. "fmt"
  11. "io"
  12. "net/http"
  13. "strconv"
  14. "strings"
  15. )
  16. type SendTemplateResponse struct {
  17. Errcode int `json:"errcode"`
  18. Errmsg string `json:"errmsg"`
  19. MsgID int `json:"msgid"`
  20. }
  21. type ClearQuotaResponse struct {
  22. Errcode int `json:"errcode"`
  23. Errmsg string `json:"errmsg"`
  24. }
  25. func SendMiniProgramReportWxMsg(reportId int) (err error) {
  26. fmt.Println("推送模板消息1 services SendMsg:", reportId)
  27. var msg string
  28. reportIdStr := strconv.Itoa(reportId)
  29. if utils.ETAMiniBridgeUrl != "" {
  30. err = third.DwSendTemplatedMsg(reportId)
  31. return
  32. }
  33. defer func() {
  34. if err != nil {
  35. fmt.Println("msg:", msg)
  36. utils.FileLog.Error(fmt.Sprintf("SendMiniProgramReportWxMsg, 发送报告模版消息失败, ReportId:%s, Err:%s", reportIdStr, err.Error()))
  37. go alarm_msg.SendAlarmMsg("SendMiniProgramReportWxMsg发送报告模版消息失败;"+"ReportId:"+reportIdStr+",Err:"+err.Error()+";msg:"+msg, 3)
  38. }
  39. }()
  40. fmt.Println("推送模板消息 services SendMsg:", reportId)
  41. report, err := models.GetReportByReportId(reportId)
  42. if err != nil {
  43. msg = "GetReportInfo Err:" + err.Error()
  44. return
  45. }
  46. if report == nil {
  47. utils.FileLog.Info("报告信息不存在")
  48. return
  49. }
  50. var openIdArr []string
  51. minClassifyId, _, err := getMinClassify(report)
  52. if err != nil {
  53. msg = "获取报告的最小分类失败 err:" + err.Error()
  54. return err
  55. }
  56. _, err = models.GetClassifyById(minClassifyId)
  57. if err != nil {
  58. msg = "获取报告分类失败 err:" + err.Error()
  59. return err
  60. }
  61. openIdArr, err = models.GetOpenIdArrByClassifyId(minClassifyId)
  62. if err != nil {
  63. msg = "GetOpenIdArrByClassifyNameSecond err:" + err.Error()
  64. return err
  65. }
  66. title := fmt.Sprintf("国能%s", report.ClassifyNameFirst)
  67. if CheckTwoWeekOrMonthReport(report.ClassifyIdFirst, report.ClassifyNameFirst) {
  68. title = fmt.Sprintf("国能%s", report.ClassifyNameSecond)
  69. }
  70. first := fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst)
  71. keyword1 := title
  72. keyword2 := report.Title
  73. keyword3 := report.PublishTime.Format(utils.FormatDateTime)
  74. keyword4 := report.Abstract
  75. var wxAppPath string
  76. weekClassifyId, err := company.GetReportClassifyIdByConfigKey("report_week_classify_id")
  77. if err != nil && !utils.IsErrNoRow(err) {
  78. msg = "获取周报ID配置失败:" + err.Error()
  79. return err
  80. }
  81. if report.ClassifyIdFirst == weekClassifyId {
  82. wxAppPath = fmt.Sprintf("pages-report/chapterList?reportId=%s", reportIdStr)
  83. } else {
  84. wxAppPath = fmt.Sprintf("pages-report/reportDetail?reportId=%s", reportIdStr)
  85. }
  86. sendInfo := new(SendWxTemplate)
  87. sendInfo.First = first
  88. sendInfo.Keyword1 = keyword1
  89. sendInfo.Keyword2 = keyword2
  90. sendInfo.Keyword3 = keyword3
  91. sendInfo.Keyword4 = keyword4
  92. sendInfo.TemplateId = utils.TemplateIdByProduct
  93. sendInfo.RedirectUrl = wxAppPath
  94. sendInfo.Resource = wxAppPath
  95. sendInfo.SendType = utils.TEMPLATE_MSG_REPORT
  96. sendInfo.OpenIdArr = openIdArr
  97. sendInfo.RedirectTarget = 1
  98. err = SendTemplateMsg(sendInfo)
  99. return
  100. }
  101. func CheckTwoWeekOrMonthReport(classifyId int, classifyName string) (ok bool) {
  102. if utils.RunMode == "debug" {
  103. miniStrArr := []string{
  104. "双周报", "月报",
  105. }
  106. if utils.InArrayByStr(miniStrArr, classifyName) {
  107. ok = true
  108. }
  109. } else {
  110. IdArr := []int{
  111. 96, 112,
  112. }
  113. if utils.InArrayByInt(IdArr, classifyId) {
  114. ok = true
  115. }
  116. }
  117. return
  118. }
  119. type SendWxTemplate struct {
  120. WxAppId string `description:"公众号appId"`
  121. First string `description:"模板消息first字段"`
  122. Keyword1 string `description:"模板消息keyword1字段"`
  123. Keyword2 string `description:"模板消息keyword2字段"`
  124. Keyword3 string `description:"模板消息keyword3字段"`
  125. Keyword4 string `description:"模板消息keyword4字段"`
  126. Keyword5 string `description:"模板消息keyword5字段"`
  127. Remark string `description:"模板消息remark字段"`
  128. TemplateId string `description:"模板id"`
  129. RedirectUrl string `description:"跳转地址"`
  130. RedirectTarget int `description:"小程序跳转目标:1:国能研报小程序,2:随手办公小程序"`
  131. Resource string `description:"资源唯一标识"`
  132. SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
  133. OpenIdArr []string `description:"消息接收者openid"`
  134. }
  135. func SendTemplateMsg(sendInfo *SendWxTemplate) (err error) {
  136. if utils.SendWxTemplateMsgUrl == `` {
  137. return
  138. }
  139. postData, err := json.Marshal(sendInfo)
  140. if err != nil {
  141. alarm_msg.SendAlarmMsg("SendTemplateMsg json.Marshal Err:"+err.Error(), 1)
  142. return err
  143. }
  144. body := io.NopCloser(strings.NewReader(string(postData)))
  145. client := &http.Client{}
  146. req, err := http.NewRequest("POST", utils.SendWxTemplateMsgUrl, body)
  147. if err != nil {
  148. alarm_msg.SendAlarmMsg("SendTemplateMsg http.NewRequest Err:"+err.Error(), 1)
  149. return err
  150. }
  151. contentType := "application/json;charset=utf-8"
  152. req.Header.Set("Content-Type", contentType)
  153. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  154. resp, err := client.Do(req)
  155. if err != nil {
  156. fmt.Println("http client.Do Err:" + err.Error())
  157. return err
  158. }
  159. defer resp.Body.Close()
  160. b, err := io.ReadAll(resp.Body)
  161. if err != nil {
  162. return err
  163. }
  164. result := new(models.BaseResponse)
  165. err = json.Unmarshal(b, &result)
  166. if err != nil {
  167. return err
  168. }
  169. if result.Ret != 200 {
  170. err = errors.New(string(b))
  171. return err
  172. }
  173. return
  174. }