|
|
|
|
@ -1,129 +1,30 @@
|
|
|
|
|
// backend/routes/mynotice.js
|
|
|
|
|
const express = require('express');
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
|
|
// 临时模拟数据
|
|
|
|
|
let mockNotices = [
|
|
|
|
|
{
|
|
|
|
|
id: 1,
|
|
|
|
|
type: 'feedback',
|
|
|
|
|
title: '关于图表导出功能的建议',
|
|
|
|
|
content: '建议增加导出为PDF格式的功能,这样更方便分享和打印。',
|
|
|
|
|
feedbackType: 'suggestion',
|
|
|
|
|
time: new Date(Date.now() - 3600000 * 2),
|
|
|
|
|
isRead: false,
|
|
|
|
|
reply: '感谢您的建议!我们已经在开发计划中,预计下个版本会增加PDF导出功能。',
|
|
|
|
|
replyTime: new Date(Date.now() - 3600000 * 1)
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 2,
|
|
|
|
|
type: 'feedback',
|
|
|
|
|
title: '界面颜色太刺眼',
|
|
|
|
|
content: '数据可视化页面的背景色太亮,长时间使用眼睛容易疲劳。',
|
|
|
|
|
feedbackType: 'ui',
|
|
|
|
|
time: new Date(Date.now() - 86400000 * 2),
|
|
|
|
|
isRead: true,
|
|
|
|
|
reply: '我们已收到您的反馈,会在下个版本中增加深色模式选项。',
|
|
|
|
|
replyTime: new Date(Date.now() - 86400000 * 1)
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 3,
|
|
|
|
|
type: 'system',
|
|
|
|
|
title: '系统维护通知',
|
|
|
|
|
content: '为了提升系统性能,我们将于本周六凌晨2:00-4:00进行系统维护,期间服务将不可用。',
|
|
|
|
|
time: new Date(Date.now() - 86400000 * 3),
|
|
|
|
|
isRead: false
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 4,
|
|
|
|
|
type: 'system',
|
|
|
|
|
title: '新功能上线',
|
|
|
|
|
content: '图表智能推荐功能已上线,系统会根据您的数据自动推荐最合适的图表类型。',
|
|
|
|
|
time: new Date(Date.now() - 86400000 * 5),
|
|
|
|
|
isRead: true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 5,
|
|
|
|
|
type: 'feedback',
|
|
|
|
|
title: '数据导入失败问题',
|
|
|
|
|
content: '导入超过10MB的CSV文件时系统会报错,提示内存不足。',
|
|
|
|
|
feedbackType: 'bug',
|
|
|
|
|
time: new Date(Date.now() - 86400000 * 7),
|
|
|
|
|
isRead: false,
|
|
|
|
|
reply: null
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 模拟用户数据
|
|
|
|
|
const mockUsers = [
|
|
|
|
|
{ id: 1, username: '张三', email: 'zhangsan@example.com' },
|
|
|
|
|
{ id: 2, username: '李四', email: 'lisi@example.com' },
|
|
|
|
|
{ id: 3, username: '王五', email: 'wangwu@example.com' }
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 模拟notice表数据
|
|
|
|
|
let mockSystemNotices = [
|
|
|
|
|
{
|
|
|
|
|
id: 1,
|
|
|
|
|
title: '系统维护通知',
|
|
|
|
|
content: '为了提升系统性能,我们将于本周六凌晨2:00-4:00进行系统维护,期间服务将不可用。',
|
|
|
|
|
admin_id: 1,
|
|
|
|
|
is_read: false,
|
|
|
|
|
created_time: new Date(Date.now() - 86400000 * 3)
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 2,
|
|
|
|
|
title: '新功能上线',
|
|
|
|
|
content: '图表智能推荐功能已上线,系统会根据您的数据自动推荐最合适的图表类型。',
|
|
|
|
|
admin_id: 1,
|
|
|
|
|
is_read: true,
|
|
|
|
|
created_time: new Date(Date.now() - 86400000 * 5)
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 模拟feedback表数据
|
|
|
|
|
let mockFeedbackList = [
|
|
|
|
|
{
|
|
|
|
|
feedback_id: 1,
|
|
|
|
|
user_id: 1,
|
|
|
|
|
type: 'suggestion',
|
|
|
|
|
content: '建议增加导出为PDF格式的功能,这样更方便分享和打印。',
|
|
|
|
|
feedback_time: new Date(Date.now() - 3600000 * 2),
|
|
|
|
|
answer: '感谢您的建议!我们已经在开发计划中,预计下个版本会增加PDF导出功能。',
|
|
|
|
|
answer_time: new Date(Date.now() - 3600000 * 1)
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
feedback_id: 2,
|
|
|
|
|
user_id: 1,
|
|
|
|
|
type: 'ui',
|
|
|
|
|
content: '数据可视化页面的背景色太亮,长时间使用眼睛容易疲劳。',
|
|
|
|
|
feedback_time: new Date(Date.now() - 86400000 * 2),
|
|
|
|
|
answer: '我们已收到您的反馈,会在下个版本中增加深色模式选项。',
|
|
|
|
|
answer_time: new Date(Date.now() - 86400000 * 1)
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
feedback_id: 3,
|
|
|
|
|
user_id: 1,
|
|
|
|
|
type: 'bug',
|
|
|
|
|
content: '导入超过10MB的CSV文件时系统会报错,提示内存不足。',
|
|
|
|
|
feedback_time: new Date(Date.now() - 86400000 * 7),
|
|
|
|
|
answer: null,
|
|
|
|
|
answer_time: null
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
const dbModule = require('../db');
|
|
|
|
|
const db = dbModule.promisePool;
|
|
|
|
|
|
|
|
|
|
// 获取用户通知列表
|
|
|
|
|
router.get('/api/mynotice', (req, res) => {
|
|
|
|
|
router.get('/', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
// 模拟从数据库获取数据
|
|
|
|
|
// 这里应该根据当前登录用户ID获取数据
|
|
|
|
|
const userId = 1; // 假设当前用户ID为1
|
|
|
|
|
// 从请求中获取用户ID(假设通过认证中间件设置)
|
|
|
|
|
const userId = req.user?.id || 1; // 默认为1用于测试
|
|
|
|
|
|
|
|
|
|
// 1. 获取用户的反馈信息
|
|
|
|
|
const userFeedback = mockFeedbackList.filter(feedback => feedback.user_id === userId);
|
|
|
|
|
const [userFeedback] = await db.execute(
|
|
|
|
|
`SELECT f.feedback_id, f.type, f.content, f.feedback_time, f.answer, f.answer_time
|
|
|
|
|
FROM feedback f
|
|
|
|
|
WHERE f.user_id = ?
|
|
|
|
|
ORDER BY f.feedback_time DESC`,
|
|
|
|
|
[userId]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 2. 获取系统通知
|
|
|
|
|
const systemNotices = mockSystemNotices;
|
|
|
|
|
const [systemNotices] = await db.execute(
|
|
|
|
|
`SELECT n.notice_id, n.title, n.content, n.admin_id, n.is_read, n.created_time
|
|
|
|
|
FROM notice n
|
|
|
|
|
ORDER BY n.created_time DESC`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 3. 合并数据并格式化
|
|
|
|
|
const formattedNotices = [];
|
|
|
|
|
@ -137,7 +38,7 @@ router.get('/api/mynotice', (req, res) => {
|
|
|
|
|
content: feedback.content,
|
|
|
|
|
feedbackType: feedback.type,
|
|
|
|
|
time: feedback.feedback_time,
|
|
|
|
|
isRead: false, // 这里应该从数据库读取已读状态
|
|
|
|
|
isRead: feedback.answer !== null, // 有回复视为已读
|
|
|
|
|
reply: feedback.answer,
|
|
|
|
|
replyTime: feedback.answer_time
|
|
|
|
|
};
|
|
|
|
|
@ -147,7 +48,7 @@ router.get('/api/mynotice', (req, res) => {
|
|
|
|
|
// 添加系统通知
|
|
|
|
|
systemNotices.forEach(notice => {
|
|
|
|
|
const formattedNotice = {
|
|
|
|
|
id: `system_${notice.id}`,
|
|
|
|
|
id: `system_${notice.notice_id}`,
|
|
|
|
|
type: 'system',
|
|
|
|
|
title: notice.title,
|
|
|
|
|
content: notice.content,
|
|
|
|
|
@ -183,15 +84,23 @@ router.get('/api/mynotice', (req, res) => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 标记单条通知为已读
|
|
|
|
|
router.post('/api/mynotice/read/:id', (req, res) => {
|
|
|
|
|
router.post('/read/:id', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
|
|
|
|
// 在实际应用中,这里应该更新数据库中的已读状态
|
|
|
|
|
// 更新mock数据
|
|
|
|
|
const notice = mockNotices.find(n => n.id == parseInt(id) || n.id == id);
|
|
|
|
|
if (notice) {
|
|
|
|
|
notice.isRead = true;
|
|
|
|
|
// 判断通知类型并更新对应的数据库记录
|
|
|
|
|
if (id.startsWith('feedback_')) {
|
|
|
|
|
// 反馈通知 - 不需要标记已读,因为反馈的已读状态由是否有回复决定
|
|
|
|
|
console.log(`用户查看了反馈 ${id}`);
|
|
|
|
|
} else if (id.startsWith('system_')) {
|
|
|
|
|
// 系统通知 - 更新notice表的is_read字段
|
|
|
|
|
const noticeId = id.replace('system_', '');
|
|
|
|
|
|
|
|
|
|
// 更新系统通知为已读
|
|
|
|
|
await db.execute(
|
|
|
|
|
'UPDATE notice SET is_read = TRUE WHERE notice_id = ?',
|
|
|
|
|
[noticeId]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
@ -210,18 +119,14 @@ router.post('/api/mynotice/read/:id', (req, res) => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 标记所有通知为已读
|
|
|
|
|
router.post('/api/mynotice/read-all', (req, res) => {
|
|
|
|
|
router.post('/read-all', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
// 在实际应用中,这里应该批量更新数据库中的已读状态
|
|
|
|
|
// 更新mock数据
|
|
|
|
|
mockNotices.forEach(notice => {
|
|
|
|
|
notice.isRead = true;
|
|
|
|
|
});
|
|
|
|
|
// 1. 将所有系统通知标记为已读
|
|
|
|
|
await db.execute(
|
|
|
|
|
'UPDATE notice SET is_read = TRUE WHERE is_read = FALSE'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 模拟更新系统通知已读状态
|
|
|
|
|
mockSystemNotices.forEach(notice => {
|
|
|
|
|
notice.is_read = true;
|
|
|
|
|
});
|
|
|
|
|
// 注意:反馈通知的已读状态由是否有回复决定,无法批量标记
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
code: 200,
|
|
|
|
|
@ -239,27 +144,30 @@ router.post('/api/mynotice/read-all', (req, res) => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 获取未读通知数量
|
|
|
|
|
router.get('/api/mynotice/unread-count', (req, res) => {
|
|
|
|
|
router.get('/unread-count', async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const userId = 1; // 假设当前用户ID为1
|
|
|
|
|
const userId = req.user?.id || 1; // 默认为1用于测试
|
|
|
|
|
|
|
|
|
|
// 获取用户的未读反馈
|
|
|
|
|
const unreadFeedback = mockFeedbackList.filter(feedback =>
|
|
|
|
|
feedback.user_id === userId && !feedback.is_read
|
|
|
|
|
).length;
|
|
|
|
|
// 1. 获取未读反馈数量(没有回复的反馈)
|
|
|
|
|
const [unreadFeedback] = await db.execute(
|
|
|
|
|
'SELECT COUNT(*) as count FROM feedback WHERE user_id = ? AND answer IS NULL',
|
|
|
|
|
[userId]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 获取未读系统通知
|
|
|
|
|
const unreadSystemNotices = mockSystemNotices.filter(notice => !notice.is_read).length;
|
|
|
|
|
// 2. 获取未读系统通知数量
|
|
|
|
|
const [unreadSystemNotices] = await db.execute(
|
|
|
|
|
'SELECT COUNT(*) as count FROM notice WHERE is_read = FALSE'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const totalUnread = unreadFeedback + unreadSystemNotices;
|
|
|
|
|
const totalUnread = parseInt(unreadFeedback[0].count) + parseInt(unreadSystemNotices[0].count);
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
code: 200,
|
|
|
|
|
message: '获取未读数量成功',
|
|
|
|
|
data: {
|
|
|
|
|
unreadCount: totalUnread,
|
|
|
|
|
feedbackUnread: unreadFeedback,
|
|
|
|
|
systemUnread: unreadSystemNotices
|
|
|
|
|
feedbackUnread: parseInt(unreadFeedback[0].count),
|
|
|
|
|
systemUnread: parseInt(unreadSystemNotices[0].count)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|