更新项目管理接口

This commit is contained in:
yaooo 2023-10-31 10:12:16 +08:00
parent 09f6d58bed
commit 3dab0af11d
2 changed files with 109 additions and 104 deletions

View File

@ -197,10 +197,16 @@ class ProjectIndex extends ApiController
//编辑 //编辑
public function edit() public function edit()
{ {
$this->checkAuth();
$this->uid = JWT_UID;
$param = get_params(); $param = get_params();
$id = isset($param['id']) ? $param['id'] : 0; $id = isset($param['id']) ? $param['id'] : 0;
$detail = (new ProjectList())->detail($id); $detail = (new ProjectList())->detail($id);
if (request()->isPost()) { 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 ($this->uid == $detail['admin_id'] || $this->uid == $detail['director_uid']) {
if (isset($param['start_time'])) { if (isset($param['start_time'])) {
$param['start_time'] = strtotime(urldecode($param['start_time'])); $param['start_time'] = strtotime(urldecode($param['start_time']));
@ -214,12 +220,6 @@ class ProjectIndex extends ApiController
return to_assign(1, '计划结束时间不能小于开始时间'); 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(); $param['update_time'] = time();
$res = ProjectList::where('id', $param['id'])->strict(false)->field(true)->update($param); $res = ProjectList::where('id', $param['id'])->strict(false)->field(true)->update($param);
if ($res) { if ($res) {
@ -238,47 +238,21 @@ class ProjectIndex extends ApiController
else{ else{
Db::name('ProjectUser')->where(array('id'=>$has['id']))->strict(false)->field(true)->update($project_user); Db::name('ProjectUser')->where(array('id'=>$has['id']))->strict(false)->field(true)->update($project_user);
} }
} }
add_log('edit', $param['id'], $param); add_log('edit', $param['id'], $param);
add_project_log($this->uid,'project',$param, $detail); add_project_log($this->uid,'project',$param, $detail);
} }
return to_assign(); $this->apiSuccess('操作成功');
} else { } else {
return to_assign(1, '只有创建人或者负责人才有权限编辑'); $this->apiError('只有创建人或者负责人才有权限修改项目');
}
}
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();
}
}
} }
//查看 //查看
public function view() public function view()
{ {
$this->checkAuth();
$param = get_params(); $param = get_params();
$id = isset($param['id']) ? $param['id'] : 0; $id = isset($param['id']) ? $param['id'] : 0;
$detail = (new ProjectList())->detail($id); $detail = (new ProjectList())->detail($id);
@ -381,15 +355,22 @@ class ProjectIndex extends ApiController
//删除 //删除
public function delete() public function delete()
{ {
if (request()->isDelete()) { $this->checkAuth();
$this->uid = JWT_UID;
$id = get_params("id"); $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(); $count_task = Db::name('ProjectTask')->where([['project_id', '=', $id], ['delete_time', '=', 0]])->count();
if ($count_task > 0) { if ($count_task > 0) {
return to_assign(1, "该项目下有关联的任务,无法删除"); $this->apiError('该项目下有关联的任务,无法删除');
} }
$detail = Db::name('Project')->where('id', $id)->find(); $detail = Db::name('Project')->where('id', $id)->find();
if ($detail['admin_id'] != $this->uid) { if ($detail['admin_id'] != $this->uid) {
return to_assign(1, "你不是该项目的创建人,无权限删除"); $this->apiError('你不是该项目的创建人,无权限删除');
} }
if (Db::name('Project')->where('id', $id)->update(['delete_time' => time()]) !== false) { if (Db::name('Project')->where('id', $id)->update(['delete_time' => time()]) !== false) {
$log_data = array( $log_data = array(
@ -403,12 +384,9 @@ class ProjectIndex extends ApiController
'create_time' => time(), 'create_time' => time(),
); );
Db::name('ProjectLog')->strict(false)->field(true)->insert($log_data); Db::name('ProjectLog')->strict(false)->field(true)->insert($log_data);
return to_assign(0, "删除成功"); $this->apiSuccess('删除成功');
} else { } else {
return to_assign(0, "删除失败"); $this->apiError('删除失败');
}
} else {
return to_assign(1, "错误的请求");
} }
} }
} }

View File

@ -1,18 +1,27 @@
<?php <?php
namespace app\project\validate; namespace app\project\validate;
use think\Validate; use think\Validate;
use think\facade\Db;
class ProjectCheck extends Validate class ProjectCheck extends Validate
{ {
protected $rule = [ protected $rule = [
'name' => 'require|unique:project', 'name' => 'require|unique:project',
'start_time' => 'require',
'end_time' => 'require',
'director_uid' => 'require',
'content' => 'require',
'code' => 'alphaNum|length:5,10|unique:project', 'code' => 'alphaNum|length:5,10|unique:project',
'id' => 'require' 'id' => 'require|checkProject'
]; ];
protected $message = [ protected $message = [
'name.require' => '项目名称不能为空', 'name.require' => '项目名称不能为空',
'name.unique' => '同样的项目名称已经存在', 'name.unique' => '项目已存在',
'start_time.require' => '项目开始时间不能为空',
'end_time.require' => '项目结束时间不能为空',
'director_uid.require' => '项目负责人不能为空',
'content.require' => '项目简介不能为空',
'code.alphaNum' => '项目代码只能为5至10为字母和数字', 'code.alphaNum' => '项目代码只能为5至10为字母和数字',
'code.length' => '项目代码只能为5至10为字母和数字', 'code.length' => '项目代码只能为5至10为字母和数字',
'code.unique' => '同样的项目代码已经存在', 'code.unique' => '同样的项目代码已经存在',
@ -20,7 +29,25 @@ class ProjectCheck extends Validate
]; ];
protected $scene = [ protected $scene = [
'add' => ['name','code'], 'add' => ['name','start_time','end_time','director_uid','content','code'],
'edit' => ['id'] '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;
}
} }