From 3da0a41155f5e128976491451e1211c4bdde8cd2 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Thu, 17 Jun 2021 22:34:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=BB=E5=8A=A1=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/ProjectController.php | 12 +- app/Models/ProjectTask.php | 24 +++ app/Tasks/PushTask.php | 17 +- resources/assets/js/store/actions.js | 158 ++++++++---------- resources/assets/js/store/mutations.js | 48 +++++- 5 files changed, 154 insertions(+), 105 deletions(-) diff --git a/app/Http/Controllers/Api/ProjectController.php b/app/Http/Controllers/Api/ProjectController.php index 35217090..16cff9c5 100755 --- a/app/Http/Controllers/Api/ProjectController.php +++ b/app/Http/Controllers/Api/ProjectController.php @@ -628,11 +628,13 @@ class ProjectController extends AbstractController 'project_id' => $project->id, 'column_id' => $column->id, ])); - return Base::retSuccess('添加成功', [ + $data = [ 'new_column' => $newColumn, 'in_top' => intval($data['top']), 'task' => ProjectTask::with(['taskUser', 'taskTag'])->find($task->id), - ]); + ]; + $task->pushMsg('add', $data); + return Base::retSuccess('添加成功', $data); } /** @@ -656,11 +658,13 @@ class ProjectController extends AbstractController 'project_id' => $task->project_id, 'column_id' => $task->column_id, ]); - return Base::retSuccess('添加成功', [ + $data = [ 'new_column' => null, 'in_top' => 0, 'task' => ProjectTask::with(['taskUser', 'taskTag'])->find($task->id), - ]); + ]; + $task->pushMsg('add', $data); + return Base::retSuccess('添加成功', $data); } /** diff --git a/app/Models/ProjectTask.php b/app/Models/ProjectTask.php index 4c13a713..360baf49 100644 --- a/app/Models/ProjectTask.php +++ b/app/Models/ProjectTask.php @@ -4,8 +4,10 @@ namespace App\Models; use App\Exceptions\ApiException; use App\Module\Base; +use App\Tasks\PushTask; use Arr; use Carbon\Carbon; +use Hhxsv5\LaravelS\Swoole\Task\Task; use Illuminate\Database\Eloquent\SoftDeletes; /** @@ -616,6 +618,28 @@ class ProjectTask extends AbstractModel return $log; } + /** + * 推送消息 + * @param string $action + * @param array $data + */ + public function pushMsg($action, $data) + { + if (!$this->project) { + return; + } + $lists = [ + 'userid' => $this->project->relationUserids(), + 'msg' => [ + 'type' => 'projectTask', + 'action' => $action, + 'data' => $data, + ] + ]; + $task = new PushTask($lists, false); + Task::deliver($task); + } + /** * 根据会员ID获取任务、项目信息(用于判断会员是否存在项目内) * @param int $task_id diff --git a/app/Tasks/PushTask.php b/app/Tasks/PushTask.php index 5e9cad89..cf390d7a 100644 --- a/app/Tasks/PushTask.php +++ b/app/Tasks/PushTask.php @@ -18,14 +18,16 @@ use Hhxsv5\LaravelS\Swoole\Task\Task; class PushTask extends AbstractTask { protected $params; + protected $retryOffline = true; /** * PushTask constructor. * @param array|string $params */ - public function __construct($params = []) + public function __construct($params = [], $retryOffline = true) { $this->params = $params; + $this->retryOffline = $retryOffline; } public function start() @@ -43,7 +45,7 @@ class PushTask extends AbstractTask self::sendTmpMsgForUserid(intval(Base::leftDelete($this->params, "RETRY::"))); } } - is_array($this->params) && self::push($this->params); + is_array($this->params) && self::push($this->params, '', 1, $this->retryOffline); } /** @@ -162,7 +164,7 @@ class PushTask extends AbstractTask 'fd' => $fid, 'msg' => $msg, ]); - $task = new PushTask($key); + $task = new PushTask($key, $retryOffline); $task->delay($delay); Task::deliver($task); } @@ -174,13 +176,4 @@ class PushTask extends AbstractTask } } } - - /** - * 推送消息(仅推送当前在线的) - * @param array $lists 消息列表 - */ - public static function pushO(array $lists, $key = '', $delay = 1) - { - self::push($lists, $key, $delay, false); - } } diff --git a/resources/assets/js/store/actions.js b/resources/assets/js/store/actions.js index ecbf8b09..b6dc2fb3 100644 --- a/resources/assets/js/store/actions.js +++ b/resources/assets/js/store/actions.js @@ -419,6 +419,9 @@ export default { if (!data.start_at || !data.end_at) { return; } + if (!data.userid != state.userId) { + return; + } let task = { id: data.id, calendarId: String(data.project_id), @@ -612,40 +615,22 @@ export default { * 添加任务 * @param state * @param dispatch + * @param commit * @param data * @returns {Promise} */ - taskAdd({state, dispatch}, data) { + taskAdd({state, dispatch, commit}, data) { return new Promise(function (resolve, reject) { const post = state.method.cloneJSON(state.method.date2string(data)); - if (state.method.isArray(post.column_id)) { - post.column_id = post.column_id.find((val) => val) - } - if (state.method.isArray(post.owner)) { - post.owner = post.owner.find((val) => val) - } + if (state.method.isArray(post.column_id)) post.column_id = post.column_id.find((val) => val) + if (state.method.isArray(post.owner)) post.owner = post.owner.find((val) => val) // dispatch("call", { url: 'project/task/add', data: post, method: 'post', }).then(result => { - const {task, in_top, new_column} = result.data; - if (state.projectDetail.id == task.project_id) { - if (new_column) { - state.projectDetail.project_column.push(new_column); - } - const column = state.projectDetail.project_column.find(({id}) => id === task.column_id); - if (column) { - if (in_top) { - column.project_task.unshift(task); - } else { - column.project_task.push(task); - } - } - } - dispatch("saveTask", task); - dispatch("getProjectOne", task.project_id); + commit("taskAddSuccess", result.data) resolve(result) }).catch(result => { reject(result) @@ -655,38 +640,18 @@ export default { /** * 添加子任务 - * @param state * @param dispatch + * @param commit * @param data {task_id, name} * @returns {Promise} */ - taskAddSub({state, dispatch}, data) { + taskAddSub({dispatch, commit}, data) { return new Promise(function (resolve, reject) { dispatch("call", { url: 'project/task/addsub', data: data, }).then(result => { - const {task} = result.data; - if (state.projectDetail.id == task.project_id) { - const column = state.projectDetail.project_column.find(({id}) => id === task.column_id); - if (column) { - const project_task = column.project_task.find(({id}) => id === task.parent_id) - if (project_task && project_task.sub_task) { - let index = project_task.sub_task.findIndex(({id}) => id === task.id) - if (index === -1) { - project_task.sub_task.push(task); - } - } - } - } - if (task.parent_id == state.projectOpenTask.id) { - let index = state.projectOpenTask.sub_task.findIndex(({id}) => id === task.id) - if (index === -1) { - state.projectOpenTask.sub_task.push(task); - } - } - dispatch("saveTask", task); - dispatch("getTaskOne", task.parent_id); + commit("taskAddSuccess", result.data) resolve(result) }).catch(result => { reject(result) @@ -1045,8 +1010,9 @@ export default { * 初始化 websocket * @param state * @param dispatch + * @param commit */ - websocketConnection({state, dispatch}) { + websocketConnection({state, dispatch, commit}) { clearTimeout(state.wsTimeout); if (state.userId === 0) { if (state.ws) { @@ -1116,49 +1082,65 @@ export default { } } }); - if (type === "dialog") { - // 更新会话 - (function (msg) { - const {mode, data} = msg; - const {dialog_id} = data; - // 更新消息列表 - if (dialog_id == state.dialogId) { - let index = state.dialogMsgList.findIndex(({id}) => id == data.id); - if (index === -1) { - state.dialogMsgList.push(data); - } else { - state.dialogMsgList.splice(index, 1, data); - } - } - // 更新最后消息 - let dialog = state.dialogList.find(({id}) => id == dialog_id); - if (dialog) { - dialog.last_msg = data; - } else { - dispatch("getDialogOne", dialog_id); - } - if (mode === "add") { - // 更新对话列表 - if (dialog) { - // 新增未读数 - if (data.userid !== state.userId) dialog.unread++; - // 移动到首位 - const index = state.dialogList.findIndex(({id}) => id == dialog_id); - if (index > -1) { - const tmp = state.dialogList[index]; - state.dialogList.splice(index, 1); - state.dialogList.unshift(tmp); + switch (type) { + /** + * 会话消息 + */ + case "dialog": // 更新会话 + (function (msg) { + const {mode, data} = msg; + const {dialog_id} = data; + // 更新消息列表 + if (dialog_id == state.dialogId) { + let index = state.dialogMsgList.findIndex(({id}) => id == data.id); + if (index === -1) { + state.dialogMsgList.push(data); + } else { + state.dialogMsgList.splice(index, 1, data); } } - // 新增任务消息数量 - state.projectDetail.project_column.some(({project_task}) => { - const task = project_task.find(({dialog_id}) => dialog_id === data.dialog_id); - if (task) task.msg_num++; - }); - // 新增总未读数 - if (data.userid !== state.userId) state.dialogMsgUnread++; - } - })(msgDetail); + // 更新最后消息 + let dialog = state.dialogList.find(({id}) => id == dialog_id); + if (dialog) { + dialog.last_msg = data; + } else { + dispatch("getDialogOne", dialog_id); + } + if (mode === "add") { + // 更新对话列表 + if (dialog) { + // 新增未读数 + if (data.userid !== state.userId) dialog.unread++; + // 移动到首位 + const index = state.dialogList.findIndex(({id}) => id == dialog_id); + if (index > -1) { + const tmp = state.dialogList[index]; + state.dialogList.splice(index, 1); + state.dialogList.unshift(tmp); + } + } + // 新增任务消息数量 + state.projectDetail.project_column.some(({project_task}) => { + const task = project_task.find(({dialog_id}) => dialog_id === data.dialog_id); + if (task) task.msg_num++; + }); + // 新增总未读数 + if (data.userid !== state.userId) state.dialogMsgUnread++; + } + })(msgDetail); + break; + + /** + * 任务消息 + */ + case "projectTask": + (function (msg) { + const {action, data} = msg; + if (action == 'add') { + commit("taskAddSuccess", data) + } + })(msgDetail); + break; } break } diff --git a/resources/assets/js/store/mutations.js b/resources/assets/js/store/mutations.js index 03048022..56c3b7fe 100644 --- a/resources/assets/js/store/mutations.js +++ b/resources/assets/js/store/mutations.js @@ -1,3 +1,49 @@ export default { - + /** + * 添加任务完成 + * @param state + * @param data + */ + taskAddSuccess(state, data) { + const {task, in_top, new_column} = data; + if (task.parent_id == 0) { + // 添加任务 + if (state.projectDetail.id == task.project_id) { + if (new_column) { + state.projectDetail.project_column.push(new_column); + } + const column = state.projectDetail.project_column.find(({id}) => id === task.column_id); + if (column) { + if (in_top) { + column.project_task.unshift(task); + } else { + column.project_task.push(task); + } + } + } + this.dispatch("getProjectOne", task.project_id); + } else { + // 添加子任务 + if (state.projectDetail.id == task.project_id) { + const column = state.projectDetail.project_column.find(({id}) => id === task.column_id); + if (column) { + const project_task = column.project_task.find(({id}) => id === task.parent_id) + if (project_task && project_task.sub_task) { + let index = project_task.sub_task.findIndex(({id}) => id === task.id) + if (index === -1) { + project_task.sub_task.push(task); + } + } + } + } + if (task.parent_id == state.projectOpenTask.id) { + let index = state.projectOpenTask.sub_task.findIndex(({id}) => id === task.id) + if (index === -1) { + state.projectOpenTask.sub_task.push(task); + } + } + this.dispatch("getTaskOne", task.parent_id); + } + this.dispatch("saveTask", task); + }, }