mgnt.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package openapi
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/silenceper/wechat/v2/domain/openapi"
  6. mpContext "github.com/silenceper/wechat/v2/miniprogram/context"
  7. ocContext "github.com/silenceper/wechat/v2/officialaccount/context"
  8. "github.com/silenceper/wechat/v2/util"
  9. )
  10. const (
  11. clearQuotaURL = "https://api.weixin.qq.com/cgi-bin/clear_quota" // 重置 API 调用次数
  12. getAPIQuotaURL = "https://api.weixin.qq.com/cgi-bin/openapi/quota/get" // 查询 API 调用额度
  13. getRidInfoURL = "https://api.weixin.qq.com/cgi-bin/openapi/rid/get" // 查询 rid 信息
  14. clearQuotaByAppSecretURL = "https://api.weixin.qq.com/cgi-bin/clear_quota/v2" // 使用 AppSecret 重置 API 调用次数
  15. )
  16. // OpenAPI openApi 管理
  17. type OpenAPI struct {
  18. ctx interface{}
  19. }
  20. // NewOpenAPI 实例化
  21. func NewOpenAPI(ctx interface{}) *OpenAPI {
  22. return &OpenAPI{ctx: ctx}
  23. }
  24. // ClearQuota 重置 API 调用次数
  25. // https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/clearQuota.html
  26. func (o *OpenAPI) ClearQuota() error {
  27. appID, _, err := o.getAppIDAndSecret()
  28. if err != nil {
  29. return err
  30. }
  31. var payload = struct {
  32. AppID string `json:"appid"`
  33. }{
  34. AppID: appID,
  35. }
  36. res, err := o.doPostRequest(clearQuotaURL, payload)
  37. if err != nil {
  38. return err
  39. }
  40. return util.DecodeWithCommonError(res, "ClearQuota")
  41. }
  42. // GetAPIQuota 查询 API 调用额度
  43. // https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/getApiQuota.html
  44. func (o *OpenAPI) GetAPIQuota(params openapi.GetAPIQuotaParams) (quota openapi.APIQuota, err error) {
  45. res, err := o.doPostRequest(getAPIQuotaURL, params)
  46. if err != nil {
  47. return
  48. }
  49. err = util.DecodeWithError(res, &quota, "GetAPIQuota")
  50. return
  51. }
  52. // GetRidInfo 查询 rid 信息
  53. // https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/getRidInfo.html
  54. func (o *OpenAPI) GetRidInfo(params openapi.GetRidInfoParams) (r openapi.RidInfo, err error) {
  55. res, err := o.doPostRequest(getRidInfoURL, params)
  56. if err != nil {
  57. return
  58. }
  59. err = util.DecodeWithError(res, &r, "GetRidInfo")
  60. return
  61. }
  62. // ClearQuotaByAppSecret 使用 AppSecret 重置 API 调用次数
  63. // https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/clearQuotaByAppSecret.html
  64. func (o *OpenAPI) ClearQuotaByAppSecret() error {
  65. id, secret, err := o.getAppIDAndSecret()
  66. if err != nil {
  67. return err
  68. }
  69. uri := fmt.Sprintf("%s?appid=%s&appsecret=%s", clearQuotaByAppSecretURL, id, secret)
  70. res, err := util.HTTPPost(uri, "")
  71. if err != nil {
  72. return err
  73. }
  74. return util.DecodeWithCommonError(res, "ClearQuotaByAppSecret")
  75. }
  76. // 获取 AppID 和 AppSecret
  77. func (o *OpenAPI) getAppIDAndSecret() (string, string, error) {
  78. switch o.ctx.(type) {
  79. case *mpContext.Context:
  80. c, ok := o.ctx.(*mpContext.Context)
  81. if !ok {
  82. return "", "", errors.New("invalid context type")
  83. }
  84. return c.AppID, c.AppSecret, nil
  85. case *ocContext.Context:
  86. c, ok := o.ctx.(*ocContext.Context)
  87. if !ok {
  88. return "", "", errors.New("invalid context type")
  89. }
  90. return c.AppID, c.AppSecret, nil
  91. default:
  92. return "", "", errors.New("invalid context type")
  93. }
  94. }
  95. // 获取 AccessToken
  96. func (o *OpenAPI) getAccessToken() (string, error) {
  97. switch o.ctx.(type) {
  98. case *mpContext.Context:
  99. return o.ctx.(*mpContext.Context).GetAccessToken()
  100. case *ocContext.Context:
  101. return o.ctx.(*ocContext.Context).GetAccessToken()
  102. default:
  103. return "", errors.New("invalid context type")
  104. }
  105. }
  106. // 创建 POST 请求
  107. func (o *OpenAPI) doPostRequest(uri string, payload interface{}) ([]byte, error) {
  108. ak, err := o.getAccessToken()
  109. if err != nil {
  110. return nil, err
  111. }
  112. uri = fmt.Sprintf("%s?access_token=%s", uri, ak)
  113. return util.PostJSON(uri, payload)
  114. }