- make sugarLogger baseapi globals.

This commit is contained in:
gazebo
2018-06-20 15:51:37 +08:00
parent 55df9f7a2f
commit 21773180a2
7 changed files with 49 additions and 55 deletions

View File

@@ -3,6 +3,7 @@ package mtpsapi
import (
"net/http"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
)
@@ -42,12 +43,12 @@ func (m *MTPSAPI) CheckRequestValidation(request *http.Request) (callbackRespons
request.ParseForm()
sign := m.signParams(request.PostForm)
if sign != request.FormValue(signKey) {
m.sugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, request.FormValue(signKey))
baseapi.SugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, request.FormValue(signKey))
return SignatureIsNotOk
}
for _, valueKey := range []string{"delivery_id", "mt_peisong_id", "order_id"} {
m.sugarLogger.Errorf("Missing mandatory param:%v", valueKey)
baseapi.SugarLogger.Errorf("Missing mandatory param:%v", valueKey)
if request.FormValue(valueKey) == "" {
return &MtpsCallbackResponse{
Code: -1,

View File

@@ -10,9 +10,9 @@ import (
"strings"
"time"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/platform/common"
"git.rosy.net.cn/baseapi/utils"
"go.uber.org/zap"
)
const (
@@ -122,18 +122,16 @@ type MtpsCreateOrderByShopInfo struct {
}
type MTPSAPI struct {
appKey string
secret string
sugarLogger *zap.SugaredLogger
client *http.Client
appKey string
secret string
client *http.Client
}
func NewMTPSAPI(appKey, secret string, sugarLogger *zap.SugaredLogger) *MTPSAPI {
func NewMTPSAPI(appKey, secret string) *MTPSAPI {
api := &MTPSAPI{
appKey: appKey,
secret: secret,
sugarLogger: sugarLogger,
client: &http.Client{Timeout: clientTimeout},
appKey: appKey,
secret: secret,
client: &http.Client{Timeout: clientTimeout},
}
return api
@@ -156,7 +154,7 @@ func (m *MTPSAPI) signParams(params url.Values) string {
}
}
// m.sugarLogger.Debug(finalStr)
// baseapi.SugarLogger.Debug(finalStr)
return fmt.Sprintf("%x", sha1.Sum([]byte(finalStr)))
}
@@ -173,29 +171,28 @@ func (m *MTPSAPI) AccessMTPS(action string, params map[string]interface{}) (retV
params2["timestamp"] = []string{utils.Int64ToStr(utils.GetCurTimestamp())}
params2["version"] = []string{"1.0"}
params2[signKey] = []string{m.signParams(params2)}
// m.sugarLogger.Debug(params2.Encode())
// baseapi.SugarLogger.Debug(params2.Encode())
request, _ := http.NewRequest("POST", mtpsAPIURL+"/"+action, strings.NewReader(params2.Encode()))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
apiAccess := &common.AccessPlatformAPIWithRetryParams{
MaxExceedLimitRetryCount: maxRetryCountWhenReachLimited,
MaxRecoverableRetryCount: maxRetryCountWhenNetworkException,
SleepSecondWhenExceedLimit: sleepSecondWhenLimited,
Client: m.client,
Request: request,
SugarLogger: m.sugarLogger,
Client: m.client,
Request: request,
}
err = common.AccessPlatformAPIWithRetry(apiAccess, func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HttpResponse2Json(response)
if err != nil {
m.sugarLogger.Warnf("HttpResponse2Json return:%v", err)
baseapi.SugarLogger.Warnf("HttpResponse2Json return:%v", err)
return common.PAErrorLevelGeneralFail, err
}
code := int(utils.MustInterface2Int64(jsonResult1["code"]))
retVal = &MTPSResult{
Code: code,
}
m.sugarLogger.Debug(jsonResult1)
baseapi.SugarLogger.Debug(jsonResult1)
if code == ResponseCodeSuccess {
if innerData, ok := jsonResult1["data"]; ok {
retVal.Data, _ = innerData.(map[string]interface{})
@@ -242,7 +239,7 @@ func (m *MTPSAPI) CreateOrderByShop(basicParams *MtpsCreateOrderByShopInfo, addP
delete(params, "expected_delivery_time")
}
if result, err := m.AccessMTPS("order/createByShop", params); err != nil {
m.sugarLogger.Debugf("result:%v", result)
baseapi.SugarLogger.Debugf("result:%v", result)
return nil, utils.NewErrorIntCode(err.Error(), result.Code)
} else {
return m.result2OrderResponse(result), nil
@@ -269,7 +266,7 @@ func (m *MTPSAPI) CancelOrder(deliveryId int64, mtPeiSongId string, cancelReason
"cancel_reason": cancelReason,
}
if result, err := m.AccessMTPS("order/delete", params); err != nil {
m.sugarLogger.Debugf("result:%v", result)
baseapi.SugarLogger.Debugf("result:%v", result)
return nil, err
} else {
return m.result2OrderResponse(result), nil