store_id = $store_id;
$this->store = Store::findOne($store_id);
$this->order_id = $order_id;
}
public function print_order()
{
$this->setting = PrinterSetting::findOne(['store_id' => $this->store_id]);//打印设置
if (!$this->setting) {
return false;
}
$this->printer = Printer::findOne(['id' => $this->setting->printer_id]);
if (!$this->printer) {
return false;
}
$this->printer_setting = json_decode($this->printer->printer_setting, true);//打印机配置
if ($this->printer->printer_type == 'kdt2') {
return $this->printer_1();
}
if ($this->printer->printer_type == 'yilianyun-k4') {
return $this->printer_2();
}
if ($this->printer->printer_type == 'feie') {
return $this->printer_3();
}
}
/**
* 365打印机
*/
private function printer_1()
{
$order = PtOrder::findOne(['id' => $this->order_id, 'is_delete' => 0]);
$addtime = date('Y-m-d H:i:s', $order->addtime);
$goods_list = $this->getPtOrderGoodsList($order->id);
header("Content-Type: text/html;charset=utf-8");
$DEVICE_NO = $this->printer_setting['name'];
$key = $this->printer_setting['key'];
$content = "";
$content .= "
";
$content .= "订单号:{$order->order_no}
";
$content .= "下单时间:{$addtime}
";
$content .= "--------------------------------
";
$content .= "名称 单价 数量 金额
";
$content .= "--------------------------------
";
foreach ($goods_list as $k => $v) {
$price = round($v['total_price'] / $v['num'], 2);
$content .= $this->r_str_pad($v['name']) . " " . str_pad($price, 5) . " " . str_pad($v['num'], 4) . " " . round($v['total_price'], 2) . '
';
}
if ($order->content) {
$content .= "备注:{$order->content}
";
}
$content .= "--------------------------------
";
$content .= "运费:{$order->express_price}元
";
$content .= "总计:{$order->total_price}元
";
$content .= "实付:{$order->pay_price}元
";
$content .= "收货人:{$order->name}
";
$content .= "收货地址:{$order->address}
";
$content .= "收货人电话:{$order->mobile}
";
// $content .= "
";
$result = $this->sendSelfFormatOrderInfo($DEVICE_NO, $key, 1, $content);
\Yii::warning('==>>' . $result);
return $result;
}
/**
* 365打印机
*/
private function sendSelfFormatOrderInfo($device_no, $key, $times, $orderInfo)
{
// $times打印次数
$selfMessage = array(
'deviceNo' => $device_no,
'printContent' => $orderInfo,
'key' => $key,
'times' => $times
);
$url = "http://open.printcenter.cn:8080/addOrder";
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded ",
'method' => 'POST',
'content' => http_build_query($selfMessage),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
/**
* @param $order_id
* @return mixed
* 订单商品详情
*/
public function getPtOrderGoodsList($order_id)
{
$order_detail_list = PtOrderDetail::find()->alias('od')
->leftJoin(['g' => PtGoods::tableName()], 'od.goods_id=g.id')
->where([
'od.is_delete' => 0,
'od.order_id' => $order_id,
])->select('od.*,g.name')->asArray()->all();
return $order_detail_list;
}
/**
* 补齐空格
* 截取七个中文字符长度
*/
private function r_str_pad($input)
{
$string = "";
$count = 0;
$c_count = 0;
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 >= 14) {
break;
}
}
if ($count < 14) {
$string = str_pad($string, 14 + $c_count);
}
return $string;
}
/**
* 易联云打印机-k4
*/
private function printer_2()
{
$machine_code = $this->printer_setting['machine_code'];//终端编号
$key = $this->printer_setting['key'];//终端密钥
$client_id = $this->printer_setting['client_id'];//用户ID
$client_key = $this->printer_setting['client_key'];//用户密钥
$time = $this->printer_setting['time'];//打印联数
$order = PtOrder::findOne(['id' => $this->order_id, 'is_delete' => 0]);
$addtime = date('Y-m-d H:i:s', $order->addtime);
$goods_list = $this->getPtOrderGoodsList($order->id);
$print = new Yprint();
$content = "
名称 | 数量 | 单价 |
" . $this->r_mb_str($v['name'], 8) . " | " . $v['num'] . " | " . $price . " |