Demand.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\CommonService;
  5. use app\common\service\ContractService;
  6. use app\common\service\DemandBiddingService;
  7. use app\common\service\DemandService;
  8. use think\exception\HttpResponseException;
  9. use think\Lang;
  10. use think\Validate;
  11. class Demand extends Api
  12. {
  13. protected $noNeedLogin = [];
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * 提交需求
  17. *
  18. * @ApiMethod (POST)
  19. * @param string $desc 需求描述
  20. * @param array $images 图片数组
  21. * @param array $files 文件数组
  22. * @param string $contact 联系方式
  23. */
  24. public function submit()
  25. {
  26. try {
  27. if (!$this->auth->isLogin()) {
  28. $this->error('请先登录');
  29. }
  30. $params = $this->request->post();
  31. // 定义验证规则
  32. $validate = new Validate([
  33. 'desc' => 'require|min:10',
  34. 'contact' => 'require',
  35. ], [
  36. 'desc.require' => '需求描述不能为空',
  37. 'desc.min' => '需求描述至少10个字符',
  38. 'contact.require' => '联系方式不能为空'
  39. ]);
  40. // 验证数据
  41. if (!$validate->check($params)) {
  42. $this->error($validate->getError());
  43. }
  44. // 验证图片数量
  45. if (isset($params['images']) && count($params['images']) > 9) {
  46. $this->error('图片数量不能超过9张');
  47. }
  48. // 验证文件数量
  49. if (isset($params['files']) && count($params['files']) > 5) {
  50. $this->error('文件数量不能超过5个');
  51. }
  52. $develop_types = explode(',', config('site.general_config_develop_types') ?? '');
  53. $params['develop_type'] = $develop_types[$params['develop_type']];
  54. $params['images'] = array_filter($params['images']);
  55. $params['files'] = array_filter(array_column($params['files'], 'url'));
  56. DemandService::create($params, $this->auth->id);
  57. $this->success('需求提交成功');
  58. } catch (\Exception $e) {
  59. if ($e instanceof HttpResponseException) {
  60. throw $e;
  61. }
  62. $this->error($e->getMessage());
  63. }
  64. }
  65. /**
  66. * 列表
  67. *
  68. * @ApiMethod (POST)
  69. * @param string $desc 需求描述
  70. * @param array $images 图片数组
  71. * @param array $files 文件数组
  72. * @param string $contact 联系方式
  73. */
  74. public function list()
  75. {
  76. try {
  77. $user_id = $this->auth->id ?? -1;
  78. $params = $this->request->param();
  79. $page_size = $params['pageSize'] ?? 10;
  80. $status = $params['status'] ?? -1;
  81. $type = $params['type'] ?? 0;
  82. $opType = $params['opType'] ?? '';
  83. $keywords = $params['keywords'] ?? '';
  84. $where = [
  85. 'publish_status' => CommonService::SUCCESS
  86. ];
  87. if ($opType == 'my') {
  88. $where['user_id'] = $user_id;
  89. unset($where['publish_status']);
  90. }
  91. if ($keywords) {
  92. $where['desc'] = ['like', "%$keywords%"];
  93. }
  94. if ($type) {
  95. $where['type'] = $type;
  96. }
  97. if ($status != -1) {
  98. if ($opType == 'all') {
  99. $develop_types = explode(',', config('site.general_config_develop_types') ?? '');
  100. $where['develop_type'] = $develop_types[$status];
  101. } else {
  102. $where['status'] = $status;
  103. }
  104. }
  105. $btnNameFun = function ($opType, $item) {
  106. $button = ['name' => '查看详情', 'theme' => 'light'];
  107. if ($opType == 'all') {
  108. if ($item->status == 1 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 1) {
  109. $button = ['name' => '确认接单', 'theme' => 'danger'];
  110. } elseif ($item->status == 2 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
  111. $button = ['name' => '合同签约', 'theme' => 'danger'];
  112. } elseif ($item->status == 3 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
  113. $button = ['name' => '工作完成', 'theme' => 'danger'];
  114. } elseif ($item->is_bid) {
  115. $button = ['name' => '已投标', 'theme' => 'light'];
  116. } elseif ($item->status == 1) {
  117. $button = ['name' => '参与投标', 'theme' => 'danger'];
  118. }
  119. } elseif ($opType == 'myQuote') {
  120. if ($item->status == 1 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 1) {
  121. $button = ['name' => '确认接单', 'theme' => 'danger'];
  122. } elseif ($item->status == 2 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
  123. $button = ['name' => '合同签约', 'theme' => 'danger'];
  124. } elseif ($item->status == 3 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
  125. $button = ['name' => '工作完成', 'theme' => 'danger'];
  126. } elseif ($item->status == 4 && $item->accept_status == 1) {
  127. $button = ['name' => '待验收', 'theme' => 'light'];
  128. } elseif ($item->status == 5 && $item->accept_status == 2) {
  129. $button = ['name' => '待收货', 'theme' => 'light'];
  130. }
  131. } elseif ($opType == 'my') {
  132. if ($item->status == 2 && $item->demand_contracts && $item->demand_contracts->status2 == 1) {
  133. $button = ['name' => '合同签约', 'theme' => 'danger'];
  134. } elseif ($item->status == 2) {
  135. $button = ['name' => '详聊沟通', 'theme' => 'danger'];
  136. } elseif ($item->status == 3 && $item->work_status == 1) {
  137. $button = ['name' => '工作中', 'theme' => 'light'];
  138. } elseif ($item->status == 4 && $item->accept_status == 1) {
  139. $button = ['name' => '前去验收', 'theme' => 'danger'];
  140. } elseif ($item->status == 5 && $item->accept_status == 2) {
  141. $button = ['name' => '确认收货', 'theme' => 'danger'];
  142. }
  143. }
  144. return $button;
  145. };
  146. $list = DemandService::getList($where, $page_size, 'id desc', '', ['user', 'demandContracts', 'demandBiddings', 'winBidding']);
  147. $list = $list->each(function ($item, $key) use ($user_id, $opType, $btnNameFun) {
  148. $demand_bidding = $item->demand_biddings;
  149. $bidUserIds = array_column($demand_bidding, 'user_id');
  150. $item->demand_biddings_count = count($demand_bidding);
  151. $item->is_bid = in_array($user_id, $bidUserIds);
  152. $item->isMyWin = $item->winBidding && $item->winBidding->user_id == $user_id;
  153. $item->button = $btnNameFun($opType, $item);
  154. });
  155. $this->success('', $list);
  156. } catch (\Exception $e) {
  157. if ($e instanceof HttpResponseException) {
  158. throw $e;
  159. }
  160. $this->error($e->getMessage());
  161. }
  162. }
  163. public function detail()
  164. {
  165. try {
  166. $user_id = $this->auth->id ?? -1;
  167. $userInfo = $this->auth->getUserinfo();
  168. $id = $this->request->param('id', 0);
  169. $where = [
  170. 'id' => $id,
  171. ];
  172. $info = DemandService::getOne($where, ['demandContracts', 'winBidding', 'demandGroupChat'], [], ['withCount' => 'demandBiddings']);
  173. if (!$info) {
  174. throw new \Exception('该需求不存在');
  175. }
  176. //更新浏览次数
  177. DemandService::where($where)->setInc('views');
  178. $steps = [
  179. ['status' => 1, 'status_name' => '招标', 'status_text' => '招标'],
  180. ['status' => 2, 'status_name' => '签约', 'status_text' => '签约'],
  181. ['status' => 3, 'status_name' => '工作', 'status_text' => '工作'],
  182. ['status' => 4, 'status_name' => '验收', 'status_text' => '验收'],
  183. ['status' => 5, 'status_name' => '收货', 'status_text' => '收货'],
  184. ];
  185. $ins = $info->status;
  186. foreach ($steps as &$step) {
  187. $step['status_text'] = $step['status'] == $ins ? ($step['status_text'] . '中') : ($step['status'] < $ins ? $step['status_text'] . '完成' : '待' . $step['status_text']);
  188. }
  189. $info->stepList = [
  190. 'steps' => $steps,
  191. 'current' => max($ins - 1, 0)
  192. ];
  193. //我的报价
  194. $info->myBidding = DemandBiddingService::getOne(['demand_id' => $id, 'user_id' => $user_id]);
  195. //中标报价
  196. // $info->winBidding = DemandBiddingService::getOne(['demand_id' => $id, 'bidding_status' => DemandBiddingService::STATUS_BIDDING_WIN]);
  197. $info->isMy = $user_id == $info->user_id;//我发布
  198. $info->isMyWin = $info->winBidding && $info->winBidding->user_id == $user_id;//我中标
  199. if ($user_id == $info->user_id) {
  200. $info->contactText = !empty($info->demand_group_chat->qr_code) ? '请识别二维码进群沟通' : '******开发接单后可见';
  201. } else {
  202. $info->contactText = $info->isMyWin && !empty($info->demand_group_chat->qr_code) ? '请识别二维码进群沟通' : '******确认接单后可见';
  203. }
  204. $info->images = array_filter($info->images);
  205. $info->files = array_map(fn($fl) => [
  206. 'url' => $fl,
  207. 'name' => urldecode(basename($fl))
  208. ], $info->files);
  209. $this->success('', $info);
  210. } catch (\Exception $e) {
  211. if ($e instanceof HttpResponseException) {
  212. throw $e;
  213. }
  214. $this->error($e->getMessage());
  215. }
  216. }
  217. public function contractDetail()
  218. {
  219. try {
  220. $demandId = $this->request->param('demandId', 0);
  221. $info = ContractService::getWinBiddingContract($demandId);
  222. if (empty($info->data_json['customData']) && ($firstContract = ContractService::getFirstContract($demandId))) {
  223. $data_json = $info->data_json;
  224. $data_json['customData'] = $firstContract->data_json['customData'] ?? [];
  225. $info->data_json = $data_json;
  226. }
  227. $this->success('', $info);
  228. } catch (\Exception $e) {
  229. if ($e instanceof HttpResponseException) {
  230. throw $e;
  231. }
  232. $this->error($e->getMessage());
  233. }
  234. }
  235. public function joinBidding()
  236. {
  237. try {
  238. if (!$this->auth->isLogin()) {
  239. $this->error('请先登录');
  240. }
  241. $user_id = $this->auth->id ?? -1;
  242. $params = $this->request->post();
  243. // 定义验证规则
  244. $validate = new Validate([
  245. 'demand_id' => 'require',
  246. 'quoted_price' => 'require',
  247. 'need_days' => 'require',
  248. ], [
  249. 'demand_id.require' => '需求id不能为空',
  250. 'quoted_price.require' => '报价不能为空',
  251. 'need_days.require' => '预计工时不能为空'
  252. ]);
  253. // 验证数据
  254. if (!$validate->check($params)) {
  255. $this->error($validate->getError());
  256. }
  257. $demand = DemandService::getOne(['id' => $params['demand_id']]);
  258. if (!$demand) {
  259. $this->error('该需求不能为空');
  260. }
  261. if ($demand->status != 1) {
  262. $this->error('该需求招标已结束,不能参与报名');
  263. }
  264. if ($demand->user_id == $user_id) {
  265. exception('不能投标自己发布的需求');
  266. }
  267. DemandBiddingService::instance()->create($params, $user_id);
  268. $this->success('报名成功');
  269. } catch (\Exception $e) {
  270. if ($e instanceof HttpResponseException) {
  271. throw $e;
  272. }
  273. $this->error($e->getMessage());
  274. }
  275. }
  276. /**
  277. * @param
  278. */
  279. public function demandOperate()
  280. {
  281. try {
  282. $param = $this->request->param();
  283. $userId = $this->auth->id ?? 0;
  284. $res = DemandService::operateDemand($param, $userId);
  285. $this->success('', $res);
  286. } catch (\Exception $e) {
  287. if ($e instanceof HttpResponseException) {
  288. throw $e;
  289. }
  290. $this->error($e->getMessage());
  291. }
  292. }
  293. }