push_ticket.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package authorization
  2. import (
  3. "encoding/json"
  4. "errors"
  5. wxerr "gitee.com/yansen_zh/wxcomponent/errors"
  6. "gitee.com/yansen_zh/wxcomponent/utils/request"
  7. )
  8. // PushTicketParam 启动票据推送服务参数
  9. type PushTicketParam struct {
  10. ComponentAppId string `json:"component_appid"`
  11. ComponentSecret string `json:"component_secret"`
  12. }
  13. // PushTicketResult 启动票据推送服务参数
  14. type PushTicketResult struct {
  15. wxerr.Error
  16. }
  17. const (
  18. apiStartPushTicket = "https://apies.weixin.qq.com/cgi-bin/component/api_start_push_ticket"
  19. )
  20. // StartPushTicket 启动 ticket 推送服务
  21. func StartPushTicket(data PushTicketParam) (*PushTicketResult, error) {
  22. if data.ComponentAppId == "" {
  23. return nil, errors.New("启动 ticket 推送服务 第三方平台的 appid 不能为空")
  24. }
  25. if data.ComponentSecret == "" {
  26. return nil, errors.New("启动 ticket 推送服务 第三方平台的 appsecret 不能为空")
  27. }
  28. resp, e2 := request.PostJSON(apiStartPushTicket, data)
  29. if e2 != nil {
  30. return nil, e2
  31. }
  32. result := PushTicketResult{}
  33. if err := json.Unmarshal(resp, &result); err != nil {
  34. return nil, err
  35. }
  36. if result.Code != 0 {
  37. return &result, result.Error
  38. }
  39. return &result, nil
  40. }