撤回消息优化

This commit is contained in:
kuaifan 2022-01-28 15:02:58 +08:00
parent 35bd038802
commit 02eb386155
3 changed files with 86 additions and 48 deletions

View File

@ -8,9 +8,7 @@ use App\Models\User;
use App\Models\WebSocketDialog; use App\Models\WebSocketDialog;
use App\Models\WebSocketDialogMsg; use App\Models\WebSocketDialogMsg;
use App\Models\WebSocketDialogMsgRead; use App\Models\WebSocketDialogMsgRead;
use App\Models\WebSocketDialogUser;
use App\Module\Base; use App\Module\Base;
use Carbon\Carbon;
use Request; use Request;
use Response; use Response;
@ -417,8 +415,18 @@ class DialogController extends AbstractController
} }
/** /**
* 聊天消息撤回 * @api {get} api/dialog/msg/withdraw 11. 聊天消息撤回
* @return array *
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup dialog
* @apiName msg__withdraw
*
* @apiParam {Number} msg_id 消息ID
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/ */
public function msg__withdraw() public function msg__withdraw()
{ {
@ -426,34 +434,9 @@ class DialogController extends AbstractController
$msg_id = intval(Request::input("msg_id")); $msg_id = intval(Request::input("msg_id"));
$msg = WebSocketDialogMsg::whereId($msg_id)->whereUserid($user->userid)->first(); $msg = WebSocketDialogMsg::whereId($msg_id)->whereUserid($user->userid)->first();
if (empty($msg)) { if (empty($msg)) {
return Base::retError("此消息不可撤回"); return Base::retError("消息不存在或已被删除");
} }
$send_dt = Carbon::parse($msg->created_at)->addMinutes(5); $msg->deleteMsg();
if ($send_dt->lt(Carbon::now()))
return Base::retError("已超过5分钟此消息不能撤回");
// 删除文件、图片
if ($msg->type == WebSocketDialogMsg::MSG_TYPE_FILE) {
if (is_array($msg->msg)) {
// 删除本体
if (!empty($msg->msg["file"]))
@unlink($msg->msg["file"]);
// 删除缩略图
if (!empty($msg->msg["thumb"]))
@unlink($msg->msg["thumb"]);
}
}
// 直接删除消息
$msg->delete();
/* 原始需求:消息直接删除,无需提示 */
// 发送撤回指令
// WebSocketDialogMsg::sendMsg($msg->dialog_id, 'withdraw', [
// "msg_id" => $msg->id, // 被撤回的消息Id
// ], $user->userid);
return Base::retSuccess("success"); return Base::retSuccess("success");
} }
} }

View File

@ -127,6 +127,35 @@ class WebSocketDialogMsg extends AbstractModel
return true; return true;
} }
/**
* 删除消息
* @return void
*/
public function deleteMsg()
{
$send_dt = Carbon::parse($this->created_at)->addMinutes(5);
if ($send_dt->lt(Carbon::now())) {
throw new ApiException('已超过5分钟此消息不能撤回');
}
$this->delete();
//
$dialog = WebSocketDialog::find($this->dialog_id);
if ($dialog) {
$userids = $dialog->dialogUser->pluck('userid')->toArray();
PushTask::push([
'userid' => $userids,
'msg' => [
'type' => 'dialog',
'mode' => 'delete',
'data' => [
'id' => $this->id,
'dialog_id' => $this->dialog_id
],
]
]);
}
}
/** /**
* 发送消息 * 发送消息
* @param int $dialog_id 会话ID 聊天室ID * @param int $dialog_id 会话ID 聊天室ID

View File

@ -1833,6 +1833,23 @@ export default {
} }
}, },
/**
* 忘记消息数据
* @param state
* @param msg_id
*/
forgetDialogMsg({state}, msg_id) {
$A.execMainDispatch("forgetDialogMsg", msg_id)
//
let ids = $A.isArray(msg_id) ? msg_id : [msg_id];
ids.some(id => {
let index = state.dialogMsgs.findIndex(item => item.id == id);
if (index > -1) {
state.dialogMsgs.splice(index, 1);
}
})
},
/** /**
* 获取会话消息 * 获取会话消息
* @param state * @param state
@ -2032,24 +2049,33 @@ export default {
(function (msg) { (function (msg) {
const {mode, data} = msg; const {mode, data} = msg;
const {dialog_id} = data; const {dialog_id} = data;
if (["add", "chat"].includes(mode) && !state.dialogMsgs.find(({id}) => id == data.id)) { switch (mode) {
// 新增任务消息数量 case 'delete':
dispatch("increaseTaskMsgNum", dialog_id); // 删除消息
if (mode === "chat") { dispatch("forgetDialogMsg", data.id)
return; break;
} case 'add':
let dialog = state.cacheDialogs.find(({id}) => id == data.dialog_id); case 'chat':
// 更新对话列表 if (!state.dialogMsgs.find(({id}) => id == data.id)) {
if (dialog) { // 新增任务消息数量
// 新增未读数 dispatch("increaseTaskMsgNum", dialog_id);
dialog.unread++; if (mode === "chat") {
} return;
Store.set('dialogMsgPush', data); }
let dialog = state.cacheDialogs.find(({id}) => id == data.dialog_id);
// 更新对话列表
if (dialog) {
// 新增未读数
dialog.unread++;
}
Store.set('dialogMsgPush', data);
}
// 更新消息列表
dispatch("saveDialogMsg", data)
// 更新最后消息
dispatch("updateDialogLastMsg", data);
break;
} }
// 更新消息列表
dispatch("saveDialogMsg", data)
// 更新最后消息
dispatch("updateDialogLastMsg", data);
})(msgDetail); })(msgDetail);
break; break;