zhibo/live-streaming/server/routes/viewHistory.js
xiao12feng8 a619bb1750 修复:首页标签分类+我的挚友处理+历史记录
优化:缘池界面+消息搜索界面
2026-01-05 16:47:07 +08:00

145 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const router = express.Router();
const viewHistoryStore = require('../store/viewHistoryStore');
const roomStore = require('../store/roomStore');
/**
* 简单的认证中间件
* 从 Authori-zation 头获取用户ID实际项目应验证 JWT
*/
function authMiddleware(req, res, next) {
const token = req.headers['authori-zation'] || req.headers['authorization'];
if (!token) {
return res.status(401).json({
code: 401,
message: '请先登录'
});
}
// 简单模拟:从 token 中提取用户ID
// 实际项目应该验证 JWT 并解析用户信息
// 这里假设 token 格式为 "Bearer userId" 或直接是 userId
let userId;
if (token.startsWith('Bearer ')) {
userId = parseInt(token.substring(7), 10);
} else {
userId = parseInt(token, 10);
}
if (isNaN(userId)) {
// 如果无法解析默认使用用户ID 1
userId = 1;
}
req.userId = userId;
next();
}
/**
* 记录观看历史
* POST /api/front/activity/record-view
*/
router.post('/record-view', authMiddleware, (req, res) => {
try {
const { targetType, targetId, targetTitle, coverImage, streamerName, authorName, viewDuration } = req.body;
if (!targetType || !targetId) {
return res.status(400).json({ code: 400, message: '缺少必要参数' });
}
viewHistoryStore.record({
userId: req.userId,
targetType,
targetId,
targetTitle,
coverImage,
streamerName,
authorName,
viewDuration
});
res.json({ code: 200, message: '记录成功', data: { success: true } });
} catch (error) {
console.error('[ViewHistory] 记录观看历史失败:', error);
res.status(500).json({ code: 500, message: '记录失败' });
}
});
/**
* 获取观看历史
* GET /api/front/activity/view-history
*/
router.get('/view-history', authMiddleware, (req, res) => {
try {
const { type, page = 1, pageSize = 20 } = req.query;
const result = viewHistoryStore.getUserHistory(
req.userId,
type,
parseInt(page),
parseInt(pageSize)
);
// 补充直播间的实时状态
result.list = result.list.map(item => {
if (item.targetType === 'room') {
const room = roomStore.getById(item.targetId);
if (room) {
return {
...item,
isLive: room.isLive,
viewerCount: room.viewerCount
};
}
}
return item;
});
res.json({ code: 200, message: '获取成功', data: result });
} catch (error) {
console.error('[ViewHistory] 获取观看历史失败:', error);
res.status(500).json({ code: 500, message: '获取失败' });
}
});
/**
* 清除观看历史
* DELETE /api/front/activity/view-history
*/
router.delete('/view-history', authMiddleware, (req, res) => {
try {
const { type } = req.query;
viewHistoryStore.clearUserHistory(req.userId, type);
res.json({ code: 200, message: '清除成功', data: 'success' });
} catch (error) {
console.error('[ViewHistory] 清除观看历史失败:', error);
res.status(500).json({ code: 500, message: '清除失败' });
}
});
/**
* 删除单条观看历史
* DELETE /api/front/activity/view-history/:id
*/
router.delete('/view-history/:id', authMiddleware, (req, res) => {
try {
const { id } = req.params;
const success = viewHistoryStore.deleteRecord(req.userId, parseInt(id));
if (success) {
res.json({ code: 200, message: '删除成功', data: 'success' });
} else {
res.status(404).json({ code: 404, message: '记录不存在' });
}
} catch (error) {
console.error('[ViewHistory] 删除观看历史失败:', error);
res.status(500).json({ code: 500, message: '删除失败' });
}
});
module.exports = router;