deliverySlipPrintUtil.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace common\components;
  3. use biz\shop\classes\ShopClass;
  4. use bizHd\order\classes\OrderGoodsClass;
  5. use bizHd\order\services\OrderService;
  6. use Yii;
  7. /**
  8. * A4 鲜花配送单打印(链科云 HTML + PC 端 hiprint)
  9. */
  10. class deliverySlipPrintUtil
  11. {
  12. /**
  13. * 根据零售订单 ID 打印 A4 配送单(链科云)
  14. */
  15. public static function printByOrderId($orderId, $mainId = 0, array $printOptions = [])
  16. {
  17. $order = OrderService::getById($orderId);
  18. if (empty($order)) {
  19. throw new CloudPrinterApiException('订单不存在');
  20. }
  21. if ($mainId > 0) {
  22. OrderService::valid($order, $mainId);
  23. }
  24. $data = self::buildDataFromOrder($order);
  25. return self::printByData($data, $printOptions);
  26. }
  27. /**
  28. * 直接按模板数据打印(链科云)
  29. */
  30. public static function printByData(array $data, array $printOptions = [])
  31. {
  32. if (!cloudPrintUtil::isEnabled()) {
  33. throw new CloudPrinterApiException('链科云打印未启用,请先在 .env 配置 LIANKENET_*');
  34. }
  35. $paper = cloudPrintPaperSpec::resolve(array_merge([
  36. 'scene' => cloudPrintPaperSpec::SCENE_DELIVERY_SLIP,
  37. ], $printOptions));
  38. $data['paper'] = $paper;
  39. $html = self::renderHtml($data);
  40. return (new cloudPrintUtil())->printHtml($html, $paper['dmPaperSize']);
  41. }
  42. /**
  43. * PC 本地 hiprint 打 A4:返回 printData + template(对齐 delivery-slip-a4 版式)
  44. * @param array $order 订单数组
  45. * @return array{printData:array,template:string}
  46. */
  47. public static function buildHiprintPayload(array $order)
  48. {
  49. return [
  50. 'printData' => self::buildHiprintDataFromOrder($order),
  51. 'template' => self::getHiprintTemplate(),
  52. ];
  53. }
  54. /**
  55. * 组装 hiprint 字段:封面用 URL(不做 data URI,避免本机打印慢)
  56. */
  57. public static function buildHiprintDataFromOrder(array $order)
  58. {
  59. $data = self::buildDataFromOrder($order, ['imageAsDataUri' => false]);
  60. $addressBlock = (string)($data['fullAddress'] ?? '');
  61. if (!empty($data['distanceText'])) {
  62. $addressBlock .= ' 配送距离: ' . $data['distanceText'];
  63. }
  64. $data['addressBlock'] = $addressBlock;
  65. $data['reachTimeText'] = trim(($data['reachDateLabel'] ?? '') . ' ' . ($data['reachDateTime'] ?? ''));
  66. $data['productCountText'] = '共 ' . ($data['productCount'] ?? '1') . ' 个';
  67. $data['serviceTip'] = '';
  68. if (!empty($data['servicePhone'])) {
  69. $data['serviceTip'] = '如在制作,配送环节出现问题或疑问,欢迎随时致电: ' . $data['servicePhone'];
  70. }
  71. return $data;
  72. }
  73. /**
  74. * 读取 A4 配送单 hiprint JSON 模板字符串
  75. */
  76. public static function getHiprintTemplate()
  77. {
  78. $path = Yii::getAlias('@common/templates/delivery-slip-a4-hiprint.json');
  79. if (!is_file($path)) {
  80. throw new CloudPrinterApiException('配送单 hiprint 模板不存在');
  81. }
  82. $json = file_get_contents($path);
  83. if ($json === false || $json === '') {
  84. throw new CloudPrinterApiException('配送单 hiprint 模板读取失败');
  85. }
  86. return $json;
  87. }
  88. /**
  89. * 组装配送单展示数据
  90. * @param array $order 订单
  91. * @param array $options imageAsDataUri=true 时封面转 base64(云打印盒需离线图)
  92. */
  93. public static function buildDataFromOrder(array $order, array $options = [])
  94. {
  95. $shopId = intval($order['shopId'] ?? 0);
  96. $shop = ShopClass::getById($shopId, true);
  97. $shopName = '';
  98. if (!empty($shop)) {
  99. $merchantName = $shop->merchantName ?? '';
  100. $branchName = $shop->shopName ?? '';
  101. $shopName = ($branchName === '首店' || $branchName === '') ? $merchantName : trim($merchantName . ' ' . $branchName);
  102. }
  103. if ($shopName === '') {
  104. $shopName = '花掌柜';
  105. }
  106. $reachPeriodMap = [0 => '上午', 1 => '下午', 2 => '晚上'];
  107. $reachPeriod = $reachPeriodMap[intval($order['reachPeriod'] ?? 0)] ?? '';
  108. $reachDate = trim((string)($order['reachDate'] ?? ''));
  109. if ($reachDate === '' || $reachDate === '0000-00-00') {
  110. $reachDateLabel = '立即发单';
  111. $reachDateTime = date('m月d日 H:i');
  112. } else {
  113. $reachDateLabel = '预约配送';
  114. $reachDateTime = date('m月d日', strtotime($reachDate)) . ' ' . $reachPeriod;
  115. }
  116. $bookName = $order['bookName'] ?? '';
  117. $bookMobile = $order['bookMobile'] ?? '';
  118. if (intval($order['anonymity'] ?? 0) === 1) {
  119. $bookName = '--';
  120. $bookMobile = '--';
  121. }
  122. $fullAddress = trim((string)($order['fullAddress'] ?? ''));
  123. $showAddress = trim((string)($order['showAddress'] ?? ''));
  124. if ($showAddress !== '' && strpos($fullAddress, $showAddress) === false) {
  125. $fullAddress .= '(' . $showAddress . ')';
  126. }
  127. $productName = trim((string)($order['orderName'] ?? ''));
  128. $goodsNum = max(1, intval($order['goodsNum'] ?? 1));
  129. if ($productName === '') {
  130. $productName = '鲜花';
  131. }
  132. $cover = self::resolveOrderCover($order);
  133. $distanceText = '';
  134. $distance = intval($order['sendDistance'] ?? ($order['deliveryDistance'] ?? 0));
  135. if ($distance > 0) {
  136. $distanceText = $distance >= 1000
  137. ? ('直线 ' . round($distance / 1000, 1) . '公里')
  138. : ('直线 ' . $distance . '米');
  139. }
  140. $servicePhone = trim((string)getenv('LIANKENET_SERVICE_PHONE'));
  141. if ($servicePhone === '' && !empty($shop)) {
  142. $servicePhone = trim((string)($shop->telephone ?? ''));
  143. }
  144. $sendNum = (string)($order['sendNum'] ?? '');
  145. $batchDay = str_pad((string)date('j'), 2, '0', STR_PAD_LEFT);
  146. // 云打印默认转 data URI;本机 hiprint 传 URL 即可
  147. $imageAsDataUri = !isset($options['imageAsDataUri']) || $options['imageAsDataUri'];
  148. $productImage = $imageAsDataUri ? self::imageToDataUri($cover) : $cover;
  149. return [
  150. 'title' => $shopName . ' 鲜花配送单',
  151. 'batchNo' => '#O' . $batchDay . ' [#' . $sendNum . ']',
  152. 'sendNum' => $sendNum,
  153. 'receiveUserName' => $order['receiveUserName'] ?? '',
  154. 'receiveMobile' => $order['receiveMobile'] ?? '',
  155. 'fullAddress' => $fullAddress,
  156. 'distanceText' => $distanceText,
  157. 'reachDateLabel' => $reachDateLabel,
  158. 'reachDateTime' => $reachDateTime,
  159. 'productName' => $productName,
  160. 'productCount' => (string)$goodsNum,
  161. 'productImage' => $productImage,
  162. 'cardInfo' => $order['cardInfo'] ?? '',
  163. 'bookName' => $bookName,
  164. 'bookMobile' => $bookMobile,
  165. 'orderSn' => $order['orderSn'] ?? '',
  166. 'servicePhone' => $servicePhone,
  167. ];
  168. }
  169. /**
  170. * 渲染 A4 HTML
  171. */
  172. public static function renderHtml(array $data)
  173. {
  174. if (empty($data['paper'])) {
  175. $data['paper'] = cloudPrintPaperSpec::resolve([
  176. 'scene' => cloudPrintPaperSpec::SCENE_DELIVERY_SLIP,
  177. ]);
  178. }
  179. $template = Yii::getAlias('@common/templates/delivery-slip-a4.php');
  180. if (!is_file($template)) {
  181. throw new CloudPrinterApiException('配送单模板不存在');
  182. }
  183. ob_start();
  184. include $template;
  185. return (string)ob_get_clean();
  186. }
  187. /**
  188. * 取订单商品封面图
  189. */
  190. protected static function resolveOrderCover(array $order)
  191. {
  192. $cover = trim((string)($order['cover'] ?? ''));
  193. if ($cover !== '') {
  194. return imgUtil::groupImg($cover) . '?x-oss-process=image/resize,m_fill,h_300,w_300';
  195. }
  196. $goodsList = OrderGoodsClass::getGoodsListById(intval($order['id'] ?? 0));
  197. if (!empty($goodsList)) {
  198. $first = $goodsList[0];
  199. $shortCover = $first['cover'] ?? '';
  200. if ($shortCover !== '') {
  201. return imgUtil::groupImg($shortCover) . '?x-oss-process=image/resize,m_fill,h_300,w_300';
  202. }
  203. }
  204. return '';
  205. }
  206. /**
  207. * 远程图片转 data URI,避免打印盒访问不了外网图
  208. */
  209. protected static function imageToDataUri($url)
  210. {
  211. $url = trim((string)$url);
  212. if ($url === '') {
  213. return '';
  214. }
  215. $binary = @file_get_contents($url);
  216. if ($binary === false || $binary === '') {
  217. return $url;
  218. }
  219. $mime = 'image/jpeg';
  220. if (function_exists('finfo_buffer')) {
  221. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  222. if ($finfo) {
  223. $detected = finfo_buffer($finfo, $binary);
  224. if (!empty($detected)) {
  225. $mime = $detected;
  226. }
  227. finfo_close($finfo);
  228. }
  229. }
  230. return 'data:' . $mime . ';base64,' . base64_encode($binary);
  231. }
  232. }