Skip to content

Commit 69f2b4f

Browse files
author
CodeJudge
committed
feat: 上次登录追踪 + 服务运行时间
1 parent 3858b38 commit 69f2b4f

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

backend/src/app.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const submissionRoutes = require('./routes/submissions');
99

1010
const app = express();
1111
const PORT = process.env.PORT || 3001;
12+
const serverStart = Date.now();
1213

1314
app.use(cors());
1415
app.use(express.json({ limit: '10mb' }));
@@ -42,7 +43,15 @@ app.use((req, res, next) => {
4243

4344
// Health check
4445
app.get('/api/health', (req, res) => {
45-
res.json({ status: 'ok', timestamp: new Date().toISOString() });
46+
const uptime = Math.floor((Date.now() - serverStart) / 1000);
47+
const hours = Math.floor(uptime / 3600);
48+
const minutes = Math.floor((uptime % 3600) / 60);
49+
res.json({
50+
status: 'ok',
51+
timestamp: new Date().toISOString(),
52+
uptime: `${hours}h ${minutes}m`,
53+
version: '1.0.0',
54+
});
4655
});
4756

4857
app.use('/api/auth', authRoutes);

backend/src/controllers/authController.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ function login(req, res) {
3333
if (!user || !bcrypt.compareSync(password, user.password)) {
3434
return res.status(401).json({ error: '邮箱或密码错误' });
3535
}
36+
// Update last login
37+
run('UPDATE users SET last_login = datetime() WHERE id = ?', [user.id]);
3638
const token = jwt.sign({ id: user.id, username: user.username, email: user.email, role: user.role }, JWT_SECRET, { expiresIn: '7d' });
3739
res.json({
3840
token,
39-
user: { id: user.id, username: user.username, email: user.email, role: user.role },
41+
user: { id: user.id, username: user.username, email: user.email, role: user.role, last_login: new Date().toISOString() },
4042
});
4143
}
4244

backend/src/utils/initDb.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ async function initDb() {
1111
email TEXT UNIQUE NOT NULL,
1212
password TEXT NOT NULL,
1313
role TEXT DEFAULT 'user',
14+
last_login DATETIME,
1415
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
1516
)
1617
`);
18+
try { exec('ALTER TABLE users ADD COLUMN last_login DATETIME'); } catch {}
1719

1820
exec(`
1921
CREATE TABLE IF NOT EXISTS problems (

0 commit comments

Comments
 (0)