feat:完善舆情监测功能
This commit is contained in:
28
src/hooks/echart.js
Normal file
28
src/hooks/echart.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { ref } from 'vue'
|
||||
import * as echarts from 'echarts'
|
||||
import { guid } from '@/utils/util'
|
||||
|
||||
export function useEchart() {
|
||||
let chart = ref(null)
|
||||
let id = ref(guid())
|
||||
const initChart = () => {
|
||||
const dom = document.getElementById(id.value)
|
||||
chart.value = echarts.init(dom)
|
||||
}
|
||||
const setOption = (params) => {
|
||||
chart.value.setOption(params)
|
||||
}
|
||||
const resize = () => {
|
||||
if (chart) {
|
||||
chart.dispose()
|
||||
chart = null
|
||||
initChart()
|
||||
}
|
||||
}
|
||||
onMounted(() => {
|
||||
// 监听窗口大小变化
|
||||
window.addEventListener('resize', resize)
|
||||
})
|
||||
|
||||
return { id, chart, setOption, initChart }
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
export function useMap() {
|
||||
let map = ref(null)
|
||||
// 初始化地图
|
||||
|
||||
57
src/hooks/socket.js
Normal file
57
src/hooks/socket.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
export function useWebSocket(url) {
|
||||
let socket = ref(null)
|
||||
let data = ref(null)
|
||||
let isConnected = ref(false)
|
||||
|
||||
const connectWebSocket = () => {
|
||||
socket.value = new WebSocket(url, 'echo-protocol', {
|
||||
headers: {
|
||||
Authorization:
|
||||
'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImE1OWFmNWYwLTU3OWItNDJkNy1hZDJhLTY0Y2JlODA5ZWI1NiJ9.BTxvu6jUWbN0qONWf5K6VzXopE8T8qXzKuX-mij21VJT4U0LdgnqToyqeNDQ2OyJ6cvpdJBzQ9mEEb-dnwrTpQ'
|
||||
}
|
||||
})
|
||||
socket.value.onopen = () => {
|
||||
isConnected.value = true
|
||||
console.log('WebSocket connected')
|
||||
sendMessage(
|
||||
JSON.stringify({
|
||||
action: 'start',
|
||||
type: 'index'
|
||||
})
|
||||
)
|
||||
}
|
||||
socket.value.onerror = (error) => {
|
||||
console.error('WebSocket error:', error)
|
||||
}
|
||||
socket.value.onmessage = (message) => {
|
||||
// 处理接收到的消息
|
||||
console.log('Received message:', JSON.parse(message.data))
|
||||
if (JSON.parse(message.data)) {
|
||||
data.value = JSON.parse(message.data)
|
||||
}
|
||||
}
|
||||
socket.value.onclose = () => {
|
||||
console.log('WebSocket disconnected')
|
||||
}
|
||||
}
|
||||
|
||||
const sendMessage = (message) => {
|
||||
if (socket.value) {
|
||||
socket.value.send(message)
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
connectWebSocket()
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
if (socket.value) {
|
||||
socket.value.close()
|
||||
}
|
||||
})
|
||||
|
||||
return { socket, data, isConnected, connectWebSocket, sendMessage }
|
||||
}
|
||||
Reference in New Issue
Block a user