Developer.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\DeveloperService;
  5. use think\exception\HttpResponseException;
  6. use think\Validate;
  7. class Developer extends Api
  8. {
  9. protected $noNeedLogin = [];
  10. protected $noNeedRight = ['*'];
  11. /**
  12. * 人才入驻资料提交
  13. *
  14. * @ApiMethod (POST)
  15. * @param string $desc 需求描述
  16. * @param array $images 图片数组
  17. * @param array $files 文件数组
  18. * @param string $contact 联系方式
  19. */
  20. public function submit()
  21. {
  22. try {
  23. if (!$this->auth->isLogin()) {
  24. $this->error('请先登录');
  25. }
  26. $userId = $this->auth->id ?? -1;
  27. $params = $this->request->post();
  28. // 定义验证规则
  29. $validate = new Validate([
  30. 'desc' => 'require|min:10',
  31. 'contact' => 'require',
  32. ], [
  33. 'desc.require' => '简介描述不能为空',
  34. 'desc.min' => '简介描述至少10个字符',
  35. 'contact.require' => '联系方式不能为空'
  36. ]);
  37. // 验证数据
  38. if (!$validate->check($params)) {
  39. $this->error($validate->getError());
  40. }
  41. if (DeveloperService::getOne(['user_id' => $userId])) {
  42. exception('您已经申请入驻,请勿重复申请!');
  43. }
  44. $params['tags'] = array_filter(array_merge($params['languageSelected'], $params['frontSelected'], $params['backendSelected']));
  45. DeveloperService::instance()->create($params, $userId);
  46. $this->success('申请已提交,等待审核通过即可接单');
  47. } catch (\Exception $e) {
  48. if ($e instanceof HttpResponseException) {
  49. throw $e;
  50. }
  51. $this->error($e->getMessage());
  52. }
  53. }
  54. }