fengniao ceshi
This commit is contained in:
@@ -7,8 +7,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TokenURL = "https://open-anubis.ele.me/anubis-webapi/openapi/token" // 正式环境
|
TokenURL = "https://open-anubis.ele.me/anubis-webapi/openapi/token" // 正式环境
|
||||||
ApiURL = "https://open-anubis.ele.me/anubis-webapi/v3/invoke" // 正式环境
|
ApiURL = "https://open-anubis.ele.me/anubis-webapi/v3/invoke" // 正式环境
|
||||||
|
RefreshTokenUrl = "https://open-anubis.ele.me/anubis-webapi/openapi/refreshToken" // 正式环境刷新token
|
||||||
//TokenURL = "https://exam-anubis.ele.me/anubis-webapi/openapi/token" // 沙箱环境
|
//TokenURL = "https://exam-anubis.ele.me/anubis-webapi/openapi/token" // 沙箱环境
|
||||||
//ApiURL = "https://exam-anubis.ele.me/anubis-webapi/v3/invoke" // 沙箱环境
|
//ApiURL = "https://exam-anubis.ele.me/anubis-webapi/v3/invoke" // 沙箱环境
|
||||||
RequestPost = "POST"
|
RequestPost = "POST"
|
||||||
@@ -17,18 +18,19 @@ const (
|
|||||||
|
|
||||||
// 注册请求api
|
// 注册请求api
|
||||||
type API struct {
|
type API struct {
|
||||||
grantType string `json:"grant_type"`
|
grantType string `json:"grant_type"`
|
||||||
code string `json:"code"`
|
code string `json:"code"`
|
||||||
appID string `json:"app_id"`
|
appID string `json:"app_id"`
|
||||||
merchantId string `json:"merchant_id"`
|
merchantId string `json:"merchant_id"`
|
||||||
signature string `json:"signature"`
|
signature string `json:"signature"`
|
||||||
timestamp int64 `json:"timestamp"`
|
timestamp int64 `json:"timestamp"`
|
||||||
accessToken string `json:"access_token"`
|
accessToken string `json:"access_token"`
|
||||||
appSecret string `json:"app_secret"`
|
refreshToken string `json:"refresh_token"`
|
||||||
version string `json:"version"`
|
appSecret string `json:"app_secret"`
|
||||||
locker sync.RWMutex
|
version string `json:"version"`
|
||||||
client *http.Client
|
locker sync.RWMutex
|
||||||
config *platformapi.APIConfig
|
client *http.Client
|
||||||
|
config *platformapi.APIConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// 请求基础结构体
|
// 请求基础结构体
|
||||||
|
|||||||
@@ -30,6 +30,12 @@ func (a *API) SetToken(token string) {
|
|||||||
a.accessToken = token
|
a.accessToken = token
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *API) SetRefreshToken(token string) {
|
||||||
|
a.locker.Lock()
|
||||||
|
defer a.locker.Unlock()
|
||||||
|
a.refreshToken = token
|
||||||
|
}
|
||||||
|
|
||||||
func (a *API) MakeFnRequestHead() map[string]interface{} {
|
func (a *API) MakeFnRequestHead() map[string]interface{} {
|
||||||
requestParam := make(map[string]interface{}, 6)
|
requestParam := make(map[string]interface{}, 6)
|
||||||
requestParam["access_token"] = a.accessToken
|
requestParam["access_token"] = a.accessToken
|
||||||
@@ -45,18 +51,21 @@ func New(appID, appSecret, merchantId, code string, config ...*platformapi.APICo
|
|||||||
if len(config) > 0 {
|
if len(config) > 0 {
|
||||||
curConfig = *config[0]
|
curConfig = *config[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 查询蜂鸟refeshToken
|
||||||
return &API{
|
return &API{
|
||||||
grantType: "authorization_code", // 授权模式,填固定值authorization_code
|
grantType: "authorization_code", // 授权模式,填固定值authorization_code
|
||||||
code: code,
|
code: code,
|
||||||
appID: appID,
|
appID: appID,
|
||||||
merchantId: merchantId,
|
merchantId: merchantId,
|
||||||
signature: "",
|
signature: "",
|
||||||
accessToken: "",
|
accessToken: "",
|
||||||
version: "1.0",
|
refreshToken: "",
|
||||||
appSecret: appSecret,
|
version: "1.0",
|
||||||
locker: sync.RWMutex{},
|
appSecret: appSecret,
|
||||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
locker: sync.RWMutex{},
|
||||||
config: &curConfig,
|
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||||
|
config: &curConfig,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,14 +134,22 @@ func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]
|
|||||||
func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) {
|
func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) {
|
||||||
parameter := make(map[string]interface{}, 6)
|
parameter := make(map[string]interface{}, 6)
|
||||||
parameter["grant_type"] = a.grantType
|
parameter["grant_type"] = a.grantType
|
||||||
parameter["code"] = a.code
|
|
||||||
parameter["app_id"] = a.appID
|
parameter["app_id"] = a.appID
|
||||||
parameter["merchant_id"] = a.merchantId
|
parameter["merchant_id"] = a.merchantId
|
||||||
|
|
||||||
result, err := a.AccessAPI(TokenURL, "", RequestPost, parameter)
|
// 先去刷新token,没有的话再去获取token(code只能使用一次,生成的token管一年)
|
||||||
if err != nil {
|
var result map[string]interface{}
|
||||||
return nil, err
|
parameter["refresh_token"] = a.refreshToken
|
||||||
|
if a.refreshToken == "" {
|
||||||
|
parameter["code"] = a.code
|
||||||
|
result, err = a.AccessAPI(TokenURL, "", RequestPost, parameter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result, err = a.AccessAPI(RefreshTokenUrl, "", RequestPost, parameter)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := utils.Map2StructByJson(result, &tokenInfo, false); err != nil {
|
if err := utils.Map2StructByJson(result, &tokenInfo, false); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,14 +13,6 @@ func Init() {
|
|||||||
api.accessToken = token.BusinessDataObj.AccessToken
|
api.accessToken = token.BusinessDataObj.AccessToken
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClient(t *testing.T) {
|
|
||||||
api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "5375691", "iCN2gIftF1Ia9nG7720nyn")
|
|
||||||
token, err := api.GetAccessToken()
|
|
||||||
api.accessToken = token.BusinessDataObj.AccessToken
|
|
||||||
fmt.Println("token===", token.BusinessDataObj.AccessToken)
|
|
||||||
fmt.Println("err=====", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建门店,
|
// 创建门店,
|
||||||
func TestCreateStore(t *testing.T) {
|
func TestCreateStore(t *testing.T) {
|
||||||
api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "5375691", "cabrXQf9eFMVWVYg4hNlwu")
|
api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "5375691", "cabrXQf9eFMVWVYg4hNlwu")
|
||||||
@@ -91,11 +83,21 @@ func TestUpdataStore(t *testing.T) {
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestClient(t *testing.T) {
|
||||||
|
api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "51658", "4W4hqacKND6NOct5gCyjbT")
|
||||||
|
token, err := api.GetAccessToken()
|
||||||
|
api.accessToken = token.BusinessDataObj.AccessToken
|
||||||
|
fmt.Println("token===", token.BusinessDataObj.AccessToken)
|
||||||
|
fmt.Println("err=====", err)
|
||||||
|
}
|
||||||
|
|
||||||
// 查询单个门店
|
// 查询单个门店
|
||||||
func TestQueryOneStore(t *testing.T) {
|
func TestQueryOneStore(t *testing.T) {
|
||||||
api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "5375691", "iCN2gIftF1Ia9nG7720nyn")
|
api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "51658", "4W4hqacKND6NOct5gCyjbT")
|
||||||
token, _ := api.GetAccessToken()
|
token, err := api.GetAccessToken()
|
||||||
|
fmt.Println("err! ========", err)
|
||||||
api.accessToken = token.BusinessDataObj.AccessToken
|
api.accessToken = token.BusinessDataObj.AccessToken
|
||||||
|
fmt.Println("err! ========accessToken", api.accessToken)
|
||||||
data, err := api.GetStore("637910")
|
data, err := api.GetStore("637910")
|
||||||
fmt.Println(data)
|
fmt.Println(data)
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
//TokenURL = "https://open-anubis.ele.me/anubis-webapi/openapi/token" // 正式环境
|
TokenURL = "https://open-anubis.ele.me/anubis-webapi/openapi/token" // 正式环境
|
||||||
//ApiURL = "https://open-anubis.ele.me/anubis-webapi/v3/invoke"// 正式环境
|
RefreshTokenUrl = "https://open-anubis.ele.me/anubis-webapi/openapi/refreshToken" // 正式环境刷新token
|
||||||
TokenURL = "https://exam-anubis.ele.me/anubis-webapi/openapi/token" // 沙箱环境
|
ApiURL = "https://open-anubis.ele.me/anubis-webapi/v3/invoke" // 正式环境
|
||||||
ApiURL = "https://exam-anubis.ele.me/anubis-webapi/v3/invoke" // 沙箱环境
|
//TokenURL = "https://exam-anubis.ele.me/anubis-webapi/openapi/token" // 沙箱环境
|
||||||
|
//ApiURL = "https://exam-anubis.ele.me/anubis-webapi/v3/invoke" // 沙箱环境
|
||||||
RequestPost = "POST"
|
RequestPost = "POST"
|
||||||
RequestGet = "GET"
|
RequestGet = "GET"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -71,15 +71,23 @@ func (a *API) signParam(params map[string]interface{}) (sig string) {
|
|||||||
// 获取access_token
|
// 获取access_token
|
||||||
func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) {
|
func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) {
|
||||||
parameter := make(map[string]interface{}, 6)
|
parameter := make(map[string]interface{}, 6)
|
||||||
parameter["grant_type"] = a.grantType
|
parameter["grant_type"] = "refresh_token"
|
||||||
parameter["code"] = a.code
|
|
||||||
parameter["app_id"] = a.appID
|
parameter["app_id"] = a.appID
|
||||||
parameter["merchant_id"] = a.merchantId
|
parameter["merchant_id"] = a.merchantId
|
||||||
|
parameter["refresh_token"] = "1"
|
||||||
|
|
||||||
result, err := a.AccessAPI(TokenURL, "", RequestPost, parameter)
|
// 先去刷新token,没有的话再去获取token(code只能使用一次,生成的token管一年)
|
||||||
|
var result map[string]interface{}
|
||||||
|
result, err = a.AccessAPI(RefreshTokenUrl, "", RequestPost, parameter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
parameter["grant_type"] = "authorization_code"
|
||||||
|
parameter["code"] = a.code
|
||||||
|
result, err = a.AccessAPI(TokenURL, "", RequestPost, parameter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := utils.Map2StructByJson(result, &tokenInfo, false); err != nil {
|
if err := utils.Map2StructByJson(result, &tokenInfo, false); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -89,6 +97,7 @@ func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tokenInfo.BusinessDataObj = businessData
|
tokenInfo.BusinessDataObj = businessData
|
||||||
|
//a.r
|
||||||
return tokenInfo, err
|
return tokenInfo, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user