- {{ item.todayCount }}
+ {{ item.todayCount }}
- 总{{ item.title }}
- {{ item.totalCount }}
+ 总{{ item.title }}
+ {{ item.totalCount }}
@@ -151,12 +151,13 @@
- 通知公告
+ 通知公告
+
- 查看更多
+
查看更多
+
+
@@ -192,12 +193,15 @@ defineOptions({
inheritAttrs: false,
});
+import { Client } from "@stomp/stompjs";
+
import { useUserStore } from "@/store/modules/user";
import { NoticeTypeEnum, getNoticeLabel } from "@/enums/NoticeTypeEnum";
-
+import { TOKEN_KEY } from "@/enums/CacheEnum";
import StatsAPI, { VisitStatsVO } from "@/api/log";
const userStore = useUserStore();
+const socketEndpoint = ref(import.meta.env.VITE_APP_WS_ENDPOINT);
const date: Date = new Date();
const greetings = computed(() => {
const hours = date.getHours();
@@ -237,6 +241,8 @@ const statisticData = ref([
},
]);
+const onlineUserCount = ref(0);
+
const visitStatsLoading = ref(true);
const visitStatsList = ref
(Array(3).fill({}));
interface VisitStats {
@@ -384,8 +390,36 @@ const getNoticeLevelTag = (type: number) => {
}
};
+let stompClient: Client;
+
+function connectWebSocket() {
+ stompClient = new Client({
+ brokerURL: socketEndpoint.value,
+ connectHeaders: {
+ Authorization: localStorage.getItem(TOKEN_KEY) || "",
+ },
+ debug: (str) => {
+ console.log(str);
+ },
+ onConnect: () => {
+ console.log("连接成功");
+ stompClient.subscribe("/topic/onlineUserCount", (message) => {
+ onlineUserCount.value = JSON.parse(message.body);
+ });
+ },
+ onStompError: (frame) => {
+ console.error("Broker reported error: " + frame.headers["message"]);
+ console.error("Additional details: " + frame.body);
+ },
+ onDisconnect: () => {},
+ });
+
+ stompClient.activate();
+}
+
onMounted(() => {
loadVisitStatsData();
+ connectWebSocket();
});