wechat_send_msg.go 7.4 KB

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