host = env('PRINT_HOST', 'http://api.poscom.cn'); $this->port = env('PRINT_PORT', '80'); $this->apiKey = env('PRINT_API_KEY', 'CBGZ8271CWDTOKCGVVP2CTR69AOWT21U'); $this->memberCode = env('PRINT_MEMBERCODE', '5BA41258D12501C9A7DC527DF6CE0169'); } public function sendMessage($msgDetail, $deviceNo, $msgNo, $data = []) { $this->reqTime = $this->getMillisecond(); $securityCode = md5($this->memberCode . $deviceNo . $msgNo . $this->reqTime . $this->apiKey); $content['reqTime'] = $this->reqTime; $content['memberCode'] = $this->memberCode; $content['deviceNo'] = $deviceNo; $content['securityCode'] = $securityCode; $url = $this->host . ':' . $this->port . '/apisc/sendMsg'; $content['charset'] = 1; // 打印内容 $content['msgDetail'] = $msgDetail; $content['msgNo'] = $msgNo; // reprint 是否验证订单编号,1:不验证订单编号,可重新打印订单 $content['reprint'] = 0; // multi 是否多订单模式,默认0:为单订单模式,1:多订单模式, // 多订单模式下 $msgDetail 为json格式,格式为{"ordernum001":"msgDetail001","ordernum002":"msgDetail002"} // 多订单模式下订单编号不能重复,必须填写。建议最大订单数为50 $content['multi'] = $data['multi'] ?? $this->multi; // 打印类型 $content['mode'] = 2; // 打印联数 $content['times'] = $data['times'] ?? ''; $res = $this->request_post($url, $content); $res = json_decode($res, true); if ($res['code'] == 0) { $res['status'] = 'SUCCESS'; } return $res; } public function cancelPrint($deviceNo) { $this->reqTime = $this->getMillisecond(); //安全校验码 md5(memberCode+reqTime+apiKey+deviceID) $securityCode = md5($this->memberCode . $this->reqTime . $this->apiKey . $deviceNo); $url = $this->host . ':' . $this->port . '/apisc/cancelPrint'; $content['memberCode'] = $this->memberCode; $content['reqTime'] = $this->reqTime; $content['securityCode'] = $securityCode; $content['deviceID'] = $deviceNo; $content['all'] = '1'; $res = $this->request_post($url, $content); $res = json_decode($res, true); if ($res['code'] == 1) { $res['status'] = 'SUCCESS'; } return $res; } public function getMillisecond() { list($t1, $t2) = explode(' ', microtime()); return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000); } /** * 模拟post进行url请求 * @param string $url * @param array $post_data * @return mixed */ public function request_post($url = '', $post_data = array()) { if (empty($url) || empty($post_data)) { return false; } $postUrl = $url; $curlPost = $post_data; $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL, $postUrl); curl_setopt($ch, CURLOPT_HEADER, 0);//设置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch);//运行curl curl_close($ch); return $data; } /** * 小票机打印 */ public function xiaopiaoPrint(Order $order){ //处理打印数据; $print_message_data = ''; $data['multi'] = 0; $goods_list = OrderGoods::with('goods')->where('order_id', $order->id)->get(); $print_message_data .= $this->addWord('取餐号:'.$order->order_number, [ 'Align' => 1, 'Bold' => 0, 'Wsize' => 1, 'Hsize' => 1 ]); $print_message_data .= $this->addBr(); $print_message_data .= $this->addLine(); $print_message_data .= $this->addWord('商户名称:'. env('MEN_DIAN', '大坪时代天街店')); $print_message_data .= $this->addWord('订单编号:'. $order->order_sn); $print_message_data .= $this->addWord('下单时间:'. $order->created_at->format('Y-m-d H:i:s')); $print_message_data .= $this->addWord('订单金额:'. number_format($order->order_price, 2).'元'); //组装商品名称 $goods_name = ''; foreach($goods_list as $goods){ $goods_name .= $goods->goods_name; $_goods_attr = json_decode($goods->goods_attrs, true); $_attrs = []; foreach($_goods_attr['info_name'] as $attr){ $_attrs[] = explode(':', $attr)[1]; } if($_attrs){ $goods_name .= '【'.implode(',', $_attrs).'】'; } $goods_name .= '*'.$goods->goods_num.', '; } $print_message_data .= $this->addWord('餐品详情:'. rtrim($goods_name, ',')); $print_message_data .= $this->addBr(); $print_message_data .= $this->addWord('RMB:'.number_format($order->pay_price, 2), [ 'Align' => 1, 'Bold' => 1, 'Wsize' => 1, 'Hsize' => 1 ]); $print_message_data .= $this->addBr(); $print_message_data .= $this->addWord('打印时间:'. Carbon::now()->format('Y-m-d H:i:s')); $print_message_data .= $this->addLine(); $print_message_data .= $this->addBr(4); return $this->sendMessage($print_message_data, '00135465922814466', $order->order_sn, $data); } /** * 标签机打印 */ public function biaoqianPrint(Order $order){ //处理打印数据; $print_message_data = []; $data['multi'] = 0; $goods_list = OrderGoods::with('goods')->where('order_id', $order->id)->get(); $i = 0; foreach($goods_list as $goods){ for ($j = 1; $j<=$goods->goods_num; $j++){ $i++; $_key = $order->order_sn.'-'.str_pad($i, 3,'0',STR_PAD_LEFT); $_goods_attr = json_decode($goods->goods_attrs, true); $_attrs = []; foreach($_goods_attr['info_name'] as $attr){ $_attrs[] = explode(':', $attr)[1]; } $print_message_data[$_key] = $this->biaoqianModel($order->order_number, $goods->goods->goods_name, implode(',', $_attrs)); } } if(count($print_message_data) > 1){ $msgDetail = json_encode($print_message_data); $data['multi'] = 1; }else{ $msgDetail = reset($print_message_data); } return $this->sendMessage($msgDetail, '00135465920907150', $order->order_sn, $data); } public function biaoqianModel($order_number, $goods_name, $goods_attrs){ return 'SIZE 30 mm,20 mm GAP 2 mm,0 mm REFERENCE 0,0 SPEED 2 DENSITY 8 DIRECTION 0 SET HEAD ON SET PRINTKEY OFF SET KEY1 ON SET KEY2 ON SHIFT 0 CLS TEXT 21,35,"TSS24.BF2",0,2,2,"'.$order_number.'" TEXT 112,38,"TSS24.BF2",0,1,1,"'.$this->toPrintStr($goods_name, 4).'" TEXT 21,96,"TSS24.BF2",0,1,1,"'.$this->toPrintStr($goods_attrs, 8).'" PRINT 1,1'; } /** * 按几个字换行,处理 * \[A] */ public function toPrintStr($strs, $num){ $length = mb_strlen($strs); $to_str = ''; for($i = 0; $i<($length/$num); $i++){ $to_str .= mb_substr($strs, $i*$num, $num); $to_str .= "\[A]"; } $to_str = rtrim($to_str, "\[A]"); return $to_str; } protected function addLine($options = []) { return $this->addWord('--------------------------------', $options); } /** * 添加文本 * * @param string $word 文本内容 * @param array $options 配置 [ * Align(对齐) => 0: 左对齐, 1: 居中, 2: 右对齐 * Bold(加粗) => 0: 不加粗, 1: 加粗 * Wsize(字体宽度) => 倍数计算,范围[0, 8] * Hsize(字体高度) => 倍数计算,范围[0, 8] * Reverse(反显) => 0: 不反显(黑字白底), 1: 反显(黑底白字) * Underline(下划线) => 0: 没下划线, 1: 有下划线 * ] * @return string */ protected function addWord($word, $options = []) { $options = array_merge([ 'Align' => 0, 'Bold' => 0, 'Wsize' => 0, 'Hsize' => 0, 'Reverse' => 0, 'Underline' => 0 ], $options); $align = $options['Align']; $bold = $options['Bold']; $width = $options['Wsize']; $height = $options['Hsize']; $reverse = $options['Reverse']; $underline = $options['Underline']; return "$word"; } /** * 添加图片 * * 图片格式jpg,png,gif * 图片最大宽高 (576px X 3000px) * 图片最大1024KB 不符合要求的图片将打印不成功 * * @param string $url 图片地址(网络地址) * @param array $options 配置 [ * Align(对齐) => 0: 左对齐, 1: 居中, 2: 右对齐 * ] * @return string */ protected function addImage($url, $options = []) { $options = array_merge([ 'Align' => 0, ], $options); $align = $options['Align']; return "$url"; } /** * 添加表格(多个) * * @param array $words 文本数组 * @param array $options 配置 [ * Type (打印机宽度类型) => 0: 58mm,1: 80mm * ] * @return string */ protected function addGroup(Array $words, $options = []) { $count = count($words); $options = array_merge([ 'Type' => 0, ], $options); $type = $options['Type']; $str = ""; foreach ($words as $value) { $str .= "$value"; } return $str . ""; } protected function addBr($count = 1) { $str = ""; for ($i = 0; $i < $count; $i++) { $str .= ""; } return $str; } protected function addCut() { return ""; } protected function addLogo() { return ""; } protected function addBox() { return ""; } }