wechat.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_public_api/models"
  5. "hongze/hongze_public_api/services"
  6. "hongze/hongze_public_api/services/alarm_msg"
  7. "hongze/hongze_public_api/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. err = services.SendWxTemplateMsg(sendItem)
  44. if err != nil {
  45. msg := "URI:" + this.Ctx.Input.URI() + "<br/> " + "Params" + string(body) + " <br/>" + "ErrMsg:" + err.Error() + ";<br/> Body:" + string(body) + "<br/>"
  46. go alarm_msg.SendAlarmMsg(msg, 1)
  47. }
  48. }()
  49. br.Ret = 200
  50. br.Msg = "推送成功"
  51. return
  52. }