error('', '没有可用的打印机'); if (isset($items['code']) && $items['code'] != 0) return $items; $printer_model = new Printer(); foreach ($items as $k => $v) { $printer = $v['printer_info'] ?? []; switch ($printer['brand']) { case '365'://365打印机 break; case 'feie'://飞鹅打印机 try { $time = time(); //请求时间 $msgInfo = array( 'user' => $printer['user'], 'stime' => $time, 'sig' => sha1($printer['user'] . $printer['ukey'] . $time), 'apiname' => 'Open_printMsg', 'sn' => $printer['printer_code'], 'content' => $v['content'], 'times' => $printer['print_num'] //打印次数$print_template ); $client = new FeieClient('api.feieyun.cn', 80); if (!$client->post('/Api/Open/', $msgInfo)) { return $this->error('', 'error'); } else { //服务器返回的JSON字符串,建议要当做日志记录起来 $result = $client->getContent(); return $result; } } catch (\Exception $e) { return $this->error('', $e->getMessage()); } break; case 'yilianyun'://易联云打印机 try { $config = new YlyConfig($printer['open_id'], $printer['apikey']); $access_token = $printer_model->getYlyToken($config, $printer['printer_id']); $machine_code = $v['printer_code'];//商户授权机器码 $origin_id = $v['origin_id']; //内部订单号(32位以内) $print = new PrintService($access_token, $config); $content = $v['content']; $print->index($machine_code, $content, $origin_id, $printer); } catch (\Exception $e) { return $this->error('', $e->getMessage()); } break; } } return $this->success($items); } /** * 易联云打印 * @param $order_info * @param $printer * @param $shop_info */ public function Ylyprint($order_info, $printer, $shop_info) { //打印模板 $print_template_model = new PrinterTemplate(); $print_template_data = $print_template_model->getPrinterTemplateInfo([['template_id', '=', $printer['template_id']]]); $print_template = $print_template_data['data']; $config = new YlyConfig($printer['open_id'], $printer['apikey']); $printer_model = new Printer(); $access_token = $printer_model->getYlyToken($config, $printer['printer_id']); $machine_code = $printer['printer_code']; //商户授权机器码 $origin_id = $order_info['order_no']; //内部订单号(32位以内) $site_name = (new Site())->getSiteInfo([['site_id', '=', $printer['site_id']]], 'site_name')['data']['site_name'] ?? ''; /**文本接口开始**/ $print = new PrintService($access_token, $config); $content = "" . $printer['print_num'] . ""; //小票名称 if ($print_template['title'] != '') { $content .= "
" . $print_template['title'] . "
"; $content .= str_repeat('.', 32); } //商城名称 if ($print_template['head'] == 1) { $content .= "
" . $site_name . "
"; $content .= str_repeat('.', 32); } if (!empty($order_info['pay_time'])) { $content .= "订单时间:" . date("Y-m-d H:i", $order_info['pay_time']) . "\n"; } else { $content .= "订单时间:" . date("Y-m-d H:i", time()) . "\n"; } $content .= "订单编号:" . $order_info['order_no'] . "\n"; $content .= str_repeat('.', 32); $content .= ""; $content .= ""; $content .= "
商品名称数量金额
"; $content .= str_repeat('.', 32); $content .= ""; foreach ($order_info['order_goods'] as $goods) { $sku_name_list = $this->r_str_pad_1($goods['sku_name'], 7); foreach ($sku_name_list as $index => $value) { if ($index == 0) { $content .= ""; } else { $content .= ""; } } } $content .= "
" . $value . "x" . numberFormat($goods['num']) . "¥" . $goods['price'] . "
" . $value . "
"; $content .= str_repeat('.', 32); if ($order_info["goods_money"] > 0) { $content .= "商品总额:¥" . $order_info["goods_money"] . "\n"; } if ($order_info["coupon_money"] > 0) { $content .= "店铺优惠券:¥" . $order_info["coupon_money"] . "\n"; } if ($order_info["promotion_money"] > 0) { $content .= "店铺优惠:¥" . $order_info["promotion_money"] . "\n"; } if ($order_info["point_money"] > 0) { $content .= "积分抵扣:¥" . $order_info["point_money"] . "\n"; } if ($order_info["adjust_money"] > 0) { $content .= "订单调价:¥" . $order_info["adjust_money"] . "\n"; } if ($order_info['reduction'] > 0) { $content .= "订单减免:¥" . $order_info["reduction"] . "\n"; } if ($order_info["balance_money"] > 0) { $content .= "余额抵扣:¥" . $order_info["balance_money"] . "\n"; } if ($order_info["delivery_money"] > 0) { $content .= "配送费用:¥" . $order_info["delivery_money"] . "\n"; } if ($order_info["invoice_money"] > 0) { $content .= "发票费用:¥" . $order_info["invoice_money"] . "\n"; } if ($order_info["invoice_delivery_money"] > 0) { $content .= "发票邮寄费用:¥" . $order_info["invoice_delivery_money"] . "\n"; } if ($order_info["goods_num"] > 0) { $content .= "订单共" . numberFormat($order_info['goods_num']) . "件商品,总计: ¥" . $order_info['order_money'] . " \n"; } $content .= str_repeat('.', 32); /******************** 备注信息 **************************/ //买家留言 if ($print_template['buy_notes'] == 1) { $content .= "买家留言:" . $order_info["buyer_message"] . "\n"; $content .= str_repeat('.', 32); } //卖家留言 if ($print_template['seller_notes'] == 1) { $content .= "卖家留言:" . $order_info["remark"] . "\n"; $content .= str_repeat('.', 32); } /******************** 买家信息 **************************/ //买家姓名 if ($print_template['buy_name'] == 1) { $content .= "" . $order_info["name"] . "\n"; } //联系方式 if ($print_template['buy_mobile'] == 1) { $content .= "" . $order_info["mobile"] . "\n"; } //地址 if ($print_template['buy_address'] == 1) { $content .= "" . $order_info['full_address'] . "-" . $order_info['address'] . "\n"; } if ($print_template['buy_name'] == 1 || $print_template['buy_mobile'] == 1 || $print_template['buy_address'] == 1) { $content .= str_repeat('.', 32); } /******************** 商城信息 **************************/ //联系方式 if ($print_template['shop_mobile'] == 1) { $content .= "" . $shop_info["mobile"] . "\n"; } //地址 if ($print_template['shop_address'] == 1) { $content .= "" . $shop_info['province_name'] . $shop_info['city_name'] . $shop_info['district_name'] . $shop_info['address'] . "\n"; } if ($print_template['shop_mobile'] == 1 || $print_template['shop_address'] == 1) { $content .= str_repeat('.', 32); } //二维码 if ($print_template['shop_qrcode'] == 1) { $content .= "" . $print_template['qrcode_url'] . ""; $content .= str_repeat('.', 32); } /******************** 门店信息 **************************/ if ($order_info['delivery_store_id'] > 0 && !empty($order_info['delivery_store_name']) && !empty($order_info['delivery_store_info'])) { $store_info = json_decode($order_info['delivery_store_info'], true); $content .= "" . $order_info["delivery_store_name"] . "\n";//门店名称 $content .= "" . $store_info["telphone"] . "\n";//门店电话 $content .= "" . $store_info["full_address"] . "\n";//门店地址 $content .= str_repeat('.', 32); } //底部内容 if (!empty($print_template['bottom'])) { $content .= "
" . $print_template['bottom'] . "
"; } try { $print->index($machine_code, $content, $origin_id); } catch (\Exception $e) { echo $e->getMessage(); } } /** * 补齐空格 * 每n个中文字符长度为一个数组元素 */ public function r_str_pad_1($input, $n = 7) { $string = ""; $count = 0; $c_count = 0; $arr = array(); for ($i = 0; $i < mb_strlen($input, 'UTF-8'); $i++) { $char = mb_substr($input, $i, 1, 'UTF-8'); $string .= $char; if (strlen($char) == 3) { $count += 2; $c_count++; } else { $count += 1; } if ($count >= $n * 2) { $arr[] = $string; $string = ''; $count = 0; $c_count = 0; } } if ($count < $n * 2) { $string = str_pad($string, $n * 2 + $c_count); $arr[] = $string; } return $arr; } /************************************************ 正式打印 end ******************************************************************/ /************************************************ 测试打印 start ******************************************************************/ /** * 测试打印 * @param $printer_id * @param $site_id * @return array */ public function testPrint($printer_id, $site_id) { //获取打印机列表 $printer_info = model('printer')->getInfo([['site_id', '=', $site_id], ['printer_id', '=', $printer_id]]); if (empty($printer_info)) { return $this->success(); } switch ($printer_info['brand']) { case '365'://365打印机 break; case 'feie'://飞鹅打印机 $res = $this->testFeieprint($printer_info); break; case 'yilianyun'://易联云打印机 $res = $this->testYlyprint($printer_info); break; } return $res; } /** * * @param $printer_info */ public function testYlyprint($printer) { $config = new YlyConfig($printer['open_id'], $printer['apikey']); $printer_model = new Printer(); $access_token = $printer_model->getYlyToken($config, $printer['printer_id']); $machine_code = $printer['printer_code']; //商户授权机器码 $origin_id = date('YmdHis') . rand(1, 999); //内部订单号(32位以内) /**文本接口开始**/ $print = new PrintService($access_token, $config); $content = "" . $printer['print_num'] . ""; $content .= "
小票名称
"; $content .= str_repeat('.', 32); $content .= "
商城名称
"; $content .= str_repeat('.', 32); $content .= "订单时间:" . date("Y-m-d H:i") . "\n"; $content .= "订单编号:" . $origin_id . "\n"; $content .= str_repeat('.', 32); $content .= ""; $content .= ""; $content .= "
商品名称数量金额
"; $content .= str_repeat('.', 32); $content .= ""; $content .= ""; $content .= ""; $content .= ""; $content .= "
烤土豆(超级辣)x35
烤豆干(超级辣)x210
烤鸡翅(超级辣)x315
"; $content .= str_repeat('.', 32); $content .= "商品总额:¥30 \n"; $content .= "订单共8件商品,总计: ¥30 \n"; $content .= str_repeat('.', 32); /******************** 备注信息 **************************/ //买家留言 $content .= "买家留言:微辣,多放孜然\n"; $content .= str_repeat('.', 32); $content .= "
谢谢惠顾,欢迎下次光临
"; try { $res = $print->index($machine_code, $content, $origin_id); if ($res->error == 0) { return $this->success(); } else { return $this->error('', $res->error_description); } } catch (\Exception $e) { return $this->error('', $e->getMessage()); } } /** * 测试飞鹅打印机 * @param $printer * @return array */ public function testFeieprint($printer) { //拼凑订单内容时可参考如下格式 //根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式 $content = '测试打印
'; $content .= '名称      单价 数量 金额
'; $content .= '--------------------------------
'; $content .= '饭       10.0 10 100.0
'; $content .= '炒饭      10.0 10 100.0
'; $content .= '蛋炒饭     10.0 10 100.0
'; $content .= '鸡蛋炒饭    10.0 10 100.0
'; $content .= '西红柿炒饭   10.0 10 100.0
'; $content .= '西红柿蛋炒饭  10.0 10 100.0
'; $content .= '西红柿鸡蛋炒饭 10.0 10 100.0
'; $content .= '--------------------------------
'; $content .= '备注:加辣
'; $content .= '合计:xx.0元
'; $content .= '送货地点:广州市南沙区xx路xx号
'; $content .= '联系电话:13888888888
'; $content .= '订餐时间:2014-08-08 08:08:08
'; $content .= 'https://mp.xgumi.com';//把二维码字符串用标签套上即可自动生成二维码 try { $time = time(); //请求时间 $msgInfo = array( 'user' => $printer['user'], 'stime' => $time, 'sig' => sha1($printer['user'] . $printer['ukey'] . $time), 'apiname' => 'Open_printMsg', 'sn' => $printer['printer_code'], 'content' => $content, 'times' => $printer['print_num'] //打印次数$print_template ); $client = new FeieClient('api.feieyun.cn', 80); if (!$client->post('/Api/Open/', $msgInfo)) { echo 'error'; } else { //服务器返回的JSON字符串,建议要当做日志记录起来 $result = json_decode($client->getContent(), true); $result['msg'] = $result['msg']; echo json_encode($result); } } catch (\Exception $e) { return $this->error('', $e->getMessage()); } } }