96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
package kuaishou_mini
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"git.rosy.net.cn/baseapi/platformapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
const (
|
|
// KuaiShouBashUrl 基础域名
|
|
KuaiShouBashUrl = "https://open.kuaishou.com" // 域名
|
|
|
|
KuaiShouAuthLogin = KuaiShouBashUrl + "/oauth2/mp/code2session" // 授权登录
|
|
KuaiShouGetToken = KuaiShouBashUrl + "/oauth2/access_token" // 获取授权token
|
|
KuaiShouPreCreateOrder = KuaiShouBashUrl + "/openapi/mp/developer/epay/create_order" // 预下单接口
|
|
)
|
|
|
|
type API struct {
|
|
appSecret string // 应用唯一标识对应的密钥
|
|
appId string // 应用唯一标识
|
|
client *http.Client
|
|
config *platformapi.APIConfig
|
|
locker sync.RWMutex
|
|
accessToken string // accessToken
|
|
expiresIn int64 // 过期时间
|
|
}
|
|
|
|
func (a *API) GetAppID() string {
|
|
return a.appId
|
|
}
|
|
|
|
func (a *API) GetSecret() string {
|
|
return a.appSecret
|
|
}
|
|
|
|
// New 初始化
|
|
func New(appSecret, appId string, config ...*platformapi.APIConfig) *API {
|
|
curConfig := platformapi.DefAPIConfig
|
|
if len(config) > 0 {
|
|
curConfig = *config[0]
|
|
}
|
|
return &API{
|
|
appSecret: appSecret,
|
|
appId: appId,
|
|
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
|
config: &curConfig,
|
|
}
|
|
}
|
|
|
|
// GetToken 获取快手token
|
|
func (a *API) GetToken() error {
|
|
if a.appId == "" || a.appSecret == "" {
|
|
return errors.New("快手appId为空或appSecret为空")
|
|
}
|
|
|
|
result, err := a.AccessAPI2(KuaiShouGetToken, map[string]interface{}{"app_id": a.appId, "app_secret": a.appSecret, "grant_type": "client_credentials"})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var data *GetAutoTokenRes
|
|
if err := utils.Map2StructByJson(result, &data, false); err != nil {
|
|
return err
|
|
}
|
|
|
|
if data.Result != 1 {
|
|
return errors.New(utils.Int64ToStr(data.Result))
|
|
}
|
|
a.expiresIn = data.ExpiresIn
|
|
a.accessToken = data.AccessToken
|
|
return nil
|
|
}
|
|
|
|
// AccessAPI2 发送请求
|
|
func (a *API) AccessAPI2(url string, params map[string]interface{}) (retVal map[string]interface{}, err error) {
|
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
|
func() *http.Request {
|
|
request, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(utils.Map2URLValues(params).Encode()))
|
|
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
return request
|
|
},
|
|
a.config,
|
|
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
|
if jsonResult1 == nil {
|
|
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
|
}
|
|
retVal = jsonResult1
|
|
return platformapi.ErrLevelSuccess, nil
|
|
})
|
|
return retVal, err
|
|
}
|