no message

This commit is contained in:
kuaifan 2024-01-11 22:45:10 +08:00
parent e74aeb9393
commit b209040978
3 changed files with 42 additions and 26 deletions

View File

@ -673,7 +673,11 @@ class DialogController extends AbstractController
* @apiGroup dialog * @apiGroup dialog
* @apiName msg__read * @apiName msg__read
* *
* @apiParam {Number} id 消息ID * @apiParam {Object} id 消息ID
* - 1、多个ID用逗号分隔1,2,3
* - 2、另一种格式:{"id": "[会话ID]"},如:{"2": 0, "3": 10}
* -- 会话ID标记id之后的消息已读
* -- 其他:标记已读
* *
* @apiSuccess {Number} ret 返回状态码1正确、0错误 * @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {String} msg 返回信息(错误描述)
@ -684,17 +688,31 @@ class DialogController extends AbstractController
$user = User::auth(); $user = User::auth();
// //
$id = Request::input('id'); $id = Request::input('id');
$ids = Base::explodeInt($id); $ids = $id && is_array($id) ? $id : array_fill_keys(Base::explodeInt($id), 'r');
// //
$dialogIds = []; $dialogIds = [];
WebSocketDialogMsg::whereIn('id', $ids)->chunkById(20, function($list) use ($user, &$dialogIds) { $markIds = [];
WebSocketDialogMsg::whereIn('id', array_keys($ids))->chunkById(100, function($list) use ($ids, $user, &$dialogIds, &$markIds) {
/** @var WebSocketDialogMsg $item */ /** @var WebSocketDialogMsg $item */
foreach ($list as $item) { foreach ($list as $item) {
$item->readSuccess($user->userid); $item->readSuccess($user->userid);
$dialogIds[$item->dialog_id] = $item->dialog_id; $dialogIds[$item->dialog_id] = $item->dialog_id;
if ($ids[$item->id] == $item->dialog_id) {
$markIds[$item->dialog_id] = min($item->id, $markIds[$item->dialog_id] ?? 0);
}
} }
}); });
// //
foreach ($markIds as $dialogId => $msgId) {
WebSocketDialogMsgRead::whereDialogId($dialogId)
->whereUserid($user->userid)
->whereReadAt(null)
->where('msg_id', '>=', $msgId)
->chunkById(100, function ($list) {
WebSocketDialogMsgRead::onlyMarkRead($list);
});
}
//
$data = []; $data = [];
$dialogUsers = WebSocketDialogUser::with(['webSocketDialog'])->whereUserid($user->userid)->whereIn('dialog_id', array_values($dialogIds))->get(); $dialogUsers = WebSocketDialogUser::with(['webSocketDialog'])->whereUserid($user->userid)->whereIn('dialog_id', array_values($dialogIds))->get();
foreach ($dialogUsers as $dialogUser) { foreach ($dialogUsers as $dialogUser) {

View File

@ -2677,9 +2677,9 @@ export default {
} else { } else {
state.dialogIns.push(data); state.dialogIns.push(data);
} }
// 会话消息总数量大于1500时只保留最近打开的30个会话 // 会话消息总数量大于5000时只保留最近打开的50个会话
const msg_max = 1500 const msg_max = 5000
const retain_num = 30 const retain_num = 500
state.dialogHistory = state.dialogHistory.filter(id => id != data.dialog_id) state.dialogHistory = state.dialogHistory.filter(id => id != data.dialog_id)
state.dialogHistory.push(data.dialog_id) state.dialogHistory.push(data.dialog_id)
if (state.dialogMsgs.length > msg_max && state.dialogHistory.length > retain_num) { if (state.dialogMsgs.length > msg_max && state.dialogHistory.length > retain_num) {
@ -3012,7 +3012,7 @@ export default {
requestData.page = 1 requestData.page = 1
} }
if (typeof requestData.pagesize === "undefined") { if (typeof requestData.pagesize === "undefined") {
requestData.pagesize = 20 requestData.pagesize = 50
} }
if (typeof requestData.latest_id === "undefined") { if (typeof requestData.latest_id === "undefined") {
requestData.latest_id = state.loadDialogLatestId requestData.latest_id = state.loadDialogLatestId
@ -3058,7 +3058,7 @@ export default {
if (data.userid == state.userId) return; if (data.userid == state.userId) return;
if (data.read_at) return; if (data.read_at) return;
data.read_at = $A.formatDate(); data.read_at = $A.formatDate();
state.readWaitData[data.id] = data.id; state.readWaitData[data.id] = state.readWaitData[data.id] || 0
// //
const dialog = state.cacheDialogs.find(({id}) => id == data.dialog_id); const dialog = state.cacheDialogs.find(({id}) => id == data.dialog_id);
if (dialog) { if (dialog) {
@ -3075,8 +3075,8 @@ export default {
} }
} }
if (mark) { if (mark) {
state.readEndMark[data.dialog_id] = Math.max(data.id, $A.runNum(state.readEndMark[data.dialog_id]))
dispatch("saveDialog", dialog) dispatch("saveDialog", dialog)
state.readWaitData[data.id] = data.dialog_id
} }
} }
} }
@ -3087,34 +3087,33 @@ export default {
if (state.userId === 0) { if (state.userId === 0) {
return; return;
} }
const ids = Object.values(state.readWaitData); if (Object.values(state.readWaitData).length === 0) {
state.readWaitData = {};
if (ids.length === 0) {
return return
} }
const ids = $A.cloneJSON(state.readWaitData);
state.readWaitData = {};
// //
dispatch("call", { dispatch("call", {
method: 'post',
url: 'dialog/msg/read', url: 'dialog/msg/read',
data: { data: {
id: ids.join(",") id: ids
} }
}).then(({data}) => { }).then(({data}) => {
for (const id in ids) {
if (ids.hasOwnProperty(id) && /^\d+$/.test(ids[id])) {
state.dialogMsgs.some(item => {
if (item.dialog_id == ids[id] && item.id >= id) {
item.read_at = $A.formatDate()
}
})
}
}
dispatch("saveDialog", data) dispatch("saveDialog", data)
}).catch(_ => { }).catch(_ => {
ids.some(id => { state.readWaitData = ids;
state.readWaitData[id] = id;
})
}).finally(_ => { }).finally(_ => {
state.readLoadNum++ state.readLoadNum++
//
for (let dialog_id in state.readEndMark) {
dispatch("dialogMsgMark", {
type: 'read',
dialog_id,
after_msg_id: state.readEndMark[dialog_id],
})
}
state.readEndMark = {}
}); });
}, 50); }, 50);
}, },

View File

@ -130,7 +130,6 @@ export default {
readLoadNum: 0, readLoadNum: 0,
readTimeout: null, readTimeout: null,
readWaitData: {}, readWaitData: {},
readEndMark: {},
// 文件 // 文件
fileLists: [], fileLists: [],