弹窗双开关控制台存储

This commit is contained in:
龚皓 2024-11-22 17:44:36 +08:00
parent a8266b78eb
commit b8dfc49d55
1 changed files with 104 additions and 36 deletions

View File

@ -1,14 +1,20 @@
<template>
<div class="settings-container">
<el-row class="popup-row">
<el-checkbox v-model="isPopupEnabled" @change="handleCheckboxChange" style="color: aliceblue;">
开启弹窗
</el-checkbox>
<el-col :sm="24" :md="24">弹窗设置</el-col>
<el-col :sm="24" :md="24">
<el-checkbox v-model="isInteractivePopupEnabled" @change="handleInteractiveChange" style="color: aliceblue;">
开启交互式弹窗
</el-checkbox>
</el-col>
<el-col :sm="24" :md="24">
<el-checkbox v-model="isResponsivePopupEnabled" @change="handleResponsiveChange" style="color: aliceblue;">
开启响应式弹窗
</el-checkbox>
</el-col>
</el-row>
<el-row class="channel-row">
<Channel/>
<Channel />
</el-row>
</div>
</template>
@ -16,51 +22,114 @@
<script lang="ts" setup>
import { ref, inject, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import type { GlobalWebSocket } from '../utils/useGlobalWebSocket';
import Channel from '@/components/Channel.vue';
import type { GlobalWebSocket } from '@/utils/useGlobalWebSocket';
import Channel from '@/components/Channel.vue';
const isPopupEnabled = ref(false); //
const globalWebSocket = inject<GlobalWebSocket>('globalWebSocket'); //
const isInteractivePopupEnabled = ref(false); //
const isResponsivePopupEnabled = ref(false); //
const globalWebSocket = inject<GlobalWebSocket>('globalWebSocket');
// globalWebSocket
if (!globalWebSocket) {
throw new Error('globalWebSocket 注入失败');
}
// localStorage
//
onMounted(() => {
const storedState = localStorage.getItem('isPopupEnabled');
isPopupEnabled.value = storedState === 'true';
// WebSocket WebSocket
if (isPopupEnabled.value && !globalWebSocket.isWebSocketConnected.value) {
globalWebSocket.connectWebSocket();
}
isInteractivePopupEnabled.value = localStorage.getItem('isInteractivePopupEnabled') === 'true';
isResponsivePopupEnabled.value = localStorage.getItem('isResponsivePopupEnabled') === 'true';
updateWebSocketConnection();
});
//
const handleCheckboxChange = () => {
if (isPopupEnabled.value) {
//
ElMessageBox.confirm('是否开启弹窗提示?', '提示', {
// const handleInteractiveChange = () => {
// if (isInteractivePopupEnabled.value) {
// isResponsivePopupEnabled.value = false;
// localStorage.setItem('isResponsivePopupEnabled', 'false');
// }
// localStorage.setItem('isInteractivePopupEnabled', String(isInteractivePopupEnabled.value));
// updateWebSocketConnection();
// };
// const handleResponsiveChange = () => {
// if (isResponsivePopupEnabled.value) {
// isInteractivePopupEnabled.value = false;
// localStorage.setItem('isInteractivePopupEnabled', 'false');
// }
// localStorage.setItem('isResponsivePopupEnabled', String(isResponsivePopupEnabled.value));
// updateWebSocketConnection();
// };
const handleInteractiveChange = () => {
if (isInteractivePopupEnabled.value) {
ElMessageBox.confirm('是否开启交互式弹窗提示?', '确认提示', {
confirmButtonText: '是',
cancelButtonText: '否',
type: 'warning',
})
.then(() => {
// WebSocket
localStorage.setItem('isPopupEnabled', 'true');
globalWebSocket.connectWebSocket();
isResponsivePopupEnabled.value = false;
localStorage.setItem('isResponsivePopupEnabled', 'false');
localStorage.setItem('isInteractivePopupEnabled', 'true');
updateWebSocketConnection();
})
.catch(() => {
//
isPopupEnabled.value = false;
localStorage.setItem('isPopupEnabled', 'false');
isInteractivePopupEnabled.value = false;
});
} else {
// WebSocket
ElMessageBox.confirm('是否关闭交互式弹窗?', '确认提示', {
confirmButtonText: '是',
cancelButtonText: '否',
type: 'warning',
})
.then(() => {
localStorage.setItem('isInteractivePopupEnabled', 'false');
updateWebSocketConnection();
})
.catch(() => {
isInteractivePopupEnabled.value = true;
});
}
};
const handleResponsiveChange = () => {
if (isResponsivePopupEnabled.value) {
ElMessageBox.confirm('是否开启响应式弹窗提示?', '确认提示', {
confirmButtonText: '是',
cancelButtonText: '否',
type: 'warning',
})
.then(() => {
isInteractivePopupEnabled.value = false;
localStorage.setItem('isInteractivePopupEnabled', 'false');
localStorage.setItem('isResponsivePopupEnabled', 'true');
updateWebSocketConnection();
})
.catch(() => {
isResponsivePopupEnabled.value = false;
});
} else {
ElMessageBox.confirm('是否关闭响应式弹窗?', '确认提示', {
confirmButtonText: '是',
cancelButtonText: '否',
type: 'warning',
})
.then(() => {
localStorage.setItem('isResponsivePopupEnabled', 'false');
updateWebSocketConnection();
})
.catch(() => {
isResponsivePopupEnabled.value = true;
});
}
};
// WebSocket
const updateWebSocketConnection = () => {
if (isInteractivePopupEnabled.value || isResponsivePopupEnabled.value) {
globalWebSocket.connectWebSocket();
} else {
globalWebSocket.closeWebSocket();
localStorage.setItem('isPopupEnabled', 'false');
}
};
</script>
@ -69,11 +138,11 @@ const handleCheckboxChange = () => {
.settings-container {
display: flex;
flex-direction: column;
background-color: #ffffff;
background-color: #F1F1F1;
width: 100%;
height: 100%;
}
.popup-row {
margin-bottom: 20px;
height: 20vh;
@ -85,12 +154,11 @@ const handleCheckboxChange = () => {
color: white;
}
.channel-row{
.channel-row {
margin: 1vh 2vw;
width: 80vw;
height: 70vh;
background-color: #001529;
border-radius: 8px;
}
</style>