Initial commit: 小众厨房项目代码

Co-authored-by: Cursor <cursoragent@cursor.com>
这个提交包含在:
可乐加冰
2026-09-23 17:04:10 +08:00
当前提交 f7d3d7d1bd
共有 1761 个文件被更改,包括 453668 次插入 和 0 次删除

查看文件

@@ -0,0 +1,98 @@
<?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 || in_array((int)$task['status'], [1, 3], true)) {
$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([
// 云端已接单,仍需查询 queryOrderState 确认物理打印完成。
'status' => 3,
'cloud_order_id' => $result['cloud_order_id'],
'printer_response' => $result['response'],
'updated_at' => time(),
'last_error' => '',
]);
\think\Queue::later(30, '\\addons\\shopro\\job\\OrderPrintStatus@handle', ['task_id' => $task['id']], 'shopro-high');
$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 [
'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,
'print_idempotent' => 'shopro-' . $order['id'],
];
}
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(),
]);
}
}

查看文件

@@ -0,0 +1,18 @@
CREATE TABLE IF NOT EXISTS `fa_shopro_order_print_task` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`order_id` int(10) unsigned NOT NULL,
`order_sn` varchar(64) NOT NULL DEFAULT '',
`status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0待处理,1已打印,2处理中,3云端已接单,-1失败',
`attempts` int(10) unsigned NOT NULL DEFAULT '0',
`verify_attempts` int(10) unsigned NOT NULL DEFAULT '0',
`cloud_order_id` varchar(64) NOT NULL DEFAULT '',
`manual_print_key` varchar(100) NOT NULL DEFAULT '',
`printer_response` varchar(1000) NOT NULL DEFAULT '',
`last_error` varchar(500) NOT NULL DEFAULT '',
`created_at` int(10) unsigned NOT NULL,
`updated_at` int(10) unsigned NOT NULL,
`printed_at` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_order_id` (`order_id`),
KEY `idx_status_updated_at` (`status`,`updated_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单云打印任务';