mirror of
https://github.com/kuaifan/dootask.git
synced 2026-03-17 19:23:26 +00:00
perf: 优化消息已读未读
This commit is contained in:
parent
e0bd225ac2
commit
eca066cbd0
@ -85,11 +85,10 @@ class DialogController extends AbstractController
|
|||||||
->where('web_socket_dialogs.id', $dialog_id)
|
->where('web_socket_dialogs.id', $dialog_id)
|
||||||
->where('u.userid', $user->userid)
|
->where('u.userid', $user->userid)
|
||||||
->first();
|
->first();
|
||||||
if ($item) {
|
if (empty($item)) {
|
||||||
$item = $item->formatData($user->userid);
|
return Base::retError('会话不存在或已被删除', ['dialog_id' => $dialog_id], -4003);
|
||||||
}
|
}
|
||||||
//
|
return Base::retSuccess('success', $item->formatData($user->userid));
|
||||||
return Base::retSuccess('success', $item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -138,7 +137,7 @@ class DialogController extends AbstractController
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @api {get} api/dialog/msg/user 04. 打开会话
|
* @api {get} api/dialog/open/user 04. 打开会话
|
||||||
*
|
*
|
||||||
* @apiDescription 需要token身份
|
* @apiDescription 需要token身份
|
||||||
* @apiVersion 1.0.0
|
* @apiVersion 1.0.0
|
||||||
@ -165,9 +164,6 @@ class DialogController extends AbstractController
|
|||||||
return Base::retError('打开会话失败');
|
return Base::retError('打开会话失败');
|
||||||
}
|
}
|
||||||
$data = WebSocketDialog::find($dialog->id)?->formatData($user->userid);
|
$data = WebSocketDialog::find($dialog->id)?->formatData($user->userid);
|
||||||
if (empty($data)) {
|
|
||||||
return Base::retError('打开会话错误');
|
|
||||||
}
|
|
||||||
return Base::retSuccess('success', $data);
|
return Base::retSuccess('success', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,6 +233,11 @@ class DialogController extends AbstractController
|
|||||||
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
|
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
|
||||||
* @apiSuccess {String} msg 返回信息(错误描述)
|
* @apiSuccess {String} msg 返回信息(错误描述)
|
||||||
* @apiSuccess {Object} data 返回数据
|
* @apiSuccess {Object} data 返回数据
|
||||||
|
* @apiSuccessExample {json} data:
|
||||||
|
{
|
||||||
|
"unread": 43, // 未读消息数
|
||||||
|
"last_umid": 308 // 最新的一条未读消息ID,用于判断是否更新前端的未读数量
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
public function msg__unread()
|
public function msg__unread()
|
||||||
{
|
{
|
||||||
@ -246,9 +247,9 @@ class DialogController extends AbstractController
|
|||||||
if ($dialog_id > 0) {
|
if ($dialog_id > 0) {
|
||||||
$builder->whereDialogId($dialog_id);
|
$builder->whereDialogId($dialog_id);
|
||||||
}
|
}
|
||||||
$unread = $builder->count();
|
|
||||||
return Base::retSuccess('success', [
|
return Base::retSuccess('success', [
|
||||||
'unread' => $unread,
|
'unread' => $builder->count(),
|
||||||
|
'last_umid' => intval($builder->orderByDesc('msg_id')->value('msg_id')),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -64,7 +64,12 @@ class WebSocketDialog extends AbstractModel
|
|||||||
// 未读信息
|
// 未读信息
|
||||||
$unreadBuilder = WebSocketDialogMsgRead::whereDialogId($this->id)->whereUserid($userid)->whereReadAt(null);
|
$unreadBuilder = WebSocketDialogMsgRead::whereDialogId($this->id)->whereUserid($userid)->whereReadAt(null);
|
||||||
$this->unread = $unreadBuilder->count();
|
$this->unread = $unreadBuilder->count();
|
||||||
$this->mention = $unreadBuilder->whereMention(1)->count();
|
$this->mention = 0;
|
||||||
|
$this->last_umid = 0;
|
||||||
|
if ($this->unread > 0) {
|
||||||
|
$this->mention = $unreadBuilder->clone()->whereMention(1)->count();
|
||||||
|
$this->last_umid = intval($unreadBuilder->clone()->orderByDesc('msg_id')->value('msg_id'));
|
||||||
|
}
|
||||||
$this->mark_unread = $this->mark_unread ?? WebSocketDialogUser::whereDialogId($this->id)->whereUserid($userid)->value('mark_unread');
|
$this->mark_unread = $this->mark_unread ?? WebSocketDialogUser::whereDialogId($this->id)->whereUserid($userid)->value('mark_unread');
|
||||||
// 对话人数
|
// 对话人数
|
||||||
$builder = WebSocketDialogUser::whereDialogId($this->id);
|
$builder = WebSocketDialogUser::whereDialogId($this->id);
|
||||||
|
|||||||
@ -165,10 +165,6 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
activated() {
|
|
||||||
this.msgRead()
|
|
||||||
},
|
|
||||||
|
|
||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
Store.set('audioSubscribe', this.msgData.id);
|
Store.set('audioSubscribe', this.msgData.id);
|
||||||
},
|
},
|
||||||
@ -266,14 +262,7 @@ export default {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.msgData._r = true;
|
this.msgData._r = true;
|
||||||
//
|
this.$store.dispatch("dialogMsgRead", this.msgData);
|
||||||
setTimeout(() => {
|
|
||||||
if (!this.$el.offsetParent) {
|
|
||||||
this.msgData._r = false;
|
|
||||||
return
|
|
||||||
}
|
|
||||||
this.$store.dispatch("dialogMsgRead", this.msgData);
|
|
||||||
}, 50)
|
|
||||||
},
|
},
|
||||||
|
|
||||||
openReadPercentage() {
|
openReadPercentage() {
|
||||||
|
|||||||
63
resources/assets/js/store/actions.js
vendored
63
resources/assets/js/store/actions.js
vendored
@ -1905,9 +1905,15 @@ export default {
|
|||||||
dispatch("saveDialog", dialog)
|
dispatch("saveDialog", dialog)
|
||||||
});
|
});
|
||||||
} else if ($A.isJson(data)) {
|
} else if ($A.isJson(data)) {
|
||||||
let index = state.cacheDialogs.findIndex(({id}) => id == data.id);
|
const index = state.cacheDialogs.findIndex(({id}) => id == data.id);
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
state.cacheDialogs.splice(index, 1, Object.assign({}, state.cacheDialogs[index], data));
|
const original = state.cacheDialogs[index];
|
||||||
|
if (typeof data.unread === "number" && data.unread > original.unread && data.last_umid <= original.last_umid) {
|
||||||
|
// 增加未读数时:新数据的最后一条未读消息id <= 原数据,则不更新未读数
|
||||||
|
data.unread = original.unread;
|
||||||
|
data.mention = original.mention;
|
||||||
|
}
|
||||||
|
state.cacheDialogs.splice(index, 1, Object.assign({}, original, data));
|
||||||
} else {
|
} else {
|
||||||
state.cacheDialogs.push(data);
|
state.cacheDialogs.push(data);
|
||||||
}
|
}
|
||||||
@ -1931,8 +1937,8 @@ export default {
|
|||||||
dispatch("updateDialogLastMsg", msg)
|
dispatch("updateDialogLastMsg", msg)
|
||||||
});
|
});
|
||||||
} else if ($A.isJson(data)) {
|
} else if ($A.isJson(data)) {
|
||||||
let dialog = state.cacheDialogs.find(({id}) => id == data.dialog_id);
|
const index = state.cacheDialogs.findIndex(({id}) => id == data.dialog_id);
|
||||||
if (dialog) {
|
if (index > -1) {
|
||||||
dispatch("saveDialog", {
|
dispatch("saveDialog", {
|
||||||
id: data.dialog_id,
|
id: data.dialog_id,
|
||||||
last_msg: data,
|
last_msg: data,
|
||||||
@ -2190,7 +2196,6 @@ export default {
|
|||||||
dialog.loading = false;
|
dialog.loading = false;
|
||||||
dialog.currentPage = result.data.current_page;
|
dialog.currentPage = result.data.current_page;
|
||||||
dialog.hasMorePages = !!result.data.next_page_url;
|
dialog.hasMorePages = !!result.data.next_page_url;
|
||||||
dispatch("saveDialog", dialog);
|
|
||||||
//
|
//
|
||||||
const ids = result.data.data.map(({id}) => id)
|
const ids = result.data.data.map(({id}) => id)
|
||||||
state.dialogMsgs = state.dialogMsgs.filter((item) => item.dialog_id != dialog_id || ids.includes(item.id));
|
state.dialogMsgs = state.dialogMsgs.filter((item) => item.dialog_id != dialog_id || ids.includes(item.id));
|
||||||
@ -2261,14 +2266,17 @@ export default {
|
|||||||
if (data.read_at) return;
|
if (data.read_at) return;
|
||||||
data.read_at = $A.formatDate();
|
data.read_at = $A.formatDate();
|
||||||
//
|
//
|
||||||
let dialog = state.cacheDialogs.find(({id}) => id == data.dialog_id);
|
const dialog = state.cacheDialogs.find(({id}) => id == data.dialog_id);
|
||||||
if (dialog && dialog.unread > 0) {
|
if (dialog && dialog.unread > 0) {
|
||||||
dialog.mark_unread = 0
|
const newData = {
|
||||||
dialog.unread--
|
id: data.dialog_id,
|
||||||
if (data.mention) {
|
mark_unread: 0,
|
||||||
dialog.mention--
|
|
||||||
}
|
}
|
||||||
dispatch("saveDialog", dialog)
|
newData.unread = dialog.unread - 1;
|
||||||
|
if (data.mention) {
|
||||||
|
newData.mention = dialog.mention - 1;
|
||||||
|
}
|
||||||
|
dispatch("saveDialog", newData)
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
state.wsReadWaitList.push(data.id);
|
state.wsReadWaitList.push(data.id);
|
||||||
@ -2375,22 +2383,26 @@ export default {
|
|||||||
// 删除消息
|
// 删除消息
|
||||||
dispatch("forgetDialogMsg", data.id)
|
dispatch("forgetDialogMsg", data.id)
|
||||||
//
|
//
|
||||||
let dialog = state.cacheDialogs.find(({id}) => id == data.dialog_id);
|
const dialog = state.cacheDialogs.find(({id}) => id == data.dialog_id);
|
||||||
if (dialog) {
|
if (dialog) {
|
||||||
// 更新最后消息
|
// 更新最后消息
|
||||||
dialog.last_at = data.last_msg && data.last_msg.created_at;
|
const newData = {
|
||||||
dialog.last_msg = data.last_msg;
|
id: data.dialog_id,
|
||||||
|
last_at: data.last_msg && data.last_msg.created_at,
|
||||||
|
last_msg: data.last_msg,
|
||||||
|
}
|
||||||
if (data.update_read) {
|
if (data.update_read) {
|
||||||
// 更新未读数量
|
// 更新未读数量
|
||||||
dispatch("call", {
|
dispatch("call", {
|
||||||
url: 'dialog/msg/unread',
|
url: 'dialog/msg/unread',
|
||||||
dialog_id: data.dialog_id
|
dialog_id: data.dialog_id
|
||||||
}).then(result => {
|
}).then(result => {
|
||||||
dialog.unread = result.data.unread
|
newData.unread = result.data.unread
|
||||||
dispatch("saveDialog", dialog)
|
newData.last_umid = result.data.last_umid
|
||||||
|
dispatch("saveDialog", newData)
|
||||||
}).catch(() => {});
|
}).catch(() => {});
|
||||||
} else {
|
} else {
|
||||||
dispatch("saveDialog", dialog)
|
dispatch("saveDialog", newData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -2404,14 +2416,17 @@ export default {
|
|||||||
}
|
}
|
||||||
if (data.userid !== state.userId) {
|
if (data.userid !== state.userId) {
|
||||||
// 更新对话新增未读数
|
// 更新对话新增未读数
|
||||||
let dialog = state.cacheDialogs.find(({id}) => id == data.dialog_id);
|
const dialog = state.cacheDialogs.find(({id}) => id == data.dialog_id);
|
||||||
if (dialog && state.cacheUnreads[data.id] === undefined) {
|
if (dialog) {
|
||||||
state.cacheUnreads[data.id] = true;
|
const newData = {
|
||||||
dialog.unread++;
|
id: data.dialog_id,
|
||||||
if (data.mention) {
|
last_umid: data.id,
|
||||||
dialog.mention++;
|
|
||||||
}
|
}
|
||||||
dispatch("saveDialog", dialog)
|
newData.unread = dialog.unread + 1;
|
||||||
|
if (data.mention) {
|
||||||
|
newData.mention = dialog.mention + 1;
|
||||||
|
}
|
||||||
|
dispatch("saveDialog", newData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Store.set('dialogMsgPush', data);
|
Store.set('dialogMsgPush', data);
|
||||||
|
|||||||
1
resources/assets/js/store/state.js
vendored
1
resources/assets/js/store/state.js
vendored
@ -29,7 +29,6 @@ const stateData = {
|
|||||||
|
|
||||||
// Dialog
|
// Dialog
|
||||||
cacheDialogs: $A.getStorageArray("cacheDialogs").map(item => Object.assign(item, {loading: false})),
|
cacheDialogs: $A.getStorageArray("cacheDialogs").map(item => Object.assign(item, {loading: false})),
|
||||||
cacheUnreads: {},
|
|
||||||
|
|
||||||
// Project
|
// Project
|
||||||
cacheProjects: $A.getStorageArray("cacheProjects"),
|
cacheProjects: $A.getStorageArray("cacheProjects"),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user