- use Must func when possible.

This commit is contained in:
gazebo
2018-06-14 09:54:19 +08:00
parent 5f6865666a
commit 9696e142ee
5 changed files with 76 additions and 12 deletions

View File

@@ -0,0 +1,25 @@
package elmapi
const (
OrderEffective = 10
MerchantValid = 12
OrderCanceled = 14
MerchantInvalid = 15
OrderForceInvalid = 17
OrderFinished = 18
)
type ELMCallbackResponse struct {
Message string `json:"message"`
}
type ELMCallbackMsg struct {
AppId int `json:"appId"`
RequestId string `json:"requestId"`
Type int `json:"type"`
Message string `json:"message"`
ShopId int `json:"shopId"`
Timestamp int64 `json:"timestamp"`
UserId int64 `json:"userId"`
Signature string `json:"signature"`
}

View File

@@ -2,7 +2,6 @@ package elmapi
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
@@ -37,7 +36,7 @@ var (
type ELMResult struct {
Id string
Result map[string]interface{}
Result interface{}
Error map[string]interface{}
}
@@ -85,7 +84,7 @@ func (e *ELMAPI) signParams(action string, payload *ELMPayload) string {
}
for _, data := range allData {
for k, v := range data {
vBytes, _ := json.Marshal(v)
vBytes := utils.MustMarshal(v)
keyValues = append(keyValues, k+"="+string(vBytes))
}
}
@@ -102,7 +101,7 @@ func (e *ELMAPI) AccessELM(action string, params map[string]interface{}) (*ELMRe
}
metas := map[string]interface{}{
"app_key": e.appKey,
"timestamp": int(utils.GetCurTimestamp()),
"timestamp": utils.GetCurTimestamp(),
}
payload := &ELMPayload{
@@ -115,11 +114,8 @@ func (e *ELMAPI) AccessELM(action string, params map[string]interface{}) (*ELMRe
}
payload.Signature = e.signParams(action, payload)
dataBytes, err := json.Marshal(payload)
if err != nil {
e.sugarLogger.Errorf("Error when marshal %v, error:%v", payload, err)
return nil, err
}
dataBytes := utils.MustMarshal(payload)
dataStr := string(dataBytes)
exceedLimitRetryCount := 0
systemErrorRetryCount := 0
@@ -162,11 +158,10 @@ func (e *ELMAPI) AccessELM(action string, params map[string]interface{}) (*ELMRe
jsonResult1, err := utils.HttpResponse2Json(response)
resultError, _ := jsonResult1["error"].(map[string]interface{})
result, _ := jsonResult1["result"].(map[string]interface{})
jsonResult := &ELMResult{
Id: jsonResult1["id"].(string),
Error: resultError,
Result: result,
Result: jsonResult1["result"],
}
if err != nil {
e.sugarLogger.Warnf("HttpResponse2Json return:%v", err)

View File

@@ -28,10 +28,23 @@ func TestAccessELM(t *testing.T) {
if err != nil {
t.Fatalf("Error when accessing AccessJDQuery result:%v, error:%v", result, err)
} else {
userIdNumber := result.Result["userId"].(json.Number)
mapResult := result.Result.(map[string]interface{})
userIdNumber := mapResult["userId"].(json.Number)
userId, err := userIdNumber.Int64()
if userId != 336072266326420104 || err != nil {
t.Fatalf("userId is not correct:%v", userIdNumber)
}
}
}
func TestGetOrder(t *testing.T) {
result, err := elmapi.GetOrder("3023582487561731159")
if err != nil {
t.Fatalf("Error when accessing AccessJDQuery result:%v, error:%v", result, err)
} else {
shopId := int(utils.MustInterface2Int64(result["shopId"]))
if shopId != 157451615 {
t.Fatalf("userId is not correct:%v", shopId)
}
}
}

15
platform/elmapi/order.go Normal file
View File

@@ -0,0 +1,15 @@
package elmapi
type ELMOrderInfo map[string]interface{}
func (e *ELMAPI) GetOrder(orderId string) (ELMOrderInfo, error) {
result, err := e.AccessELM("eleme.order.getOrder", map[string]interface{}{
"orderId": orderId,
})
if err == nil {
innerResult := ELMOrderInfo(result.Result.(map[string]interface{}))
return innerResult, nil
}
return nil, err
}