+ APICookie

This commit is contained in:
gazebo
2019-07-24 16:18:11 +08:00
parent 74c07ba61e
commit 32b53c9c9e
14 changed files with 168 additions and 121 deletions

View File

@@ -156,6 +156,8 @@ type GoodsDetail struct {
}
type API struct {
platformapi.APICookie
appKey string
secret string
client *http.Client
@@ -196,22 +198,30 @@ func (a *API) signParams(params url.Values) string {
return fmt.Sprintf("%x", sha1.Sum([]byte(finalStr)))
}
func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *ResponseResult, err error) {
func (a *API) AccessAPI2(baseURL, action string, params map[string]interface{}) (retVal *ResponseResult, err error) {
if params == nil {
panic("params is nil!")
}
params2 := utils.Map2URLValues(params)
params2.Set("appkey", a.appKey)
params2.Set("timestamp", utils.Int64ToStr(utils.GetCurTimestamp()))
params2.Set("version", "1.0")
params2.Set(signKey, a.signParams(params2))
if baseURL == mtpsAPIURL {
params2.Set("appkey", a.appKey)
params2.Set("timestamp", utils.Int64ToStr(utils.GetCurTimestamp()))
params2.Set("version", "1.0")
params2.Set(signKey, a.signParams(params2))
} else if a.GetCookieCount() == 0 {
return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法")
}
// baseapi.SugarLogger.Debug(params2.Encode())
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
request, _ := http.NewRequest(http.MethodPost, mtpsAPIURL+"/"+action, strings.NewReader(params2.Encode()))
request, _ := http.NewRequest(http.MethodPost, baseURL+"/"+action, strings.NewReader(params2.Encode()))
request.Header.Set("charset", "UTF-8")
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if baseURL != mtpsAPIURL {
a.FillRequestCookies(request)
}
return request
},
a.config,
@@ -236,6 +246,10 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R
return retVal, err
}
func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *ResponseResult, err error) {
return a.AccessAPI2(mtpsAPIURL, action, params)
}
func (a *API) result2OrderResponse(result *ResponseResult) (order *OrderResponse) {
order = new(OrderResponse)
order.MtPeisongID = result.Data["mt_peisong_id"].(string)