diff --git a/platformapi/qywxapi/cgibin.go b/platformapi/qywxapi/cgibin.go new file mode 100644 index 00000000..b0580562 --- /dev/null +++ b/platformapi/qywxapi/cgibin.go @@ -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 +} diff --git a/platformapi/qywxapi/cgibin_test.go b/platformapi/qywxapi/cgibin_test.go new file mode 100644 index 00000000..448bd48e --- /dev/null +++ b/platformapi/qywxapi/cgibin_test.go @@ -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) +} diff --git a/platformapi/qywxapi/qywxapi.go b/platformapi/qywxapi/qywxapi.go new file mode 100644 index 00000000..90e14d24 --- /dev/null +++ b/platformapi/qywxapi/qywxapi.go @@ -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 +} diff --git a/platformapi/qywxapi/qywxapi_test.go b/platformapi/qywxapi/qywxapi_test.go new file mode 100644 index 00000000..97de724c --- /dev/null +++ b/platformapi/qywxapi/qywxapi_test.go @@ -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", "") +}