| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- namespace common\components;
- use Yii;
- /**
- * 链科云打印 v3 业务封装,配置读取 .env
- */
- class cloudPrintUtil
- {
- /** @var CloudPrinter|null */
- protected static $client;
- /** @var array|null */
- protected static $printerConfig;
- /**
- * 是否启用链科云打印(.env LIANKENET_ENABLED=1 且密钥齐全)
- */
- public static function isEnabled()
- {
- if (getenv('LIANKENET_ENABLED') != '1') {
- return false;
- }
- return !empty(getenv('LIANKENET_API_KEY'))
- && !empty(getenv('LIANKENET_DEVICE_ID'))
- && !empty(getenv('LIANKENET_DEVICE_KEY'));
- }
- /**
- * 从 .env 创建链科客户端
- */
- public static function createClient()
- {
- if (!self::isEnabled()) {
- throw new CloudPrinterApiException('链科云打印未启用或配置不完整');
- }
- if (self::$client instanceof CloudPrinter) {
- return self::$client;
- }
- self::$client = new CloudPrinter(
- getenv('LIANKENET_API_KEY'),
- getenv('LIANKENET_DEVICE_ID'),
- getenv('LIANKENET_DEVICE_KEY'),
- getenv('LIANKENET_DEBUG') == '1',
- intval(getenv('LIANKENET_TIMEOUT') ?: 10),
- getenv('LIANKENET_SERVER') ?: 'https://cloud.liankenet.com/'
- );
- return self::$client;
- }
- /**
- * 打印订单小票(与 printUtil::printMsg 入参一致,便于切换)
- */
- public function printMsg($content, $shop = null, $orderSn = '')
- {
- $plainText = $this->feieContentToText($content);
- if ($plainText === '') {
- return false;
- }
- $chunks = $this->splitContent($plainText, 4500);
- foreach ($chunks as $chunk) {
- $this->printTextChunk($chunk, $shop, $orderSn);
- }
- return true;
- }
- /**
- * 打印标签内容(链科走文本任务,飞鹅标签指令无法直接复用)
- */
- public function printLabelMsg($content)
- {
- $plainText = $this->feieContentToText($content);
- if ($plainText === '') {
- return false;
- }
- return $this->printTextChunk($plainText);
- }
- /**
- * 直接打印本地文件
- */
- public function printFile($filePath, $optionalArray = [], $paperSize = null)
- {
- $client = self::createClient();
- $printer = $this->resolvePrinterConfig($client);
- $paperSize = $paperSize ?? intval(getenv('LIANKENET_PAPER_SIZE') ?: 9);
- return $client->addJobByPath(
- $printer['port'],
- $printer['model'],
- $paperSize,
- $filePath,
- $optionalArray
- );
- }
- /**
- * 打印 HTML 页面(A4 配送单等排版页面)
- */
- public function printHtml($html, $paperSize = null, $optionalArray = [])
- {
- $html = trim((string)$html);
- if ($html === '') {
- return false;
- }
- $tempFile = $this->createTempHtmlFile($html);
- try {
- return $this->printFile($tempFile, $optionalArray, $paperSize);
- } finally {
- if (is_file($tempFile)) {
- @unlink($tempFile);
- }
- }
- }
- /**
- * 生成可打印 HTML 临时文件
- */
- protected function createTempHtmlFile($html)
- {
- $tempFile = tempnam(sys_get_temp_dir(), 'lk_print_');
- if ($tempFile === false) {
- throw new CloudPrinterApiException('无法创建临时打印文件');
- }
- $target = $tempFile . '.html';
- @unlink($tempFile);
- file_put_contents($target, $html);
- return $target;
- }
- /**
- * 飞鹅排版标签转纯文本
- */
- protected function feieContentToText($content)
- {
- $text = str_ireplace(['<BR>', '<br>', '<br/>', '<br />'], "\n", (string)$content);
- $text = preg_replace('/<\/?CB>/i', '', $text);
- $text = preg_replace('/<\/?B>/i', '', $text);
- $text = preg_replace('/<\/?C>/i', '', $text);
- $text = preg_replace('/<QR>.*?<\/QR>/is', '', $text);
- $text = preg_replace('/<[^>]+>/', '', $text);
- $text = str_replace(["\r\n", "\r"], "\n", $text);
- return trim($text);
- }
- /**
- * 长内容分段,避免单文件过大
- */
- protected function splitContent($text, $maxLength = 4500)
- {
- if (strlen($text) <= $maxLength) {
- return [$text];
- }
- $lines = explode("\n", $text);
- $chunks = [];
- $current = '';
- foreach ($lines as $line) {
- $candidate = $current === '' ? $line : $current . "\n" . $line;
- if (strlen($candidate) > $maxLength) {
- if ($current !== '') {
- $chunks[] = $current;
- }
- $current = $line;
- } else {
- $current = $candidate;
- }
- }
- if ($current !== '') {
- $chunks[] = $current;
- }
- return $chunks;
- }
- /**
- * 提交一段文本为打印任务
- */
- protected function printTextChunk($text, $shop = null, $orderSn = '')
- {
- $tempFile = $this->createTempTextFile($text);
- try {
- $client = self::createClient();
- $printer = $this->resolvePrinterConfig($client);
- $paperSize = intval(getenv('LIANKENET_PAPER_SIZE') ?: 9);
- $client->addJobByPath(
- $printer['port'],
- $printer['model'],
- $paperSize,
- $tempFile,
- $this->buildOptionalParams()
- );
- } catch (\Throwable $e) {
- Yii::error('链科云打印失败: ' . $e->getMessage(), __METHOD__);
- $shopName = '';
- if (is_object($shop)) {
- $shopName = ($shop->merchantName ?? '') . ($shop->shopName ?? '');
- }
- noticeUtil::push(
- trim($shopName . ' 链科云打印未成功,订单号:' . $orderSn . ',原因:' . $e->getMessage()),
- '15280215347'
- );
- return false;
- } finally {
- if (is_file($tempFile)) {
- @unlink($tempFile);
- }
- }
- return true;
- }
- /**
- * 生成 UTF-8 BOM 文本临时文件,便于 Windows 打印盒识别中文
- */
- protected function createTempTextFile($text)
- {
- $tempFile = tempnam(sys_get_temp_dir(), 'lk_print_');
- if ($tempFile === false) {
- throw new CloudPrinterApiException('无法创建临时打印文件');
- }
- $target = $tempFile . '.txt';
- @unlink($tempFile);
- file_put_contents($target, "\xEF\xBB\xBF" . $text);
- return $target;
- }
- /**
- * 解析打印机 USB 口与驱动名:优先 .env,否则取列表首台
- */
- protected function resolvePrinterConfig(CloudPrinter $client)
- {
- if (is_array(self::$printerConfig)) {
- return self::$printerConfig;
- }
- $port = trim((string)getenv('LIANKENET_DEVICE_PORT'));
- $model = trim((string)getenv('LIANKENET_PRINTER_MODEL'));
- if ($port !== '' && $model !== '') {
- self::$printerConfig = ['port' => $port, 'model' => $model];
- return self::$printerConfig;
- }
- $list = $client->getPrinterList();
- if (empty($list)) {
- throw new CloudPrinterApiException('链科设备下没有可用打印机');
- }
- $printer = $list[0];
- self::$printerConfig = [
- 'port' => $printer->port ?? '',
- 'model' => $printer->driver_name ?? '',
- ];
- if (self::$printerConfig['port'] === '' || self::$printerConfig['model'] === '') {
- throw new CloudPrinterApiException('链科打印机信息不完整');
- }
- return self::$printerConfig;
- }
- /**
- * 可选打印参数(份数、颜色、方向等,按 v3 文档扩展)
- */
- protected function buildOptionalParams()
- {
- $params = [];
- $copies = intval(getenv('LIANKENET_COPIES') ?: 1);
- if ($copies > 1) {
- $params['dmCopies'] = $copies;
- }
- return $params;
- }
- }
|