feieUser = config('feie.user'); $this->feieUkey = config('feie.ukey'); $this->feieHost = config('feie.host'); $this->feiePort = config('feie.port'); $this->feieApiPath = config('feie.api_path'); } public function feiePrint($globalOrderId) { // TODO 对象数组=》二维数组 $data = OrderMain::query()->with(['orders' => function($query){ $query->with('store','orderGoods'); } ])->where('global_order_id',$globalOrderId)->first(); $this->log->event('feieprint_rpc', ['order_data' => json_encode($data)]); if (empty($data)) { return ; } // foreach ($data['order'] as $key => &$item) { // $item = (array)$item; // } $printSn = Feprint::query()->where('market_id',$data->market_id)->value('sn'); $this->log->event('feieprint_rpc', ['printSn' => json_encode($printSn)]); if (env('APP_ENV') === 'dev') { $client = new FeiePrintClient($this->feieHost, $this->feiePort); $msgInfo = array( 'user' => $this->feieUser, 'stime' => time(), 'sig' => sha1($this->feieUser . $this->feieUkey . time()), 'apiname' => 'Open_queryPrinterStatus', 'sn' => $printSn ); $client->post($this->feieApiPath, $msgInfo); $result = json_decode($client->getContent(),true); if($result['data'] != '在线,工作状态正常。'){ return [ 'msg' => 'ok', 'ret' => 0, 'data' => '920527381_20200927151404_1155818771', 'serverExecutedTime' => 4 ]; } } $content = $this->printFormat($data, 4, 14, 7, 7); $this->log->event('feieprint_rpc', ['content' => json_encode($content)]); $res = $this->printMsg($printSn, $content, 1); $this->log->event('feieprint_rpc', ['res' => json_encode($res)]); return $res; } /** * [打印订单接口 Open_printMsg] * @param [string] $sn [打印机编号sn] * @param [string] $content [打印内容] * @param [string] $times [打印联数] * @return [string] [接口返回值] */ protected function printMsg($sn, $content, $times = 1) { $time = time(); //请求时间 $msgInfo = array( 'user' => $this->feieUser, 'stime' => $time, 'sig' => sha1($this->feieUser . $this->feieUkey . $time), 'apiname' => 'Open_printMsg', 'sn' => $sn, 'content' => $content, 'times' => $times//打印次数 ); $client = new FeiePrintClient($this->feieHost, $this->feiePort); if (!$client->post($this->feieApiPath, $msgInfo)) { echo 'error'; } else { // 服务器返回的JSON字符串,建议要当做日志记录起来 $result = $client->getContent(); return $result; } } protected function printFormat($arr, $A, $B, $C, $D) { $orderInfo = '懒族生活
'; $orderInfo .= '数量 名称 单价 金额
'; $orderInfo .= '--------------------------------
'; //$shopnum 当前为第几个店铺 $shopnum = 0; //循环处理子订单 foreach ($arr->orders as $k5 => $order) { $orderInfo .= '
'; $shopnum++; $orderInfo .= "(" . $shopnum . ")" .$order->store->name . '
'; $subNum = 0; //循环处理子订单下商品 foreach ($order['orderGoods'] as $goods){ //店铺商品数量小计 $subNum += $goods->number; $orderInfo .= str_pad($goods->number,$A,' ',STR_PAD_RIGHT); //商品名处理 $nameLength = mb_strwidth($goods->name); $nameArr = mb_str_split($goods->name); $length = $A; foreach ($nameArr as $name){ $len = mb_strwidth($name); $length += $len; if($length >= ($A + $B +$C + $D)){ $orderInfo .= '
'; $length = $A; } $orderInfo .= $name; } //商品名长度是否超过一行 $goodsTotal = bcmul($goods->number,$goods->price,2); $priceLen = mb_strwidth($goods->price); $totalLen = mb_strwidth($goodsTotal); if($nameLength >= ($B+$C+$D-$priceLen-$totalLen)){ $orderInfo .= '
'; $orderInfo .= str_pad($goods->price,$A+$B+$C,' ',STR_PAD_LEFT); }else{ $orderInfo .= str_pad($goods->price,$C+$B+$A-$length,' ',STR_PAD_LEFT); } $orderInfo .= str_pad($goodsTotal,$D,' ',STR_PAD_LEFT); } /** * 订单商品处理结束 */ //处理订单备注 $orderInfo .= '--------------------------------
'; //订单小计 $orderInfo .= $this->space($subNum,$order->money); if(!empty($order->note)){ $orderInfo .='
'; $orderInfo .='
'; $note = '备注:'.$order->note; $orderInfo .= $note.'
'; } } $orderInfo .= '--------------------------------
'; if ($arr->services_money > 0) { $orderInfo .= $this->space('服务费:',$arr->services_money); } if($arr->shipping_type != 3 && $arr->delivery_money > 0){ $orderInfo .= $this->space('配送费:',$arr->delivery_money); } if($arr->coupon_money > 0){ $orderInfo .= $this->space('红包:',$arr->coupon_money); } $total = '合计:'.$arr->money; $userName = $arr->name; if(strlen($userName)>18){ $userName=substr($userName,0,18).'...'; } $userLength = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $userName); $totalLength = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $total); $userLength = mb_strlen($userLength); $totalLength = mb_strlen($totalLength); $orderInfo .= str_pad($userName,$A+$B+$userLength,' ',STR_PAD_RIGHT); $orderInfo .= str_pad($total,$C+$D+$totalLength+1,' ',STR_PAD_LEFT); $orderInfo .= '
'; $orderInfo .= '配送方式:' . $arr->shipping_type_text . '
'; if($arr->shipping_type != Shipping::TYPE_SELF_TAKE){ $orderInfo .= '送货地点:' . $arr->address . '
'; } if(!empty($arr->tel)){ $tel = substr_replace( $arr->tel, '****', 3, 4); $orderInfo .= '联系电话:' . $tel . '
'; } $orderInfo .= '下单时间:' . $arr->pay_time_text . '
'; if($arr->shipping_type != 3 && $arr->delivery_money > 0) { $orderInfo .= '送达时间:' . $arr->delivery_time_note . '
'; }else{ $orderInfo .= '自提时间:' . $arr->delivery_time_note . '
'; } //$orderInfo .= 'http://www.feieyun.com';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码 return $orderInfo; } function space($name,$price,$len = 32) { $length = mb_strwidth($name); $str = $name; $str .= str_pad($price ,$len-$length,' ',STR_PAD_LEFT); return $str; } }