You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

186 lines
7.1 KiB

<?php
namespace App\Service\v3\Implementations;
use App\Libs\FeiePrintClient;
use App\Model\v3\OrderMain;
use App\Service\v3\Interfaces\FeiePrintServiceInterface;
use Hyperf\DbConnection\Db;
class FeiePrintService implements FeiePrintServiceInterface
{
// *必填*:飞鹅云后台注册账号
const USER = '13161443713@163.com';
// *必填*: 飞鹅云后台注册账号后生成的UKEY 【备注:这不是填打印机的KEY】
const UKEY = 'XsaHzgePdyWTfcMX';
// *必填*:打印机编号,必须要在管理后台里添加打印机或调用API接口添加之后,才能调用API
const SN = '550510805';
// 以下参数不需要修改
// 接口IP或域名
const IP = 'api.feieyun.cn';
// 接口IP端口
const PORT = 80;
// 接口路径
const PATH = '/Api/Open/';
protected $feieUser = '';
protected $feieUkey = '';
protected $feieHost = '';
protected $feiePort = '';
protected $feieApiPath = '';
public function __construct()
{
$this->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();
if (empty($data)) {
return ;
}
// foreach ($data['order'] as $key => &$item) {
// $item = (array)$item;
// }
$content = $this->printFormat($data, 4, 14, 7, 7);
$res = $this->printMsg('920527381', $content, 1);
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 = '<CB>懒族生活</CB><BR>';
$orderInfo .= '数量 名称 单价 金额<BR>';
$orderInfo .= '--------------------------------<BR>';
//$shopnum 当前为第几个店铺
$shopnum = 0;
//循环处理子订单
foreach ($arr->orders as $k5 => $order) {
$orderInfo .= ' <BR>';
$shopnum++;
$orderInfo .= "<C>(" . $shopnum . ")" .$order->store->name . '</C><BR>';
$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 .= '<BR> ';
$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 .= '<BR>';
$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 .= '--------------------------------<BR>';
//订单小计
$orderInfo .= $this->space($subNum,$order->money);
if(!empty($order->note)){
$orderInfo .='<BR>';
$orderInfo .='<BR>';
$note = '备注:'.$order->note;
$orderInfo .= $note.'<BR>';
}
}
$orderInfo .= '--------------------------------<BR>';
if ($arr->services_money > 0) {
$orderInfo .= $this->space('服务费:',$arr->services_money);
}
if($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 .= '<BR>';
$orderInfo .= '送货地点:' . $arr->address . '<BR>';
$tel = substr_replace( $arr->tel, '****', 3, 4);
$orderInfo .= '联系电话:' . $tel . '<BR>';
$orderInfo .= '下单时间:' . $arr->pay_time_text . '<BR>';
$orderInfo .= '送达时间:' . $arr->delivery_time_note . '<BR>';
//$orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
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;
}
}