mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-08-15 15:51:00 +00:00
fix: 后端代码格式化
This commit is contained in:
@@ -3,73 +3,73 @@ import { useWebSocket } from './useWebSocket'
|
||||
import { getToken } from '~/utils/auth'
|
||||
|
||||
const count = ref(0)
|
||||
let isInitialized = false;
|
||||
let isInitialized = false
|
||||
|
||||
export function useChannelsUnreadCount() {
|
||||
const config = useRuntimeConfig();
|
||||
const API_BASE_URL = config.public.apiBaseUrl;
|
||||
const { subscribe, isConnected, connect } = useWebSocket();
|
||||
const config = useRuntimeConfig()
|
||||
const API_BASE_URL = config.public.apiBaseUrl
|
||||
const { subscribe, isConnected, connect } = useWebSocket()
|
||||
|
||||
const fetchChannelUnread = async () => {
|
||||
const token = getToken();
|
||||
const token = getToken()
|
||||
if (!token) {
|
||||
count.value = 0;
|
||||
return;
|
||||
count.value = 0
|
||||
return
|
||||
}
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/channels/unread-count`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
})
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
count.value = data;
|
||||
const data = await response.json()
|
||||
count.value = data
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to fetch channel unread count:', e);
|
||||
console.error('Failed to fetch channel unread count:', e)
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
const setupWebSocketListener = () => {
|
||||
const destination = '/user/queue/channel-unread';
|
||||
const destination = '/user/queue/channel-unread'
|
||||
|
||||
subscribe(destination, (message) => {
|
||||
const unread = parseInt(message.body, 10);
|
||||
const unread = parseInt(message.body, 10)
|
||||
if (!isNaN(unread)) {
|
||||
count.value = unread;
|
||||
count.value = unread
|
||||
}
|
||||
}).then(subscription => {
|
||||
}).then((subscription) => {
|
||||
if (subscription) {
|
||||
console.log('频道未读消息订阅成功');
|
||||
console.log('频道未读消息订阅成功')
|
||||
}
|
||||
});
|
||||
};
|
||||
})
|
||||
}
|
||||
|
||||
const initialize = () => {
|
||||
const token = getToken();
|
||||
const token = getToken()
|
||||
if (!token) {
|
||||
count.value = 0;
|
||||
return;
|
||||
count.value = 0
|
||||
return
|
||||
}
|
||||
|
||||
if (!isConnected.value) {
|
||||
connect(token);
|
||||
connect(token)
|
||||
}
|
||||
|
||||
fetchChannelUnread();
|
||||
setupWebSocketListener();
|
||||
};
|
||||
fetchChannelUnread()
|
||||
setupWebSocketListener()
|
||||
}
|
||||
|
||||
const setFromList = (channels) => {
|
||||
count.value = Array.isArray(channels) ? channels.filter((c) => c.unreadCount > 0).length : 0;
|
||||
};
|
||||
count.value = Array.isArray(channels) ? channels.filter((c) => c.unreadCount > 0).length : 0
|
||||
}
|
||||
|
||||
const hasUnread = computed(() => count.value > 0);
|
||||
const hasUnread = computed(() => count.value > 0)
|
||||
|
||||
if (!isInitialized) {
|
||||
const token = getToken();
|
||||
const token = getToken()
|
||||
if (token) {
|
||||
isInitialized = true;
|
||||
initialize();
|
||||
isInitialized = true
|
||||
initialize()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,5 +79,5 @@ export function useChannelsUnreadCount() {
|
||||
fetchChannelUnread,
|
||||
initialize,
|
||||
setFromList,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ export function useReactionTypes() {
|
||||
reactionTypes.value = [...(window.reactionTypes || [])]
|
||||
return reactionTypes.value
|
||||
}
|
||||
|
||||
|
||||
isLoading = true
|
||||
try {
|
||||
const token = getToken()
|
||||
@@ -47,6 +47,6 @@ export function useReactionTypes() {
|
||||
reactionTypes: readonly(reactionTypes),
|
||||
fetchReactionTypes,
|
||||
initialize,
|
||||
isInitialized: readonly(isInitialized)
|
||||
isInitialized: readonly(isInitialized),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,76 +1,76 @@
|
||||
import { ref, watch, onMounted } from 'vue';
|
||||
import { useWebSocket } from './useWebSocket';
|
||||
import { getToken } from '~/utils/auth';
|
||||
import { ref, watch, onMounted } from 'vue'
|
||||
import { useWebSocket } from './useWebSocket'
|
||||
import { getToken } from '~/utils/auth'
|
||||
|
||||
const count = ref(0);
|
||||
let isInitialized = false;
|
||||
const count = ref(0)
|
||||
let isInitialized = false
|
||||
|
||||
export function useUnreadCount() {
|
||||
const config = useRuntimeConfig();
|
||||
const API_BASE_URL = config.public.apiBaseUrl;
|
||||
const { subscribe, isConnected, connect } = useWebSocket();
|
||||
const config = useRuntimeConfig()
|
||||
const API_BASE_URL = config.public.apiBaseUrl
|
||||
const { subscribe, isConnected, connect } = useWebSocket()
|
||||
|
||||
const fetchUnreadCount = async () => {
|
||||
const token = getToken();
|
||||
const token = getToken()
|
||||
if (!token) {
|
||||
count.value = 0;
|
||||
return;
|
||||
count.value = 0
|
||||
return
|
||||
}
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/messages/unread-count`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
})
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
count.value = data;
|
||||
const data = await response.json()
|
||||
count.value = data
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch unread count:', error);
|
||||
console.error('Failed to fetch unread count:', error)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const setupWebSocketListener = () => {
|
||||
console.log('设置未读消息订阅...');
|
||||
const destination = '/user/queue/unread-count';
|
||||
|
||||
console.log('设置未读消息订阅...')
|
||||
const destination = '/user/queue/unread-count'
|
||||
|
||||
subscribe(destination, (message) => {
|
||||
const unreadCount = parseInt(message.body, 10);
|
||||
const unreadCount = parseInt(message.body, 10)
|
||||
if (!isNaN(unreadCount)) {
|
||||
count.value = unreadCount;
|
||||
count.value = unreadCount
|
||||
}
|
||||
}).then(subscription => {
|
||||
}).then((subscription) => {
|
||||
if (subscription) {
|
||||
console.log('未读消息订阅成功');
|
||||
console.log('未读消息订阅成功')
|
||||
}
|
||||
});
|
||||
};
|
||||
})
|
||||
}
|
||||
|
||||
const initialize = () => {
|
||||
const token = getToken();
|
||||
const token = getToken()
|
||||
if (!token) {
|
||||
count.value = 0;
|
||||
return;
|
||||
count.value = 0
|
||||
return
|
||||
}
|
||||
|
||||
if (!isConnected.value) {
|
||||
connect(token);
|
||||
connect(token)
|
||||
}
|
||||
|
||||
fetchUnreadCount();
|
||||
setupWebSocketListener();
|
||||
};
|
||||
|
||||
fetchUnreadCount()
|
||||
setupWebSocketListener()
|
||||
}
|
||||
|
||||
if (!isInitialized) {
|
||||
const token = getToken();
|
||||
const token = getToken()
|
||||
if (token) {
|
||||
isInitialized = true;
|
||||
initialize();
|
||||
isInitialized = true
|
||||
initialize()
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
count,
|
||||
fetchUnreadCount,
|
||||
initialize,
|
||||
};
|
||||
}
|
||||
initialize,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ const resubscribeCallbacks = new Map()
|
||||
// Helper for unified subscription logging
|
||||
const logSubscriptionActivity = (action, destination, subscriptionId = 'N/A') => {
|
||||
console.log(
|
||||
`[SUB_MAN] ${action} | Dest: ${destination} | SubID: ${subscriptionId} | Active: ${activeSubscriptions.value.size}`
|
||||
`[SUB_MAN] ${action} | Dest: ${destination} | SubID: ${subscriptionId} | Active: ${activeSubscriptions.value.size}`,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ const connect = (token) => {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
const config = useRuntimeConfig()
|
||||
const WEBSOCKET_URL = config.public.websocketUrl
|
||||
const socketUrl = `${WEBSOCKET_URL}/api/sockjs`
|
||||
@@ -31,9 +30,7 @@ const connect = (token) => {
|
||||
connectHeaders: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
debug: function (str) {
|
||||
|
||||
},
|
||||
debug: function (str) {},
|
||||
reconnectDelay: 10000,
|
||||
heartbeatIncoming: 4000,
|
||||
heartbeatOutgoing: 4000,
|
||||
@@ -42,7 +39,7 @@ const connect = (token) => {
|
||||
stompClient.onConnect = (frame) => {
|
||||
isConnected.value = true
|
||||
resubscribeCallbacks.forEach((callback, destination) => {
|
||||
doSubscribe(destination, callback)
|
||||
doSubscribe(destination, callback)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -50,16 +47,14 @@ const connect = (token) => {
|
||||
console.error('Full frame:', frame)
|
||||
}
|
||||
|
||||
stompClient.onWebSocketError = (event) => {
|
||||
|
||||
}
|
||||
stompClient.onWebSocketError = (event) => {}
|
||||
|
||||
stompClient.onWebSocketClose = (event) => {
|
||||
isConnected.value = false;
|
||||
activeSubscriptions.value.clear();
|
||||
logSubscriptionActivity('Cleared all subscriptions due to WebSocket close', 'N/A');
|
||||
};
|
||||
|
||||
isConnected.value = false
|
||||
activeSubscriptions.value.clear()
|
||||
logSubscriptionActivity('Cleared all subscriptions due to WebSocket close', 'N/A')
|
||||
}
|
||||
|
||||
stompClient.onDisconnect = (frame) => {
|
||||
isConnected.value = false
|
||||
}
|
||||
@@ -92,7 +87,7 @@ const unsubscribe = (destination) => {
|
||||
const unsubscribeAll = () => {
|
||||
logSubscriptionActivity('Unsubscribing from ALL', `Total: ${activeSubscriptions.value.size}`)
|
||||
const destinations = [...activeSubscriptions.value.keys()]
|
||||
destinations.forEach(dest => {
|
||||
destinations.forEach((dest) => {
|
||||
unsubscribe(dest)
|
||||
})
|
||||
}
|
||||
@@ -148,16 +143,20 @@ const subscribe = (destination, callback) => {
|
||||
const sub = doSubscribe(destination, callback)
|
||||
resolve(sub)
|
||||
} else {
|
||||
const unwatch = watch(isConnected, (newVal) => {
|
||||
if (newVal) {
|
||||
setTimeout(() => {
|
||||
const sub = doSubscribe(destination, callback)
|
||||
unwatch()
|
||||
resolve(sub)
|
||||
}, 100)
|
||||
}
|
||||
}, { immediate: false })
|
||||
|
||||
const unwatch = watch(
|
||||
isConnected,
|
||||
(newVal) => {
|
||||
if (newVal) {
|
||||
setTimeout(() => {
|
||||
const sub = doSubscribe(destination, callback)
|
||||
unwatch()
|
||||
resolve(sub)
|
||||
}, 100)
|
||||
}
|
||||
},
|
||||
{ immediate: false },
|
||||
)
|
||||
|
||||
setTimeout(() => {
|
||||
unwatch()
|
||||
if (!isConnected.value) {
|
||||
|
||||
Reference in New Issue
Block a user