diff --git a/app/api/controller/ProjectIndex.php b/app/api/controller/ProjectIndex.php index 9229cb2..6013a56 100644 --- a/app/api/controller/ProjectIndex.php +++ b/app/api/controller/ProjectIndex.php @@ -197,88 +197,62 @@ class ProjectIndex extends ApiController //编辑 public function edit() { + $this->checkAuth(); + $this->uid = JWT_UID; $param = get_params(); $id = isset($param['id']) ? $param['id'] : 0; $detail = (new ProjectList())->detail($id); - if (request()->isPost()) { - if ($this->uid == $detail['admin_id'] || $this->uid == $detail['director_uid']) { - if (isset($param['start_time'])) { - $param['start_time'] = strtotime(urldecode($param['start_time'])); - if ($param['start_time'] >= $detail['end_time']) { - return to_assign(1, '开始时间不能大于计划结束时间'); + try { + validate(ProjectCheck::class)->scene('edit')->check($param); + } catch (ValidateException $e) { + $this->apiError($e->getError()); + } + if ($this->uid == $detail['admin_id'] || $this->uid == $detail['director_uid']) { + if (isset($param['start_time'])) { + $param['start_time'] = strtotime(urldecode($param['start_time'])); + if ($param['start_time'] >= $detail['end_time']) { + return to_assign(1, '开始时间不能大于计划结束时间'); + } + } + if (isset($param['end_time'])) { + $param['end_time'] = strtotime(urldecode($param['end_time'])); + if ($param['end_time'] <= $detail['start_time']) { + return to_assign(1, '计划结束时间不能小于开始时间'); + } + } + $param['update_time'] = time(); + $res = ProjectList::where('id', $param['id'])->strict(false)->field(true)->update($param); + if ($res) { + if(isset($param['director_uid'])){ + $project_user=array( + 'uid'=>$param['director_uid'], + 'admin_id'=>$this->uid, + 'project_id'=>$param['id'], + 'create_time'=>time(), + 'delete_time'=>0, + ); + $has = Db::name('ProjectUser')->where(array('uid'=>$param['director_uid'],'project_id'=>$param['id']))->find(); + if(empty($has)){ + Db::name('ProjectUser')->strict(false)->field(true)->insert($project_user); + } + else{ + Db::name('ProjectUser')->where(array('id'=>$has['id']))->strict(false)->field(true)->update($project_user); } } - if (isset($param['end_time'])) { - $param['end_time'] = strtotime(urldecode($param['end_time'])); - if ($param['end_time'] <= $detail['start_time']) { - return to_assign(1, '计划结束时间不能小于开始时间'); - } - } - try { - validate(ProjectCheck::class)->scene('edit')->check($param); - } catch (ValidateException $e) { - // 验证失败 输出错误信息 - return to_assign(1, $e->getError()); - } - $param['update_time'] = time(); - $res = ProjectList::where('id', $param['id'])->strict(false)->field(true)->update($param); - if ($res) { - if(isset($param['director_uid'])){ - $project_user=array( - 'uid'=>$param['director_uid'], - 'admin_id'=>$this->uid, - 'project_id'=>$param['id'], - 'create_time'=>time(), - 'delete_time'=>0, - ); - $has = Db::name('ProjectUser')->where(array('uid'=>$param['director_uid'],'project_id'=>$param['id']))->find(); - if(empty($has)){ - Db::name('ProjectUser')->strict(false)->field(true)->insert($project_user); - } - else{ - Db::name('ProjectUser')->where(array('id'=>$has['id']))->strict(false)->field(true)->update($project_user); - } - - } - - add_log('edit', $param['id'], $param); - add_project_log($this->uid,'project',$param, $detail); - } - return to_assign(); - } else { - return to_assign(1, '只有创建人或者负责人才有权限编辑'); - } - } - else{ - if (empty($detail)) { - return to_assign(1, '项目不存在'); - } else { - //项目阶段 - $step_array = Db::name('Step') - ->field('s.*,a.name as check_name') - ->alias('s') - ->join('Admin a', 'a.id = s.flow_uid', 'LEFT') - ->order('s.sort asc') - ->where(array('s.action_id' => $id, 's.type' => 2, 's.delete_time' => 0)) - ->select()->toArray(); - foreach ($step_array as $kk => &$vv) { - $vv['start_time'] = date('Y-m-d', $vv['start_time']); - $vv['end_time'] = date('Y-m-d', $vv['end_time']); - $flow_names = Db::name('Admin')->where([['id','in',$vv['flow_ids']]])->column('name'); - $vv['flow_names'] = implode(',',$flow_names); - } - - View::assign('step_array', $step_array); - View::assign('detail', $detail); - View::assign('id', $id); - return view(); + add_log('edit', $param['id'], $param); + add_project_log($this->uid,'project',$param, $detail); } + $this->apiSuccess('操作成功'); + } else { + $this->apiError('只有创建人或者负责人才有权限修改项目'); } + } //查看 public function view() { + $this->checkAuth(); $param = get_params(); $id = isset($param['id']) ? $param['id'] : 0; $detail = (new ProjectList())->detail($id); @@ -381,34 +355,38 @@ class ProjectIndex extends ApiController //删除 public function delete() { - if (request()->isDelete()) { - $id = get_params("id"); - $count_task = Db::name('ProjectTask')->where([['project_id', '=', $id], ['delete_time', '=', 0]])->count(); - if ($count_task > 0) { - return to_assign(1, "该项目下有关联的任务,无法删除"); - } - $detail = Db::name('Project')->where('id', $id)->find(); - if ($detail['admin_id'] != $this->uid) { - return to_assign(1, "你不是该项目的创建人,无权限删除"); - } - if (Db::name('Project')->where('id', $id)->update(['delete_time' => time()]) !== false) { - $log_data = array( - 'module' => 'project', - 'field' => 'delete', - 'action' => 'delete', - 'project_id' => $detail['id'], - 'admin_id' => $this->uid, - 'old_content' => '', - 'new_content' => $detail['name'], - 'create_time' => time(), - ); - Db::name('ProjectLog')->strict(false)->field(true)->insert($log_data); - return to_assign(0, "删除成功"); - } else { - return to_assign(0, "删除失败"); - } - } else { - return to_assign(1, "错误的请求"); + $this->checkAuth(); + $this->uid = JWT_UID; + $id = get_params("id"); + $project = Db::name('project')->where([ + 'id' => $id + ])->findOrEmpty(); + if (empty($project)) { + $this->apiError('项目不存在'); } + $count_task = Db::name('ProjectTask')->where([['project_id', '=', $id], ['delete_time', '=', 0]])->count(); + if ($count_task > 0) { + $this->apiError('该项目下有关联的任务,无法删除'); + } + $detail = Db::name('Project')->where('id', $id)->find(); + if ($detail['admin_id'] != $this->uid) { + $this->apiError('你不是该项目的创建人,无权限删除'); + } + if (Db::name('Project')->where('id', $id)->update(['delete_time' => time()]) !== false) { + $log_data = array( + 'module' => 'project', + 'field' => 'delete', + 'action' => 'delete', + 'project_id' => $detail['id'], + 'admin_id' => $this->uid, + 'old_content' => '', + 'new_content' => $detail['name'], + 'create_time' => time(), + ); + Db::name('ProjectLog')->strict(false)->field(true)->insert($log_data); + $this->apiSuccess('删除成功'); + } else { + $this->apiError('删除失败'); + } } } diff --git a/app/project/validate/ProjectCheck.php b/app/project/validate/ProjectCheck.php index 4a18329..845dd72 100644 --- a/app/project/validate/ProjectCheck.php +++ b/app/project/validate/ProjectCheck.php @@ -1,18 +1,27 @@ 'require|unique:project', - 'code' => 'alphaNum|length:5,10|unique:project', - 'id' => 'require' + 'name' => 'require|unique:project', + 'start_time' => 'require', + 'end_time' => 'require', + 'director_uid' => 'require', + 'content' => 'require', + 'code' => 'alphaNum|length:5,10|unique:project', + 'id' => 'require|checkProject' ]; protected $message = [ 'name.require' => '项目名称不能为空', - 'name.unique' => '同样的项目名称已经存在', + 'name.unique' => '项目已存在', + 'start_time.require' => '项目开始时间不能为空', + 'end_time.require' => '项目结束时间不能为空', + 'director_uid.require' => '项目负责人不能为空', + 'content.require' => '项目简介不能为空', 'code.alphaNum' => '项目代码只能为5至10为字母和数字', 'code.length' => '项目代码只能为5至10为字母和数字', 'code.unique' => '同样的项目代码已经存在', @@ -20,7 +29,25 @@ class ProjectCheck extends Validate ]; protected $scene = [ - 'add' => ['name','code'], - 'edit' => ['id'] + 'add' => ['name','start_time','end_time','director_uid','content','code'], + 'edit' => ['id', 'name','start_time','end_time','director_uid','content','code'] ]; + + public function checkProject($id, $rule, $data) + { + $project = Db::name('project')->where([ + 'id' => $id + ])->findOrEmpty(); + if (empty($project)) { + return '项目不存在'; + } + $oProject = Db::name('project')->where([ + 'name' => $data['name'] + ])->findOrEmpty(); + if (!empty($oProject) && ($oProject['id'] != $id)) { + return '项目已存在'; + } + return true; + } + } \ No newline at end of file