From 887cf8c01fb8da320d2178958deb77fa8587cdf8 Mon Sep 17 00:00:00 2001 From: vapao Date: Mon, 7 Mar 2022 09:21:09 +0800 Subject: [PATCH] =?UTF-8?q?U=20=E7=B3=BB=E7=BB=9F=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E5=B0=86=E6=98=BE=E7=A4=BA=E5=B7=B2=E8=AF=BB=E7=9A=84=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/apps/notify/views.py | 2 +- spug_web/src/layout/Notification.js | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/spug_api/apps/notify/views.py b/spug_api/apps/notify/views.py index 0435039..98da155 100644 --- a/spug_api/apps/notify/views.py +++ b/spug_api/apps/notify/views.py @@ -8,7 +8,7 @@ from libs import json_response, JsonParser, Argument class NotifyView(View): def get(self, request): - notifies = Notify.objects.filter(unread=True) + notifies = Notify.objects.all() return json_response(notifies) def patch(self, request): diff --git a/spug_web/src/layout/Notification.js b/spug_web/src/layout/Notification.js index cc55226..6f25d55 100644 --- a/spug_web/src/layout/Notification.js +++ b/spug_web/src/layout/Notification.js @@ -56,8 +56,8 @@ export default function () { setLoading(true); http.get('/api/notify/') .then(res => { + setReads(res.filter(x => !x.unread).map(x => x.id)) setNotifies(res); - setReads([]) }) .finally(() => setLoading(false)) } @@ -68,8 +68,7 @@ export default function () { ws = new WebSocket(`${protocol}//${window.location.host}/api/ws/notify/?x-token=${X_TOKEN}`); ws.onopen = () => ws.send('ok'); ws.onmessage = e => { - if (e.data === 'pong') { - } else { + if (e.data !== 'pong') { fetch(); const {title, content} = JSON.parse(e.data); const key = `open${Date.now()}`; @@ -80,6 +79,12 @@ export default function () { } } + function handleVisible(visible) { + if (visible) { + fetch() + } + } + function handleRead(e, item) { e.stopPropagation(); if (reads.indexOf(item.id) === -1) { @@ -95,9 +100,10 @@ export default function () { http.patch('/api/notify/', {ids}) } + const count = notifies.length - reads.length; return (
- } title={{item.title}} description={[ -
{item.content}
, +
{item.content}
,
{moment(item['created_at']).fromNow()}
]}/> @@ -124,7 +130,7 @@ export default function () { )}> - + 0 ? count : 0}>