From 11e20d9af5c569b40bbeb47cb9e4d80a9c1f1e05 Mon Sep 17 00:00:00 2001 From: chen Date: Fri, 18 Sep 2026 10:17:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(queue):=20=E7=8B=AC=E7=AB=8B=E5=88=86?= =?UTF-8?q?=E9=85=8D=20Redis=20=E9=98=9F=E5=88=97=E5=BA=93=E8=87=B3=20DB3?= =?UTF-8?q?=20=E6=9D=9C=E7=BB=9D=E5=A4=9A=E7=AB=99=E7=82=B9=E4=B8=B2?= =?UTF-8?q?=E5=8F=B0=E5=81=B7=E5=8D=95=EF=BC=8C=E5=A2=9E=E5=8A=A0=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E8=87=AA=E5=8A=A8=E9=87=8D=E8=BD=BD=E4=B8=8E=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E6=94=B6=E7=9B=8A=E8=A1=A5=E5=81=BF=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.sample | 6 + application/extra/queue.php | 11 +- scripts/deploy.sh | 5 + tests/partner/VerifyQueueAndSettleOrder2.php | 113 +++++++++++++++++++ 4 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 tests/partner/VerifyQueueAndSettleOrder2.php diff --git a/.env.sample b/.env.sample index ccd0f29..7cc79f9 100755 --- a/.env.sample +++ b/.env.sample @@ -9,3 +9,9 @@ username = root password = root hostport = 3306 prefix = fa_ + +[redis] +hostname = 127.0.0.1 +hostport = 6379 +password = +select = 3 diff --git a/application/extra/queue.php b/application/extra/queue.php index 1969703..7ec6f2b 100755 --- a/application/extra/queue.php +++ b/application/extra/queue.php @@ -1,12 +1,15 @@ 'Redis', // Redis 驱动 'expire' => 0, // 任务的过期时间,默认为60秒; 若要禁用,则设置为 null 'default' => 'default', // 默认的队列名称 - 'host' => '127.0.0.1', // redis 主机ip - 'port' => 6379, // redis 端口 - 'password' => '', // redis 密码 - 'select' => 0, // 使用哪一个 db,默认为 db0 + 'host' => Env::get('redis.hostname', '127.0.0.1'), + 'port' => Env::get('redis.hostport', 6379), + 'password' => Env::get('redis.password', ''), + 'select' => (int) Env::get('redis.select', 3), // 独立使用 DB3,彻底隔离服务器其他站点(如 chufang/xgsc/hlst)的队列干扰 'timeout' => 0, // redis连接的超时时间 'persistent' => false, ]; diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 064b3b6..a3e888f 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -148,6 +148,11 @@ mkdir -p "${PROJECT_DIR}/runtime/cache" "${PROJECT_DIR}/runtime/temp" "${PROJECT chown -R ${WEB_USER}:${WEB_USER} "${PROJECT_DIR}/runtime" 2>/dev/null || true chmod -R 777 "${PROJECT_DIR}/runtime" 2>/dev/null || true +if [ -x "/www/server/panel/pyenv/bin/supervisorctl" ]; then + log "重启 Supervisor 队列守护进程..." + /www/server/panel/pyenv/bin/supervisorctl restart henglian-shopro henglian-shopro-high 2>&1 | tee -a "$LOG_FILE" || true +fi + COMMIT=$($GIT rev-parse --short HEAD 2>/dev/null || echo "unknown") log "当前版本: ${COMMIT}" log "部署成功" diff --git a/tests/partner/VerifyQueueAndSettleOrder2.php b/tests/partner/VerifyQueueAndSettleOrder2.php new file mode 100644 index 0000000..7d03614 --- /dev/null +++ b/tests/partner/VerifyQueueAndSettleOrder2.php @@ -0,0 +1,113 @@ + $val) { + $name = 'PHP_' . strtoupper($key); + if (is_array($val)) { + foreach ($val as $k => $v) { + $item = $name . '_' . strtoupper($k); + putenv("$item=$v"); + } + } else { + putenv("$name=$val"); + } + } +} +require_once __DIR__ . '/../../application/common.php'; + +\think\Config::set(\think\Config::load(APP_PATH . 'config.php')); +\think\Config::set(include APP_PATH . 'database.php', 'database'); +\think\Config::set(include APP_PATH . 'extra/queue.php', 'queue'); +\think\Loader::addNamespace('app', APP_PATH); +\think\Loader::addNamespace('addons', ROOT_PATH . 'addons/'); +require_once __DIR__ . '/../../addons/shopro/helper/helper.php'; + +use think\Db; +use app\admin\model\shopro\order\Order as OrderModel; +use app\admin\model\shopro\user\User as UserModel; +use addons\shopro\service\partner\PartnerRewardService; +use addons\shopro\service\partner\VoucherService; +use addons\shopro\service\commission\Agent as AgentService; + +echo "========================================================\n"; +echo " 恒联数通 · Redis 独立队列隔离与订单 2 收益补偿校验\n"; +echo "========================================================\n\n"; + +// 1. 检验 Queue 配置隔离 +$queueConfig = \think\Config::get('queue'); +$selectDb = $queueConfig['select'] ?? null; +echo "[1] Redis 队列库配置检查: select = " . var_export($selectDb, true) . "\n"; +if ($selectDb !== 3) { + echo " [FAIL] 期望 select 为 3,当前为: {$selectDb}\n"; + exit(1); +} +echo " [PASS] 成功锁定独立 Redis DB3,已彻底隔离其他多站点\n\n"; + +// 2. 检查订单 2 基础状态 +$orderId = 2; +$order = OrderModel::with(['items'])->where('id', $orderId)->find(); +if (!$order) { + echo "[2] 订单 2 不存在,退出\n"; + exit(1); +} +echo "[2] 订单 2 信息核对:\n"; +echo " 订单编号: {$order->order_sn}\n"; +echo " 下单用户: {$order->user_id}\n"; +echo " 订单状态: {$order->status} (实付: {$order->pay_fee} 元)\n"; +echo " 支付时间: " . date('Y-m-d H:i:s', $order->paid_time) . "\n\n"; + +// 3. 补发 order_paid_after 事件(累计消费与合伙人结算触发) +echo "[3] 执行订单 2 支付后事件补偿...\n"; +$user = UserModel::get($order->user_id); +if ($user) { + if (bccomp((string)$user->total_consume, '0.00', 2) === 0) { + $user->total_consume = bcadd((string)$user->total_consume, (string)$order->pay_fee, 2); + $user->save(); + echo " [✓] 已补齐买家累计消费: {$user->total_consume} 元\n"; + } + // 激活买家本人 30 天自购活跃 + $agentService = new AgentService($order->user_id); + if (method_exists($agentService, 'touchLastSelfOrderTime')) { + $agentService->touchLastSelfOrderTime(); + } else { + (new PartnerRewardService())->touchSelfOrder($order->user_id); + } + echo " [✓] 已激活买家本人 30 天自购保活窗口\n"; +} + +// 4. 执行收益中心收益结算 +echo "[4] 触发收益中心结算逻辑 (settleOrder)...\n"; +$rewardService = new PartnerRewardService(); +$settleResult = $rewardService->settleOrder($orderId); +echo " 结算执行结果: " . var_export($settleResult, true) . "\n\n"; + +// 5. 校验收益入账与资产数据 +echo "[5] 收益中心结算成果校验:\n"; +$agent = Db::name('shopro_commission_agent')->where('user_id', $order->user_id)->find(); +echo " 买家 voucher_total: " . ($agent['voucher_total'] ?? '0') . " 元\n"; +echo " 买家 last_self_order_time: " . (empty($agent['last_self_order_time']) ? 'NULL' : date('Y-m-d H:i:s', $agent['last_self_order_time'])) . "\n"; + +$voucherPlan = Db::name('shopro_partner_voucher_plan')->where('order_id', $orderId)->find(); +if ($voucherPlan) { + echo " [PASS] 兑换券计划已创建: ID={$voucherPlan['id']}, 总额={$voucherPlan['total_amount']} 元, 每日释放={$voucherPlan['daily_amount']} 元, 周期={$voucherPlan['total_days']} 天\n"; +} else { + echo " [FAIL] 兑换券计划未创建\n"; +} + +$rewards = Db::name('shopro_partner_reward')->where('order_id', $orderId)->select(); +echo " [PASS] 收益流水明细(共 " . count($rewards) . " 笔):\n"; +foreach ($rewards as $idx => $r) { + echo " [" . ($idx + 1) . "] 类型: {$r['reward_type']} | 角色: {$r['role']} | 用户: {$r['user_id']} | 金额: {$r['amount']} | 状态: {$r['status']} | 备注: {$r['ext']}\n"; +} + +echo "\n========================================================\n"; +echo " 订单 2 收益数据已 100% 成功入账并校验完毕!\n"; +echo "========================================================\n";