push_ticket.go 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Copyright (c) 2022 Yansen Zhang
  3. * wxcomponent is licensed under Mulan PSL v2.
  4. * You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. * You may obtain a copy of Mulan PSL v2 at:
  6. * http://license.coscl.org.cn/MulanPSL2
  7. * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. * See the Mulan PSL v2 for more details.
  11. **/
  12. package authorization
  13. import (
  14. "encoding/json"
  15. "errors"
  16. wxerr "gitee.com/yansen_zh/wxcomponent/errors"
  17. "gitee.com/yansen_zh/wxcomponent/utils/request"
  18. )
  19. // PushTicketParam 启动票据推送服务参数
  20. type PushTicketParam struct {
  21. ComponentAppId string `json:"component_appid"`
  22. ComponentSecret string `json:"component_secret"`
  23. }
  24. // PushTicketResult 启动票据推送服务参数
  25. type PushTicketResult struct {
  26. wxerr.Error
  27. }
  28. const (
  29. apiStartPushTicket = "https://apies.weixin.qq.com/cgi-bin/component/api_start_push_ticket"
  30. )
  31. // StartPushTicket 启动 ticket 推送服务
  32. func StartPushTicket(data PushTicketParam) (*PushTicketResult, error) {
  33. if data.ComponentAppId == "" {
  34. return nil, errors.New("启动 ticket 推送服务 第三方平台的 appid 不能为空")
  35. }
  36. if data.ComponentSecret == "" {
  37. return nil, errors.New("启动 ticket 推送服务 第三方平台的 appsecret 不能为空")
  38. }
  39. resp, e2 := request.PostJSON(apiStartPushTicket, data)
  40. if e2 != nil {
  41. return nil, e2
  42. }
  43. result := PushTicketResult{}
  44. if err := json.Unmarshal(resp, &result); err != nil {
  45. return nil, err
  46. }
  47. if result.Code != 0 {
  48. return &result, result.Error
  49. }
  50. return &result, nil
  51. }