64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package qywxapi
|
|
|
|
import (
|
|
"git.rosy.net.cn/baseapi/platformapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
prodURL = "https://qyapi.weixin.qq.com"
|
|
|
|
tokenURL = "cgi-bin/service/get_provider_token"
|
|
)
|
|
|
|
type API struct {
|
|
token string
|
|
appID string
|
|
secret string
|
|
client *http.Client
|
|
config *platformapi.APIConfig
|
|
}
|
|
|
|
func New(appID, secret, token string, config ...*platformapi.APIConfig) *API {
|
|
curConfig := platformapi.DefAPIConfig
|
|
if len(config) > 0 {
|
|
curConfig = *config[0]
|
|
}
|
|
return &API{
|
|
token: token,
|
|
appID: appID,
|
|
secret: secret,
|
|
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
|
config: &curConfig,
|
|
}
|
|
}
|
|
|
|
func (a *API) SetToken(token string) {
|
|
a.token = token
|
|
}
|
|
|
|
func (a *API) AccessAPI(action string, body string, isPost bool) (retVal map[string]interface{}, err error) {
|
|
fullURL := utils.GenerateGetURL(prodURL, action, nil)
|
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
|
func() *http.Request {
|
|
var request *http.Request
|
|
if !isPost {
|
|
request, _ = http.NewRequest(http.MethodGet, fullURL, strings.NewReader(body))
|
|
} else {
|
|
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(body))
|
|
}
|
|
return request
|
|
},
|
|
a.config,
|
|
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
|
retVal = jsonResult1
|
|
if retVal != nil {
|
|
return platformapi.ErrLevelSuccess, nil
|
|
}
|
|
return platformapi.ErrLevelCodeIsNotOK, err
|
|
})
|
|
return retVal, err
|
|
}
|