wechat_send_msg.go 8.2 KB

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