添加任务推送消息

This commit is contained in:
kuaifan 2021-06-17 22:34:03 +08:00
parent c74fdb18e6
commit 3da0a41155
5 changed files with 154 additions and 105 deletions

View File

@ -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);
}
/**

View File

@ -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

View File

@ -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);
}
}

View File

@ -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<unknown>}
*/
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<unknown>}
*/
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
}

View File

@ -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);
},
}