getAccessToken($printer['client_id']); if (!empty($token)) { return $token; } // 没有则调用接口去获取 $client = new YlyOauthClient($config); $token = $client->getToken(); $accessToken = $token->access_token; // 自有应用模式access_token无失效时间,接口访问频次:10次/日 (new YlyPrinterCache())->setAccessToken($printer['client_id'], $accessToken, 20 * 24 * 3600); return $accessToken; } /** * @notes 获取打印模板 * @param $printer * @return array * @author Tab * @date 2021/11/16 19:37 */ public function tempalte($printer) { $withoutField = 'create_time, update_time, delete_time'; return PrinterTemplate::withoutField($withoutField)->findOrEmpty($printer['template_id'])->toArray(); } /** * @notes 开始打印 * @param $order * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author Tab * @date 2021/11/16 19:37 */ public function startPrint($order, $scene = PrinterEnum::ORDER_PRIINT) { // 打印机状态 $status = false; // 打印机列表 $printerLists = Printer::where('status', YesNoEnum::YES)->select()->toArray(); foreach ($printerLists as $printer) { $response = (new YlyPrinterService())->getPrintStatus($printer); if (! in_array($response->body->state, [ 1, 2 ])) { continue; } $status = true; if ($scene == PrinterEnum::ORDER_PAY && !$printer['auto_print']) { // 订单支付但未开启打印跳过 continue; } $template = $this->tempalte($printer); $this->singlePrint($printer, $order, $template); } return $status; } /** * @notes 添加打印机 * @param $params * @return mixed * @author Tab * @date 2021/11/16 19:37 */ public function addPrinter($params) { $print = $this->getPrinter($params); $response = $print->addPrinter($params['machine_code'],$params['private_key'],$params['name']); return $response; } /** * @notes 删除打印机 * @param $params * @author Tab * @date 2021/11/16 17:32 */ public function deletePrinter($params) { $print = $this->getPrinter($params); $print->deletePrinter($params['machine_code']); } /** * @notes 获取打印机状态 * @param $params * @return mixed * @author Tab * @date 2021/11/16 18:04 */ public function getPrintStatus($params){ $print = $this->getPrinter($params); $response = $print->getPrintStatus($params['machine_code']); return $response; } /** * @notes 获取打印机接口 * @param $params * @return PrinterService * @author Tab * @date 2021/11/16 18:52 */ public function getPrinter($params) { $config = new YlyConfig($params['client_id'], $params['client_secret']); $accessToken = $this->getAccessToken($params, $config); return new PrinterService($accessToken, $config); } /** * @notes 获取打印接口 * @param $params * @return PrintService * @author Tab * @date 2021/11/16 18:53 */ public function getPrint($params) { $config = new YlyConfig($params['client_id'], $params['client_secret']); $accessToken = $this->getAccessToken($params, $config); return new PrintService($accessToken, $config); } /** * @notes 单次打印 * @param $printer * @param $order * @param $template * @author Tab * @date 2021/11/16 18:53 */ public function singlePrint($printer, $order, $template) { $shopName = ConfigService::get('shop', 'name'); $print = $this->getPrint($printer); $content = "".$printer['print_number'].""; $content .= "
".$template['ticket_name']."
" . PHP_EOL; $content .= str_repeat('-', 32).PHP_EOL; if ($template['show_shop_name'] && $shopName) { $content .= "
".$shopName."
"; $content .= str_repeat('-', 32).PHP_EOL; } $content .= "订单编号:".$order['sn'].PHP_EOL; $content .= "支付方式:".$order['pay_way_desc'].PHP_EOL; $content .= "配送方式:".$order['delivery_type_desc'].PHP_EOL; $content .= str_repeat('-', 32).PHP_EOL; // 商品信息 $all_original_money = 0; $content .= ""; foreach ($order['orderGoods'] as $goods){ $content .= ""; $content .= ""; switch ($order['order_type']) { // 拼团 秒杀 砍价 使用实际价格 case OrderEnum::TEAM_ORDER: case OrderEnum::SECKILL_ORDER: case OrderEnum::BARGAIN_ORDER: $content .= ""; $all_original_money += $goods['goods_price'] * $goods['goods_num']; break; // 普通 虚拟 使用原价 default: $content .= ""; $all_original_money += $goods['original_price'] * $goods['goods_num']; break; } } $content .= '
商品信息数量单价
".mb_substr($goods['goods_snap']->goods_name, 0, 6)."".$goods['goods_num']."¥".$goods['goods_price']."
¥".$goods['original_price']."
'; $content .= str_repeat('-', 32).PHP_EOL; // 合计信息 switch ($order['order_type']) { // 拼团 秒杀 砍价 使用实际价格 case OrderEnum::TEAM_ORDER: case OrderEnum::SECKILL_ORDER: case OrderEnum::BARGAIN_ORDER: $content .= "订单原价:,¥". bcadd($order['goods_price'], 0, 2) . ""; break; // 普通 虚拟 使用原价 default: $content .= "订单原价:,¥". bcadd($all_original_money, 0, 2) . ""; break; } $content .= "会员折扣:,-¥".$order['member_amount'].""; $content .= "优惠券:,-¥".$order['discount_amount'].""; $content .= "商品改价:," . ($order['change_price'] >= 0 ? "+" : "-") . "¥" . abs($order['change_price']) . ""; $content .= "运费:,¥".$order['express_price'].""; $content .= "实付金额:,¥".$order['order_amount'].""; // 收货信息 if ($order['delivery_type'] == DeliveryEnum::EXPRESS) { if($template['consignee_info']->name && $template['consignee_info']->mobile && $template['consignee_info']->address){ $content .= str_repeat('-', 32).PHP_EOL; $content .= "收货信息".PHP_EOL; } if($template['consignee_info']->name){ $content .= "收货人:".$order['address']->contact.PHP_EOL; } if($template['consignee_info']->mobile){ $content .= "联系方式:".$order['address']->mobile.PHP_EOL; } if($template['consignee_info']->address){ $content .= "收货地址:".$order['address']->address.PHP_EOL; } } // 自提订单 if ($order['delivery_type'] == DeliveryEnum::SELF_DELIVERY) { //门店信息 if($template['selffetch_shop']->shop_name || $template['selffetch_shop']->contacts || $template['selffetch_shop']->shop_address){ $content .= str_repeat('-', 32).PHP_EOL; $content .= "门店信息".PHP_EOL; } if($template['selffetch_shop']->shop_name){ $content .= "自提门店:".$order['selffetch_shop']['name'].PHP_EOL; } if($template['selffetch_shop']->contacts){ $content .= "联系人:".$order['selffetch_shop']['contact'].PHP_EOL; } if($template['selffetch_shop']->shop_address){ $content .= "门店地址:".$order['selffetch_shop']['detailed_address'].PHP_EOL; } //提货人信息 if($template['verification_info']->contacts || $template['verification_info']->mobile || $template['verification_info']->pickup_code){ $content .= str_repeat('-', 32).PHP_EOL; $content .= "提货人信息".PHP_EOL; } if($template['verification_info']->contacts){ $content .= "提货人:".$order['address']->contact.PHP_EOL; } if($template['verification_info']->mobile){ $content .= "联系方式:".$order['address']->mobile.PHP_EOL; } if($template['verification_info']->pickup_code){ $content .= "提货码:".$order['pickup_code'].PHP_EOL; } } $content .= str_repeat('-', 32).PHP_EOL; // 买家留言 if($template['show_buyer_message'] && $order['user_remark']){ $content .= '买家留言:'.$order['user_remark'].PHP_EOL; $content .= str_repeat('-', 32).PHP_EOL; } // 二维码 if($template['show_qrcode'] && $template['qrcode_name']){ $content .= "
".$template['qrcode_name']."
".PHP_EOL; } if($template['show_qrcode'] && $template['qrcode_content']){ $content .= "".$template['qrcode_content']."".PHP_EOL; } $content .= str_repeat('-', 32).PHP_EOL; // 底部信息 $content .= "
".$template['bottom']."
".PHP_EOL; $print->index($printer['machine_code'], $content, $order['sn']); } }