jcq消费消息
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
package jdshopapi
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
//订单出库
|
||||
@@ -53,3 +59,106 @@ func (a *API) GetOrder(orderID int64) (err error) {
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
type VoucherInfoGetResult struct {
|
||||
Sig string `json:"sig"`
|
||||
Data struct {
|
||||
Act string `json:"act"`
|
||||
Effective int64 `json:"effective"`
|
||||
Expired int64 `json:"expired"`
|
||||
ID string `json:"id"`
|
||||
Key string `json:"key"`
|
||||
Service string `json:"service"`
|
||||
Stype int `json:"stype"`
|
||||
} `json:"data"`
|
||||
ExternalData struct {
|
||||
Zone string `json:"zone"`
|
||||
} `json:"externalData"`
|
||||
}
|
||||
|
||||
//获取解密凭证
|
||||
//https://open.jd.com/home/home#/doc/api?apiCateId=243&apiId=3093&apiName=jingdong.jos.voucher.info.get
|
||||
func (a *API) VoucherInfoGet() (voucherInfoGetResult *VoucherInfoGetResult, err error) {
|
||||
result, err := a.AccessAPI("jingdong.jos.voucher.info.get", prodURL, nil)
|
||||
if err == nil {
|
||||
data, err2 := base64.StdEncoding.DecodeString(result["jingdong_jos_voucher_info_get_responce"].(map[string]interface{})["response"].(map[string]interface{})["data"].(map[string]interface{})["voucher"].(string))
|
||||
if err2 == nil {
|
||||
json.Unmarshal(data, &voucherInfoGetResult)
|
||||
}
|
||||
err = err2
|
||||
}
|
||||
return voucherInfoGetResult, err
|
||||
}
|
||||
|
||||
type KeyGetSign struct {
|
||||
SdkVer int `json:"sdk_ver"`
|
||||
Ts int64 `json:"ts"`
|
||||
Tid string `json:"tid"`
|
||||
}
|
||||
|
||||
type KeyGetResult struct {
|
||||
Code string `json:"code"`
|
||||
Response struct {
|
||||
StatusCode int `json:"status_code"`
|
||||
KeyCacheDisabled int `json:"key_cache_disabled"`
|
||||
KeyBackupDisabled int `json:"key_backup_disabled"`
|
||||
Ts int64 `json:"ts"`
|
||||
EncService string `json:"enc_service"`
|
||||
ErrorMsg string `json:"errorMsg"`
|
||||
Tid string `json:"tid"`
|
||||
ServiceKeyList []struct {
|
||||
CurrentKeyVersion int `json:"current_key_version"`
|
||||
Keys []struct {
|
||||
ID string `json:"id"`
|
||||
KeyExp int64 `json:"key_exp"`
|
||||
KeyStatus int `json:"key_status"`
|
||||
KeyDigest string `json:"key_digest"`
|
||||
KeyType string `json:"key_type"`
|
||||
KeyString string `json:"key_string"`
|
||||
KeyEffective int64 `json:"key_effective"`
|
||||
Version int `json:"version"`
|
||||
} `json:"keys"`
|
||||
Service string `json:"service"`
|
||||
GrantUsage string `json:"grant_usage"`
|
||||
} `json:"service_key_list"`
|
||||
} `json:"response"`
|
||||
}
|
||||
|
||||
//获取解密密钥
|
||||
//https://open.jd.com/home/home#/doc/api?apiCateId=243&apiId=4262&apiName=jingdong.jos.master.key.get
|
||||
func (a *API) KeyGet() (keyGetResult *KeyGetResult, err error) {
|
||||
voucherInfoGetResult, err := a.VoucherInfoGet()
|
||||
if err != nil {
|
||||
return keyGetResult, err
|
||||
}
|
||||
var (
|
||||
sdkVer = 2
|
||||
ts = time.Now().Unix()
|
||||
tid = voucherInfoGetResult.Data.ID
|
||||
)
|
||||
keyGetSign := &KeyGetSign{
|
||||
SdkVer: sdkVer,
|
||||
Ts: ts,
|
||||
Tid: tid,
|
||||
}
|
||||
data, err := json.Marshal(keyGetSign)
|
||||
if err != nil {
|
||||
return keyGetResult, err
|
||||
}
|
||||
key, err := base64.StdEncoding.DecodeString(voucherInfoGetResult.Data.Key)
|
||||
if err != nil {
|
||||
return keyGetResult, err
|
||||
}
|
||||
h := hmac.New(sha256.New, key)
|
||||
h.Write(data)
|
||||
result, err := a.AccessAPI("jingdong.jos.master.key.get", prodURL, map[string]interface{}{
|
||||
"sig": base64.StdEncoding.EncodeToString(h.Sum(nil)),
|
||||
"sdk_ver": sdkVer,
|
||||
"ts": ts,
|
||||
"tid": tid,
|
||||
})
|
||||
if err == nil {
|
||||
utils.Map2StructByJson(result["jingdong_jos_master_key_get_responce"], &keyGetResult, false)
|
||||
}
|
||||
return keyGetResult, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user