97 行
3.6 KiB
PHP
97 行
3.6 KiB
PHP
<?php
|
|
|
|
namespace addons\shopro\job;
|
|
|
|
use addons\shopro\model\Order;
|
|
use app\common\library\PrinterService;
|
|
use think\Db;
|
|
use think\queue\Job;
|
|
|
|
class OrderPrint extends BaseJob
|
|
{
|
|
public function handle(Job $job, $data)
|
|
{
|
|
$orderId = isset($data['order_id']) ? (int)$data['order_id'] : 0;
|
|
$task = $orderId ? Db::name('shopro_order_print_task')->where('order_id', $orderId)->find() : null;
|
|
|
|
if (!$task || (int)$task['status'] === 1) {
|
|
$job->delete();
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$order = Order::with('item')->where('id', $orderId)->find();
|
|
if (!$order) {
|
|
throw new \RuntimeException('订单不存在');
|
|
}
|
|
|
|
Db::name('shopro_order_print_task')->where('id', $task['id'])->update([
|
|
'status' => 2,
|
|
'attempts' => (int)$task['attempts'] + 1,
|
|
'updated_at' => time(),
|
|
'last_error' => '',
|
|
]);
|
|
|
|
$result = (new PrinterService())->printOrder($this->buildOrderData($order));
|
|
if (empty($result['accepted'])) {
|
|
$this->failTask($task['id'], !empty($result['error']) ? $result['error'] : '云打印未受理', $result);
|
|
$job->delete();
|
|
return;
|
|
}
|
|
|
|
Db::name('shopro_order_print_task')->where('id', $task['id'])->update([
|
|
'status' => 1,
|
|
'printer_response' => $result['response'],
|
|
'printed_at' => time(),
|
|
'updated_at' => time(),
|
|
'last_error' => '',
|
|
]);
|
|
$job->delete();
|
|
} catch (\Exception $e) {
|
|
if ($task) {
|
|
$this->failTask($task['id'], $e->getMessage(), []);
|
|
}
|
|
\think\Log::error('订单小票打印失败,订单ID ' . $orderId . ': ' . $e->getMessage());
|
|
$job->delete();
|
|
}
|
|
}
|
|
|
|
protected function buildOrderData($order)
|
|
{
|
|
$items = [];
|
|
foreach ((array)$order['item'] as $item) {
|
|
$items[] = [
|
|
'name' => (isset($item['goods_title']) ? $item['goods_title'] : '商品') . (!empty($item['goods_sku_text']) ? ' (' . $item['goods_sku_text'] . ')' : ''),
|
|
'num' => isset($item['goods_num']) ? $item['goods_num'] : 1,
|
|
'price' => isset($item['goods_price']) ? $item['goods_price'] : '0.00',
|
|
];
|
|
}
|
|
|
|
$address = (isset($order['province_name']) ? $order['province_name'] : '')
|
|
. (isset($order['city_name']) ? $order['city_name'] : '')
|
|
. (isset($order['area_name']) ? $order['area_name'] : '')
|
|
. (isset($order['address']) ? $order['address'] : '');
|
|
|
|
return [
|
|
'shop_name' => '小众厨房设备商城',
|
|
'order_sn' => $order['order_sn'],
|
|
'createtime' => $order['createtime'],
|
|
'consignee' => isset($order['consignee']) ? $order['consignee'] : '客户',
|
|
'phone' => isset($order['phone']) ? $order['phone'] : '',
|
|
'address' => $address ?: '到店自提/商家配送',
|
|
'total_fee' => isset($order['pay_fee']) ? sprintf('%.2f', $order['pay_fee']) : $order['total_fee'],
|
|
'items' => $items,
|
|
];
|
|
}
|
|
|
|
protected function failTask($taskId, $error, $result)
|
|
{
|
|
Db::name('shopro_order_print_task')->where('id', $taskId)->update([
|
|
'status' => -1,
|
|
'printer_response' => isset($result['response']) ? $result['response'] : '',
|
|
'last_error' => mb_substr($error, 0, 500, 'UTF-8'),
|
|
'updated_at' => time(),
|
|
]);
|
|
}
|
|
}
|