wechat_send_msg.go 7.3 KB

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