jd decode
This commit is contained in:
@@ -3,10 +3,10 @@ package jdapi
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.rosy.net.cn/jx-callback/globals"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -285,10 +285,13 @@ func (a *API) AccessAPINoPage2(apiStr string, jdParams map[string]interface{}, k
|
|||||||
if jsonResult["code"].(string) != "0" {
|
if jsonResult["code"].(string) != "0" {
|
||||||
return nil, errors.New(jsonResult["msg"].(string))
|
return nil, errors.New(jsonResult["msg"].(string))
|
||||||
}
|
}
|
||||||
if jsonResult["data"] == nil || utils.IsNil(jsonResult["data"]) {
|
if (jsonResult["data"] == nil || utils.IsNil(jsonResult["data"])) && (jsonResult["encryptData"] != nil || utils.IsNil(jsonResult["encryptData"])) {
|
||||||
return nil, errors.New("data is nil")
|
decodeData, err := JDDecodeToData(a.appSecret, jsonResult["encryptData"].(string))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
jsonResult["data"] = decodeData
|
||||||
}
|
}
|
||||||
globals.SugarLogger.Debugf("====AccessAPINoPage2============%s", utils.Format4Output(jsonResult, false))
|
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
if err := utils.UnmarshalUseNumber([]byte(jsonResult["data"].(string)), &data); err != nil {
|
if err := utils.UnmarshalUseNumber([]byte(jsonResult["data"].(string)), &data); err != nil {
|
||||||
return nil, platformapi.ErrResponseDataFormatWrong
|
return nil, platformapi.ErrResponseDataFormatWrong
|
||||||
@@ -311,13 +314,14 @@ func genNormalHavePageResultParser(dataKey string) (handler PageResultParser) {
|
|||||||
var retVal []interface{}
|
var retVal []interface{}
|
||||||
|
|
||||||
tempResult := data[dataKey]
|
tempResult := data[dataKey]
|
||||||
|
var result0 = make(map[string]interface{}, 0)
|
||||||
if resultStr, ok := tempResult.(string); ok {
|
if resultStr, ok := tempResult.(string); ok {
|
||||||
if err := utils.UnmarshalUseNumber([]byte(resultStr), &tempResult); err != nil {
|
if err := utils.UnmarshalUseNumber([]byte(resultStr), &result0); err != nil {
|
||||||
return nil, 0, platformapi.ErrResponseDataFormatWrong
|
return nil, 0, platformapi.ErrResponseDataFormatWrong
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result = tempResult.(map[string]interface{})
|
result = result0
|
||||||
|
|
||||||
if totalCount == 0 {
|
if totalCount == 0 {
|
||||||
for _, totalCountKey := range havePageTotalCountKeys {
|
for _, totalCountKey := range havePageTotalCountKeys {
|
||||||
@@ -388,15 +392,15 @@ func (a *API) AccessAPIHavePage(apiStr string, jdParams map[string]interface{},
|
|||||||
localJdParams[KeyPageNo] = curPage
|
localJdParams[KeyPageNo] = curPage
|
||||||
localJdParams[KeyPageSize] = pageSize
|
localJdParams[KeyPageSize] = pageSize
|
||||||
jsonResult, err := a.AccessAPI(apiStr, localJdParams)
|
jsonResult, err := a.AccessAPI(apiStr, localJdParams)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, totalCount, err
|
return nil, totalCount, err
|
||||||
}
|
}
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
if err := utils.UnmarshalUseNumber([]byte(jsonResult["data"].(string)), &data); err != nil {
|
encryptData, err := JDDecodeToData(a.appSecret, jsonResult["encryptData"].(string))
|
||||||
|
|
||||||
|
if err := utils.UnmarshalUseNumber([]byte(encryptData), &data); err != nil {
|
||||||
return nil, totalCount, platformapi.ErrResponseDataFormatWrong
|
return nil, totalCount, platformapi.ErrResponseDataFormatWrong
|
||||||
}
|
}
|
||||||
|
|
||||||
innerCode := forceInnerCode2Str(data["code"])
|
innerCode := forceInnerCode2Str(data["code"])
|
||||||
if innerCode != "0" {
|
if innerCode != "0" {
|
||||||
err2 := utils.NewErrorCode(getErrMsgFromData(data), innerCode, 1)
|
err2 := utils.NewErrorCode(getErrMsgFromData(data), innerCode, 1)
|
||||||
@@ -460,3 +464,17 @@ func JavaDateHook(fromType reflect.Type, toType reflect.Type, data interface{})
|
|||||||
func JdMap2StructByJson(inObj interface{}, outObjAddr interface{}, weaklyTypedInput bool) (err error) {
|
func JdMap2StructByJson(inObj interface{}, outObjAddr interface{}, weaklyTypedInput bool) (err error) {
|
||||||
return utils.Map2Struct(inObj, outObjAddr, true, "", JavaDateHook)
|
return utils.Map2Struct(inObj, outObjAddr, true, "", JavaDateHook)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func JDDecodeToData(appSecret, data string) (string, error) {
|
||||||
|
key := appSecret[:16]
|
||||||
|
iv := appSecret[16:32]
|
||||||
|
sDec, err := base64.StdEncoding.DecodeString(data)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
resp, err := utils.AESCBCDecpryt(sDec, []byte(key), []byte(iv))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(resp), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package jdapi
|
package jdapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -23,7 +25,7 @@ func init() {
|
|||||||
// sandbox
|
// sandbox
|
||||||
// api = New("594ab45a-9a73-4a43-82b0-a64cbd55d883", "06692746f7224695ad4788ce340bc854", "d6b42a35a7414a5490d811654d745c84")
|
// api = New("594ab45a-9a73-4a43-82b0-a64cbd55d883", "06692746f7224695ad4788ce340bc854", "d6b42a35a7414a5490d811654d745c84")
|
||||||
// prod
|
// prod
|
||||||
api = New("73e2e9f6-b21e-4dcd-8c92-71e4e100b07e", "21b627c23ea04c69b64b48d0b361213e", "51cd27a748e64c829b4b7f83f4844610")
|
api = New("73e2e9f6-b21e-4dcd-8c92-71e4e100b07e", "1dba76d40cac446ca500c0391a0b6c9d", "a88d031a1e7b462cb1579f12e97fe7f4")
|
||||||
// 天天果园
|
// 天天果园
|
||||||
//api = New("c45e6510-00ba-4be2-977e-bcb9c9792cc7", "5d5577a2506f41b8b4ec520ba83490f5", "0b01b9eeb15b41dab1c3d05d95c17a26")
|
//api = New("c45e6510-00ba-4be2-977e-bcb9c9792cc7", "5d5577a2506f41b8b4ec520ba83490f5", "0b01b9eeb15b41dab1c3d05d95c17a26")
|
||||||
// 京东果园
|
// 京东果园
|
||||||
@@ -57,6 +59,18 @@ func TestAccessAPI(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGet(t *testing.T) {
|
||||||
|
key := "0bcbe9d6e6124cf2"
|
||||||
|
iv := "aef2856a540f1326"
|
||||||
|
decryDetail := "8FvHJcQmVojAIU61SNaS1ermHN2UVWknueRHFSNf2q5EbxNNmznoTYpRu7ySc/8CuU+QGZ9UIBMCyTuFafY3PuszEokEKc8M1Qfv/+o15h5bIU8LXfwRKOCm3JYzZtTOvJVU0hk/USvtDgraToszFl2hQZjZN5gGH1af0X8vopo="
|
||||||
|
sDec, err := base64.StdEncoding.DecodeString(decryDetail)
|
||||||
|
data, err := utils.AESCBCDecpryt(sDec, []byte(key), []byte(iv))
|
||||||
|
localJdParams := make(map[string]interface{})
|
||||||
|
ans, err := utils.Unmarshal2Map(data[:len(sDec)], localJdParams)
|
||||||
|
fmt.Println(ans["billId"])
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestAccessAPINoPage(t *testing.T) {
|
func TestAccessAPINoPage(t *testing.T) {
|
||||||
result, err := api.AccessAPINoPage("address/allcities", nil, []string{"yn"}, nil, nil)
|
result, err := api.AccessAPINoPage("address/allcities", nil, []string{"yn"}, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func TestPickUp(t *testing.T) {
|
|||||||
|
|
||||||
func TestOrderQuery(t *testing.T) {
|
func TestOrderQuery(t *testing.T) {
|
||||||
jdParams := map[string]interface{}{
|
jdParams := map[string]interface{}{
|
||||||
"orderId": "813344594000041",
|
"orderId": "2225735125000294",
|
||||||
}
|
}
|
||||||
result, totalCount, err := api.OrderQuery(jdParams)
|
result, totalCount, err := api.OrderQuery(jdParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -31,30 +31,30 @@ func TestDelVipPrice(t *testing.T) {
|
|||||||
// t.Log(utils.Format4Output(result, false))
|
// t.Log(utils.Format4Output(result, false))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryOpenUseable(t *testing.T) {
|
//func TestQueryOpenUseable(t *testing.T) {
|
||||||
result, err := api.QueryOpenUseable([]*BaseStockCenterRequest{
|
// result, err := api.QueryOpenUseable([]*BaseStockCenterRequest{
|
||||||
&BaseStockCenterRequest{
|
// &BaseStockCenterRequest{
|
||||||
StationNo: mustExistStoreID,
|
// StationNo: mustExistStoreID,
|
||||||
SkuId: mustExistSkuID,
|
// SkuId: mustExistSkuID,
|
||||||
},
|
// },
|
||||||
})
|
// })
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
t.Log(utils.Format4Output(result, false))
|
// t.Log(utils.Format4Output(result, false))
|
||||||
}
|
//}
|
||||||
|
|
||||||
func TestQueryStockCenter(t *testing.T) {
|
//func TestQueryStockCenter(t *testing.T) {
|
||||||
result, err := api.QueryStockCenter(mustExistStoreJXID, []*SkuIdEntity{
|
// result, err := api.QueryStockCenter(mustExistStoreJXID, []*SkuIdEntity{
|
||||||
&SkuIdEntity{
|
// &SkuIdEntity{
|
||||||
OutSkuId: mustExistSkuJXID,
|
// OutSkuId: mustExistSkuJXID,
|
||||||
},
|
// },
|
||||||
}, "test")
|
// }, "test")
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
t.Log(utils.Format4Output(result, false))
|
// t.Log(utils.Format4Output(result, false))
|
||||||
}
|
//}
|
||||||
|
|
||||||
func TestBatchUpdateVendibility(t *testing.T) {
|
func TestBatchUpdateVendibility(t *testing.T) {
|
||||||
result, err := api.BatchUpdateVendibility("", "100130", "", []*StockVendibility{
|
result, err := api.BatchUpdateVendibility("", "100130", "", []*StockVendibility{
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -27,6 +29,35 @@ func NewErrorCode(errMsg, code string, level ...int) *ErrorWithCode {
|
|||||||
return retVal
|
return retVal
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DecryptDESECB(d, key []byte) string {
|
||||||
|
data, err := base64.StdEncoding.DecodeString(string(d))
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
block, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
bs := block.BlockSize()
|
||||||
|
if len(data)%bs != 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
out := make([]byte, len(data))
|
||||||
|
dst := out
|
||||||
|
for len(data) > 0 {
|
||||||
|
block.Decrypt(dst, data[:bs])
|
||||||
|
data = data[bs:]
|
||||||
|
dst = dst[bs:]
|
||||||
|
}
|
||||||
|
out = PKCS5UnPadding(out)
|
||||||
|
return string(out)
|
||||||
|
}
|
||||||
|
func PKCS5UnPadding(origData []byte) []byte {
|
||||||
|
length := len(origData)
|
||||||
|
unpadding := int(origData[length-1])
|
||||||
|
return origData[:(length - unpadding)]
|
||||||
|
}
|
||||||
|
|
||||||
func NewErrorIntCode(errMsg string, code int, level ...int) *ErrorWithCode {
|
func NewErrorIntCode(errMsg string, code int, level ...int) *ErrorWithCode {
|
||||||
return NewErrorCode(errMsg, Int2Str(code), level...)
|
return NewErrorCode(errMsg, Int2Str(code), level...)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AESCBCEncpryt(data, aesKey, iv []byte) (encryptedData []byte, err error) {
|
func AESCBCEncpryt(data, aesKey, iv []byte) (encryptedData []byte, err error) {
|
||||||
@@ -23,9 +24,11 @@ func AESCBCDecpryt(encryptedData, aesKey, iv []byte) (decryptedData []byte, err
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
fmt.Println(c.BlockSize(), len(encryptedData))
|
||||||
cfbdec := cipher.NewCBCDecrypter(c, iv[:c.BlockSize()])
|
cfbdec := cipher.NewCBCDecrypter(c, iv[:c.BlockSize()])
|
||||||
decryptedData = make([]byte, len(encryptedData))
|
decryptedData = make([]byte, len(encryptedData))
|
||||||
cfbdec.CryptBlocks(decryptedData, encryptedData)
|
cfbdec.CryptBlocks(decryptedData, encryptedData)
|
||||||
|
|
||||||
decryptedData = PKCSUnPadding(decryptedData)
|
decryptedData = PKCSUnPadding(decryptedData)
|
||||||
return decryptedData, nil
|
return decryptedData, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user