| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace common\components;
- use bizHd\order\services\OrderService;
- use Yii;
- /**
- * 订单节日贺卡云打印(文字暂写死,纸张规格可配置)
- */
- class greetingCardPrintUtil
- {
- /**
- * 根据订单 ID 打印节日贺卡
- */
- 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);
- }
- return self::printByData(self::buildDefaultData(), $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_GREETING_CARD,
- ], $printOptions));
- $data['paper'] = $paper;
- $html = self::renderHtml($data);
- return (new cloudPrintUtil())->printHtml($html, $paper['dmPaperSize']);
- }
- /**
- * 默认贺卡文案(写死,后续可改为订单 cardInfo / 收花人姓名等)
- */
- public static function buildDefaultData()
- {
- return [
- 'cardTitle' => '生日快乐贺卡',
- 'toText' => 'To 陈小姐',
- 'titleText' => '生日快乐',
- 'messageLines' => [
- '愿你今天被鲜花和温柔包围,',
- '生日快乐,天天开心。',
- ],
- 'fromText' => 'From 你的朋友',
- ];
- }
- /**
- * 渲染贺卡 HTML
- */
- public static function renderHtml(array $data)
- {
- $data = array_merge(self::buildDefaultData(), $data);
- if (empty($data['paper'])) {
- $data['paper'] = cloudPrintPaperSpec::resolve([
- 'scene' => cloudPrintPaperSpec::SCENE_GREETING_CARD,
- ]);
- }
- $template = Yii::getAlias('@common/templates/greeting-card-a4.php');
- if (!is_file($template)) {
- throw new CloudPrinterApiException('节日贺卡模板不存在');
- }
- ob_start();
- include $template;
- return (string)ob_get_clean();
- }
- }
|