Skip to content

Commit

Permalink
add: 增加 openapi 相关接口
Browse files Browse the repository at this point in the history
  • Loading branch information
royalrick committed Nov 12, 2023
1 parent 81b02e2 commit 6f916ee
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 1 deletion.
33 changes: 33 additions & 0 deletions openapi/clear_quota.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package openapi

import (
"github.com/medivhzhan/weapp/v3/request"
)

const apiClearQuota = "/cgi-bin/clear_quota"

type ClearQuotaRequest struct {
// 必填 要被清空的账号的appid
Appid string `json:"appid"`
}

type ClearQuotaResponse struct {
request.CommonError
}

// 重置API调用次数
// doc: https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/clearQuota.html
func (cli *OpenApi) ClearQuota(req *ClearQuotaRequest) (*ClearQuotaResponse, error) {

uri, err := cli.conbineURI(apiClearQuota, nil, true)
if err != nil {
return nil, err
}

res := new(ClearQuotaResponse)
if err := cli.request.Post(uri, req, res); err != nil {
return nil, err
}

return res, nil
}
35 changes: 35 additions & 0 deletions openapi/clear_quota_by_app_secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package openapi

import (
"github.com/medivhzhan/weapp/v3/request"
)

const apiClearQuotaByAppSecret = "/cgi-bin/clear_quota/v2"

type ClearQuotaByAppSecretRequest struct {
// 必填 要被清空的账号的appid
Appid string `query:"appid"`
// 唯一凭证密钥,即 AppSecret,获取方式同 appid
AppSecret string `query:"appsecret"`
}

type ClearQuotaByAppSecretResponse struct {
request.CommonError
}

// 使用AppSecret重置 API 调用次数
// doc: https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/clearQuotaByAppSecret.html
func (cli *OpenApi) ClearQuotaByAppSecret(req *ClearQuotaByAppSecretRequest) (*ClearQuotaByAppSecretResponse, error) {

uri, err := cli.conbineURI(apiClearQuotaByAppSecret, req, true)
if err != nil {
return nil, err
}

res := new(ClearQuotaByAppSecretResponse)
if err := cli.request.Post(uri, nil, res); err != nil {
return nil, err
}

return res, nil
}
47 changes: 47 additions & 0 deletions openapi/get_api_quota.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package openapi

import (
"github.com/medivhzhan/weapp/v3/request"
)

const apiGetApiQuota = "/cgi-bin/openapi/quota/get"

type GetApiQuotaRequest struct {
// @必填
// api的请求地址,例如"/cgi-bin/message/custom/send";不要前缀“https://api.weixin.qq.com” ,也不要漏了"/",否则都会76003的报错
CgiPath string `json:"cgi_path"`
}

type GetApiQuotaResponse struct {
request.CommonError
Quota struct {
DailyLimit int64 `json:"daily_limit"` // 当天该账号可调用该接口的次数
Used int64 `json:"used"` // 当天已经调用的次数
Remain int64 `json:"remain"` // 当天剩余调用次数
RateLimit struct {
CallCount int64 `json:"call_count"` // 周期内可调用数量,单位 次
RefreshSecond int64 `json:"refresh_second"` // 更新周期,单位 秒
} `json:"rate_limit"` // 普通调用频率限制
ComponentRateLimit struct {
CallCount int64 `json:"call_count"` // 周期内可调用数量,单位 次
RefreshSecond int64 `json:"refresh_second"` // 更新周期,单位 秒
} `json:"component_rate_limit"` // 代调用频率限制
} `json:"quota"` // quota详情
}

// 查询API调用额度
// doc: https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/getApiQuota.html
func (cli *OpenApi) GetApiQuota(req *GetApiQuotaRequest) (*GetApiQuotaResponse, error) {

uri, err := cli.conbineURI(apiGetApiQuota, nil, true)
if err != nil {
return nil, err
}

res := new(GetApiQuotaResponse)
if err := cli.request.Post(uri, req, res); err != nil {
return nil, err
}

return res, nil
}
42 changes: 42 additions & 0 deletions openapi/get_rid_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package openapi

import (
"github.com/medivhzhan/weapp/v3/request"
)

const apiGetRidInfo = "/cgi-bin/openapi/rid/get"

type GetRidInfoRequest struct {
// @必填
// 调用接口报错返回的rid
Rid string `json:"rid"`
}

type GetRidInfoResponse struct {
request.CommonError
Request struct {
InvokeTime int64 `json:"invoke_time"` // 发起请求的时间戳
CostInMs int64 `json:"cost_in_ms"` // 请求毫秒级耗时
RequestURL string `json:"request_url"` // 请求的URL参数
RequestBody string `json:"request_body"` // post请求的请求参数
ResponseBody string `json:"response_body"` // 接口请求返回参数
ClientIP string `json:"client_ip"` // 接口请求的客户端ip
} `json:"request"` // 该rid对应的请求详情
}

// 查询rid信息
// doc: https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/openApi-mgnt/getRidInfo.html
func (cli *OpenApi) GetRidInfo(req *GetRidInfoRequest) (*GetRidInfoResponse, error) {

uri, err := cli.conbineURI(apiGetRidInfo, nil, true)
if err != nil {
return nil, err
}

res := new(GetRidInfoResponse)
if err := cli.request.Post(uri, req, res); err != nil {
return nil, err
}

return res, nil
}
21 changes: 21 additions & 0 deletions openapi/openapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package openapi

import (
"github.com/medivhzhan/weapp/v3/request"
)

type OpenApi struct {
request *request.Request
// 组成完整的 URL 地址
// 默认包含 AccessToken
conbineURI func(url string, req interface{}, withToken bool) (string, error)
}

func NewOpenApi(request *request.Request, conbineURI func(url string, req interface{}, withToken bool) (string, error)) *OpenApi {
sm := OpenApi{
request: request,
conbineURI: conbineURI,
}

return &sm
}
8 changes: 7 additions & 1 deletion weapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/medivhzhan/weapp/v3/livebroadcast"
"github.com/medivhzhan/weapp/v3/logger"
"github.com/medivhzhan/weapp/v3/ocr"
"github.com/medivhzhan/weapp/v3/openapi"
"github.com/medivhzhan/weapp/v3/operation"
"github.com/medivhzhan/weapp/v3/phonenumber"
"github.com/medivhzhan/weapp/v3/request"
Expand Down Expand Up @@ -179,7 +180,7 @@ func (cli *Client) StableAccessToken(forceRefresh bool) (string, error) {
return data.(string), nil
}

if cli.accessTokenGetter != nil {
if !forceRefresh && cli.accessTokenGetter != nil {
token, expireIn := cli.accessTokenGetter(cli.appid, cli.secret)
cli.cache.Set(key, token, time.Duration(expireIn)*time.Second)
return token, nil
Expand Down Expand Up @@ -257,6 +258,11 @@ func (cli *Client) NewOperation() *operation.Operation {
return operation.NewOperation(cli.request, cli.conbineURI)
}

// openApi管理
func (cli *Client) NewOpenApi() *openapi.OpenApi {
return openapi.NewOpenApi(cli.request, cli.conbineURI)
}

// 小程序码
func (cli *Client) NewWXACode() *wxacode.WXACode {
return wxacode.NewWXACode(cli.request, cli.conbineURI)
Expand Down

0 comments on commit 6f916ee

Please sign in to comment.