feat: 用户端与师傅端双小程序架构解耦,新增师傅独立认证及超时自动关单机制
1. 账号体系与数据库解耦: - 师傅表 fa_master 解除对 fa_user.id 的外键依赖,升级为独立认证账号主表; - 新增师傅端独立支撑表:fa_master_security、fa_master_token、fa_master_wechat、fa_master_agreement_read; - 新增工单超时未接单自动关闭与原路退款配置(order_unaccepted_close_days)。 2. 服务端(Server): - 新增师傅端独立 API 控制器与认证库(MasterAuth, MasterBase, MasterMessage, MasterSecurity, MasterToken); - WxLogin 支持双小程序端(user/master)独立换取 session_key、openid 与 access_token; - 新增控制台调度命令 php think order:autoclose,支持订单超时自动关闭与售后退款闭环; - 修复 PHP 8.5 兼容性问题(Request.php 的 (bool) 强制转换); - 系统业务配置全面收敛至数据库 fa_config 及 site.php。 3. 小程序端: - yjyf-app(用户端):彻底剥离师傅端路由、组件及静态资源,增加隐私授权弹窗与评价模块; - yjyf-master-app(师傅端):全新独立微信小程序工程(wx140d90ceda786107),实现工作台、接单大厅、资质认证、工单协同与钱包提现完整闭环。
这个提交包含在:
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\library;
|
||||
|
||||
use think\Db;
|
||||
use think\Log;
|
||||
|
||||
/**
|
||||
* 超时未接单订单自动关闭服务
|
||||
*/
|
||||
class OrderAutoCloseService
|
||||
{
|
||||
/** 允许自动关闭的未接单状态集合 */
|
||||
const CLOSEABLE_STATUSES = [
|
||||
OrderStatus::PENDING_PAY,
|
||||
OrderStatus::PENDING_ACCEPT,
|
||||
OrderStatus::QUOTING,
|
||||
];
|
||||
|
||||
/**
|
||||
* 读取后台配置的超时未接单自动关闭天数(开关关闭或天数为 0 表示关闭该功能)
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function getCloseDays()
|
||||
{
|
||||
$switchVal = Db::name('config')->where('name', 'order_unaccepted_close_switch')->value('value');
|
||||
if ($switchVal === null || $switchVal === '') {
|
||||
$switchVal = config('site.order_unaccepted_close_switch');
|
||||
}
|
||||
if ($switchVal !== null && $switchVal !== '' && (int)$switchVal === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$val = Db::name('config')->where('name', 'order_unaccepted_close_days')->value('value');
|
||||
if ($val === null || $val === '') {
|
||||
$val = config('site.order_unaccepted_close_days');
|
||||
}
|
||||
return max(0, (int)$val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描并自动关闭超时未接单订单
|
||||
*
|
||||
* @param int $limit 单次最大处理条数
|
||||
* @param int|null $overrideDays 覆盖配置天数(主要用于测试或手动指定)
|
||||
* @return array
|
||||
*/
|
||||
public static function closeExpiredOrders($limit = 100, $overrideDays = null)
|
||||
{
|
||||
$days = $overrideDays !== null ? max(0, (int)$overrideDays) : self::getCloseDays();
|
||||
$result = [
|
||||
'days' => $days,
|
||||
'scanned' => 0,
|
||||
'closed' => 0,
|
||||
'refunded' => 0,
|
||||
'refund_failed' => 0,
|
||||
'details' => [],
|
||||
];
|
||||
|
||||
if ($days <= 0) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$now = time();
|
||||
$threshold = $now - ($days * 86400);
|
||||
$limit = max(1, (int)$limit);
|
||||
|
||||
$orders = Db::name('order')
|
||||
->whereIn('status', self::CLOSEABLE_STATUSES)
|
||||
->where('master_id', 0)
|
||||
->where('createtime', '>', 0)
|
||||
->where('createtime', '<=', $threshold)
|
||||
->field('id')
|
||||
->order('id asc')
|
||||
->limit($limit)
|
||||
->select();
|
||||
|
||||
$result['scanned'] = count($orders);
|
||||
foreach ($orders as $row) {
|
||||
$itemResult = self::closeSingleOrder((int)$row['id'], $days, $threshold, $now);
|
||||
if ($itemResult['status'] === 'closed') {
|
||||
$result['closed']++;
|
||||
if ($itemResult['refund_state'] === 'success') {
|
||||
$result['refunded']++;
|
||||
} elseif ($itemResult['refund_state'] === 'failed') {
|
||||
$result['refund_failed']++;
|
||||
}
|
||||
}
|
||||
$result['details'][] = $itemResult;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭单笔超时未接单订单:事务内仅做状态流转与本地数据回滚,事务外执行退款与消息通知
|
||||
*
|
||||
* @param int $orderId
|
||||
* @param int $days
|
||||
* @param int $threshold
|
||||
* @param int $now
|
||||
* @return array
|
||||
*/
|
||||
public static function closeSingleOrder($orderId, $days, $threshold, $now)
|
||||
{
|
||||
$orderId = (int)$orderId;
|
||||
$reason = "下单超过{$days}天未有师傅接单,系统自动关闭";
|
||||
$lockedOrder = null;
|
||||
$needRefund = false;
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$lockedOrder = Db::name('order')->where('id', $orderId)->lock(true)->find();
|
||||
if (
|
||||
!$lockedOrder
|
||||
|| (int)$lockedOrder['master_id'] !== 0
|
||||
|| !in_array((int)$lockedOrder['status'], self::CLOSEABLE_STATUSES, true)
|
||||
|| !OrderStatus::can((int)$lockedOrder['status'], OrderStatus::CANCELLED)
|
||||
|| (int)$lockedOrder['createtime'] <= 0
|
||||
|| (int)$lockedOrder['createtime'] > $threshold
|
||||
) {
|
||||
Db::rollback();
|
||||
return ['order_id' => $orderId, 'status' => 'skipped', 'refund_state' => 'none'];
|
||||
}
|
||||
|
||||
if (isset($lockedOrder['invoice_status']) && in_array($lockedOrder['invoice_status'], ['wait', 'issued'], true)) {
|
||||
Db::rollback();
|
||||
Log::warning("[OrderAutoCloseService] 订单 #{$orderId} 处于开票流程中,跳过自动关闭");
|
||||
return ['order_id' => $orderId, 'status' => 'skipped', 'refund_state' => 'none'];
|
||||
}
|
||||
|
||||
$fromStatus = (int)$lockedOrder['status'];
|
||||
$updated = Db::name('order')
|
||||
->where('id', $orderId)
|
||||
->where('status', $fromStatus)
|
||||
->where('master_id', 0)
|
||||
->update([
|
||||
'status' => OrderStatus::CANCELLED,
|
||||
'cancel_reason' => mb_substr($reason, 0, 255),
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
if (!$updated) {
|
||||
Db::rollback();
|
||||
return ['order_id' => $orderId, 'status' => 'skipped', 'refund_state' => 'none'];
|
||||
}
|
||||
|
||||
// 退还已占用的优惠券
|
||||
if ((int)$lockedOrder['coupon_id'] > 0) {
|
||||
Db::name('user_coupon')
|
||||
->where('id', (int)$lockedOrder['coupon_id'])
|
||||
->where('user_id', (int)$lockedOrder['user_id'])
|
||||
->where('order_id', $orderId)
|
||||
->where('status', 'used')
|
||||
->update([
|
||||
'status' => 'unused',
|
||||
'use_time' => 0,
|
||||
'order_id' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
// 竞价单将等待中的报价标记为已过期
|
||||
if ($lockedOrder['price_type'] === 'quote') {
|
||||
Db::name('order_quote')
|
||||
->where('order_id', $orderId)
|
||||
->where('status', 'wait')
|
||||
->update(['status' => 'expired']);
|
||||
}
|
||||
|
||||
Db::name('order_log')->insert([
|
||||
'order_id' => $orderId,
|
||||
'status' => OrderStatus::CANCELLED,
|
||||
'remark' => $reason,
|
||||
'operator_type' => 'system',
|
||||
'operator_id' => 0,
|
||||
'createtime' => $now,
|
||||
]);
|
||||
|
||||
$needRefund = ((int)$lockedOrder['pay_status'] === 1 && (float)$lockedOrder['pay_amount'] > 0);
|
||||
Db::commit();
|
||||
} catch (\Throwable $e) {
|
||||
Db::rollback();
|
||||
Log::error("[OrderAutoCloseService] 关闭订单 #{$orderId} 异常:" . $e->getMessage());
|
||||
return [
|
||||
'order_id' => $orderId,
|
||||
'status' => 'error',
|
||||
'refund_state' => 'none',
|
||||
'error' => $e->getMessage(),
|
||||
];
|
||||
}
|
||||
|
||||
// 事务外执行原路退款与消息提醒
|
||||
$refundState = 'none';
|
||||
$userId = (int)$lockedOrder['user_id'];
|
||||
$orderNo = (string)$lockedOrder['order_no'];
|
||||
$payAmount = number_format((float)$lockedOrder['pay_amount'], 2, '.', '');
|
||||
|
||||
if ($needRefund) {
|
||||
try {
|
||||
PaymentService::refundOrder($orderId, $reason);
|
||||
PaymentService::syncCancelledOrderRefund($orderId);
|
||||
$refundState = 'success';
|
||||
self::notifyUser(
|
||||
$userId,
|
||||
$orderId,
|
||||
'订单超时自动关闭及退款通知',
|
||||
"您的订单 {$orderNo} 因下单超过 {$days} 天未有师傅接单已自动关闭,实付金额 ¥{$payAmount} 已发起原路退款,请注意查收。",
|
||||
$now
|
||||
);
|
||||
} catch (\Throwable $re) {
|
||||
$refundState = 'failed';
|
||||
Log::error("[OrderAutoCloseService] 订单 #{$orderId} 自动退款异常:" . $re->getMessage());
|
||||
try {
|
||||
PaymentService::syncCancelledOrderRefund($orderId);
|
||||
} catch (\Throwable $se) {
|
||||
Log::error("[OrderAutoCloseService] 订单 #{$orderId} 同步退款状态异常:" . $se->getMessage());
|
||||
}
|
||||
self::notifyUser(
|
||||
$userId,
|
||||
$orderId,
|
||||
'订单超时自动关闭通知',
|
||||
"您的订单 {$orderNo} 因下单超过 {$days} 天未有师傅接单已自动关闭,退款正在系统重试处理中,如有疑问请联系客服。",
|
||||
$now
|
||||
);
|
||||
}
|
||||
} else {
|
||||
self::notifyUser(
|
||||
$userId,
|
||||
$orderId,
|
||||
'订单超时自动关闭通知',
|
||||
"您的订单 {$orderNo} 因下单超过 {$days} 天未有师傅接单已自动关闭。",
|
||||
$now
|
||||
);
|
||||
}
|
||||
|
||||
return [
|
||||
'order_id' => $orderId,
|
||||
'order_no' => $orderNo,
|
||||
'status' => 'closed',
|
||||
'refund_state' => $refundState,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送用户站内信提醒
|
||||
*/
|
||||
protected static function notifyUser($userId, $orderId, $title, $content, $now)
|
||||
{
|
||||
if ((int)$userId <= 0) {
|
||||
return;
|
||||
}
|
||||
Db::name('message')->insert([
|
||||
'receive_type' => 'user',
|
||||
'receive_id' => (int)$userId,
|
||||
'type' => 'order',
|
||||
'title' => mb_substr((string)$title, 0, 100),
|
||||
'content' => mb_substr((string)$content, 0, 500),
|
||||
'order_id' => (int)$orderId,
|
||||
'is_read' => 0,
|
||||
'createtime' => $now,
|
||||
]);
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户