package jdshopapi import ( "crypto/hmac" "crypto/sha256" "encoding/base64" "encoding/json" "fmt" "time" "git.rosy.net.cn/baseapi/utils" ) //订单出库 //https://open.jd.com/home/home#/doc/api?apiCateId=55&apiId=1948&apiName=jingdong.pop.order.shipment func (a *API) OrderShipment(orderID int64, logiCoprId, logiNo string) (err error) { result, err := a.AccessAPI("jingdong.pop.order.shipment", prodURL, map[string]interface{}{ "orderId": orderID, "logiCoprId": logiCoprId, "logiNo": logiNo, }) if err == nil { if !result["jingdong_pop_order_shipment_responce"].(map[string]interface{})["sopjosshipment_result"].(map[string]interface{})["success"].(bool) { return fmt.Errorf("OrderShipment error: %v", result["jingdong_pop_order_shipment_responce"].(map[string]interface{})["sopjosshipment_result"].(map[string]interface{})["chineseErrCode"]) } } return err } //获取商家物流公司 //https://open.jd.com/home/home#/doc/api?apiCateId=75&apiId=582&apiName=360buy.get.vender.all.delivery.company func (a *API) GetDeliveryCompany() (result interface{}, err error) { var params = map[string]interface{}{ "fields": "id,name", } data, _ := json.Marshal(params) result, err = a.AccessAPI("360buy.get.vender.all.delivery.company", prodURL, map[string]interface{}{ "360buy_param_json": string(data), }) if err == nil { } return result, err } type GetOrderResult struct { } //查询单个订单 //https://open.jd.com/home/home#/doc/api?apiCateId=55&apiId=4247&apiName=jingdong.pop.order.get func (a *API) GetOrder(orderID int64) (err error) { _, err = a.AccessAPI("jingdong.pop.order.get", prodURL, map[string]interface{}{ "order_id": orderID, "optional_fields": `orderType,payType,orderTotalPrice,orderSellerPrice, orderPayment,freightPrice,orderState,orderStateRemark, orderStartTime,orderEndTime,orderRemark,consigneeInfo, itemInfoList,pauseBizInfo`, "order_state": "WAIT_SELLER_STOCK_OUT,PAUSE", }) 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 }