wechat_send_msg.go 7.1 KB

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