From ce7f6383d05699d09410b3b1b972568545d4c0e1 Mon Sep 17 00:00:00 2001 From: doublekou <951513186@qq.com> Date: Mon, 27 Nov 2023 16:44:24 +0800 Subject: [PATCH] =?UTF-8?q?websocket=E8=BF=9E=E6=8E=A5=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Message/index.vue | 181 +++++------------- src/hooks/websocket.ts | 10 +- .../RightContent/AvatarDropdown.vue | 14 +- .../customerList/customerList.vue | 4 +- src/views/login/components/LoginContent.vue | 38 ++-- vite.config.ts | 6 +- 6 files changed, 94 insertions(+), 159 deletions(-) diff --git a/src/components/Message/index.vue b/src/components/Message/index.vue index 288f8bb..348c107 100644 --- a/src/components/Message/index.vue +++ b/src/components/Message/index.vue @@ -5,20 +5,7 @@
查看更多
-
- +
@@ -27,7 +14,9 @@
{{ item.createTime }}
- +
diff --git a/src/hooks/websocket.ts b/src/hooks/websocket.ts index 9b8b5f7..5f0ae7b 100644 --- a/src/hooks/websocket.ts +++ b/src/hooks/websocket.ts @@ -1,6 +1,7 @@ import { useWebSocket, type UseWebSocketReturn } from '@vueuse/core' import { useUserStore } from '@/stores/user-store' import { emitter } from '@/hooks/mitt' +import { message } from 'ant-design-vue' let useWebSocketReturn: UseWebSocketReturn | undefined = undefined //它是一个自定义的Vue hook,用于管理WebSocket连接 @@ -54,10 +55,13 @@ const useAdminWebSocket = () => { event = 'plaintext' dataMsg = value } - emitter.emit(event, dataMsg) - console.log(dataMsg, 'dataMsg') + // emitter.emit(event, dataMsg) + // console.log(dataMsg, 'dataMsg') // Show alert with the received message - alert(JSON.stringify(dataMsg)) + // alert(JSON.stringify(dataMsg.title) + JSON.stringify(dataMsg.content)) + message.info( + JSON.stringify(dataMsg.title).slice(1, -1) +':'+ JSON.stringify(dataMsg.content).slice(1, -1) + ) } ) diff --git a/src/layouts/components/RightContent/AvatarDropdown.vue b/src/layouts/components/RightContent/AvatarDropdown.vue index 11c3187..5e21dd1 100644 --- a/src/layouts/components/RightContent/AvatarDropdown.vue +++ b/src/layouts/components/RightContent/AvatarDropdown.vue @@ -4,7 +4,7 @@
@@ -83,15 +83,20 @@ import Message from '@/components/Message/index.vue' import { ref, watch, onMounted, onUnmounted } from 'vue' import { doRequest } from '@/utils/axios/request' import { messageAccessLogs } from '@/api/customermanagement/customerList' +import useAdminWebSocket from '@/hooks/websocket' //获取当前账号角色 import { useUserStore } from '@/stores/user-store' const { userInfo } = useUserStore() const userInfoCurrent = ref('') onMounted(() => { - console.log(userInfo, 'userInfo?.permissions') - // userInfoCurrent = userInfo?.roleCodes userInfoCurrent.value = userInfo?.roleCodes?.toLocaleString() + if ( + userInfoCurrent.value === 'ROLE_SALESMAN' || + userInfoCurrent.value === 'ROLE_STORE_SALESMAN' + ) { + useAdminWebSocket() + } }) const visible = ref(false) @@ -103,7 +108,7 @@ interface DataItem { createTime: string content: string messageKey: string - status: number | undefined + state: number | undefined title: string show: boolean } @@ -185,6 +190,7 @@ const getMessageList = () => { { onSuccess: (res: any) => { newclueList.value = res.data.records + console.log(newclueList.value, 'newclueList.value') newclueList.value.forEach(item => { item.show = true }) diff --git a/src/views/customermanagement/customerList/customerList.vue b/src/views/customermanagement/customerList/customerList.vue index a498980..3477cb0 100644 --- a/src/views/customermanagement/customerList/customerList.vue +++ b/src/views/customermanagement/customerList/customerList.vue @@ -87,6 +87,7 @@ import CustomerListSearch from '@/views/customermanagement/customerList/customer import CustomerListViewModal from '@/views/customermanagement/customerList/customerListModal.vue' import CustomerListEditModal from '@/views/customermanagement/customerList/customerListEditModal.vue' import { useRoute } from 'vue-router' +// import useAdminWebSocket from '@/hooks/websocket' //删除 // import { doRequest } from '@/utils/axios/request' @@ -97,6 +98,7 @@ onMounted(() => { if (customerId) { searchTable({ clueId: customerId }) } + // useAdminWebSocket() }) defineOptions({ name: 'CustomerList' }) @@ -160,7 +162,7 @@ const columns: ProColumns[] = [ dataIndex: 'index', width: '80px', customRender: function ({ index, record }: { index: number; record: any }) { - if (record.clueStageName === '阶段二') { + if (record.isNewClue === true) { return { children: [ h('span', [ diff --git a/src/views/login/components/LoginContent.vue b/src/views/login/components/LoginContent.vue index cb68558..4045a48 100644 --- a/src/views/login/components/LoginContent.vue +++ b/src/views/login/components/LoginContent.vue @@ -80,7 +80,7 @@ import { projectTitle, projectDesc, enableLoginCaptcha } from '@/config' import { SliderCaptcha as LoginCaptcha } from '@/components/Captcha' import { useI18n } from 'vue-i18n' import { getCodeData } from '@/api/auth' -import useAdminWebSocket from '@/hooks/websocket' +// import useAdminWebSocket from '@/hooks/websocket' const { t } = useI18n() @@ -172,24 +172,26 @@ function handleSubmit(captchaId?: string) { store(res) const nextPath = (router.currentRoute.value.query.redirect as string) || '/' router.push(nextPath) + // 获取 WebSocket 连接对象 - const webSocket = useAdminWebSocket() - - // 确保 WebSocket 连接已经建立 - if (webSocket && webSocket.status.value === 'OPEN') { - // 要发送的消息数据 - const messageData = { - type: 'chatMessage', - content: 'Hello, WebSocket!' - } - - // 发送消息 - webSocket.send(JSON.stringify(messageData)) - // 输出发送的消息内容 - console.log('Sent message:', JSON.stringify(messageData)) - } else { - console.error('WebSocket connection is not open') - } + // const webSocket = + // useAdminWebSocket() + + // // 确保 WebSocket 连接已经建立 + // if (webSocket && webSocket.status.value === 'OPEN') { + // // 要发送的消息数据 + // const messageData = { + // type: 'chatMessage', + // content: 'Hello, WebSocket!' + // } + + // // 发送消息 + // webSocket.send(JSON.stringify(messageData)) + // // 输出发送的消息内容 + // console.log('Sent message:', JSON.stringify(messageData)) + // } else { + // console.error('WebSocket connection is not open') + // } }) .catch(err => { // console.log(accountLoginFormRef.value, 'accountLoginFormRef') diff --git a/vite.config.ts b/vite.config.ts index 4014482..9167971 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -26,9 +26,9 @@ import { antdvStyleDeps } from './src/utils/resolvers' //jt // export const serverAddress = 'http://172.18.1.8:8000' //ts -// export const serverAddress = 'http://172.18.0.228:8000' +export const serverAddress = 'http://172.18.0.228:8000' //测试 -export const serverAddress = 'http://39.100.77.21:8000' +// export const serverAddress = 'http://39.100.77.21:8000' //线上ip // export const serverAddress = 'http://175.27.212.74:80' @@ -45,7 +45,7 @@ export default defineConfig({ '^/api': { target: serverAddress, changeOrigin: true, - // ws: true, + ws: true, rewrite: path => { return path.replace(/^\/api/, '') }