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">
import { createTextVNode, defineComponent, onMounted } from 'vue';
import { createTextVNode, defineComponent } from 'vue';
import { useDialog, useLoadingBar, useMessage, useNotification } from 'naive-ui';
import useContentLoading from '@/hooks/common/loading';
import { initWebSocket } from '@/utils/websocket';
import { initSSE } from '@/utils/sse';
defineOptions({
name: 'AppProvider'
@ -27,12 +25,6 @@ const ContextHolder = defineComponent({
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>
<template>

View File

@ -1,9 +1,11 @@
<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 type { LayoutMode } from '@sa/materials';
import { useAppStore } from '@/store/modules/app';
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 GlobalSider from '../modules/global-sider/index.vue';
import GlobalTab from '../modules/global-tab/index.vue';
@ -100,6 +102,12 @@ function getSiderCollapsedWidth() {
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>
<template>

View File

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

View File

@ -5,10 +5,10 @@ import { localStg } from './storage';
// 初始化
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;
}
const token = localStg.get('token');
const sseUrl = `${url}?Authorization=Bearer ${token}&clientid=${import.meta.env.VITE_APP_CLIENT_ID}`;
const { data, error } = useEventSource(sseUrl, [], {
autoReconnect: {

View File

@ -32,12 +32,12 @@ let socketError = 0; // 错误次数
// 初始化socket
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;
}
socketUrl = url;
// 初始化 websocket
const token = localStg.get('token');
websocket = new WebSocket(`${url}?Authorization=Bearer ${token}&clientid=${import.meta.env.VITE_APP_CLIENT_ID}`);
websocketonopen();
websocketonmessage();