aa
This commit is contained in:
16
platformapi/qywxapi/cgibin.go
Normal file
16
platformapi/qywxapi/cgibin.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package qywxapi
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
|
func (a *API) GetProviderToken() (token string, err error) {
|
||||||
|
params := map[string]interface{}{
|
||||||
|
"corpid": a.appID,
|
||||||
|
"provider_secret": a.secret,
|
||||||
|
}
|
||||||
|
str, _ := json.Marshal(params)
|
||||||
|
result, err := a.AccessAPI(tokenURL, string(str), true)
|
||||||
|
if result["provider_access_token"] != "" {
|
||||||
|
return result["provider_access_token"].(string), err
|
||||||
|
}
|
||||||
|
return token, err
|
||||||
|
}
|
||||||
11
platformapi/qywxapi/cgibin_test.go
Normal file
11
platformapi/qywxapi/cgibin_test.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package qywxapi
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestGetProviderToken(t *testing.T) {
|
||||||
|
result, err := api.GetProviderToken()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err.Error())
|
||||||
|
}
|
||||||
|
sugarLogger.Debug(result)
|
||||||
|
}
|
||||||
59
platformapi/qywxapi/qywxapi.go
Normal file
59
platformapi/qywxapi/qywxapi.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
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) 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
|
||||||
|
}
|
||||||
19
platformapi/qywxapi/qywxapi_test.go
Normal file
19
platformapi/qywxapi/qywxapi_test.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package qywxapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.rosy.net.cn/baseapi"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
api *API
|
||||||
|
sugarLogger *zap.SugaredLogger
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
logger, _ := zap.NewDevelopment()
|
||||||
|
sugarLogger = logger.Sugar()
|
||||||
|
baseapi.Init(sugarLogger)
|
||||||
|
|
||||||
|
api = New("ww9a156bfa070e1857", "VlOJSlXw6TJRzYaUax-lIY8smcCIvfDe-ZZoIsYu7vfRYGIdhfs3UQCmB0papgk9", "")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user