440 lines
		
	
	
		
			19 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			440 lines
		
	
	
		
			19 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| /**
 | |
|  * @copyright Copyright (c) 2023-2024 美天智能科技
 | |
|  * @author 李志强
 | |
|  * @link http://www.meteteme.com
 | |
|  */
 | |
| 
 | |
| declare(strict_types=1);
 | |
| 
 | |
| namespace app\task\controller;
 | |
| 
 | |
| use app\base\BaseController;
 | |
| use app\model\Task as TaskList;
 | |
| use app\task\validate\TaskCheck;
 | |
| use think\exception\ValidateException;
 | |
| use think\facade\Db;
 | |
| use think\facade\View;
 | |
| 
 | |
| class Index extends BaseController
 | |
| {
 | |
|     public function index()
 | |
|     {
 | |
|         if (request()->isAjax()) {
 | |
|             $param = get_params();
 | |
|             $param['uid'] = $this->uid;
 | |
|             $list = (new TaskList())->list($param);
 | |
|             return table_assign(0, '', $list);
 | |
|         } else {
 | |
|             View::assign('type', get_task_cate());
 | |
|             View::assign('cate', get_work_cate());
 | |
|             View::assign('task_cate', get_task_cate());
 | |
|             View::assign('project', get_project());
 | |
|             View::assign('creater', get_admin_name());
 | |
|             View::assign('user_info', get_login_admin());
 | |
|             return view();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //添加
 | |
|     public function add()
 | |
|     {
 | |
|         $param = get_params();
 | |
|         if (request()->isPost()) {
 | |
|             //markdown数据处理
 | |
|             if (isset($param['table-align'])) {
 | |
|                 unset($param['table-align']);
 | |
|             }
 | |
|             if (isset($param['docContent-html-code'])) {
 | |
|                 $param['content'] = $param['docContent-html-code'];
 | |
|                 $param['md_content'] = $param['docContent-markdown-doc'];
 | |
|                 unset($param['docContent-html-code']);
 | |
|                 unset($param['docContent-markdown-doc']);
 | |
|             }
 | |
|             if (isset($param['ueditorcontent'])) {
 | |
|                 $param['content'] = $param['ueditorcontent'];
 | |
|                 $param['md_content'] = '';
 | |
|             }
 | |
|             if (isset($param['end_time'])) {
 | |
|                 $param['end_time'] = strtotime(urldecode($param['end_time']));
 | |
|             }
 | |
|             if (isset($param['flow_status'])) {
 | |
|                 if ($param['flow_status'] == 3) {
 | |
|                     $param['over_time'] = time();
 | |
|                 } else {
 | |
|                     $param['over_time'] = 0;
 | |
|                 }
 | |
|             }
 | |
|             if (!isset($param['director_uid'])) {
 | |
|                 $param['director_uid'] = '';
 | |
|             }
 | |
|             if (!empty($param['id']) && $param['id'] > 0) {
 | |
|                 $task = (new TaskList())->detail($param['id']);
 | |
|                 try {
 | |
|                     validate(TaskCheck::class)->scene('edit')->check($param);
 | |
|                 } catch (ValidateException $e) {
 | |
|                     // 验证失败 输出错误信息
 | |
|                     return to_assign(1, $e->getError());
 | |
|                 }
 | |
|                 $param['update_time'] = time();
 | |
|                 $param['project_id'] = Db::name('Task')->where('id', $param['id'])->value('project_id');
 | |
|                 $res = TaskList::where('id', $param['id'])->strict(false)->field(true)->update($param);
 | |
|                 if ($res) {
 | |
|                     add_log('edit', $param['id'], $param, $task);
 | |
| 
 | |
|                     $ddurl = Db::name('Project')->where('id', $param['project_id'])->value('ddurl');
 | |
|                     $param['admin_id'] = $this->uid;
 | |
|                     if ($param['director_uid']) {
 | |
|                         // 检查 director_uid 是否被修改
 | |
|                         if ($param['director_uid'] != $task['director_uid']) {
 | |
|                             $send_user = Db::name('Admin')->where('id', $param['admin_id'])->value('name');
 | |
|                             $user_name = Db::name('Admin')->where('id', $param['director_uid'])->value('name');
 | |
|                             $text1 = "**" . $send_user . "** 给你指派了一个新任务,请及时跟进处理。";
 | |
|                             $text2 = "**" . '@' . $user_name . "**";
 | |
|                             $msg = $text2 . "\n\n" . $text1 . "\n\n";
 | |
| 
 | |
|                             $data = array(
 | |
|                                 'msgtype' => 'actionCard',
 | |
|                                 'actionCard' => array(
 | |
|                                     'text' => $msg,
 | |
|                                     'btnOrientation' => '0',
 | |
|                                     'btns' => array(
 | |
|                                         array(
 | |
|                                             'title' => '查看详情',
 | |
|                                             'actionURL' => 'dingtalk://dingtalkclient/page/link?url=' . urlencode('http://project.meteteme.com/task/index/index') . '&pc_slide=false'
 | |
|                                         )
 | |
|                                     )
 | |
|                                 ),
 | |
|                                 'at' => array(
 | |
|                                     'atUserIds' => array($user_name),
 | |
|                                     'isAtAll' => false
 | |
|                                 )
 | |
|                             );
 | |
| 
 | |
|                             $data_string = json_encode($data);
 | |
| 
 | |
|                             if ($data_string === false) {
 | |
|                                 return to_assign(1, 'JSON编码错误');
 | |
|                             }
 | |
| 
 | |
|                             $header = array(
 | |
|                                 'Content-Type: application/json; charset=utf-8',
 | |
|                                 'Content-Length: ' . strlen($data_string)
 | |
|                             );
 | |
| 
 | |
|                             $options = array(
 | |
|                                 'http' => array(
 | |
|                                     'header' => $header,
 | |
|                                     'method' => 'POST',
 | |
|                                     'content' => $data_string,
 | |
|                                     'ignore_errors' => true
 | |
|                                 ),
 | |
|                             );
 | |
|                             $context = stream_context_create($options);
 | |
| 
 | |
|                             $result = file_get_contents($ddurl, false, $context);
 | |
| 
 | |
|                             if ($result === false) {
 | |
|                                 return to_assign(1, '钉钉消息发送失败');
 | |
|                             }
 | |
|                         }
 | |
|                     }
 | |
| 
 | |
|                 }
 | |
|                 return to_assign();
 | |
|             } else {
 | |
|                 try {
 | |
|                     validate(TaskCheck::class)->scene('add')->check($param);
 | |
|                 } catch (ValidateException $e) {
 | |
|                     // 验证失败 输出错误信息
 | |
|                     return to_assign(1, $e->getError());
 | |
|                 }
 | |
|                 $param['create_time'] = time();
 | |
|                 $param['admin_id'] = $this->uid;
 | |
| 
 | |
|                 $is_bug = $param['is_bug'] == 1 ? 'BUG缺陷' : ($param['is_bug'] == 2 ? '优化建议' : '普通任务');
 | |
|                 $param['title'] = ' [ ' . $is_bug . ' ] ' . ' - ' . $param['title'];
 | |
| 
 | |
|                 $touid = $param['director_uid'];
 | |
|                 $touid = Db::name('Admin')->where('id', $touid)->value('name');
 | |
|                 $ddurl = Db::name('Project')->where('id', $param['project_id'])->value('ddurl');
 | |
|                 $result = '';
 | |
| 
 | |
|                 //添加数据
 | |
|                 $sid = TaskList::strict(false)->field(true)->insertGetId($param);
 | |
| 
 | |
|                 if ($sid) {
 | |
| 
 | |
|                     add_log('add', $sid, $param);
 | |
|                     $log_data = array(
 | |
|                         'module' => 'task',
 | |
|                         'task_id' => $sid,
 | |
|                         'new_content' => $param['title'],
 | |
|                         'field' => 'new',
 | |
|                         'action' => 'add',
 | |
|                         'admin_id' => $this->uid,
 | |
|                         'create_time' => time(),
 | |
|                     );
 | |
|                     Db::name('Log')->strict(false)->field(true)->insert($log_data);
 | |
| 
 | |
|                     //钉钉机器人通知部分
 | |
| 
 | |
|                     if ($ddurl) {
 | |
|                         $send_user = Db::name('Admin')->where('id', $param['admin_id'])->value('name');
 | |
|                         $user_name = Db::name('Admin')->where('id', $param['director_uid'])->value('name');
 | |
|                         $title = $param['title'];
 | |
|                         $text1 = "**" . $send_user . "** 发布了一个新任务,请及时跟进处理。";
 | |
|                         $text2 = "**" . '@' . $user_name . "**";
 | |
|                         $msg = $text2 . "\n\n" . $text1 . "\n\n" . $title;
 | |
| 
 | |
|                         $data = array(
 | |
|                             'msgtype' => 'actionCard',
 | |
|                             'actionCard' => array(
 | |
|                                 'title' => $title,
 | |
|                                 'text' => $msg,
 | |
|                                 'btnOrientation' => '0',
 | |
|                                 'btns' => array(
 | |
|                                     array(
 | |
|                                         'title' => '查看详情',
 | |
|                                         'actionURL' => 'dingtalk://dingtalkclient/page/link?url=' . urlencode('http://project.meteteme.com/task/index/index') . '&pc_slide=false'
 | |
|                                     )
 | |
|                                 )
 | |
|                             ),
 | |
|                             'at' => array(
 | |
|                                 'atUserIds' => array($user_name),
 | |
|                                 'isAtAll' => false
 | |
|                             )
 | |
|                         );
 | |
| 
 | |
|                         $data_string = json_encode($data);
 | |
| 
 | |
|                         if ($data_string === false) {
 | |
|                             return to_assign(1, 'JSON编码错误');
 | |
|                         }
 | |
| 
 | |
|                         $header = array(
 | |
|                             'Content-Type: application/json; charset=utf-8',
 | |
|                             'Content-Length: ' . strlen($data_string)
 | |
|                         );
 | |
| 
 | |
|                         $options = array(
 | |
|                             'http' => array(
 | |
|                                 'header' => $header,
 | |
|                                 'method' => 'POST',
 | |
|                                 'content' => $data_string,
 | |
|                                 'ignore_errors' => true
 | |
|                             ),
 | |
|                         );
 | |
|                         $context = stream_context_create($options); // Define $context here
 | |
| 
 | |
|                         $result = file_get_contents($ddurl, false, $context);
 | |
| 
 | |
|                         // Handle the response as needed
 | |
|                         if ($result === false) {
 | |
|                             return to_assign(1, '钉钉消息发送失败');
 | |
|                         }
 | |
|                     } else {
 | |
|                         return to_assign(1, '未提供有效的 $ddurl');
 | |
|                     }
 | |
| 
 | |
| 
 | |
|                     return to_assign(0, '添加成功', ['id' => $sid, 'ddurl' => $ddurl, '钉钉发送' => $result ? '发送成功' : '发送失败', '钉钉消息' => $context]);
 | |
|                 }
 | |
| 
 | |
|             }
 | |
|         } else {
 | |
|             $id = isset($param['id']) ? $param['id'] : 0;
 | |
|             if ($id > 0) {
 | |
|                 $detail = (new TaskList())->detail($id);
 | |
|                 if (empty($detail)) {
 | |
|                     return to_assign(1, '任务不存在');
 | |
|                 }
 | |
|                 View::assign('detail', $detail);
 | |
|             }
 | |
|             if (isset($param['project_id'])) {
 | |
|                 View::assign('project_id', $param['project_id']);
 | |
|             }
 | |
|             View::assign('project', get_project());
 | |
|             View::assign('cate', get_work_cate());
 | |
|             View::assign('type', get_task_cate());
 | |
|             View::assign('id', $id);
 | |
|             return view();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //查看
 | |
|     public function view()
 | |
|     {
 | |
|         $param = get_params();
 | |
|         $id = isset($param['id']) ? $param['id'] : 0;
 | |
|         $detail = (new TaskList())->detail($id);
 | |
|         if (empty($detail)) {
 | |
|             return to_assign(1, '任务不存在');
 | |
|         } else {
 | |
|             $role_uid = [$detail['admin_id'], $detail['director_uid']];
 | |
|             $role_edit = 'view';
 | |
|             if (in_array($this->uid, $role_uid)) {
 | |
|                 $role_edit = 'edit';
 | |
|             }
 | |
|             $project_ids = Db::name('ProjectUser')->where(['uid' => $this->uid, 'delete_time' => 0])->column('project_id');
 | |
|             if (in_array($detail['project_id'], $project_ids) || in_array($this->uid, $role_uid) || in_array($this->uid, explode(",", $detail['assist_admin_ids']))) {
 | |
|                 $file_array = Db::name('FileInterfix')
 | |
|                     ->field('mf.id,mf.topic_id,mf.admin_id,f.name,f.filesize,f.filepath,a.name as admin_name')
 | |
|                     ->alias('mf')
 | |
|                     ->join('File f', 'mf.file_id = f.id', 'LEFT')
 | |
|                     ->join('Admin a', 'mf.admin_id = a.id', 'LEFT')
 | |
|                     ->order('mf.create_time desc')
 | |
|                     ->where(array('mf.topic_id' => $id, 'mf.module' => 'task'))
 | |
|                     ->select()->toArray();
 | |
| 
 | |
|                 $link_array = Db::name('LinkInterfix')
 | |
|                     ->field('i.id,i.topic_id,i.admin_id,i.desc,i.url,a.name as admin_name')
 | |
|                     ->alias('i')
 | |
|                     ->join('Admin a', 'i.admin_id = a.id', 'LEFT')
 | |
|                     ->order('i.create_time desc')
 | |
|                     ->where(array('i.topic_id' => $id, 'i.module' => 'task', 'delete_time' => 0))
 | |
|                     ->select()->toArray();
 | |
| 
 | |
|                 View::assign('detail', $detail);
 | |
|                 View::assign('file_array', $file_array);
 | |
|                 View::assign('link_array', $link_array);
 | |
|                 View::assign('role_edit', $role_edit);
 | |
|                 View::assign('id', $id);
 | |
|                 return view();
 | |
|             } else {
 | |
|                 return to_assign(1, '您没权限查看该任务');
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //删除
 | |
|     public function delete()
 | |
|     {
 | |
|         if (request()->isDelete()) {
 | |
|             $id = get_params("id");
 | |
|             $detail = Db::name('Task')->where('id', $id)->find();
 | |
|             if ($detail['admin_id'] != $this->uid) {
 | |
|                 return to_assign(1, "你不是该任务的创建人,无权限删除");
 | |
|             }
 | |
|             if (Db::name('Task')->where('id', $id)->update(['delete_time' => time()]) !== false) {
 | |
|                 $log_data = array(
 | |
|                     'module' => 'task',
 | |
|                     'field' => 'delete',
 | |
|                     'action' => 'delete',
 | |
|                     'task_id' => $detail['id'],
 | |
|                     'admin_id' => $this->uid,
 | |
|                     'old_content' => '',
 | |
|                     'new_content' => $detail['title'],
 | |
|                     'create_time' => time(),
 | |
|                 );
 | |
|                 Db::name('Log')->strict(false)->field(true)->insert($log_data);
 | |
|                 return to_assign(0, "删除成功");
 | |
|             } else {
 | |
|                 return to_assign(0, "删除失败");
 | |
|             }
 | |
|         } else {
 | |
|             return to_assign(1, "错误的请求");
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     //获取任务基础资料
 | |
|     public function gettask()
 | |
|     {
 | |
|         $param = get_params();
 | |
|         $id = isset($param['id']) ? $param['id'] : 0;
 | |
|         if ($id > 0) {
 | |
|             $task = Db::name('Task')->field('id, director_uid, project_id, admin_id, plan_hours, end_time, over_time, assist_admin_ids, cate, type, is_bug, priority, flow_status, done_ratio')
 | |
|                 ->where('id', $id)
 | |
|                 ->find();
 | |
|             if ($task) {
 | |
|                 // 更新任务信息
 | |
|                 $updateData = [
 | |
|                     'director_uid' => $param['director_uid'] ?? $task['director_uid'],
 | |
|                     'assist_admin_ids' => $param['assist_admin_ids'] ?? $task['assist_admin_ids'],
 | |
|                     'priority' => $param['priority'] ?? $task['priority'],
 | |
|                     'type' => $param['type'] ?? $task['type'],
 | |
|                     'is_bug' => $param['is_bug'] ?? $task['is_bug'],
 | |
|                     'cate' => $param['cate'] ?? $task['cate'],
 | |
|                     'plan_hours' => $param['plan_hours'] ?? $task['plan_hours'],
 | |
|                     'end_time' => isset($param['end_time']) ? strtotime($param['end_time']) : $task['end_time'],
 | |
|                     'over_time' => isset($param['over_time']) ? strtotime($param['over_time']) : $task['over_time'],
 | |
|                     'flow_status' => $param['flow_status'] ?? $task['flow_status'],
 | |
|                     'done_ratio' => $param['done_ratio'] ?? $task['done_ratio'],
 | |
|                     'admin_id' => $param['admin_id'] ?? $task['admin_id'],
 | |
|                 ];
 | |
| 
 | |
|                 // 判断任务状态,并修改任务进度
 | |
|                 switch ($updateData['flow_status']) {
 | |
|                     case 1:
 | |
|                         $updateData['done_ratio'] = 0;
 | |
|                         break;
 | |
|                     case 2:
 | |
|                         $updateData['done_ratio'] = 50;
 | |
|                         break;
 | |
|                     case 3:
 | |
|                         $updateData['done_ratio'] = 100;
 | |
|                         break;
 | |
|                     default:
 | |
|                         $updateData['done_ratio'] = 0;
 | |
|                         break;
 | |
|                 }
 | |
| 
 | |
|                 Db::name('Task')->where('id', $id)->update($updateData);
 | |
| 
 | |
|                 // 置换日期格式'YYYY-MM-DD'
 | |
|                 if (isset($updateData['end_time'])) {
 | |
|                     $task['end_time'] = $updateData['end_time'] > 0 ? date('Y-m-d', $updateData['end_time']) : '';
 | |
|                     if ($task['end_time'] === '1970-01-01') {
 | |
|                         $task['end_time'] = '';
 | |
|                     }
 | |
|                 }
 | |
|                 if (isset($updateData['over_time'])) {
 | |
|                     $task['over_time'] = $updateData['over_time'] > 0 ? date('Y-m-d', $updateData['over_time']) : '';
 | |
|                     if ($task['over_time'] === '1970-01-01') {
 | |
|                         $task['over_time'] = '';
 | |
|                     }
 | |
|                 }
 | |
| 
 | |
|                 switch ($updateData['priority']) {
 | |
|                     case 1:
 | |
|                         $task['priority'] = '低';
 | |
|                         break;
 | |
|                     case 2:
 | |
|                         $task['priority'] = '中';
 | |
|                         break;
 | |
|                     case 3:
 | |
|                         $task['priority'] = '高';
 | |
|                         break;
 | |
|                     case 4:
 | |
|                         $task['priority'] = '紧急';
 | |
|                         break;
 | |
|                 }
 | |
| 
 | |
|                 return json(['code' => 0, 'msg' => '更新成功', 'data' => ['detail' => $task]]);
 | |
|             } else {
 | |
|                 return json(['code' => 1, 'msg' => '任务不存在']);
 | |
|             }
 | |
|         } else {
 | |
|             return json(['code' => 1, 'msg' => '参数错误']);
 | |
|         }
 | |
|     }
 | |
| 
 | |
| 
 | |
|     //获取工作类型
 | |
|     public function getworkcate()
 | |
|     {
 | |
|         $cateList = Db::name('WorkCate')->field('id, title')->order('')->select();
 | |
|         $typeList = Db::name('TaskCate')->field('id, title')->order('')->select();
 | |
|         $adminList = Db::name('Admin')->field('id, name')->order('')->select();
 | |
|         $response = [
 | |
|             'code' => 0,
 | |
|             'data' => [
 | |
|                 'cate' => $cateList,
 | |
|                 'admin_id' => $adminList,
 | |
|                 'type' => $typeList
 | |
|             ]
 | |
|         ];
 | |
|         return json($response);
 | |
|     }
 | |
| 
 | |
| } |