wechat_send_msg.go 7.7 KB

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