146 lines
4.0 KiB
JavaScript
146 lines
4.0 KiB
JavaScript
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
|
||
|
|
// 持久化文件路径
|
||
|
|
const STORAGE_FILE = path.join(__dirname, '../../data/viewHistory.json');
|
||
|
|
|
||
|
|
// 确保数据目录存在
|
||
|
|
const ensureDataDir = () => {
|
||
|
|
const dir = path.dirname(STORAGE_FILE);
|
||
|
|
if (!fs.existsSync(dir)) {
|
||
|
|
fs.mkdirSync(dir, { recursive: true });
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 从文件加载观看历史数据
|
||
|
|
const loadViewHistory = () => {
|
||
|
|
try {
|
||
|
|
ensureDataDir();
|
||
|
|
if (fs.existsSync(STORAGE_FILE)) {
|
||
|
|
const data = fs.readFileSync(STORAGE_FILE, 'utf8');
|
||
|
|
return JSON.parse(data);
|
||
|
|
}
|
||
|
|
} catch (e) {
|
||
|
|
console.warn('[ViewHistoryStore] Failed to load view history from file:', e.message);
|
||
|
|
}
|
||
|
|
return [];
|
||
|
|
};
|
||
|
|
|
||
|
|
// 保存观看历史数据到文件
|
||
|
|
const saveViewHistory = (history) => {
|
||
|
|
try {
|
||
|
|
ensureDataDir();
|
||
|
|
fs.writeFileSync(STORAGE_FILE, JSON.stringify(history, null, 2), 'utf8');
|
||
|
|
} catch (e) {
|
||
|
|
console.error('[ViewHistoryStore] Failed to save view history to file:', e.message);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// 内存存储(从文件加载)
|
||
|
|
let viewHistory = loadViewHistory();
|
||
|
|
console.log(`[ViewHistoryStore] Loaded ${viewHistory.length} view history records from storage`);
|
||
|
|
|
||
|
|
const viewHistoryStore = {
|
||
|
|
// 记录观看历史
|
||
|
|
record(data) {
|
||
|
|
const { userId, targetType, targetId, targetTitle, coverImage, streamerName, authorName, viewDuration } = data;
|
||
|
|
|
||
|
|
// 查找是否已存在相同的记录
|
||
|
|
const existingIndex = viewHistory.findIndex(
|
||
|
|
h => h.userId === userId && h.targetType === targetType && h.targetId === targetId
|
||
|
|
);
|
||
|
|
|
||
|
|
const now = new Date().toISOString();
|
||
|
|
|
||
|
|
if (existingIndex >= 0) {
|
||
|
|
// 更新现有记录
|
||
|
|
viewHistory[existingIndex] = {
|
||
|
|
...viewHistory[existingIndex],
|
||
|
|
targetTitle: targetTitle || viewHistory[existingIndex].targetTitle,
|
||
|
|
coverImage: coverImage || viewHistory[existingIndex].coverImage,
|
||
|
|
streamerName: streamerName || viewHistory[existingIndex].streamerName,
|
||
|
|
authorName: authorName || viewHistory[existingIndex].authorName,
|
||
|
|
viewDuration: viewDuration || viewHistory[existingIndex].viewDuration,
|
||
|
|
updateTime: now,
|
||
|
|
viewCount: (viewHistory[existingIndex].viewCount || 0) + 1
|
||
|
|
};
|
||
|
|
} else {
|
||
|
|
// 创建新记录
|
||
|
|
viewHistory.push({
|
||
|
|
id: Date.now(),
|
||
|
|
userId,
|
||
|
|
targetType,
|
||
|
|
targetId,
|
||
|
|
targetTitle,
|
||
|
|
coverImage,
|
||
|
|
streamerName,
|
||
|
|
authorName,
|
||
|
|
viewDuration,
|
||
|
|
createTime: now,
|
||
|
|
updateTime: now,
|
||
|
|
viewCount: 1
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
saveViewHistory(viewHistory);
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
|
||
|
|
// 获取用户的观看历史
|
||
|
|
getUserHistory(userId, type, page = 1, pageSize = 20) {
|
||
|
|
let filtered = viewHistory.filter(h => h.userId === userId);
|
||
|
|
|
||
|
|
// 按类型过滤
|
||
|
|
if (type) {
|
||
|
|
filtered = filtered.filter(h => h.targetType === type);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 按更新时间倒序排序
|
||
|
|
filtered.sort((a, b) => new Date(b.updateTime) - new Date(a.updateTime));
|
||
|
|
|
||
|
|
// 分页
|
||
|
|
const total = filtered.length;
|
||
|
|
const start = (page - 1) * pageSize;
|
||
|
|
const end = start + pageSize;
|
||
|
|
const list = filtered.slice(start, end);
|
||
|
|
|
||
|
|
return {
|
||
|
|
list,
|
||
|
|
total,
|
||
|
|
page,
|
||
|
|
pageSize,
|
||
|
|
totalPages: Math.ceil(total / pageSize)
|
||
|
|
};
|
||
|
|
},
|
||
|
|
|
||
|
|
// 清除用户的观看历史
|
||
|
|
clearUserHistory(userId, type) {
|
||
|
|
if (type) {
|
||
|
|
viewHistory = viewHistory.filter(h => !(h.userId === userId && h.targetType === type));
|
||
|
|
} else {
|
||
|
|
viewHistory = viewHistory.filter(h => h.userId !== userId);
|
||
|
|
}
|
||
|
|
saveViewHistory(viewHistory);
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
|
||
|
|
// 删除单条观看历史
|
||
|
|
deleteRecord(userId, id) {
|
||
|
|
const index = viewHistory.findIndex(h => h.id === id && h.userId === userId);
|
||
|
|
if (index >= 0) {
|
||
|
|
viewHistory.splice(index, 1);
|
||
|
|
saveViewHistory(viewHistory);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
},
|
||
|
|
|
||
|
|
// 清空所有观看历史 (测试用)
|
||
|
|
clear() {
|
||
|
|
viewHistory = [];
|
||
|
|
saveViewHistory(viewHistory);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
module.exports = viewHistoryStore;
|