京东物流开放平台对接云打印功能

这是京东开发文档的流程图,根据自己的需求分析,我选择的是接入方式一

这是接入方式一的流程图:

所有第一步我们先下载他的打印组件并安装:京东物流开放平台 

第二步呢就是看你有没有自定义快递面单的需求,由于目前我没有特别的需求,所以我这里选择直接去复制他的官方模板的链接就好了

京东物流快递面单

我直接选择了第一个,把它保存下来

在准备好这些之后我们就可以去获取我们已经下好的快递单的加密后的密文数据了。

获取打印数据的文档

首先要注意的是,接入云打印是还需要额外导入京东提供的jar包的:

然后按需下载自己语言的包就好了,再通过maven或者其他管理工具把它安装到自己的依赖仓库当中,然后把它导入到项目内,我的是这样:

 完成这一步之后我们就可以开始写代码了,其实很简单,就是根据要啥参数传啥参数就好了:

 public static PullDataServicePullDataLopResponse getPullData(String JDTrackNumber){
        System.out.println("获取打印数据,传入单号:"+JDTrackNumber);
        PullDataServicePullDataLopRequest request = new PullDataServicePullDataLopRequest();

        PullDataReqDTO dto = new PullDataReqDTO();
        dto.setCpCode("JD");
        ArrayList<WayBillInfo> wayBillInfos = new ArrayList<>();
        WayBillInfo wayBillInfo = new WayBillInfo();
        wayBillInfo.setJdWayBillCode(JDTrackNumber);
        

        wayBillInfos.add(wayBillInfo);
        dto.setWayBillInfos(wayBillInfos);

        //cpCode为JD\JDKY\JDDJ\ZY时,key的值是ewCustomerCode;cpCode是非京东物流的其他物流服务时,key的值是eCustomerCode。长度13-14
        //cpCode为JD\JDKY\JDDJ\ZY时,value传商家编码(京东快递传商家编码,京东快运、京东大件传事业部编码);cpCode是非京东物流的其他物流服务时,value传下运单时无界电子面单店铺的vendorid或vendorcode。长度1-30
        HashMap<String, String> map = new HashMap<>();
        map.put("ewCustomerCode",customerCode);
        dto.setParameters(map);
        dto.setObjectId(UUID.randomUUID().toString());

        request.setPullDataReqDTO(dto);
        LopPlugin lopPlugin = OAuth2PluginFactory.produceLopPlugin(appKey, AppSecret,accessToken);
        request.addLopPlugin(lopPlugin);
        PullDataRespDTO result = null;


        System.out.println("查询组装报文数据的请求参数");
        //{"cpCode":"JD","objectId":"f129ee85-e66f-4661-8bce-704ac80274c9","parameters":{"eCustomerCode":"028K2335278"},"wayBillInfos":[{"jdWayBillCode":"JDVD06181125116"}]}
        System.out.println(JSON.toJSONString(dto));

        PullDataServicePullDataLopResponse response=null;
        try {
            response = client.execute(request);
            System.out.println("获取打印数据,返回code:"+response.getCode());
            System.out.println(JSON.toJSONString(response));
        } catch (Exception e) {
            throw new XBException(3000,e.getMessage());
        }
        return response;

    }

根据他的文档需要的参数类型进行创建即可,然后发送请求。

完成这一步之后就剩最后一步了,组装报文发送给我们刚刚安装的京东打印组件就好了,

这里我是这样做的,在后端组装好报文数据,然后发送给前端,在前端通过websocket与打印组件建立通信,然后发送json数据给他即可:

以下是后端组装报文数据的代码:

 @GetMapping("printPullData")
    public Result printPullData(String trackingNumber,HttpServletRequest request){
        //获取给云打印的打印数据
        PullDataServicePullDataLopResponse pullDataDto = JDUtil.getPullData(trackingNumber);
        //组装发送给打印组件的数据
        //京东快递打印模版uri:https://template-content.jd.com/template-oss?tempCode=jdkd76x105
        JDPullDataDto jdPullDataDto = new JDPullDataDto();
        jdPullDataDto.setOrderType("PRINT");
        jdPullDataDto.setKey(UUID.randomUUID().toString());

        HashMap<String, Object> hashMap = new HashMap<>();
        System.out.println("获取的打印数据");
        hashMap.put("tempUrl","https://template-content.jd.com/template-oss?tempCode=jdkd76x105");

        System.out.println("A");
        System.out.println(JSON.toJSONString(pullDataDto.getResult().getPrePrintDatas()));

        String perPrintData = pullDataDto.getResult().getPrePrintDatas().get(0).getPerPrintData();
        ArrayList<String> strings = new ArrayList<>(1);
        strings.add(perPrintData);
        hashMap.put("printData",strings);
        jdPullDataDto.setParameters(hashMap);
       
        return Result.success(jdPullDataDto);
    }

前端与组件通信:

       layer.confirm(s+",是否打印快递单?", {icon: 3, offset:'50px',},
                                function(){
                                    $.getJSON("${pageContext.request.contextPath}/facilityDetailInfo/printPullData", {
                                        trackingNumber: res.data,
                                    }, (res) => {
                                        if (res.code == 200) {
                                            var pullData = res.data;
                                            console.log("pullData", pullData);

                                            // 替换为服务器的实际 IP 地址
                                            const socket = new WebSocket('ws://127.0.0.1:9113');

                                            socket.onopen = function(event) {
                                                console.log("WebSocket 连接成功,开始发送消息");
                                                // 发送消息到服务器,确保pullData为字符串类型
                                                socket.send(JSON.stringify(pullData));
                                            };

                                            // 当接收到服务器的消息时执行的回调函数
                                            socket.onmessage = function(event) {
                                                console.log("打印组件回复的消息", event.data);

                                                // 根据业务需求处理接收到的消息后关闭连接
                                                socket.close();
                                            };

                                            socket.onerror = function(event) {
                                                console.error("WebSocket 连接出现错误", event);
                                            };

                                            socket.onclose = function(event) {
                                                console.log("WebSocket 连接已关闭", event);
                                            };

                                        } else {
                                            alert(res.message);
                                        }
                                        setTimeout(()=>{
                                            location.reload();
                                        },10000)
                                    });
                                }, function(){
                                    location.reload();
                                });

因为我的电脑没有打印机,所有默认打印行为是pdf,输出如下:

完结。

万水千山总是情,老板给分行不行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值