From e680d2af5ac005f2e6043bd0eb50fbce9ec337d8 Mon Sep 17 00:00:00 2001 From: beabigegg Date: Thu, 4 Sep 2025 11:31:53 +0800 Subject: [PATCH] 14th_check --- app/api/notification.py | 30 +++++++++++++-------------- frontend/src/layouts/MainLayout.vue | 15 ++++++++++++-- frontend/src/services/notification.js | 4 ++-- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/app/api/notification.py b/app/api/notification.py index 9ff8189..23a9799 100644 --- a/app/api/notification.py +++ b/app/api/notification.py @@ -9,7 +9,7 @@ Modified: 2024-01-28 """ from flask import Blueprint, jsonify, request, g -from flask_jwt_extended import jwt_required, get_jwt_identity +from app.utils.decorators import jwt_login_required from sqlalchemy import desc, and_, or_ from datetime import datetime, timedelta from app import db @@ -22,12 +22,12 @@ notification_bp = Blueprint('notification', __name__, url_prefix='/notifications @notification_bp.route('', methods=['GET']) -@jwt_required() +@jwt_login_required def get_notifications(): """獲取當前用戶的通知列表""" try: # 獲取當前用戶 - current_user_id = get_jwt_identity() + current_user_id = g.current_user_id # 獲取查詢參數 page = request.args.get('page', 1, type=int) @@ -94,11 +94,11 @@ def get_notifications(): @notification_bp.route('/', methods=['GET']) -@jwt_required() +@jwt_login_required def get_notification(notification_id): """獲取單個通知詳情""" try: - current_user_id = get_jwt_identity() + current_user_id = g.current_user_id # 查找通知 notification = Notification.query.filter_by( @@ -131,11 +131,11 @@ def get_notification(notification_id): @notification_bp.route('//read', methods=['POST']) -@jwt_required() +@jwt_login_required def mark_notification_read(notification_id): """標記通知為已讀""" try: - current_user_id = get_jwt_identity() + current_user_id = g.current_user_id # 查找通知 notification = Notification.query.filter_by( @@ -166,11 +166,11 @@ def mark_notification_read(notification_id): @notification_bp.route('/read-all', methods=['POST']) -@jwt_required() +@jwt_login_required def mark_all_read(): """標記所有通知為已讀""" try: - current_user_id = get_jwt_identity() + current_user_id = g.current_user_id # 取得所有未讀通知 unread_notifications = Notification.query.filter_by( @@ -201,11 +201,11 @@ def mark_all_read(): @notification_bp.route('/', methods=['DELETE']) -@jwt_required() +@jwt_login_required def delete_notification(notification_id): """刪除通知""" try: - current_user_id = get_jwt_identity() + current_user_id = g.current_user_id # 查找通知 notification = Notification.query.filter_by( @@ -237,11 +237,11 @@ def delete_notification(notification_id): @notification_bp.route('/clear', methods=['POST']) -@jwt_required() +@jwt_login_required def clear_read_notifications(): """清空所有已讀通知""" try: - current_user_id = get_jwt_identity() + current_user_id = g.current_user_id # 刪除所有已讀通知 deleted_count = Notification.query.filter_by( @@ -266,11 +266,11 @@ def clear_read_notifications(): @notification_bp.route('/test', methods=['POST']) -@jwt_required() +@jwt_login_required def create_test_notification(): """創建測試通知(開發用)""" try: - current_user_id = get_jwt_identity() + current_user_id = g.current_user_id # 創建測試通知 test_notification = create_notification( diff --git a/frontend/src/layouts/MainLayout.vue b/frontend/src/layouts/MainLayout.vue index 02aed18..75ff037 100644 --- a/frontend/src/layouts/MainLayout.vue +++ b/frontend/src/layouts/MainLayout.vue @@ -246,7 +246,13 @@ const handleMenuClick = () => { const showNotifications = async () => { notificationDrawerVisible.value = true // 載入最新通知 - await notificationStore.fetchNotifications() + console.log('🔔 正在載入通知...') + try { + await notificationStore.fetchNotifications() + console.log('🔔 通知載入完成:', notificationStore.notifications.length) + } catch (error) { + console.error('🔔 通知載入失敗:', error) + } } const handleUserMenuCommand = async (command) => { @@ -331,7 +337,12 @@ onMounted(() => { // initWebSocket() // 載入通知 - notificationStore.fetchNotifications() + console.log('🔔 初始化載入通知...') + notificationStore.fetchNotifications().then(() => { + console.log('🔔 初始化通知載入完成:', notificationStore.notifications.length) + }).catch(error => { + console.error('🔔 初始化通知載入失敗:', error) + }) // 監聽窗口大小變化 window.addEventListener('resize', handleResize) diff --git a/frontend/src/services/notification.js b/frontend/src/services/notification.js index fdf2f8c..9d94bb0 100644 --- a/frontend/src/services/notification.js +++ b/frontend/src/services/notification.js @@ -29,14 +29,14 @@ export const notificationAPI = { * @param {string} notificationId - 通知ID */ markAsRead(notificationId) { - return request.put(`/notifications/${notificationId}/read`) + return request.post(`/notifications/${notificationId}/read`) }, /** * 標記所有通知為已讀 */ markAllAsRead() { - return request.put('/notifications/mark-all-read') + return request.post('/notifications/read-all') }, /**