wechat_send_msg.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. // SendMiniProgramReportWxMsg 推送报告微信模板消息-小程序链接
  26. func SendMiniProgramReportWxMsg(reportId int) (err error) {
  27. fmt.Println("推送模板消息1 services SendMsg:", reportId)
  28. var msg string
  29. reportIdStr := strconv.Itoa(reportId)
  30. if utils.ETAMiniBridgeUrl != "" {
  31. err = third.DwSendTemplatedMsg(reportId)
  32. return
  33. }
  34. defer func() {
  35. if err != nil {
  36. fmt.Println("msg:", msg)
  37. utils.FileLog.Error(fmt.Sprintf("SendMiniProgramReportWxMsg, 发送报告模版消息失败, ReportId:%s, Err:%s", reportIdStr, err.Error()))
  38. go alarm_msg.SendAlarmMsg("SendMiniProgramReportWxMsg发送报告模版消息失败;"+"ReportId:"+reportIdStr+",Err:"+err.Error()+";msg:"+msg, 3)
  39. //go utils.SendEmail("SendMiniProgramReportWxMsg发送报告模版消息失败"+"【"+utils.APPNAME+"】"+"【"+utils.RunMode+"】"+time.Now().Format("2006-01-02 15:04:05"), "ReportId:"+reportIdStr+";"+msg+";Err:"+err.Error(), toUser)
  40. }
  41. }()
  42. fmt.Println("推送模板消息 services SendMsg:", reportId)
  43. report, err := models.GetReportByReportId(reportId)
  44. if err != nil {
  45. msg = "GetReportInfo Err:" + err.Error()
  46. return
  47. }
  48. if report == nil {
  49. utils.FileLog.Info("报告信息不存在")
  50. return
  51. }
  52. //if report.MsgIsSend == 1 {
  53. // return
  54. //}
  55. //accessToken, err := models.GetWxAccessToken()
  56. //if err != nil {
  57. // msg = "GetWxAccessToken Err:" + err.Error()
  58. // return
  59. //}
  60. //if accessToken == "" {
  61. // msg = "accessToken is empty"
  62. // return
  63. //}
  64. var openIdArr []string
  65. minClassifyId, _, err := getMinClassify(report)
  66. if err != nil {
  67. msg = "获取报告的最小分类失败 err:" + err.Error()
  68. return err
  69. }
  70. // 判断分类是否存在
  71. _, err = models.GetClassifyById(minClassifyId)
  72. if err != nil {
  73. msg = "获取报告分类失败 err:" + err.Error()
  74. return err
  75. }
  76. // 获取该分类关联的openid列表
  77. openIdArr, err = models.GetOpenIdArrByClassifyId(minClassifyId)
  78. if err != nil {
  79. msg = "GetOpenIdArrByClassifyNameSecond err:" + err.Error()
  80. return err
  81. }
  82. //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  83. //fmt.Println("send start")
  84. //utils.FileLog.Info("send start")
  85. //sendMap := make(map[string]interface{})
  86. //sendData := make(map[string]interface{})
  87. title := fmt.Sprintf("国能%s", report.ClassifyNameFirst)
  88. if CheckTwoWeekOrMonthReport(report.ClassifyIdFirst, report.ClassifyNameFirst) {
  89. title = fmt.Sprintf("国能%s", report.ClassifyNameSecond)
  90. }
  91. //redirectUrl := utils.TemplateRedirectUrl + strconv.Itoa(reportId)
  92. first := fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst)
  93. keyword1 := title
  94. keyword2 := report.Title
  95. keyword3 := report.PublishTime.Format(utils.FormatDateTime)
  96. keyword4 := report.Abstract
  97. //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
  98. //sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
  99. //sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
  100. //sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"}
  101. //sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"}
  102. //
  103. //sendMap["template_id"] = utils.TemplateIdByProduct
  104. ////sendMap["url"] = redirectUrl
  105. //sendMap["data"] = sendData
  106. var wxAppPath string
  107. weekClassifyId, err := company.GetReportClassifyIdByConfigKey("report_week_classify_id")
  108. if err != nil && !utils.IsErrNoRow(err) {
  109. msg = "获取周报ID配置失败:" + err.Error()
  110. return err
  111. }
  112. // 周报走这个逻辑,我也不清楚为什么,原先就是这样(2024-6-20 16:46:07)
  113. if report.ClassifyIdFirst == weekClassifyId {
  114. wxAppPath = fmt.Sprintf("pages-report/chapterList?reportId=%s", reportIdStr)
  115. } else {
  116. wxAppPath = fmt.Sprintf("pages-report/reportDetail?reportId=%s", reportIdStr)
  117. }
  118. //if wxAppPath != "" {
  119. // sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxYbAppId, "pagepath": wxAppPath}
  120. //}
  121. //err = sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_REPORT)
  122. sendInfo := new(SendWxTemplate)
  123. sendInfo.First = first
  124. sendInfo.Keyword1 = keyword1
  125. sendInfo.Keyword2 = keyword2
  126. sendInfo.Keyword3 = keyword3
  127. sendInfo.Keyword4 = keyword4
  128. sendInfo.TemplateId = utils.TemplateIdByProduct
  129. sendInfo.RedirectUrl = wxAppPath
  130. sendInfo.Resource = wxAppPath
  131. sendInfo.SendType = utils.TEMPLATE_MSG_REPORT
  132. sendInfo.OpenIdArr = openIdArr
  133. sendInfo.RedirectTarget = 1
  134. err = SendTemplateMsg(sendInfo)
  135. return
  136. }
  137. // CheckTwoWeekOrMonthReport 校验推送报告是否为双周报或者月报
  138. func CheckTwoWeekOrMonthReport(classifyId int, classifyName string) (ok bool) {
  139. if utils.RunMode == "debug" {
  140. miniStrArr := []string{
  141. "双周报", "月报",
  142. }
  143. if utils.InArrayByStr(miniStrArr, classifyName) {
  144. ok = true
  145. }
  146. } else {
  147. // 此处生产环境用ID主要是担心分类改了名字...
  148. IdArr := []int{
  149. 96, 112,
  150. }
  151. if utils.InArrayByInt(IdArr, classifyId) {
  152. ok = true
  153. }
  154. }
  155. return
  156. }
  157. type SendWxTemplate struct {
  158. WxAppId string `description:"公众号appId"`
  159. First string `description:"模板消息first字段"`
  160. Keyword1 string `description:"模板消息keyword1字段"`
  161. Keyword2 string `description:"模板消息keyword2字段"`
  162. Keyword3 string `description:"模板消息keyword3字段"`
  163. Keyword4 string `description:"模板消息keyword4字段"`
  164. Keyword5 string `description:"模板消息keyword5字段"`
  165. Remark string `description:"模板消息remark字段"`
  166. TemplateId string `description:"模板id"`
  167. RedirectUrl string `description:"跳转地址"`
  168. RedirectTarget int `description:"小程序跳转目标:1:国能研报小程序,2:随手办公小程序"`
  169. Resource string `description:"资源唯一标识"`
  170. SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
  171. OpenIdArr []string `description:"消息接收者openid"`
  172. }
  173. // SendTemplateMsg 推送模板消息
  174. func SendTemplateMsg(sendInfo *SendWxTemplate) (err error) {
  175. if utils.SendWxTemplateMsgUrl == `` {
  176. // 找不到推送服务
  177. return
  178. }
  179. postData, err := json.Marshal(sendInfo)
  180. if err != nil {
  181. alarm_msg.SendAlarmMsg("SendTemplateMsg json.Marshal Err:"+err.Error(), 1)
  182. return err
  183. }
  184. body := io.NopCloser(strings.NewReader(string(postData)))
  185. client := &http.Client{}
  186. req, err := http.NewRequest("POST", utils.SendWxTemplateMsgUrl, body)
  187. if err != nil {
  188. alarm_msg.SendAlarmMsg("SendTemplateMsg http.NewRequest Err:"+err.Error(), 1)
  189. return err
  190. }
  191. contentType := "application/json;charset=utf-8"
  192. req.Header.Set("Content-Type", contentType)
  193. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  194. resp, err := client.Do(req)
  195. if err != nil {
  196. fmt.Println("http client.Do Err:" + err.Error())
  197. return err
  198. }
  199. defer resp.Body.Close()
  200. b, err := io.ReadAll(resp.Body)
  201. if err != nil {
  202. return err
  203. }
  204. result := new(models.BaseResponse)
  205. err = json.Unmarshal(b, &result)
  206. if err != nil {
  207. return err
  208. }
  209. if result.Ret != 200 {
  210. err = errors.New(string(b))
  211. return err
  212. }
  213. return
  214. }