feat(order): 下单支持一口价/师傅报价竞价/用户自主定价三种方式

- 下单页新增计价方式选择;自主定价填写总价,师傅报价单进入报价大厅
- 用户订单详情展示师傅报价列表并可选定师傅;选定后同步师傅、金额与订单状态
- 师傅大厅新增用户定价筛选与按价接单;报价单禁止直接抢单
- 后端订单新增 custom 价态、金额口径统一由 calcAmount 输出
- 报价中状态纳入用户可取消范围;超时回流重新开放报价
- 新增 fa_order.price_type custom 枚举迁移脚本(schema_phase6)
这个提交包含在:
linda
2026-10-28 17:48:23 +08:00
父节点 3bba2d6e94
当前提交 e1bf139c5a
共有 11 个文件被更改,包括 500 次插入 和 50 次删除

查看文件

@@ -98,6 +98,7 @@ class Home extends Base
/**
* 费用试算(基础价 + 楼层费 + 加急费 - 优惠)
* 支持下单页实时切换:一口价 / 师傅报价 / 用户自主定价
*/
public function calc()
{
@@ -110,28 +111,48 @@ class Home extends Base
'floor' => (int)$this->request->param('floor', 1),
'has_elevator' => (int)$this->request->param('has_elevator', 1),
'urgent' => (int)$this->request->param('urgent', 0),
], (int)$this->request->param('coupon_id', 0), $this->uid());
], (int)$this->request->param('coupon_id', 0), $this->uid(),
(string)$this->request->param('price_type', ''),
(float)$this->request->param('user_price', 0));
$this->success('', $result);
}
/**
* 金额计算(下单与试算共用,保证前后端口径一致)
*
* @param array $service 服务行
* @param array $params floor / has_elevator / urgent
* @param int $couponId 用户优惠券 id
* @param int $uid 用户 id
* @param string $priceType 计价方式:fixed 一口价 / quote 师傅报价 / custom 用户自主定价
* @param float $userPrice 用户自主定价金额(总价,含楼层/加急)
*/
public static function calcAmount($service, $params, $couponId = 0, $uid = 0)
public static function calcAmount($service, $params, $couponId = 0, $uid = 0, $priceType = '', $userPrice = 0)
{
$allowTypes = ['fixed', 'quote', 'range', 'custom'];
$type = in_array($priceType, $allowTypes, true) ? $priceType : $service['price_type'];
$base = (float)$service['base_price'];
$floor = max(0, (int)$params['floor']);
$freeFloor = (int)$service['free_floor'];
$floorFee = (float)$service['floor_fee'];
$floorAmount = 0;
if ((int)$params['has_elevator'] === 0 && $floor > $freeFloor) {
$floorAmount = ($floor - $freeFloor) * $floorFee;
}
$urgentAmount = 0;
$urgentAmount = (int)$params['urgent'] === 1 ? round($base * 0.2, 2) : 0;
$total = round($base + $floorAmount + $urgentAmount, 2);
if ($type === 'custom') {
// 用户自主定价:出价即订单总价,已包含楼层费/加急费,师傅按此价接单
$base = max(0, round((float)$userPrice, 2));
$total = $base;
} else {
$floor = max(0, (int)$params['floor']);
$freeFloor = (int)$service['free_floor'];
$floorFee = (float)$service['floor_fee'];
if ((int)$params['has_elevator'] === 0 && $floor > $freeFloor) {
$floorAmount = ($floor - $freeFloor) * $floorFee;
}
$urgentAmount = (int)$params['urgent'] === 1 ? round($base * 0.2, 2) : 0;
$total = round($base + $floorAmount + $urgentAmount, 2);
}
$discount = 0;
$couponInfo = null;
@@ -151,7 +172,8 @@ class Home extends Base
}
return [
'price_type' => $service['price_type'],
'price_type' => $type,
'user_price' => $type === 'custom' ? round($base, 2) : 0,
'base_amount' => round($base, 2),
'floor_amount' => round($floorAmount, 2),
'urgent_amount' => round($urgentAmount, 2),
@@ -159,8 +181,8 @@ class Home extends Base
'discount_amount' => round($discount, 2),
'pay_amount' => max(0, round($total - $discount, 2)),
'coupon' => $couponInfo,
'free_floor' => $freeFloor,
'floor_fee' => round($floorFee, 2),
'free_floor' => $type === 'custom' ? 0 : (int)$service['free_floor'],
'floor_fee' => $type === 'custom' ? 0 : round((float)$service['floor_fee'], 2),
];
}

查看文件

@@ -144,7 +144,7 @@ class Master extends Base
}
/**
* 订单大厅:支持排序与单类型筛选(一口价 / 报价 / 悬赏)
* 订单大厅:支持排序与单类型筛选(一口价 / 师傅报价 / 用户自主定价)
*/
public function hall()
{
@@ -155,17 +155,20 @@ class Master extends Base
$page = max(1, (int)$this->request->param('page', 1));
$limit = min(30, max(1, (int)$this->request->param('limit', 10)));
$sort = $this->request->param('sort', 'new'); // new 最新 / amount 报酬 / urgent 急单 / time 时效
$type = $this->request->param('type', 'all'); // all / fixed 一口价 / quote 报价
$type = $this->request->param('type', 'all'); // all / fixed 一口价 / quote 师傅报价 / custom 用户自主定价
$build = function () use ($catIds, $type) {
$q = Db::name('order')->where('status', OrderStatus::PENDING_ACCEPT)->where('master_id', 0);
$q = Db::name('order')->where('master_id', 0)
->where('status', 'in', [OrderStatus::PENDING_ACCEPT, OrderStatus::QUOTING]);
if (!empty($catIds)) {
$q->where('cat_id', 'in', $catIds);
}
if ($type === 'fixed') {
$q->where('price_type', 'fixed');
$q->where('status', OrderStatus::PENDING_ACCEPT)->where('price_type', 'fixed');
} elseif ($type === 'quote') {
$q->where('price_type', 'quote');
$q->where('status', 'in', [OrderStatus::PENDING_ACCEPT, OrderStatus::QUOTING])->where('price_type', 'quote');
} elseif ($type === 'custom') {
$q->where('status', OrderStatus::PENDING_ACCEPT)->where('price_type', 'custom');
}
return $q;
};
@@ -193,6 +196,10 @@ class Master extends Base
$row['status_text'] = OrderStatus::label($row['status']);
$row['left_seconds'] = max(0, (int)$row['createtime'] + 3600 - $now);
$row['quote_count'] = (int)Db::name('order_quote')->where('order_id', $row['id'])->count();
$row['price_type_text'] = $row['price_type'] === 'fixed' ? '一口价'
: ($row['price_type'] === 'quote' ? '师傅报价'
: ($row['price_type'] === 'custom' ? '用户自主定价' : '按面积计价'));
$row['user_price'] = $row['price_type'] === 'custom' ? (float)$row['base_amount'] : 0;
}
unset($row);
@@ -217,6 +224,9 @@ class Master extends Base
if (!$order) {
$this->error('订单不存在');
}
if ($order['price_type'] === 'quote') {
$this->error('该订单为师傅报价订单,请提交报价');
}
if ((int)$order['status'] !== OrderStatus::PENDING_ACCEPT || (int)$order['master_id'] !== 0) {
$this->error('手慢了,该订单已被接走');
}
@@ -433,9 +443,15 @@ class Master extends Base
if (!$order) {
$this->error('订单不存在');
}
if ((int)$order['status'] !== OrderStatus::PENDING_ACCEPT) {
if ($order['price_type'] !== 'quote') {
$this->error('该订单不是报价竞价订单');
}
if (!in_array((int)$order['status'], [OrderStatus::PENDING_ACCEPT, OrderStatus::QUOTING], true)) {
$this->error('该订单已不在报价阶段');
}
if ((int)$order['master_id'] > 0) {
$this->error('该订单已选定师傅');
}
if ($amount <= 0) {
$this->error('请填写报价金额');
}
@@ -453,6 +469,13 @@ class Master extends Base
'status' => 'wait',
'createtime' => time(),
]);
// 兼容旧流程:已支付但仍在待接单的报价单,收到首个报价时进入「报价中」
if ((int)$order['status'] === OrderStatus::PENDING_ACCEPT) {
$this->transit($order, OrderStatus::QUOTING, '收到师傅报价,订单进入报价中', [], 'master', (int)$master['id']);
}
$this->notify('user', (int)$order['user_id'], '收到新报价', '师傅 ' . $master['realname'] . ' 对订单 ' . $order['order_no'] . ' 报价 ¥' . round($amount, 2) . ',请前往订单详情选择', 'order', $orderId);
$this->success('报价已提交');
}
@@ -518,13 +541,17 @@ class Master extends Base
->limit(50)
->select();
foreach ($rows as $row) {
$toStatus = $row['price_type'] === 'quote' ? OrderStatus::QUOTING : OrderStatus::PENDING_ACCEPT;
Db::name('order')->where('id', $row['id'])->update([
'master_id' => 0,
'status' => OrderStatus::PENDING_ACCEPT,
'status' => $toStatus,
'accept_time' => null,
'updatetime' => time(),
]);
$this->addLog($row['id'], OrderStatus::PENDING_ACCEPT, '超 2 小时未联系客户,订单自动回流大厅', 'system', 0);
if ($row['price_type'] === 'quote') {
Db::name('order_quote')->where('order_id', $row['id'])->where('status', 'accept')->update(['status' => 'wait']);
}
$this->addLog($row['id'], $toStatus, $row['price_type'] === 'quote' ? '师傅超 2 小时未联系客户,订单重新开放报价' : '超 2 小时未联系客户,订单自动回流大厅', 'system', 0);
}
}

查看文件

@@ -39,6 +39,20 @@ class Order extends Base
$this->error('服务不存在或已下架');
}
// 下单页可选择:一口价 / 师傅报价 / 用户自主定价(未传则沿用服务默认)
$priceType = trim((string)$this->request->post('price_type', $service['price_type']));
if (!in_array($priceType, ['fixed', 'quote', 'custom', 'range'], true)) {
$priceType = $service['price_type'];
}
$userPrice = round((float)$this->request->post('user_price', 0), 2);
if ($priceType === 'custom') {
if ($userPrice < 1 || $userPrice > 100000) {
$this->error('请填写 1-100000 元的出价');
}
} else {
$userPrice = 0;
}
$address = Db::name('address')->where('id', $addressId)->where('user_id', $this->uid())->find();
if (!$address) {
$this->error('请选择服务地址');
@@ -52,7 +66,8 @@ class Order extends Base
'floor' => (int)$address['floor'],
'has_elevator' => (int)$address['has_elevator'],
'urgent' => $urgent,
], $couponId, $this->uid());
], $couponId, $this->uid(), $priceType, $userPrice);
$amount['price_type'] = $priceType;
$orderNo = $this->makeOrderNo();
$verifyCode = (string)mt_rand(1000, 9999);
@@ -65,7 +80,7 @@ class Order extends Base
'service_id' => (int)$service['id'],
'service_name' => $service['name'],
'service_image' => $service['image'],
'price_type' => $service['price_type'],
'price_type' => $priceType,
'goods_images' => is_array($goodsImages) ? json_encode($goodsImages, JSON_UNESCAPED_UNICODE) : (string)$goodsImages,
'goods_model' => mb_substr($goodsModel, 0, 100),
'goods_num' => $goodsNum,
@@ -96,7 +111,13 @@ class Order extends Base
'updatetime' => time(),
]);
$this->addLog($id, OrderStatus::PENDING_PAY, '用户下单', 'user', $this->uid());
$modeText = [
'fixed' => '一口价',
'quote' => '师傅报价',
'custom' => '用户自主定价',
'range' => '按面积计价',
];
$this->addLog($id, OrderStatus::PENDING_PAY, '用户下单(' . (isset($modeText[$priceType]) ? $modeText[$priceType] : '一口价') . ')', 'user', $this->uid());
$this->success('下单成功', [
'order_id' => (int)$id,
@@ -131,8 +152,12 @@ class Order extends Base
'transaction_id' => 'MOCK' . date('YmdHis') . mt_rand(1000, 9999),
'updatetime' => time(),
]);
$this->transit($order, OrderStatus::PENDING_ACCEPT, '支付成功,订单已进入接单大厅', [], 'user', $this->uid());
$this->addLog($orderId, OrderStatus::PENDING_ACCEPT, '订单进入师傅接单大厅', 'system', 0);
$nextStatus = $order['price_type'] === 'quote' ? OrderStatus::QUOTING : OrderStatus::PENDING_ACCEPT;
$nextRemark = $nextStatus === OrderStatus::QUOTING
? '支付成功,订单已进入报价大厅,等待师傅报价'
: '支付成功,订单已进入接单大厅';
$this->transit($order, $nextStatus, $nextRemark, [], 'user', $this->uid());
$this->addLog($orderId, $nextStatus, $nextStatus === OrderStatus::QUOTING ? '订单进入师傅报价大厅' : '订单进入师傅接单大厅', 'system', 0);
// 核销优惠券
if ((int)$order['coupon_id'] > 0) {
@@ -217,11 +242,161 @@ class Order extends Base
$media = Db::name('order_media')->where('order_id', $orderId)->order('id asc')->select();
// 报价订单:返回师傅报价列表,供用户比价并选择师傅
$quotes = [];
if ($order['price_type'] === 'quote') {
$rows = Db::name('order_quote')->alias('q')
->join('master m', 'm.id = q.master_id', 'LEFT')
->field('q.id,q.order_id,q.master_id,q.amount,q.description,q.valid_hours,q.expire_time,q.status,q.createtime,m.realname,m.mobile,m.avatar,m.score,m.order_count,m.level')
->where('q.order_id', $orderId)
->order('q.amount asc,q.id asc')
->select();
$statusText = ['wait' => '等待选择', 'accept' => '已选中', 'reject' => '未选中', 'expired' => '已过期'];
$now = time();
foreach ($rows as $q) {
if ($q['status'] === 'wait' && (int)$q['expire_time'] > 0 && (int)$q['expire_time'] < $now) {
Db::name('order_quote')->where('id', (int)$q['id'])->update(['status' => 'expired']);
$q['status'] = 'expired';
}
$quotes[] = [
'id' => (int)$q['id'],
'master_id' => (int)$q['master_id'],
'realname' => $q['realname'] ?: '师傅',
'avatar' => $q['avatar'],
'score' => (float)$q['score'],
'order_count' => (int)$q['order_count'],
'level' => (int)$q['level'],
'amount' => (float)$q['amount'],
'description' => $q['description'],
'valid_hours' => (int)$q['valid_hours'],
'status' => $q['status'],
'status_text' => isset($statusText[$q['status']]) ? $statusText[$q['status']] : $q['status'],
'time_text' => date('m-d H:i', (int)$q['createtime']),
'expire_text' => (int)$q['expire_time'] > 0 ? date('m-d H:i', (int)$q['expire_time']) : '长期有效',
];
}
}
$this->success('', [
'order' => $this->formatOrder($order),
'master' => $master,
'logs' => $logs,
'media' => $media,
'quotes' => $quotes,
'can_choose_quote' => $order['price_type'] === 'quote'
&& (int)$order['status'] === OrderStatus::QUOTING
&& (int)$order['master_id'] === 0,
]);
}
/**
* 报价订单:用户选择师傅报价(选定后订单进入已接单)
* @ApiMethod (POST)
*/
public function chooseQuote()
{
$orderId = (int)$this->request->post('order_id');
$quoteId = (int)$this->request->post('quote_id');
$order = $this->ownOrder($orderId, 'user');
if ($order['price_type'] !== 'quote') {
$this->error('该订单不是师傅报价订单');
}
if ((int)$order['status'] !== OrderStatus::QUOTING) {
$this->error('当前状态(' . OrderStatus::label((int)$order['status']) . ')不可选择报价');
}
if ((int)$order['master_id'] > 0) {
$this->error('已选定师傅,请勿重复选择');
}
$quote = Db::name('order_quote')->where('id', $quoteId)->where('order_id', $orderId)->find();
if (!$quote) {
$this->error('报价不存在');
}
if ($quote['status'] !== 'wait') {
$this->error('该报价已失效,请选择其他师傅');
}
if ((int)$quote['expire_time'] > 0 && (int)$quote['expire_time'] < time()) {
Db::name('order_quote')->where('id', $quoteId)->update(['status' => 'expired']);
$this->error('该报价已过期,请选择其他师傅');
}
$master = Db::name('master')->where('id', (int)$quote['master_id'])->find();
if (!$master) {
$this->error('师傅信息不存在');
}
$quoteAmount = round((float)$quote['amount'], 2);
$payAmount = max(0, round($quoteAmount - (float)$order['discount_amount'], 2));
$remark = '客户已选择 ' . $master['realname'] . ' 的报价 ¥' . $quoteAmount . ',等待师傅联系';
Db::startTrans();
try {
$this->transit($order, OrderStatus::ACCEPTED, $remark, [
'master_id' => (int)$quote['master_id'],
'accept_time' => time(),
'base_amount' => $quoteAmount,
'floor_amount' => 0,
'urgent_amount' => 0,
'total_amount' => $quoteAmount,
'pay_amount' => $payAmount,
], 'user', $this->uid());
Db::name('order_quote')->where('id', $quoteId)->update(['status' => 'accept']);
Db::name('order_quote')->where('order_id', $orderId)->where('id', '<>', $quoteId)
->where('status', 'wait')->update(['status' => 'reject']);
$this->notify('user', (int)$order['user_id'], '已选定服务师傅', $remark, 'order', $orderId);
$this->notify('master', (int)$master['user_id'], '您的报价已被选中', '订单 ' . $order['order_no'] . ' 客户已选择您的报价 ¥' . $quoteAmount . ',请尽快联系客户', 'order', $orderId);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
$this->error('选择报价失败:' . $e->getMessage());
}
$this->success('已选定师傅,请等待师傅联系');
}
/**
* 报价订单:报价列表(详情页轮询/刷新使用)
*/
public function quotes()
{
$orderId = (int)$this->request->param('order_id');
$order = $this->ownOrder($orderId, 'user');
if ($order['price_type'] !== 'quote') {
$this->success('', ['list' => [], 'can_choose' => false]);
}
$rows = Db::name('order_quote')->alias('q')
->join('master m', 'm.id = q.master_id', 'LEFT')
->field('q.id,q.master_id,q.amount,q.description,q.valid_hours,q.expire_time,q.status,q.createtime,m.realname,m.avatar,m.score,m.order_count,m.level')
->where('q.order_id', $orderId)
->order('q.amount asc,q.id asc')
->select();
$statusText = ['wait' => '等待选择', 'accept' => '已选中', 'reject' => '未选中', 'expired' => '已过期'];
$list = [];
foreach ($rows as $q) {
$list[] = [
'id' => (int)$q['id'],
'master_id' => (int)$q['master_id'],
'realname' => $q['realname'] ?: '师傅',
'avatar' => $q['avatar'],
'score' => (float)$q['score'],
'order_count' => (int)$q['order_count'],
'amount' => (float)$q['amount'],
'description' => $q['description'],
'status' => $q['status'],
'status_text' => isset($statusText[$q['status']]) ? $statusText[$q['status']] : $q['status'],
'time_text' => date('m-d H:i', (int)$q['createtime']),
'expire_text' => (int)$q['expire_time'] > 0 ? date('m-d H:i', (int)$q['expire_time']) : '长期有效',
];
}
$this->success('', [
'list' => $list,
'can_choose' => (int)$order['status'] === OrderStatus::QUOTING && (int)$order['master_id'] === 0,
]);
}
@@ -355,7 +530,22 @@ class Order extends Base
'appoint_text' => $appoint ? date('m-d H:i', $appoint) : '尽快上门',
'create_text' => date('Y-m-d H:i', (int)$row['createtime']),
'urgent' => (int)$row['urgent'],
'price_type_text' => $this->priceTypeText($row['price_type']),
]);
}
/**
* 计价方式文案(前后端与后台统一口径)
*/
protected function priceTypeText($type)
{
$map = [
'fixed' => '一口价',
'quote' => '师傅报价',
'custom' => '用户自主定价',
'range' => '按面积计价',
];
return isset($map[$type]) ? $map[$type] : '一口价';
}
}