123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?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 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']);
- }
- 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'];
- if ($opType == 'all') {
- if ($item->status == 1 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 1) {
- $button = ['name' => '确认接单', 'theme' => 'danger'];
- } elseif ($item->status == 2 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
- $button = ['name' => '合同签约', 'theme' => 'danger'];
- } elseif ($item->status == 3 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
- $button = ['name' => '工作完成', 'theme' => 'danger'];
- } elseif ($item->is_bid) {
- $button = ['name' => '已投标', 'theme' => 'light'];
- } elseif ($item->status == 1) {
- $button = ['name' => '参与投标', 'theme' => 'danger'];
- }
- } elseif ($opType == 'myQuote') {
- if ($item->status == 1 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 1) {
- $button = ['name' => '确认接单', 'theme' => 'danger'];
- } elseif ($item->status == 2 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
- $button = ['name' => '合同签约', 'theme' => 'danger'];
- } elseif ($item->status == 3 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
- $button = ['name' => '工作完成', 'theme' => 'danger'];
- } elseif ($item->status == 4 && $item->accept_status == 1) {
- $button = ['name' => '待验收', 'theme' => 'light'];
- } elseif ($item->status == 5 && $item->accept_status == 2) {
- $button = ['name' => '待收货', 'theme' => 'light'];
- }
- } elseif ($opType == 'my') {
- if ($item->status == 2 && $item->demand_contracts && $item->demand_contracts->status2 == 1) {
- $button = ['name' => '合同签约', 'theme' => 'danger'];
- } elseif ($item->status == 2) {
- $button = ['name' => '详聊沟通', 'theme' => 'danger'];
- } elseif ($item->status == 3 && $item->work_status == 1) {
- $button = ['name' => '工作中', 'theme' => 'light'];
- } elseif ($item->status == 4 && $item->accept_status == 1) {
- $button = ['name' => '前去验收', 'theme' => 'danger'];
- } elseif ($item->status == 5 && $item->accept_status == 2) {
- $button = ['name' => '确认收货', 'theme' => 'danger'];
- }
- }
- return $button;
- };
- $list = DemandService::getList($where, $page_size, 'id desc', '', ['user', 'demandContracts', 'demandBiddings', 'winBidding']);
- $list = $list->each(function ($item, $key) use ($user_id, $opType, $btnNameFun) {
- $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->isMyWin = $item->winBidding && $item->winBidding->user_id == $user_id;
- $item->button = $btnNameFun($opType, $item);
- });
- $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('该需求不存在');
- }
- //更新浏览次数
- 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 = 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;//我中标
- if ($user_id == $info->user_id) {
- $info->contactText = !empty($info->demand_group_chat->qr_code) ? '请识别二维码进群沟通' : '******开发接单后可见';
- } else {
- $info->contactText = $info->isMyWin && !empty($info->demand_group_chat->qr_code) ? '请识别二维码进群沟通' : '******确认接单后可见';
- }
- $info->images = array_filter($info->images);
- $info->files = array_map(fn($fl) => [
- 'url' => $fl,
- 'name' => urldecode(basename($fl))
- ], $info->files);
- $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);
- $info = ContractService::getWinBiddingContract($demandId);
- 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());
- }
- }
- }
|