wechat.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_pub/models"
  5. "eta/eta_pub/services"
  6. "eta/eta_pub/services/alarm_msg"
  7. "eta/eta_pub/utils"
  8. )
  9. // 发送模板消息
  10. type WechatController struct {
  11. BaseAuthController
  12. }
  13. // @Title 推送模板消息
  14. // @Description 推送模板消息
  15. // @Success 200 新增成功
  16. // @router /send_template_msg [post]
  17. func (this *WechatController) SendTemplateMsg() {
  18. br := new(models.BaseResponse).Init()
  19. defer func() {
  20. this.Data["json"] = br
  21. this.ServeJSON()
  22. }()
  23. body := this.Ctx.Input.RequestBody
  24. sendItem := new(models.SendWxTemplate)
  25. err := json.Unmarshal(body, &sendItem)
  26. if err != nil {
  27. br.Msg = "参数解析失败"
  28. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  29. return
  30. }
  31. if sendItem.TemplateId == "" {
  32. br.Msg = "参数错误,模板id为空"
  33. return
  34. }
  35. if len(sendItem.OpenIdArr) <= 0 {
  36. br.Msg = "openid为空"
  37. return
  38. }
  39. if sendItem.WxAppId == "" {
  40. sendItem.WxAppId = utils.WxAppId //默认是弘则研究公众号的appId
  41. }
  42. go func() {
  43. if sendItem.RedirectTarget == 3 {
  44. err = services.SendWxTemplateMsgCygx(sendItem)
  45. } else {
  46. err = services.SendWxTemplateMsg(sendItem)
  47. }
  48. if err != nil {
  49. msg := "URI:" + this.Ctx.Input.URI() + "<br/> " + "Params" + string(body) + " <br/>" + "ErrMsg:" + err.Error() + ";<br/> Body:" + string(body) + "<br/>"
  50. go alarm_msg.SendAlarmMsg(msg, 1)
  51. }
  52. }()
  53. br.Ret = 200
  54. br.Msg = "推送成功"
  55. return
  56. }
  57. // GetAccessToken
  58. // @Title 获取微信token
  59. // @Description 获取微信token
  60. // @Success 200
  61. // @router /access_token [post]
  62. func (this *WechatController) GetAccessToken() {
  63. br := new(models.BaseResponse).Init()
  64. defer func() {
  65. this.Data["json"] = br
  66. this.ServeJSON()
  67. }()
  68. body := this.Ctx.Input.RequestBody
  69. req := new(models.WxTokenReq)
  70. err := json.Unmarshal(body, &req)
  71. if err != nil {
  72. br.Msg = "参数解析失败"
  73. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  74. return
  75. }
  76. if req.WxAppId == "" {
  77. br.Msg = "参数错误,微信AppId为空"
  78. return
  79. }
  80. if req.WxAppSecret == "" {
  81. br.Msg = "参数错误,微信AppSecret为空"
  82. return
  83. }
  84. token, err, errMsg := services.GetWxAccessToken(req.WxAppId, req.WxAppSecret)
  85. if err != nil {
  86. br.Msg = errMsg
  87. br.ErrMsg = err.Error()
  88. return
  89. }
  90. data := models.WxTokenResp{AccessToken: token}
  91. br.Ret = 200
  92. br.Msg = "推送成功"
  93. br.Data = data
  94. return
  95. }
  96. // @Title 推送类目模板消息
  97. // @Description 推送模板消息
  98. // @Success 200 新增成功
  99. // @router /send_category_template_msg [post]
  100. func (this *WechatController) SendCategoryTemplateMsg() {
  101. br := new(models.BaseResponse).Init()
  102. defer func() {
  103. this.Data["json"] = br
  104. this.ServeJSON()
  105. }()
  106. body := this.Ctx.Input.RequestBody
  107. sendItem := new(models.SendWxCategoryTemplate)
  108. err := json.Unmarshal(body, &sendItem)
  109. if err != nil {
  110. br.Msg = "参数解析失败"
  111. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  112. return
  113. }
  114. if sendItem.TemplateId == "" {
  115. br.Msg = "参数错误,模板id为空"
  116. return
  117. }
  118. if len(sendItem.OpenIdArr) <= 0 {
  119. br.Msg = "openid为空"
  120. return
  121. }
  122. sendItem.WxAppId = utils.WxMfyxAppId //默认是买方研选公众号
  123. go func() {
  124. err = services.SendWxCategoryTemplateMsgCygx(sendItem)
  125. if err != nil {
  126. msg := "URI:" + this.Ctx.Input.URI() + "<br/> " + "Params" + string(body) + " <br/>" + "ErrMsg:" + err.Error() + ";<br/> Body:" + string(body) + "<br/>"
  127. go alarm_msg.SendAlarmMsg(msg, 1)
  128. }
  129. }()
  130. br.Ret = 200
  131. br.Msg = "推送成功"
  132. return
  133. }