wechat.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }