wechat_send_msg.go 7.6 KB

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