| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace common\components;
- use biz\shop\classes\ShopClass;
- use bizHd\order\classes\OrderGoodsClass;
- use bizHd\order\services\OrderService;
- use Yii;
- /**
- * A4 鲜花配送单云打印
- */
- class deliverySlipPrintUtil
- {
- /**
- * 根据零售订单 ID 打印 A4 配送单
- */
- public static function printByOrderId($orderId, $mainId = 0, array $printOptions = [])
- {
- $order = OrderService::getById($orderId);
- if (empty($order)) {
- throw new CloudPrinterApiException('订单不存在');
- }
- if ($mainId > 0) {
- OrderService::valid($order, $mainId);
- }
- $data = self::buildDataFromOrder($order);
- return self::printByData($data, $printOptions);
- }
- /**
- * 直接按模板数据打印
- */
- public static function printByData(array $data, array $printOptions = [])
- {
- if (!cloudPrintUtil::isEnabled()) {
- throw new CloudPrinterApiException('链科云打印未启用,请先在 .env 配置 LIANKENET_*');
- }
- $paper = cloudPrintPaperSpec::resolve(array_merge([
- 'scene' => cloudPrintPaperSpec::SCENE_DELIVERY_SLIP,
- ], $printOptions));
- $data['paper'] = $paper;
- $html = self::renderHtml($data);
- return (new cloudPrintUtil())->printHtml($html, $paper['dmPaperSize']);
- }
- /**
- * 组装配送单展示数据
- */
- public static function buildDataFromOrder(array $order)
- {
- $shopId = intval($order['shopId'] ?? 0);
- $shop = ShopClass::getById($shopId, true);
- $shopName = '';
- if (!empty($shop)) {
- $merchantName = $shop->merchantName ?? '';
- $branchName = $shop->shopName ?? '';
- $shopName = ($branchName === '首店' || $branchName === '') ? $merchantName : trim($merchantName . ' ' . $branchName);
- }
- if ($shopName === '') {
- $shopName = '花掌柜';
- }
- $reachPeriodMap = [0 => '上午', 1 => '下午', 2 => '晚上'];
- $reachPeriod = $reachPeriodMap[intval($order['reachPeriod'] ?? 0)] ?? '';
- $reachDate = trim((string)($order['reachDate'] ?? ''));
- if ($reachDate === '' || $reachDate === '0000-00-00') {
- $reachDateLabel = '立即发单';
- $reachDateTime = date('m月d日 H:i');
- } else {
- $reachDateLabel = '预约配送';
- $reachDateTime = date('m月d日', strtotime($reachDate)) . ' ' . $reachPeriod;
- }
- $bookName = $order['bookName'] ?? '';
- $bookMobile = $order['bookMobile'] ?? '';
- if (intval($order['anonymity'] ?? 0) === 1) {
- $bookName = '--';
- $bookMobile = '--';
- }
- $fullAddress = trim((string)($order['fullAddress'] ?? ''));
- $showAddress = trim((string)($order['showAddress'] ?? ''));
- if ($showAddress !== '' && strpos($fullAddress, $showAddress) === false) {
- $fullAddress .= '(' . $showAddress . ')';
- }
- $productName = trim((string)($order['orderName'] ?? ''));
- $goodsNum = max(1, intval($order['goodsNum'] ?? 1));
- if ($productName === '') {
- $productName = '鲜花';
- }
- $cover = self::resolveOrderCover($order);
- $distanceText = '';
- $distance = intval($order['sendDistance'] ?? ($order['deliveryDistance'] ?? 0));
- if ($distance > 0) {
- $distanceText = $distance >= 1000
- ? ('直线 ' . round($distance / 1000, 1) . '公里')
- : ('直线 ' . $distance . '米');
- }
- $servicePhone = trim((string)getenv('LIANKENET_SERVICE_PHONE'));
- if ($servicePhone === '' && !empty($shop)) {
- $servicePhone = trim((string)($shop->telephone ?? ''));
- }
- $sendNum = (string)($order['sendNum'] ?? '');
- $batchDay = str_pad((string)date('j'), 2, '0', STR_PAD_LEFT);
- return [
- 'title' => $shopName . ' 鲜花配送单',
- 'batchNo' => '#O' . $batchDay . ' [#' . $sendNum . ']',
- 'sendNum' => $sendNum,
- 'receiveUserName' => $order['receiveUserName'] ?? '',
- 'receiveMobile' => $order['receiveMobile'] ?? '',
- 'fullAddress' => $fullAddress,
- 'distanceText' => $distanceText,
- 'reachDateLabel' => $reachDateLabel,
- 'reachDateTime' => $reachDateTime,
- 'productName' => $productName,
- 'productCount' => (string)$goodsNum,
- 'productImage' => self::imageToDataUri($cover),
- 'cardInfo' => $order['cardInfo'] ?? '',
- 'bookName' => $bookName,
- 'bookMobile' => $bookMobile,
- 'orderSn' => $order['orderSn'] ?? '',
- 'servicePhone' => $servicePhone,
- ];
- }
- /**
- * 渲染 A4 HTML
- */
- public static function renderHtml(array $data)
- {
- if (empty($data['paper'])) {
- $data['paper'] = cloudPrintPaperSpec::resolve([
- 'scene' => cloudPrintPaperSpec::SCENE_DELIVERY_SLIP,
- ]);
- }
- $template = Yii::getAlias('@common/templates/delivery-slip-a4.php');
- if (!is_file($template)) {
- throw new CloudPrinterApiException('配送单模板不存在');
- }
- ob_start();
- include $template;
- return (string)ob_get_clean();
- }
- /**
- * 取订单商品封面图
- */
- protected static function resolveOrderCover(array $order)
- {
- $cover = trim((string)($order['cover'] ?? ''));
- if ($cover !== '') {
- return imgUtil::groupImg($cover) . '?x-oss-process=image/resize,m_fill,h_300,w_300';
- }
- $goodsList = OrderGoodsClass::getGoodsListById(intval($order['id'] ?? 0));
- if (!empty($goodsList)) {
- $first = $goodsList[0];
- $shortCover = $first['cover'] ?? '';
- if ($shortCover !== '') {
- return imgUtil::groupImg($shortCover) . '?x-oss-process=image/resize,m_fill,h_300,w_300';
- }
- }
- return '';
- }
- /**
- * 远程图片转 data URI,避免打印盒访问不了外网图
- */
- protected static function imageToDataUri($url)
- {
- $url = trim((string)$url);
- if ($url === '') {
- return '';
- }
- $binary = @file_get_contents($url);
- if ($binary === false || $binary === '') {
- return $url;
- }
- $mime = 'image/jpeg';
- if (function_exists('finfo_buffer')) {
- $finfo = finfo_open(FILEINFO_MIME_TYPE);
- if ($finfo) {
- $detected = finfo_buffer($finfo, $binary);
- if (!empty($detected)) {
- $mime = $detected;
- }
- finfo_close($finfo);
- }
- }
- return 'data:' . $mime . ';base64,' . base64_encode($binary);
- }
- }
|