Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/components/UserOnlineUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
'use client';

import { useEffect } from 'react';
import { getAuthInfoFromBrowserCookie } from '@/lib/auth';

/**
* 用户在线心跳组件
* 页面加载时记录一次用户的在线时间
* 仅执行一次(组件挂载时)
* 仅在用户登录状态下执行
*/
export default function UserOnlineUpdate() {
useEffect(() => {
const updateOnline = async () => {
try {
// 检查用户是否已登录
const authInfo = getAuthInfoFromBrowserCookie();
if (!authInfo?.username) {
// 用户未登录,跳过在线时间更新
return;
}
const response = await fetch('/api/user/online', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
Expand All @@ -37,3 +45,4 @@ export default function UserOnlineUpdate() {
return null;
}