1. 程式人生 > >菜鳥電子面單,自定義區設定方法

菜鳥電子面單,自定義區設定方法

菜鳥電子面單列印元件,自定義列印區展示內容

客戶一般要求自定義列印區裡面,展示訂單備貨資訊。

但是官方文件菜鳥列印互動協議裡面介紹的data設定goodsInfo這種方法,經過試驗是沒法使用的。


後來發現由於templateURL可以自己定義。那就自己設定一個url,並定義好列印內容,內容為備貨商品展示。

但要注意的是,這個URL必須為滿足應答頭為application/json,否則報錯


排版的問題,比如長寬高的設定值怎麼辦,我一般先到菜鳥列印元件下的設計器先去排版。

再通過獲取使用者的列印模板的介面獲取自定義模板的URL,訪問連線

 內容格式為菜鳥要求的格式,如下所示:

<?xml version='1.0'encoding='UTF-8'?>

<layout id='CUSTOM_AREA'xmlns='http://cloudprint.cainiao.com/print'   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'   xsi:schemaLocation='http://cloudprint.cainiao.com/printhttp://cloudprint-docs-resource.oss-cn-shanghai.aliyuncs.com/lpml_schema.xsd'   xmlns:editor='http://cloudprint.cainiao.com/schema/editor'top='140'    width='100' height='40'style='zIndex: 1; overflow: hidden;'>   

<layout id='element_layout_9F7484A0D698A082' left='5' top='5'width='26'        height='6'editor:_for_='element_text_B9D9F95086F51FEE'        style='zIndex:3;overflow:visible;'>

<text value='11112測試規格:規格222 數量:1'style='wrap:true;direction:horizontal;letterSpacing:0;fontSize:9;lineHeight:5;fontItalic:false;fontFamily:宋體;fontUnderline:false;valign:top;align:left;fontWeight:false'/>

</layout>

</layout>

Java程式碼如下:

@RequestMapping(value = "/alipay/taobao/myPrintTemplate/{orderId}", method= RequestMethod.GET)

    publicvoid myprinttemplate(

           @PathVariable(value="orderId") Integer orderId,

           HttpServletResponse response){

       response.setCharacterEncoding("utf-8");

       response.setContentType("application/json");

       List<GoodsOrderDetail> dbDetailList = goodsOrderDetailMapper.selectGoodsOrderDetailByOrderId(orderId);

       StringBuilder sb = new StringBuilder();

       for (GoodsOrderDetail detail : dbDetailList) {

           sb.append(detail.getGoodsName());

           sb.append(" 規格:");

           sb.append(detail.getSpecification());

           sb.append(" 數量:");

           sb.append(detail.getGoodsNum());

       }

       StringBuffer result = new StringBuffer("<?xmlversion='1.0' encoding='UTF-8'?>");

           result.append("<layout id='CUSTOM_AREA'xmlns='http://cloudprint.cainiao.com/print'");

           result.append("   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'");

           result.append("   xsi:schemaLocation='http://cloudprint.cainiao.com/printhttp://cloudprint-docs-resource.oss-cn-shanghai.aliyuncs.com/lpml_schema.xsd'");

           result.append("   xmlns:editor='http://cloudprint.cainiao.com/schema/editor'top='140'");

           result.append("    width='100' height='40' style='zIndex: 1;overflow: hidden;'>");

           result.append("    <layoutid='element_layout_9F7484A0D698A082' left='5' top='5' width='26'");

           result.append("       height='6' editor:_for_='element_text_B9D9F95086F51FEE'");

           result.append("       style='zIndex:3;overflow:visible;'>");

           result.append("<text value='");

           result.append(sb.toString());

           result.append("'style='wrap:true;direction:horizontal;letterSpacing:0;fontSize:9;lineHeight:5;fontItalic:false;fontFamily:宋體;fontUnderline:false;valign:top;align:left;fontWeight:false'/>");

           result.append("   </layout>");

           result.append("</layout>");

       try

         response.getWriter().write(result.toString()); 

     } catch (Exception e) { 

         e.printStackTrace(); 

     } 

    }