You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pdd-order-api/app/libs/tool/class.CloudCustomAreaTool.php

211 lines
9.6 KiB
PHTML

11 months ago
<?php
class CloudCustomAreaTool {
public static function convertPxToNum($string) {
return str_replace('px', '', $string);
}
public static function getNodeKeyName($node, $glue = '_') {
$fields = $node['fields'] ?: [];
$names = [];
switch ($node['type']) {
case 'barcode':
$names[] = $node['name'];
break;
default:
foreach ($fields as $field) {
$names[] = $field['name'];
}
break;
}
return implode($glue, $names);
}
public static function getFieldValueExpression($node, $glue = '_') {
$fieldKey = self::getNodeKeyName($node, $glue);
return empty($fieldKey) ? '' : "<%=_data.{$fieldKey}%>";
}
public static function convertTextNode($node, $globalStyle, $waybillType) {
$top = OrderPrintTool::convertPx2Mm($node['style']['top']);
$left = OrderPrintTool::convertPx2Mm($node['style']['left']);
$width = OrderPrintTool::convertPx2Mm($node['style']['width']);
$height = OrderPrintTool::convertPx2Mm($node['style']['height']);
$fontFamily = $node['style']['font-family'] ?: $globalStyle['fontFamily'];
$fontSize = $node['style']['font-size'] ?: $globalStyle['fontSize'];
$fontWeight = $node['style']['font-weight'] ?: $globalStyle['fontWeight'];
$textAlign = $node['style']['text-align'] ?: ($globalStyle['textAlign'] ?: '');
if ($waybillType == LogisticsConst::waybillKsCloud) {
$textTpl = '<text left="%s" top="%s" width="%s" height="%s" style="overflow:hidden;%s"><![CDATA[%s]]></text>';
$textStyleArr = ["fontFamily:{$fontFamily};", "fontSize:{$fontSize};", "fontWeight:{$fontWeight};"];
if (!empty($textAlign)) {
$textStyleArr[] = "align:{$textAlign};";
}
return sprintf($textTpl, $left, $top, $width, $height, implode('', $textStyleArr), self::getFieldValueExpression($node));
}
$layoutTpl = '<layout id="%s" left="%s" top="%s" width="%s" height="%s" style="overflow:hidden;">%s</layout>';
$textTpl = '<text style="%s"><![CDATA[%s]]></text>';
$textStyleArr = ["fontFamily:{$fontFamily};", "fontSize:{$fontSize};", "fontWeight:{$fontWeight};"];
if (!empty($textAlign)) {
$textStyleArr[] = "align:{$textAlign};";
}
$textXml = sprintf($textTpl, implode('', $textStyleArr), self::getFieldValueExpression($node));
return sprintf($layoutTpl, self::getLayoutId(), $left, $top, $width, $height, $textXml);
}
private static function getLayoutId() {
list(, $num) = explode('.', microtime(true));
return sprintf('element_layout_%s', ($num + 1000));
}
public static function convertLineNode($node) {
$top = OrderPrintTool::convertPx2Mm($node['style']['top']);
$left = OrderPrintTool::convertPx2Mm($node['style']['left']);
$width = OrderPrintTool::convertPx2Mm($node['style']['width']);
$height = OrderPrintTool::convertPx2Mm($node['style']['height']);
$lineTpl = '<line startX="%s" startY="%s" endX="%s" endY="%s" style="lineType:solid;lineWidth: 1;" />';
$startX = $left;
$startY = $top;
$endX = $left + $width;
$endY = $top + $height;
switch ($node['lineType']) {
case 'horizontal':
$lineXml = sprintf($lineTpl, $startX, $startY, $endX, $startY);
break;
case 'vertical':
$lineXml = sprintf($lineTpl, $startX, $startY, $startX, $endY);
break;
default:
$lineXml = '';
break;
}
return $lineXml;
}
public static function convertImageNode($node, $waybillType) {
$top = OrderPrintTool::convertPx2Mm($node['style']['top']);
$left = OrderPrintTool::convertPx2Mm($node['style']['left']);
$width = OrderPrintTool::convertPx2Mm($node['style']['width']);
$height = OrderPrintTool::convertPx2Mm($node['style']['height']);
if ($waybillType == LogisticsConst::waybillKsCloud) {
$imageTpl = '<image left="%s" top="%s" width="%s" height="%s" src="%s" allowFailure="false" />';
$imgSrc = '';
if (!empty($node['imgSrc'])) {
$imgSrc = $node['imgSrc'];
}
return sprintf($imageTpl, $left, $top, $width, $height, $imgSrc);
}
$imageTpl = '<image width="%s" height="%s" src="%s" allowFailure="false" />';
$layoutTpl = '<layout id="%s" left="%s" top="%s" width="%s" height="%s" style="overflow:hidden;">%s</layout>';
if (!empty($node['imgSrc'])) {
$imageXml = sprintf($imageTpl, $width, $height, $node['imgSrc']);
}
return sprintf($layoutTpl, self::getLayoutId(), $left, $top, $width, $height, empty($imageXml) ? '' : $imageXml);
}
public static function convertBarcodeNode($node, $waybillType) {
$top = OrderPrintTool::convertPx2Mm($node['style']['top']);
$left = OrderPrintTool::convertPx2Mm($node['style']['left']);
$width = OrderPrintTool::convertPx2Mm($node['style']['width']);
$height = OrderPrintTool::convertPx2Mm($node['style']['height']);
if ($waybillType == LogisticsConst::waybillKsCloud) {
$isHideText = $node['showBarcodeContent'] == 1 ? 'true' : 'false';
$barcodeWay = $node['showBarcodeWay'] == 'vertical' ? 90 : 0;
$barcodeTpl = '<barcode left="%s" top="%s" width="%s" height="%s" type="code128" style="overflow:hidden;hideText:%s;rotation:%s"><![CDATA[%s]]> </barcode>';
return sprintf($barcodeTpl, $left, $top, $width, $height, $isHideText, $barcodeWay, self::getFieldValueExpression($node));
}
$layoutTpl = '<layout id="%s" left="%s" top="%s" width="%s" height="%s" style="overflow:hidden;">%s</layout>';
$barcodeTpl = '<barcode width="%s" height="%s" type="code128" style="hideText:true"><![CDATA[%s]]> </barcode>';
$barcodeXml = sprintf($barcodeTpl, $width, $height, self::getFieldValueExpression($node));
return sprintf($layoutTpl, self::getLayoutId(), $left, $top, $width, $height, $barcodeXml);
}
public static function convertQrcodeNode($node, $waybillType) {
$top = OrderPrintTool::convertPx2Mm($node['style']['top']);
$left = OrderPrintTool::convertPx2Mm($node['style']['left']);
$width = OrderPrintTool::convertPx2Mm($node['style']['width']);
$height = OrderPrintTool::convertPx2Mm($node['style']['height']);
if ($waybillType == LogisticsConst::waybillKsCloud) {
$barcodeTpl = '<barcode left="%s" top="%s" width="%s" height="%s" type="qrcode" style="overflow:hidden;hideText:true"><![CDATA[%s]]> </barcode>';
return sprintf($barcodeTpl, self::getLayoutId(), $left, $top, $width, $height, ($node['url'] ?: ''));
}
$layoutTpl = '<layout id="%s" left="%s" top="%s" width="%s" height="%s" style="overflow:hidden;">%s</layout>';
$barcodeTpl = '<barcode width="%s" height="%s" type="qrcode" style="hideText:true"><![CDATA[%s]]> </barcode>';
$barcodeXml = sprintf($barcodeTpl, $width, $height, ($node['url'] ?: ''));
return sprintf($layoutTpl, self::getLayoutId(), $left, $top, $width, $height, $barcodeXml);
}
public static function convertNodes($nodes, $globalStyle, $waybillType) {
$xmlNodes = [];
foreach ($nodes as $node) {
switch ($node['type']) {
case 'normal':
case 'itemInfo':
$xmlNodes[] = self::convertTextNode($node, $globalStyle, $waybillType);
break;
case 'line':
$xmlNodes[] = self::convertLineNode($node);
break;
case 'barcode':
$xmlNodes[] = self::convertBarcodeNode($node, $waybillType);
break;
case 'qrcode':
$xmlNodes[] = self::convertQrcodeNode($node, $waybillType);
break;
case 'image':
$xmlNodes[] = self::convertImageNode($node, $waybillType);
break;
}
}
return $xmlNodes;
}
public static function getCustomAreaXml($nodes, $globalStyle, $customAreaX, $customAreaY, $customAreaWidth, $customAreaHeight, $waybillType) {
$xmlNodes = self::convertNodes($nodes, $globalStyle, $waybillType);
$customAreaLayoutTpl = '<layout id="CUSTOM_AREA" left="%s" top="%s" width="%s" height="%s">%s</layout>';
$nodeXml = $xmlNodes ? implode('', $xmlNodes) : '';
return sprintf($customAreaLayoutTpl, $customAreaX, $customAreaY, $customAreaWidth, $customAreaHeight, $nodeXml);
}
public static function getCloudCustomAreaTemplateOssPath($venderId, $opVenderExpressTplId, $platform) {
$fileName = $opVenderExpressTplId . '.xml';
return sprintf('express_template/%s_custom_template/%s/%s', $platform, $venderId, $fileName);
}
public static function getCustomTemplateUrl($memberId, $opMemberExpressTplId, $gmtModified, $waybillType) {
$ossPath = self::getCloudCustomAreaTemplateOssPath($memberId, $opMemberExpressTplId, LogisticsConst::getThirdPlatformByWaybillType($waybillType));
$pubOssPath = OssTool::getPubOssUrlByOssPath($ossPath, OssConst::idcJcloud);
if ($gmtModified) {
$seg = (strpos($pubOssPath, '?') === false) ? '?' : '&';
$pubOssPath = $pubOssPath . $seg . '_ts_=' . strtotime($gmtModified);
}
return $pubOssPath;
}
}