Files
2026-10-28 17:36:00 +08:00

388 lines
19 KiB
PHP

<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\service\CommonService;
use app\common\service\ContractService;
use app\common\service\DemandBiddingService;
use app\common\service\DemandService;
use app\common\service\OrderShipService;
use app\common\service\VirtualPayService;
use think\exception\HttpResponseException;
use think\Lang;
use think\Validate;
class Demand extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
/**
* 提交需求
*
* @ApiMethod (POST)
* @param string $desc 需求描述
* @param array $images 图片数组
* @param array $files 文件数组
* @param string $contact 联系方式
*/
public function submit()
{
try {
if (!$this->auth->isLogin()) {
$this->error('请先登录');
}
$params = $this->request->post();
// 定义验证规则
$validate = new Validate([
'desc' => 'require|min:10',
'contact' => 'require',
], [
'desc.require' => '需求描述不能为空',
'desc.min' => '需求描述至少10个字符',
'contact.require' => '联系方式不能为空'
]);
// 验证数据
if (!$validate->check($params)) {
$this->error($validate->getError());
}
// 验证图片数量
if (isset($params['images']) && count($params['images']) > 9) {
$this->error('图片数量不能超过9张');
}
// 验证文件数量
if (isset($params['files']) && count($params['files']) > 5) {
$this->error('文件数量不能超过5个');
}
$develop_types = explode(',', config('site.general_config_develop_types') ?? '');
$params['develop_type'] = $develop_types[$params['develop_type']];
$params['images'] = array_filter($params['images']);
$params['files'] = array_filter(array_column($params['files'], 'url'));
DemandService::create($params, $this->auth->id);
$this->success('需求提交成功');
} catch (\Exception $e) {
if ($e instanceof HttpResponseException) {
throw $e;
}
$this->error($e->getMessage());
}
}
/**
* 列表
*
* @ApiMethod (POST)
* @param string $desc 需求描述
* @param array $images 图片数组
* @param array $files 文件数组
* @param string $contact 联系方式
*/
public function list()
{
try {
$user_id = $this->auth->id ?? -1;
$params = $this->request->param();
$page_size = $params['pageSize'] ?? 10;
$status = $params['status'] ?? -1;
$type = $params['type'] ?? 0;
$opType = $params['opType'] ?? '';
$keywords = $params['keywords'] ?? '';
$where = [
'publish_status' => CommonService::SUCCESS
];
if ($opType == 'my') {
$where['user_id'] = $user_id;
unset($where['publish_status']);
} elseif ($opType == 'myQuote') {
$demand_ids = DemandBiddingService::where(['user_id' => $user_id])->column('demand_id');
$where['id'] = ['in', $demand_ids];
}
if ($keywords) {
$where['desc'] = ['like', "%$keywords%"];
}
if ($type) {
$where['type'] = $type;
}
if ($status != -1) {
if ($opType == 'all') {
$develop_types = explode(',', config('site.general_config_develop_types') ?? '');
$where['develop_type'] = $develop_types[$status];
} else {
$where['status'] = $status;
}
}
$btnNameFun = function ($opType, $item) {
$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'];
} elseif ($item->status == 2 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
if ($item->demand_contracts && $item->demand_contracts->status == ContractService::STATUS_FINISHED && $item->demand_contracts->status2 == ContractService::STATUS_ONGOING) {
// 开发者已签约,等待客户签约
$button = ['key' => '', 'name' => '签约中', 'theme' => 'light'];
} else {
$button = ['key' => DemandService::BUTTON_CONTRACT_DEV, 'name' => '合同签约', 'theme' => 'danger'];
}
} elseif ($item->status == 3 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
$button = ['key' => DemandService::BUTTON_COMPLETE_WORK, 'name' => '工作完成', 'theme' => 'danger'];
} elseif ($item->status == 5 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
//客户支付尾款后,开发者发货交付
if ($item->hasDelivery) {
$button = ['key' => '', 'name' => '已发货', 'theme' => 'light'];
} else {
$button = ['key' => DemandService::BUTTON_DELIVER_GOODS, 'name' => '去发货', 'theme' => 'danger'];
}
} elseif ((int)$item->status === DemandService::STATUS_ACCEPTING && $item->isMyWin && (int)$item->accept_status === CommonService::ONGOING) {
// 与「我投标的」一致:接单开发者等待客户验收
$button = ['key' => '', 'name' => '待验收', 'theme' => 'light'];
} elseif (in_array((int)$item->status, [DemandService::STATUS_NO, DemandService::STATUS_CANCEL], true)) {
// 已关闭、已取消不再因为投过标而显示「已投标」
$button = ['name' => '查看详情', 'theme' => 'light'];
} elseif ($item->is_bid) {
$button = ['key' => '', 'name' => '已投标', 'theme' => 'light'];
} elseif ((int)$item->status === DemandService::STATUS_BIDDING && !$item->isMy) {
$button = ['key' => 'bid', 'name' => '参与投标', 'theme' => 'danger'];
}
} elseif ($opType == 'myQuote') {
if ($item->status == 1 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 1) {
$button = ['key' => DemandService::BUTTON_CONFIRM, 'name' => '确认接单', 'theme' => 'danger'];
} elseif ($item->status == 2 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
if ($item->demand_contracts && $item->demand_contracts->status == ContractService::STATUS_FINISHED && $item->demand_contracts->status2 == ContractService::STATUS_ONGOING) {
// 开发者已签约,等待客户签约
$button = ['key' => '', 'name' => '签约中', 'theme' => 'light'];
} else {
$button = ['key' => DemandService::BUTTON_CONTRACT_DEV, 'name' => '合同签约', 'theme' => 'danger'];
}
} elseif ($item->status == 3 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
$button = ['key' => DemandService::BUTTON_COMPLETE_WORK, 'name' => '工作完成', 'theme' => 'danger'];
} elseif ((int)$item->status === DemandService::STATUS_ACCEPTING && $item->isMyWin && (int)$item->accept_status === CommonService::ONGOING) {
$button = ['key' => '', 'name' => '待验收', 'theme' => 'light'];
} elseif ($item->status == 5 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
//客户支付尾款后,开发者发货交付
if ($item->hasDelivery) {
$button = ['key' => '', 'name' => '已发货', 'theme' => 'light'];
} else {
$button = ['key' => DemandService::BUTTON_DELIVER_GOODS, 'name' => '去发货', 'theme' => 'danger'];
}
} elseif ((int)$item->status === DemandService::STATUS_CONFIRM_RECEIPT && $item->isMyWin && (int)$item->accept_status === CommonService::SUCCESS) {
$button = ['key' => '', 'name' => '待收货', 'theme' => 'light'];
}
} elseif ($opType == 'my') {
if ($item->status == 2 && $item->demand_contracts && $item->demand_contracts->status2 == 1) {
$button = ['key' => DemandService::BUTTON_CONTRACT_CUSTOMER, 'name' => '合同签约', 'theme' => 'danger'];
} elseif ($item->status == 2) {
$button = ['key' => '', 'name' => '详聊沟通', 'theme' => 'danger'];
} elseif ($item->status == 3 && $item->work_status == 1) {
$button = ['key' => '', 'name' => '工作中', 'theme' => 'light'];
} elseif ($item->status == 4 && $item->accept_status == 1) {
$button = ['key' => DemandService::BUTTON_ACCEPT_PASSED, 'name' => '前去验收', 'theme' => 'danger'];
} elseif ($item->status == 5 && $item->accept_status == 2) {
//开发者发货后客户才能确认收货
if ($item->hasDelivery) {
$button = ['key' => DemandService::BUTTON_CONFIRM_RECEIPT, 'name' => '确认收货', 'theme' => 'danger'];
} else {
$button = ['key' => '', 'name' => '待发货', 'theme' => 'light'];
}
}
}
return $button;
};
$list = DemandService::getList($where, $page_size, 'id desc', '', ['user', 'demandContracts', 'demandBiddings', 'winBidding']);
//已发货的需求集合(发货表),用于开发者视角的发货按钮展示
$deliveredDemandIds = OrderShipService::getDeliveredDemandIds($list->column('id'));
$list = $list->each(function ($item, $key) use ($user_id, $opType, $btnNameFun, $deliveredDemandIds) {
$demand_bidding = $item->demand_biddings;
$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) {
if ($e instanceof HttpResponseException) {
throw $e;
}
$this->error($e->getMessage());
}
}
public function detail()
{
try {
$user_id = $this->auth->id ?? -1;
$userInfo = $this->auth->getUserinfo();
$id = $this->request->param('id', 0);
$where = [
'id' => $id,
];
$info = DemandService::getOne($where, ['demandContracts', 'winBidding', 'demandGroupChat'], [], ['withCount' => 'demandBiddings']);
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 = [
['status' => 1, 'status_name' => '招标', 'status_text' => '招标'],
['status' => 2, 'status_name' => '签约', 'status_text' => '签约'],
['status' => 3, 'status_name' => '工作', 'status_text' => '工作'],
['status' => 4, 'status_name' => '验收', 'status_text' => '验收'],
['status' => 5, 'status_name' => '收货', 'status_text' => '收货'],
];
$ins = $info->status;
foreach ($steps as &$step) {
$step['status_text'] = $step['status'] == $ins ? ($step['status_text'] . '中') : ($step['status'] < $ins ? $step['status_text'] . '完成' : '待' . $step['status_text']);
}
$info->stepList = [
'steps' => $steps,
'current' => max($ins - 1, 0)
];
// 招标中返回自己的报价,便于继续投标;成交报价只给发布者和接单开发者
$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) {
$info->contactText = !empty($info->demand_group_chat->qr_code) ? $qrcodeText : '******开发接单后可见';
} else {
$info->contactText = $info->isMyWin && !empty($info->demand_group_chat->qr_code) ? $qrcodeText : '******确认接单后可见';
}
$info->images = array_filter($info->images);
$info->files = array_map(fn($fl) => [
'url' => $fl,
'name' => urldecode(basename($fl))
], $info->files);
// 成交报价、合同和交付内容仅发布者与接单开发者可见
if ($canSeeDeal) {
$info->delivery = OrderShipService::getDemandDelivery($id);
} else {
$info->delivery = null;
$info->hidden(['winBidding', 'demandContracts']);
}
$this->success('', $info);
} catch (\Exception $e) {
if ($e instanceof HttpResponseException) {
throw $e;
}
$this->error($e->getMessage());
}
}
public function contractDetail()
{
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);
$platform = $demand ? VirtualPayService::normalizePayPlatform($demand->pay_platform) : '';
$info->pay_platform = $platform !== '' ? $platform : 'android';
if (empty($info->data_json['customData']) && ($firstContract = ContractService::getFirstContract($demandId))) {
$data_json = $info->data_json;
$data_json['customData'] = $firstContract->data_json['customData'] ?? [];
$info->data_json = $data_json;
}
$this->success('', $info);
} catch (\Exception $e) {
if ($e instanceof HttpResponseException) {
throw $e;
}
$this->error($e->getMessage());
}
}
public function joinBidding()
{
try {
if (!$this->auth->isLogin()) {
$this->error('请先登录');
}
$user_id = $this->auth->id ?? -1;
$params = $this->request->post();
// 定义验证规则
$validate = new Validate([
'demand_id' => 'require',
'quoted_price' => 'require',
'need_days' => 'require',
], [
'demand_id.require' => '需求id不能为空',
'quoted_price.require' => '报价不能为空',
'need_days.require' => '预计工时不能为空'
]);
// 验证数据
if (!$validate->check($params)) {
$this->error($validate->getError());
}
$demand = DemandService::getOne(['id' => $params['demand_id']]);
if (!$demand) {
$this->error('该需求不能为空');
}
if ($demand->status != 1) {
$this->error('该需求招标已结束,不能参与报名');
}
if ($demand->user_id == $user_id) {
exception('不能投标自己发布的需求');
}
DemandBiddingService::instance()->create($params, $user_id);
$this->success('报名成功');
} catch (\Exception $e) {
if ($e instanceof HttpResponseException) {
throw $e;
}
$this->error($e->getMessage());
}
}
/**
* @param
*/
public function demandOperate()
{
try {
$param = $this->request->param();
$userId = $this->auth->id ?? 0;
$res = DemandService::operateDemand($param, $userId);
$this->success('', $res);
} catch (\Exception $e) {
if ($e instanceof HttpResponseException) {
throw $e;
}
$this->error($e->getMessage());
}
}
}