jd decode

This commit is contained in:
richboo111
2022-10-26 14:33:29 +08:00
parent bfe333c010
commit c37c869c33
6 changed files with 100 additions and 34 deletions

View File

@@ -3,10 +3,10 @@ package jdapi
import (
"bytes"
"crypto/md5"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"git.rosy.net.cn/jx-callback/globals"
"net/http"
"reflect"
"sort"
@@ -285,10 +285,13 @@ func (a *API) AccessAPINoPage2(apiStr string, jdParams map[string]interface{}, k
if jsonResult["code"].(string) != "0" {
return nil, errors.New(jsonResult["msg"].(string))
}
if jsonResult["data"] == nil || utils.IsNil(jsonResult["data"]) {
return nil, errors.New("data is nil")
if (jsonResult["data"] == nil || utils.IsNil(jsonResult["data"])) && (jsonResult["encryptData"] != nil || utils.IsNil(jsonResult["encryptData"])) {
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{}
if err := utils.UnmarshalUseNumber([]byte(jsonResult["data"].(string)), &data); err != nil {
return nil, platformapi.ErrResponseDataFormatWrong
@@ -311,13 +314,14 @@ func genNormalHavePageResultParser(dataKey string) (handler PageResultParser) {
var retVal []interface{}
tempResult := data[dataKey]
var result0 = make(map[string]interface{}, 0)
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
}
}
result = tempResult.(map[string]interface{})
result = result0
if totalCount == 0 {
for _, totalCountKey := range havePageTotalCountKeys {
@@ -388,15 +392,15 @@ func (a *API) AccessAPIHavePage(apiStr string, jdParams map[string]interface{},
localJdParams[KeyPageNo] = curPage
localJdParams[KeyPageSize] = pageSize
jsonResult, err := a.AccessAPI(apiStr, localJdParams)
if err != nil {
return nil, totalCount, err
}
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
}
innerCode := forceInnerCode2Str(data["code"])
if innerCode != "0" {
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) {
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
}

View File

@@ -1,6 +1,8 @@
package jdapi
import (
"encoding/base64"
"fmt"
"net/http"
"testing"
@@ -23,7 +25,7 @@ func init() {
// sandbox
// api = New("594ab45a-9a73-4a43-82b0-a64cbd55d883", "06692746f7224695ad4788ce340bc854", "d6b42a35a7414a5490d811654d745c84")
// 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")
// 京东果园
@@ -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) {
result, err := api.AccessAPINoPage("address/allcities", nil, []string{"yn"}, nil, nil)
if err != nil {

View File

@@ -30,7 +30,7 @@ func TestPickUp(t *testing.T) {
func TestOrderQuery(t *testing.T) {
jdParams := map[string]interface{}{
"orderId": "813344594000041",
"orderId": "2225735125000294",
}
result, totalCount, err := api.OrderQuery(jdParams)
if err != nil {

View File

@@ -31,30 +31,30 @@ func TestDelVipPrice(t *testing.T) {
// t.Log(utils.Format4Output(result, false))
}
func TestQueryOpenUseable(t *testing.T) {
result, err := api.QueryOpenUseable([]*BaseStockCenterRequest{
&BaseStockCenterRequest{
StationNo: mustExistStoreID,
SkuId: mustExistSkuID,
},
})
if err != nil {
t.Fatal(err)
}
t.Log(utils.Format4Output(result, false))
}
//func TestQueryOpenUseable(t *testing.T) {
// result, err := api.QueryOpenUseable([]*BaseStockCenterRequest{
// &BaseStockCenterRequest{
// StationNo: mustExistStoreID,
// SkuId: mustExistSkuID,
// },
// })
// if err != nil {
// t.Fatal(err)
// }
// t.Log(utils.Format4Output(result, false))
//}
func TestQueryStockCenter(t *testing.T) {
result, err := api.QueryStockCenter(mustExistStoreJXID, []*SkuIdEntity{
&SkuIdEntity{
OutSkuId: mustExistSkuJXID,
},
}, "test")
if err != nil {
t.Fatal(err)
}
t.Log(utils.Format4Output(result, false))
}
//func TestQueryStockCenter(t *testing.T) {
// result, err := api.QueryStockCenter(mustExistStoreJXID, []*SkuIdEntity{
// &SkuIdEntity{
// OutSkuId: mustExistSkuJXID,
// },
// }, "test")
// if err != nil {
// t.Fatal(err)
// }
// t.Log(utils.Format4Output(result, false))
//}
func TestBatchUpdateVendibility(t *testing.T) {
result, err := api.BatchUpdateVendibility("", "100130", "", []*StockVendibility{

View File

@@ -1,6 +1,8 @@
package utils
import (
"crypto/aes"
"encoding/base64"
"fmt"
"strconv"
"strings"
@@ -27,6 +29,35 @@ func NewErrorCode(errMsg, code string, level ...int) *ErrorWithCode {
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 {
return NewErrorCode(errMsg, Int2Str(code), level...)
}

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"fmt"
)
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 {
return nil, err
}
fmt.Println(c.BlockSize(), len(encryptedData))
cfbdec := cipher.NewCBCDecrypter(c, iv[:c.BlockSize()])
decryptedData = make([]byte, len(encryptedData))
cfbdec.CryptBlocks(decryptedData, encryptedData)
decryptedData = PKCSUnPadding(decryptedData)
return decryptedData, nil
}