Demand.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. } elseif ($opType == 'myQuote') {
  91. $demand_ids = DemandBiddingService::where(['user_id' => $user_id])->column('demand_id');
  92. $where['id'] = ['in', $demand_ids];
  93. }
  94. if ($keywords) {
  95. $where['desc'] = ['like', "%$keywords%"];
  96. }
  97. if ($type) {
  98. $where['type'] = $type;
  99. }
  100. if ($status != -1) {
  101. if ($opType == 'all') {
  102. $develop_types = explode(',', config('site.general_config_develop_types') ?? '');
  103. $where['develop_type'] = $develop_types[$status];
  104. } else {
  105. $where['status'] = $status;
  106. }
  107. }
  108. $btnNameFun = function ($opType, $item) {
  109. $button = ['name' => '查看详情', 'theme' => 'light'];
  110. if ($opType == 'all') {
  111. if ($item->status == 1 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 1) {
  112. $button = ['name' => '确认接单', 'theme' => 'danger'];
  113. } elseif ($item->status == 2 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
  114. $button = ['name' => '合同签约', 'theme' => 'danger'];
  115. } elseif ($item->status == 3 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
  116. $button = ['name' => '工作完成', 'theme' => 'danger'];
  117. } elseif ($item->is_bid) {
  118. $button = ['name' => '已投标', 'theme' => 'light'];
  119. } elseif ($item->status == 1) {
  120. $button = ['name' => '参与投标', 'theme' => 'danger'];
  121. }
  122. } elseif ($opType == 'myQuote') {
  123. if ($item->status == 1 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 1) {
  124. $button = ['name' => '确认接单', 'theme' => 'danger'];
  125. } elseif ($item->status == 2 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
  126. $button = ['name' => '合同签约', 'theme' => 'danger'];
  127. } elseif ($item->status == 3 && $item->isMyWin && $item->winBidding->bidding_status == 2 && $item->winBidding->process_status == 2) {
  128. $button = ['name' => '工作完成', 'theme' => 'danger'];
  129. } elseif ($item->status == 4 && $item->accept_status == 1) {
  130. $button = ['name' => '待验收', 'theme' => 'light'];
  131. } elseif ($item->status == 5 && $item->accept_status == 2) {
  132. $button = ['name' => '待收货', 'theme' => 'light'];
  133. }
  134. } elseif ($opType == 'my') {
  135. if ($item->status == 2 && $item->demand_contracts && $item->demand_contracts->status2 == 1) {
  136. $button = ['name' => '合同签约', 'theme' => 'danger'];
  137. } elseif ($item->status == 2) {
  138. $button = ['name' => '详聊沟通', 'theme' => 'danger'];
  139. } elseif ($item->status == 3 && $item->work_status == 1) {
  140. $button = ['name' => '工作中', 'theme' => 'light'];
  141. } elseif ($item->status == 4 && $item->accept_status == 1) {
  142. $button = ['name' => '前去验收', 'theme' => 'danger'];
  143. } elseif ($item->status == 5 && $item->accept_status == 2) {
  144. $button = ['name' => '确认收货', 'theme' => 'danger'];
  145. }
  146. }
  147. return $button;
  148. };
  149. $list = DemandService::getList($where, $page_size, 'id desc', '', ['user', 'demandContracts', 'demandBiddings', 'winBidding']);
  150. $list = $list->each(function ($item, $key) use ($user_id, $opType, $btnNameFun) {
  151. $demand_bidding = $item->demand_biddings;
  152. $bidUserIds = array_column($demand_bidding, 'user_id');
  153. $item->demand_biddings_count = count($demand_bidding);
  154. $item->is_bid = in_array($user_id, $bidUserIds);
  155. $item->isMyWin = $item->winBidding && $item->winBidding->user_id == $user_id;
  156. $item->button = $btnNameFun($opType, $item);
  157. });
  158. $this->success('', $list);
  159. } catch (\Exception $e) {
  160. if ($e instanceof HttpResponseException) {
  161. throw $e;
  162. }
  163. $this->error($e->getMessage());
  164. }
  165. }
  166. public function detail()
  167. {
  168. try {
  169. $user_id = $this->auth->id ?? -1;
  170. $userInfo = $this->auth->getUserinfo();
  171. $id = $this->request->param('id', 0);
  172. $where = [
  173. 'id' => $id,
  174. ];
  175. $info = DemandService::getOne($where, ['demandContracts', 'winBidding', 'demandGroupChat'], [], ['withCount' => 'demandBiddings']);
  176. if (!$info) {
  177. throw new \Exception('该需求不存在');
  178. }
  179. //更新浏览次数
  180. DemandService::where($where)->setInc('views');
  181. $steps = [
  182. ['status' => 1, 'status_name' => '招标', 'status_text' => '招标'],
  183. ['status' => 2, 'status_name' => '签约', 'status_text' => '签约'],
  184. ['status' => 3, 'status_name' => '工作', 'status_text' => '工作'],
  185. ['status' => 4, 'status_name' => '验收', 'status_text' => '验收'],
  186. ['status' => 5, 'status_name' => '收货', 'status_text' => '收货'],
  187. ];
  188. $ins = $info->status;
  189. foreach ($steps as &$step) {
  190. $step['status_text'] = $step['status'] == $ins ? ($step['status_text'] . '中') : ($step['status'] < $ins ? $step['status_text'] . '完成' : '待' . $step['status_text']);
  191. }
  192. $info->stepList = [
  193. 'steps' => $steps,
  194. 'current' => max($ins - 1, 0)
  195. ];
  196. //我的报价
  197. $info->myBidding = DemandBiddingService::getOne(['demand_id' => $id, 'user_id' => $user_id]);
  198. //中标报价
  199. // $info->winBidding = DemandBiddingService::getOne(['demand_id' => $id, 'bidding_status' => DemandBiddingService::STATUS_BIDDING_WIN]);
  200. $info->isMy = $user_id == $info->user_id;//我发布
  201. $info->isMyWin = $info->winBidding && $info->winBidding->user_id == $user_id;//我中标
  202. if ($user_id == $info->user_id) {
  203. $info->contactText = !empty($info->demand_group_chat->qr_code) ? '请识别二维码进群沟通' : '******开发接单后可见';
  204. } else {
  205. $info->contactText = $info->isMyWin && !empty($info->demand_group_chat->qr_code) ? '请识别二维码进群沟通' : '******确认接单后可见';
  206. }
  207. $info->images = array_filter($info->images);
  208. $info->files = array_map(fn($fl) => [
  209. 'url' => $fl,
  210. 'name' => urldecode(basename($fl))
  211. ], $info->files);
  212. $this->success('', $info);
  213. } catch (\Exception $e) {
  214. if ($e instanceof HttpResponseException) {
  215. throw $e;
  216. }
  217. $this->error($e->getMessage());
  218. }
  219. }
  220. public function contractDetail()
  221. {
  222. try {
  223. $demandId = $this->request->param('demandId', 0);
  224. $info = ContractService::getWinBiddingContract($demandId);
  225. if (empty($info->data_json['customData']) && ($firstContract = ContractService::getFirstContract($demandId))) {
  226. $data_json = $info->data_json;
  227. $data_json['customData'] = $firstContract->data_json['customData'] ?? [];
  228. $info->data_json = $data_json;
  229. }
  230. $this->success('', $info);
  231. } catch (\Exception $e) {
  232. if ($e instanceof HttpResponseException) {
  233. throw $e;
  234. }
  235. $this->error($e->getMessage());
  236. }
  237. }
  238. public function joinBidding()
  239. {
  240. try {
  241. if (!$this->auth->isLogin()) {
  242. $this->error('请先登录');
  243. }
  244. $user_id = $this->auth->id ?? -1;
  245. $params = $this->request->post();
  246. // 定义验证规则
  247. $validate = new Validate([
  248. 'demand_id' => 'require',
  249. 'quoted_price' => 'require',
  250. 'need_days' => 'require',
  251. ], [
  252. 'demand_id.require' => '需求id不能为空',
  253. 'quoted_price.require' => '报价不能为空',
  254. 'need_days.require' => '预计工时不能为空'
  255. ]);
  256. // 验证数据
  257. if (!$validate->check($params)) {
  258. $this->error($validate->getError());
  259. }
  260. $demand = DemandService::getOne(['id' => $params['demand_id']]);
  261. if (!$demand) {
  262. $this->error('该需求不能为空');
  263. }
  264. if ($demand->status != 1) {
  265. $this->error('该需求招标已结束,不能参与报名');
  266. }
  267. if ($demand->user_id == $user_id) {
  268. exception('不能投标自己发布的需求');
  269. }
  270. DemandBiddingService::instance()->create($params, $user_id);
  271. $this->success('报名成功');
  272. } catch (\Exception $e) {
  273. if ($e instanceof HttpResponseException) {
  274. throw $e;
  275. }
  276. $this->error($e->getMessage());
  277. }
  278. }
  279. /**
  280. * @param
  281. */
  282. public function demandOperate()
  283. {
  284. try {
  285. $param = $this->request->param();
  286. $userId = $this->auth->id ?? 0;
  287. $res = DemandService::operateDemand($param, $userId);
  288. $this->success('', $res);
  289. } catch (\Exception $e) {
  290. if ($e instanceof HttpResponseException) {
  291. throw $e;
  292. }
  293. $this->error($e->getMessage());
  294. }
  295. }
  296. }