纯讨论我也是第一次对接,有更好的优化方案一起讨论
天猫单据推送到数据库
聚石塔RDS数据库创建保存订单,购买聚石塔云服务器进行塔内访问,可以访问到RDS数据库,聚石塔云服务器系统与自研系统对接,推送RDS数据库单据,经过单据处理,得到相应信息,注意订单中商品信息,得有对应关系。根据RDS数据库订单更新时间来时时更新本地订单信息,本地保存一般表有:订单主表,订单子表,订单买家信息表,商品信息关系表(平台的SKU关系),店铺表,店铺单据更新表
京东单据推送到数据库
与天猫一样,入云鼎
天猫获取运单号并且打印电子面单(下载相应的淘宝SDK)
1、通过权限授权拿到token 进行app应用授权
官方有文档,可以搜一下
/**
* 获取登录用的code
*这个方法返回的是https地址,前端去访问登录,会自动回调设置好的接口
* @return
*/
public String getTBCodeUrl(){
Map<String, String> map = new HashMap<>();
map.put("response_type","code");
map.put("client_id","需要授权的APPKEY");
map.put("state","1212");
map.put("view","web");
map.put("from_site","fuwu");
//自己应用的接口,用来接收请求到的CODE
map.put("redirect_uri","获取登录CODE的回调接口");
return "获取登录CODE的淘宝地址"+ buildGetParameterString(map);
}
/**
* 获取SessionKey
* @param TbCode
* 回调的接口得到code,进行下一步操作
* @return
*/
public String getSessionKey(String TbCode){
if (StringUtils.isEmpty(TbCode)){
throw new CommonException(RespError.ERROR,"连接超时");
}
String url="https://oauth.taobao.com/token";
Map<String, String> props = new HashMap<String, String>();
props.put("grant_type", "authorization_code");/*测试时,需把test参数换成自己应用对应的值*/
props.put("code", TbCode);
props.put("client_id", APPKEY);
props.put("client_secret", APPSECRET);
props.put("redirect_uri", "回调接口");
props.put("state", "1212");
props.put("view", "web");
String s = "";
try {
s = WebUtils.doPost(url, props, 30000, 30000);
JSONObject parseObject = JSON.parseObject(s);
Object accessToken = parseObject.get("access_token");
if (!ObjectUtils.isEmpty(accessToken)){
return (String) accessToken;
}
} catch (IOException e) {
return null;
}
return null;
}
/**
* 数据拼接
* @param parameters
* @return
*/
private static String buildGetParameterString(Map<String, String> parameters)
{
String getParameterString = "";
for(Map.Entry<String, String> param : parameters.entrySet())
{
if(param.getValue() == null)
{
continue;
}
getParameterString += (getParameterString.length() < 1) ? ("?") : ("&");
getParameterString += param.getKey() + "=" + param.getValue();
}
return (getParameterString);
}
2、校验订单状态,防止拉取过来的订单已经下过订单
API 接口 taobao.trade.fullinfo.get
/**
* 获取订单信息
* taobao.trade.fullinfo.get
* @param orderId
* @param sessionKey 获取的token
* SERVERURL 淘宝的https请求地址
*/
public void chekOrderStatus( String orderId ,String sessionKey) {
// create Client
TopApiClient client = new DefaultTopApiClient(APPKEY,APPSECRET,SERVERURL);
Ability648 apiPackage = new Ability648(client);
// create domain
// create request
TaobaoTradeFullinfoGetRequest request = new TaobaoTradeFullinfoGetRequest();
/*
tid,type,status,payment,orders,promotion_details
*/
List<String> arr = new ArrayList<>();
arr.add("status");
request.setFields(arr);
request.setTid(Long.valueOf(orderId));
TaobaoTradeFullinfoGetResponse response = null;
try {
response = apiPackage.taobaoTradeFullinfoGet(request,sessionKey);
} catch (IOException e) {
throw new CommonException(RespError.ERROR,orderId+"订单不满足打印面单要求,原因:"+e.getMessage());
}
if(!response.isSuccess()){
throw new CommonException(RespError.ERROR,orderId+"订单不满足打印面单要求");
}
TaobaoTradeFullinfoGetTrade trade = response.getTrade();
String status = trade.getStatus();
//校验是否满足订单所需的状态
if (!TreadStatusEnum.WAIT_SELLER_SEND_GOODS.getValue().equals(status)){
throw new CommonException(RespError.ERROR,orderId+"订单不满足打印面单要求,订单状态为:"+TreadStatusEnum.valueOf(status).getLabel());
}
}
3、获取运单号
API 接口 cainiao.waybill.ii.get
/**
* 对应的淘宝API
* cainiao.waybill.ii.get
* @param orderMainVo
* @return
*/
public CainiaoWaybillIiGetResponse expressPrint( OrderMainVo orderMainVo,String sessionKey){
TopApiClient client = new DefaultTopApiClient(APPKEY,APPSECRET,SERVERURL);
Defaultability apiPackage = new Defaultability(client);
// create domain
CainiaoWaybillIiGetAddressDto cainiaoWaybillIiGetAddressDto = new CainiaoWaybillIiGetAddressDto();
cainiaoWaybillIiGetAddressDto.setCity("市");
cainiaoWaybillIiGetAddressDto.setDetail("详情地址");
cainiaoWaybillIiGetAddressDto.setDistrict("区");
cainiaoWaybillIiGetAddressDto.setProvince("省");
cainiaoWaybillIiGetAddressDto.setTown("街道");
//发货人地址
CainiaoWaybillIiGetUserInfoDto cainiaoWaybillIiGetUserInfoDto = new CainiaoWaybillIiGetUserInfoDto();
//发货人手机号等信息
cainiaoWaybillIiGetUserInfoDto.setAddress(cainiaoWaybillIiGetAddressDto);
cainiaoWaybillIiGetUserInfoDto.setMobile("手机");
cainiaoWaybillIiGetUserInfoDto.setName("发货人名字");
cainiaoWaybillIiGetUserInfoDto.setPhone("手机");
//订单信息
CainiaoWaybillIiGetOrderInfoDto cainiaoWaybillIiGetOrderInfoDto = new CainiaoWaybillIiGetOrderInfoDto();
cainiaoWaybillIiGetOrderInfoDto.setOrderChannelsType("TB");
//订单id
List<String> tradeOrderList = new ArrayList<>();
tradeOrderList.add(orderMainVo.getId());
cainiaoWaybillIiGetOrderInfoDto.setTradeOrderList(tradeOrderList);
//子订单信息
List<OrderInfo> list = orderMainVo.getList();
List<CainiaoWaybillIiGetItem> cainiaoWaybillIiGetItems = new ArrayList<>();
for (OrderInfo orderInfo : list) {
CainiaoWaybillIiGetItem cainiaoWaybillIiGetItem = new CainiaoWaybillIiGetItem();
cainiaoWaybillIiGetItem.setCount(Long.valueOf(orderInfo.getQuantity()));
cainiaoWaybillIiGetItem.setName(orderInfo.getShortName());
cainiaoWaybillIiGetItems.add(cainiaoWaybillIiGetItem);
}
//主订单信息
CainiaoWaybillIiGetPackageInfoDto cainiaoWaybillIiGetPackageInfoDto = new CainiaoWaybillIiGetPackageInfoDto();
cainiaoWaybillIiGetPackageInfoDto.setId(orderMainVo.getId());
//插入子订单信息
cainiaoWaybillIiGetPackageInfoDto.setItems(cainiaoWaybillIiGetItems); //
cainiaoWaybillIiGetPackageInfoDto.setGoodsDescription("医疗器械");
cainiaoWaybillIiGetPackageInfoDto.setLength(30L);
cainiaoWaybillIiGetPackageInfoDto.setWidth(30L);
cainiaoWaybillIiGetPackageInfoDto.setHeight(50L);
//拼接收货人信息,我这边的自己的订单对象
CainiaoWaybillIiGetAddressDto cainiaoWaybillIiGetAddressBuyerDto = new CainiaoWaybillIiGetAddressDto();
cainiaoWaybillIiGetAddressBuyerDto.setCity(orderMainVo.getCityName());
cainiaoWaybillIiGetAddressBuyerDto.setDetail(orderMainVo.getAddressDetail());
cainiaoWaybillIiGetAddressBuyerDto.setDistrict(orderMainVo.getCountyName());
cainiaoWaybillIiGetAddressBuyerDto.setProvince(orderMainVo.getProvinceName());
cainiaoWaybillIiGetAddressBuyerDto.setTown(orderMainVo.getReceiverTown());
CainiaoWaybillIiGetRecipientInfoDto cainiaoWaybillIiGetRecipientInfoDto = new CainiaoWaybillIiGetRecipientInfoDto();
cainiaoWaybillIiGetRecipientInfoDto.setAddress(cainiaoWaybillIiGetAddressBuyerDto);
//手机号与保密信息
cainiaoWaybillIiGetRecipientInfoDto.setMobile(orderMainVo.getTelNumber());
cainiaoWaybillIiGetRecipientInfoDto.setName(orderMainVo.getUserName());
cainiaoWaybillIiGetRecipientInfoDto.setPhone(orderMainVo.getTelNumber());
//解密码OAID 必须上传OAID 淘宝会自动解密
cainiaoWaybillIiGetRecipientInfoDto.setOaid(orderMainVo.getOaid());
//订单号
cainiaoWaybillIiGetRecipientInfoDto.setTid(orderMainVo.getId());
cainiaoWaybillIiGetRecipientInfoDto.setCaid("As268woscee");
CainiaoWaybillIiGetTradeOrderInfoDto cainiaoWaybillIiGetTradeOrderInfoDto = new CainiaoWaybillIiGetTradeOrderInfoDto();
//请求编号 保证唯一就行
cainiaoWaybillIiGetTradeOrderInfoDto.setObjectId("TB"+orderMainVo.getId());
//订单信息
cainiaoWaybillIiGetTradeOrderInfoDto.setOrderInfo(cainiaoWaybillIiGetOrderInfoDto);
//包裹信息
cainiaoWaybillIiGetTradeOrderInfoDto.setPackageInfo(cainiaoWaybillIiGetPackageInfoDto);
//收件人信息
cainiaoWaybillIiGetTradeOrderInfoDto.setRecipient(cainiaoWaybillIiGetRecipientInfoDto);
//面单面板
//模板信息
cainiaoWaybillIiGetTradeOrderInfoDto.setTemplateUrl(SFTEMPLATE);
//商家ID
cainiaoWaybillIiGetTradeOrderInfoDto.setUserId(USERID);
CainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest = new CainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest();
//快递公司编号
cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest.setCpCode("SF");
//电商标快 顺丰的快递类型
cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest.setProductCode("247");
cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest.setSender(cainiaoWaybillIiGetUserInfoDto);
List<CainiaoWaybillIiGetTradeOrderInfoDto> cainiaoWaybillIiGetTradeOrderInfoDtos = new ArrayList<>();
cainiaoWaybillIiGetTradeOrderInfoDtos.add(cainiaoWaybillIiGetTradeOrderInfoDto);
cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest.setTradeOrderInfoDtos(cainiaoWaybillIiGetTradeOrderInfoDtos);
cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest.setDmsSorting(false);
cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest.setThreePlTiming(false);
cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest.setNeedEncrypt(false);
cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest.setMultiPackagesShipment(false);
//与模板信息对应的编号
cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest.setBrandCode("SF");
//月结账号
cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest.setCustomerCode(SFAMIND);
cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest.setCallDoorPickUp(false);
// create request
CainiaoWaybillIiGetRequest request = new CainiaoWaybillIiGetRequest();
request.setParamWaybillCloudPrintApplyNewRequest(cainiaoWaybillIiGetWaybillCloudPrintApplyNewRequest);
try {
CainiaoWaybillIiGetResponse response = apiPackage.cainiaoWaybillIiGet(request,sessionKey);
return response;
} catch (IOException e) {
return null;
}
}
4、获取运单号成功,需求上传运单号,改变订单状态
API接口 taobao.logistics.online.send
记得同时更新本地订单信息
/**
* 通知淘宝已下单
* taobao.logistics.online.send
* @param expressNo
* @param orderId
* @param sessionKey
* @return
*/
public Boolean sendTbExpressNo(String expressNo,String orderId,String sessionKey){
TaobaoClient client = new DefaultTaobaoClient(SERVERURL, APPKEY, APPSECRET);
LogisticsOnlineSendRequest req = new LogisticsOnlineSendRequest();
req.setTid(Long.valueOf(orderId));
req.setOutSid(expressNo);
req.setCompanyCode("SF");
LogisticsOnlineSendResponse rsp = null;
try {
rsp = client.execute(req, sessionKey);
} catch (ApiException e) {
return false;
}
String errorCode = rsp.getErrorCode();
if (!StringUtils.isEmpty(errorCode)){
String subMessage = rsp.getSubMessage();
String subMsg = rsp.getSubMsg();
String msg = rsp.getMsg();
log.error("订单:"+orderId+"快递订单:"+expressNo+"下快递错误信息:"+subMsg+subMessage+msg);
return false;
}
System.out.println(rsp.getBody());
return true;
}
后面就是面单打印的数据了
官方文档
官方有文档,可以搜一下
订单原本的打印数据 printData 可以通过获取完运单号之后,返回数据中获取printData
也可自定义模板详情看文档
后续更新京东这方面的对接