Demand.php 12 KB

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