Initial commit: 小众厨房项目代码

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

查看文件

@@ -0,0 +1,85 @@
<?php
namespace addons\shopro\job;
use addons\shopro\library\traits\ActivityCache;
use addons\shopro\library\traits\Groupon;
use addons\shopro\library\traits\StockSale;
use addons\shopro\model\Config;
use addons\shopro\model\GoodsComment;
use addons\shopro\model\Order;
use addons\shopro\model\OrderAction;
use addons\shopro\model\OrderItem;
use think\queue\Job;
/**
* 订单支付完成
*/
class OrderPayed extends BaseJob
{
use StockSale, ActivityCache, Groupon;
/**
* 订单支付完成
*/
public function payed(Job $job, $data)
{
try {
$order = $data['order'];
$user = $data['user'];
$order = Order::with('item')->where('id', $order['id'])->find();
// 数据库删订单的问题常见,这里被删的订单直接把队列移除
if ($order) {
\think\Db::transaction(function () use ($order, $user, $data) {
// 订单减库存
$this->realForwardStockSale($order);
// 判断,如果是拼团 真实加入团
if (strpos($order['activity_type'], 'groupon') !== false) {
$this->joinGroupon($order, $user);
}
// 处理发票审核
if ($order->invoice_status == 1) {
$invoice = \addons\shopro\model\OrderInvoice::get(['order_id' => $order->id]);
if ($invoice) {
$invoice->status = 0;
$invoice->save();
}
}
// 处理消费返积分 TODO: 待测试
$scoreConfig = Config::where('name', 'score')->find();
$scoreConfig = json_decode($scoreConfig['value'], true);
if (!empty($scoreConfig['consume_get_score']) && !empty($scoreConfig['consume_get_score_ratio'])) {
$scoreRatio = intval($scoreConfig['consume_get_score_ratio']);
$score = intval($scoreRatio * 0.01 * $order->total_fee);
if ($score > 0) {
\addons\shopro\model\User::score($score, $user['id'], 'consume_get_score', $order['id']);
}
}
// 检测有没有自动发货的商品,有就自动发货
$order->checkDispatchAndSend($order, ['user' => $user]);
});
// 支付成功的外部副作用必须在事务提交后触发,避免数据库回滚时已经打印小票。
\think\Hook::listen('order_payed_after', ['order' => $order]);
}
// 删除 job
$job->delete();
} catch (\Exception $e) {
// 队列执行失败
$error = json_encode([
'a' => $e->getLine(),
'b' => $e->getFile(),
'c' => $e->getTrace(),
'd' => $e->getMessage()
], JSON_UNESCAPED_UNICODE);
\think\Log::error('queue-' . get_class() . '-payed' . ':执行失败,错误信息:' . $error);
}
}
}

查看文件

@@ -0,0 +1,99 @@
<?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 [
'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,
'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,45 @@
<?php
namespace addons\shopro\job;
use app\common\library\PrinterService;
use think\Db;
use think\queue\Job;
class OrderPrintStatus extends BaseJob
{
public function handle(Job $job, $data)
{
$taskId = isset($data['task_id']) ? (int)$data['task_id'] : 0;
$task = $taskId ? Db::name('shopro_order_print_task')->where('id', $taskId)->find() : null;
if (!$task || (int)$task['status'] !== 3 || empty($task['cloud_order_id'])) {
$job->delete();
return;
}
$result = (new PrinterService())->queryOrderState($task['cloud_order_id']);
$attempts = (int)$task['verify_attempts'] + 1;
$update = [
'verify_attempts' => $attempts,
'updated_at' => time(),
'printer_response' => $result['response'],
'last_error' => !empty($result['accepted']) ? '' : $result['error'],
];
if (!empty($result['printed'])) {
$update['status'] = 1;
$update['printed_at'] = time();
Db::name('shopro_order_print_task')->where('id', $taskId)->update($update);
$job->delete();
return;
}
Db::name('shopro_order_print_task')->where('id', $taskId)->update($update);
if ($attempts < 30) {
\think\Queue::later(60, '\\addons\\shopro\\job\\OrderPrintStatus@handle', ['task_id' => $taskId], 'shopro-high');
} else {
\think\Log::warning('订单小票云端状态未确认,任务ID ' . $taskId);
}
$job->delete();
}
}