wechat.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. 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. // RefreshWxAccessToken
  58. // @Title 强制刷新token
  59. // @Description 推送模板消息
  60. // @Success 200 新增成功
  61. // @router /test/refresh [post]
  62. func (this *WechatController) RefreshWxAccessToken() {
  63. br := new(models.BaseResponse).Init()
  64. token,err,errMsg:=services.RefreshWxAccessToken()
  65. br.Data = token
  66. if err != nil{
  67. br.Msg = errMsg
  68. br.ErrMsg = err.Error()
  69. }
  70. br.Ret = 200
  71. br.Msg = "推送成功"
  72. return
  73. }