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

View File

@@ -28,10 +28,23 @@ func TestAccessELM(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Error when accessing AccessJDQuery result:%v, error:%v", result, err) t.Fatalf("Error when accessing AccessJDQuery result:%v, error:%v", result, err)
} else { } else {
userIdNumber := result.Result["userId"].(json.Number) mapResult := result.Result.(map[string]interface{})
userIdNumber := mapResult["userId"].(json.Number)
userId, err := userIdNumber.Int64() userId, err := userIdNumber.Int64()
if userId != 336072266326420104 || err != nil { if userId != 336072266326420104 || err != nil {
t.Fatalf("userId is not correct:%v", userIdNumber) 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
}

View File

@@ -144,6 +144,10 @@ func GetAPIOperator() string {
return time.Now().Format("2006-01-02_15:04:05") return time.Now().Format("2006-01-02_15:04:05")
} }
func Timestamp2Str(timestamp int64) string {
return time.Unix(timestamp/1000, 0).Format("2006-01-02 15:04:05")
}
func HttpResponse2Json(response *http.Response) (map[string]interface{}, error) { func HttpResponse2Json(response *http.Response) (map[string]interface{}, error) {
var jsonResult map[string]interface{} var jsonResult map[string]interface{}
bodyData, err := ioutil.ReadAll(response.Body) bodyData, err := ioutil.ReadAll(response.Body)
@@ -157,3 +161,15 @@ func HttpResponse2Json(response *http.Response) (map[string]interface{}, error)
} }
return jsonResult, nil return jsonResult, nil
} }
func MustInterface2Int64(data interface{}) int64 {
dataNumber, ok := data.(json.Number)
if !ok {
panic(fmt.Sprintf("error when convert:%v", data))
}
retVal, err := dataNumber.Int64()
if err != nil {
panic(err.Error())
}
return retVal
}