需求按钮状态优化

This commit is contained in:
Dev
2026-10-28 16:59:16 +08:00
parent f552fb957f
commit d47beb3f56
13 changed files with 166 additions and 43 deletions
+34 -9
View File
@@ -120,6 +120,9 @@ class Demand extends Api
$button = ['name' => '查看详情', 'theme' => 'light'];
// key 为按钮动作标识(复用 DemandService::BUTTON_* 常量),
// 前端据此判断按钮类型,避免依赖中文文案;无动作的展示型按钮 key 为空
if ((int)$item->status === DemandService::STATUS_FINISHED) {
return ['key' => DemandService::BUTTON_FINISHED, 'name' => '交易完成', 'theme' => 'light'];
}
if ($opType == 'all') {
if ($item->status == 1 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 1) {
$button = ['key' => DemandService::BUTTON_CONFIRM, 'name' => '确认接单', 'theme' => 'danger'];
@@ -196,9 +199,12 @@ class Demand extends Api
$bidUserIds = array_column($demand_bidding, 'user_id');
$item->demand_biddings_count = count($demand_bidding);
$item->is_bid = in_array($user_id, $bidUserIds);
$item->isMy = $item->user_id == $user_id;
$item->isMyWin = $item->winBidding && $item->winBidding->user_id == $user_id;
$item->hasDelivery = in_array($item->id, $deliveredDemandIds);
$item->button = $btnNameFun($opType, $item);
// 列表不返回报价、合同和中标明细,避免其它开发者看到成交价与交付相关信息
$item->hidden(['demandBiddings', 'demandContracts', 'winBidding']);
});
$this->success('', $list);
} catch (\Exception $e) {
@@ -222,6 +228,13 @@ class Demand extends Api
if (!$info) {
throw new \Exception('该需求不存在');
}
$info->isMy = $user_id == $info->user_id;//我发布
$info->isMyWin = $info->winBidding && $info->winBidding->user_id == $user_id;//我中标
$canSeeDeal = $info->isMy || $info->isMyWin;
// 交易完成的订单,只有发布者和接单开发者可以查看
if ((int)$info->status === DemandService::STATUS_FINISHED && !$canSeeDeal) {
throw new \Exception('仅发布者和接单开发者可查看');
}
//更新浏览次数
DemandService::where($where)->setInc('views');
$steps = [
@@ -239,12 +252,10 @@ class Demand extends Api
'steps' => $steps,
'current' => max($ins - 1, 0)
];
//我的报价
$info->myBidding = DemandBiddingService::getOne(['demand_id' => $id, 'user_id' => $user_id]);
//中标报价
// $info->winBidding = DemandBiddingService::getOne(['demand_id' => $id, 'bidding_status' => DemandBiddingService::STATUS_BIDDING_WIN]);
$info->isMy = $user_id == $info->user_id;//我发布
$info->isMyWin = $info->winBidding && $info->winBidding->user_id == $user_id;//我中标
// 招标中返回自己的报价,便于继续投标;成交报价只给发布者和接单开发者
$info->myBidding = ((int)$info->status === DemandService::STATUS_BIDDING || $canSeeDeal)
? DemandBiddingService::getOne(['demand_id' => $id, 'user_id' => $user_id])
: null;
//小程序内无法直接识别二维码,需先长按保存再用微信扫一扫
$qrcodeText = '请使用微信扫码进群沟通';
if ($user_id == $info->user_id) {
@@ -257,8 +268,13 @@ class Demand extends Api
'url' => $fl,
'name' => urldecode(basename($fl))
], $info->files);
//交付内容(订单发货表,开发者发货后展示)
$info->delivery = OrderShipService::getDemandDelivery($id);
// 成交报价、合同和交付内容仅发布者与接单开发者可见
if ($canSeeDeal) {
$info->delivery = OrderShipService::getDemandDelivery($id);
} else {
$info->delivery = null;
$info->hidden(['winBidding', 'demandContracts']);
}
$this->success('', $info);
} catch (\Exception $e) {
@@ -273,8 +289,17 @@ class Demand extends Api
{
try {
$demandId = $this->request->param('demandId', 0);
$userId = $this->auth->id ?? 0;
$demand = DemandService::getOne(['id' => $demandId], ['winBidding']);
if (!$demand) {
$this->error('该需求不存在');
}
$isMy = $demand->user_id == $userId;
$isMyWin = $demand->winBidding && $demand->winBidding->user_id == $userId;
if (!$isMy && !$isMyWin) {
$this->error('仅发布者和接单开发者可查看');
}
$info = ContractService::getWinBiddingContract($demandId);
$demand = DemandService::getOne(['id' => $demandId]);
$platform = $demand ? VirtualPayService::normalizePayPlatform($demand->pay_platform) : '';
$info->pay_platform = $platform !== '' ? $platform : 'android';
if (empty($info->data_json['customData']) && ($firstContract = ContractService::getFirstContract($demandId))) {
@@ -96,7 +96,7 @@ class DemandDeal extends Command
Db::transaction(function () use ($item) {
//根据订单列表查找冻结金额日志,根据冻结日志解冻冻结金额到余额
$userId = $item->winBidding->user_id;
$settleTotalAmount = 0;
$settleTotalAmount = '0';
foreach ($item->orders as $order) {
$logWhere = [
'user_id' => $userId,
@@ -109,7 +109,7 @@ class DemandDeal extends Command
$log = AccountLogService::where($logWhere)->find();
if ($log) {
$amount = $log->amount;
$settleTotalAmount += $amount;
$settleTotalAmount = bcadd($settleTotalAmount, (string)$amount, 2);
//写入余额日志
$title = OrderService::ORDER_TYPE_MAP[$order->order_type] ?? '需求收益';
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '解冻-冻结金额扣除', AccountLogService::TYPE_UNFREEZE, 0, AccountLogService::INC_EXP_EXPEND, $order->id, $order->total_amount);
@@ -134,7 +134,7 @@ class DemandDeal extends Command
$msgData = [
'values' => [
$item->parent_order_no,
round($settleTotalAmount, 2),
$settleTotalAmount,
SubscribeMessageService::MSG_MAP[SubscribeMessageService::MSG_TEMP_DEMAND_SETTLE]['remark'],
]
];
@@ -71,6 +71,7 @@ class DemandService
const BUTTON_ACCEPT_PASSED = 'acceptPassed'; //验收通过
const BUTTON_CONFIRM_RECEIPT = 'confirmReceipt'; //确认收货
const BUTTON_DELIVER_GOODS = 'deliverGoods'; //开发者发货
const BUTTON_FINISHED = 'finished'; //交易完成,仅发布者和接单开发者可进入详情
/**
* 获取平台服务费比例(0~1),后台填写大于 1 时按百分比处理,如 10 表示 10%
@@ -69,11 +69,17 @@ class MessageService
//按模板字段名组装可读内容:工单号:xxx;服务项目:xxx;...;备注:xxx
$values = $msgData['values'] ?? [];
$valueNames = $template['valueNames'] ?? [];
$fields = $template['fields'] ?? [];
$lines = [];
foreach ($valueNames as $key => $name) {
if (isset($values[$key]) && $values[$key] !== '') {
$lines[] = $name . ':' . $values[$key];
if (!isset($values[$key]) || $values[$key] === '') {
continue;
}
$value = $values[$key];
if (strpos($fields[$key] ?? '', 'amount') === 0) {
$value = SubscribeMessageService::formatAmount($value);
}
$lines[] = $name . ':' . $value;
}
// 备注处理:避免正文出现两行「备注」或语义冲突
// 1) 模板字段里已含「备注」时,发送方自定义了备注文案,不再追加模板默认 remark
@@ -58,7 +58,7 @@ class SubscribeMessageService
'remark' => '该工单已结算完成',
/*
订单编号:200912162851392635fdbf4149
结算金额:99.9元
结算金额:¥99.9
结算说明:订单已收货结算完成
*/
'fields' => ['character_string4', 'amount1', 'thing2'],
@@ -231,11 +231,46 @@ class SubscribeMessageService
public static function packData($template_id, $values): array
{
$fields = self::MSG_MAP[$template_id]['fields'] ?? [];
$param_data = [];
foreach ($fields as $key => $field) {
$param_data[$field] = ['value' => $values[$key] ?? ''];
$value = $values[$key] ?? '';
//微信 amount 类型不带币种符号时会固定展示 5 位小数,如 2.37 显示成 2.37000
if (strpos($field, 'amount') === 0) {
$value = self::formatAmount($value);
}
$param_data[$field] = ['value' => $value];
}
return $param_data ?? [];
return $param_data;
}
/**
* 订阅消息金额:最多两位小数,去掉末尾多余的 0,并加上币种符号。
* 微信 amount 类型不带币种符号时会把 2.37 展示成 2.37000。
* @param mixed $amount
* @return string 如 ¥2.37、¥2.3、¥2
*/
public static function formatAmount($amount): string
{
if ($amount === '' || $amount === null || !is_numeric($amount)) {
return (string)$amount;
}
$number = (string)$amount;
if (stripos($number, 'e') !== false) {
$number = sprintf('%.8F', (float)$amount);
}
$negative = strncmp($number, '-', 1) === 0;
if ($negative) {
$number = substr($number, 1);
}
//先加 0.005 再截到 2 位,等价于四舍五入,避免 bcmath 直接截断
$rounded = bcadd($number, '0.005', 2);
$rounded = rtrim(rtrim($rounded, '0'), '.');
if ($rounded === '') {
$rounded = '0';
}
return '¥' . ($negative ? '-' : '') . $rounded;
}
}