fix: 修复消息推送在没有登录还会建立连接

This commit is contained in:
xlsea 2024-09-11 14:36:46 +08:00
parent 07e30bd591
commit 2b14b84c67
5 changed files with 20 additions and 20 deletions

View File

@ -1,9 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { createTextVNode, defineComponent, onMounted } from 'vue'; import { createTextVNode, defineComponent } from 'vue';
import { useDialog, useLoadingBar, useMessage, useNotification } from 'naive-ui'; import { useDialog, useLoadingBar, useMessage, useNotification } from 'naive-ui';
import useContentLoading from '@/hooks/common/loading'; import useContentLoading from '@/hooks/common/loading';
import { initWebSocket } from '@/utils/websocket';
import { initSSE } from '@/utils/sse';
defineOptions({ defineOptions({
name: 'AppProvider' name: 'AppProvider'
@ -27,12 +25,6 @@ const ContextHolder = defineComponent({
return () => createTextVNode(); return () => createTextVNode();
} }
}); });
onMounted(() => {
const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
initWebSocket(`${protocol + window.location.host + import.meta.env.VITE_APP_BASE_API}/resource/websocket`);
initSSE(`${import.meta.env.VITE_APP_BASE_API}/resource/sse`);
});
</script> </script>
<template> <template>

View File

@ -1,9 +1,11 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed, defineAsyncComponent } from 'vue'; import { computed, defineAsyncComponent, onMounted } from 'vue';
import { AdminLayout, LAYOUT_SCROLL_EL_ID } from '@sa/materials'; import { AdminLayout, LAYOUT_SCROLL_EL_ID } from '@sa/materials';
import type { LayoutMode } from '@sa/materials'; import type { LayoutMode } from '@sa/materials';
import { useAppStore } from '@/store/modules/app'; import { useAppStore } from '@/store/modules/app';
import { useThemeStore } from '@/store/modules/theme'; import { useThemeStore } from '@/store/modules/theme';
import { initWebSocket } from '@/utils/websocket';
import { initSSE } from '@/utils/sse';
import GlobalHeader from '../modules/global-header/index.vue'; import GlobalHeader from '../modules/global-header/index.vue';
import GlobalSider from '../modules/global-sider/index.vue'; import GlobalSider from '../modules/global-sider/index.vue';
import GlobalTab from '../modules/global-tab/index.vue'; import GlobalTab from '../modules/global-tab/index.vue';
@ -100,6 +102,12 @@ function getSiderCollapsedWidth() {
return w; return w;
} }
onMounted(() => {
const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
initWebSocket(`${protocol + window.location.host + import.meta.env.VITE_APP_BASE_API}/resource/websocket`);
initSSE(`${import.meta.env.VITE_APP_BASE_API}/resource/sse`);
});
</script> </script>
<template> <template>

View File

@ -1,15 +1,15 @@
/** Default theme settings */ /** Default theme settings */
export const themeSettings: App.Theme.ThemeSetting = { export const themeSettings: App.Theme.ThemeSetting = {
themeScheme: 'light', themeScheme: 'auto',
grayscale: false, grayscale: false,
colourWeakness: false, colourWeakness: false,
recommendColor: false, recommendColor: false,
themeColor: '#2080f0', themeColor: '#0E42D2',
otherColor: { otherColor: {
info: '#2080f0', info: '#0E42D2',
success: '#52c41a', success: '#009A29',
warning: '#faad14', warning: '#D25F00',
error: '#f5222d' error: '#CB2634'
}, },
isInfoFollowPrimary: true, isInfoFollowPrimary: true,
layout: { layout: {

View File

@ -5,10 +5,10 @@ import { localStg } from './storage';
// 初始化 // 初始化
export const initSSE = (url: any) => { export const initSSE = (url: any) => {
if (import.meta.env.VITE_APP_SSE === 'N') { const token = localStg.get('token');
if (import.meta.env.VITE_APP_SSE === 'N' || !token) {
return; return;
} }
const token = localStg.get('token');
const sseUrl = `${url}?Authorization=Bearer ${token}&clientid=${import.meta.env.VITE_APP_CLIENT_ID}`; const sseUrl = `${url}?Authorization=Bearer ${token}&clientid=${import.meta.env.VITE_APP_CLIENT_ID}`;
const { data, error } = useEventSource(sseUrl, [], { const { data, error } = useEventSource(sseUrl, [], {
autoReconnect: { autoReconnect: {

View File

@ -32,12 +32,12 @@ let socketError = 0; // 错误次数
// 初始化socket // 初始化socket
export function initWebSocket(url: any) { export function initWebSocket(url: any) {
if (import.meta.env.VITE_APP_WEBSOCKET === 'N') { const token = localStg.get('token');
if (import.meta.env.VITE_APP_WEBSOCKET === 'N' || !token) {
return null; return null;
} }
socketUrl = url; socketUrl = url;
// 初始化 websocket // 初始化 websocket
const token = localStg.get('token');
websocket = new WebSocket(`${url}?Authorization=Bearer ${token}&clientid=${import.meta.env.VITE_APP_CLIENT_ID}`); websocket = new WebSocket(`${url}?Authorization=Bearer ${token}&clientid=${import.meta.env.VITE_APP_CLIENT_ID}`);
websocketonopen(); websocketonopen();
websocketonmessage(); websocketonmessage();