diff --git a/web/.env b/web/.env index 4abe4b9..bdea0bd 100644 --- a/web/.env +++ b/web/.env @@ -6,8 +6,6 @@ VUE_APP_TITLE=DvAdmin # 网络请求公用地址 VUE_APP_API=/api/ -# websocket地址 -VUE_APP_WEBSOCKET="" # 仓库地址 VUE_APP_REPO=https://github.com/d2-projects/d2-admin-start-kit diff --git a/web/.env.development b/web/.env.development index eab5c45..2b35a9a 100644 --- a/web/.env.development +++ b/web/.env.development @@ -6,5 +6,4 @@ VUE_APP_TITLE=企业级后台管理系统 VUE_APP_PM_ENABLED = true # 后端接口地址及端口(域名) VUE_APP_API = "http://127.0.0.1:8000" -VUE_APP_WEBSOCKET = "ws://127.0.0.1:8000" diff --git a/web/src/api/websocket.js b/web/src/api/websocket.js index e9b24e1..c9a47ec 100644 --- a/web/src/api/websocket.js +++ b/web/src/api/websocket.js @@ -4,7 +4,7 @@ import store from '@/store' function initWebSocket (e) { const token = util.cookies.get('token') if (token) { - const wsUri = process.env.VUE_APP_WEBSOCKET + '/ws/' + token + '/' + const wsUri = util.wsBaseURL() + 'ws/' + token + '/' this.socket = new WebSocket(wsUri)// 这里面的this都指向vue this.socket.onerror = webSocketOnError this.socket.onmessage = webSocketOnMessage @@ -57,7 +57,7 @@ function webSocketOnMessage (e) { const { content } = data if (content.model === 'message_center') { const unread = content.unread - store.dispatch('d2admin/messagecenter/setUnread',unread) + store.dispatch('d2admin/messagecenter/setUnread', unread) } } } diff --git a/web/src/libs/util.js b/web/src/libs/util.js index 266436f..735d0bb 100644 --- a/web/src/libs/util.js +++ b/web/src/libs/util.js @@ -62,6 +62,40 @@ util.baseURL = function () { } return baseURL } + +util.wsBaseURL = function () { + var baseURL = process.env.VUE_APP_API + var param = baseURL.split('/')[3] || '' + if (window.pluginsAll && window.pluginsAll.indexOf('dvadmin-tenant-web') !== -1 && (!param || baseURL.startsWith('/'))) { + // 1.把127.0.0.1 替换成和前端一样域名 + // 2.把 ip 地址替换成和前端一样域名 + // 3.把 /api 或其他类似的替换成和前端一样域名 + // document.domain + var host = baseURL.split('/')[2] + if (host) { + var prot = baseURL.split(':')[2] || 80 + if (prot === 80 || prot === 443) { + host = document.domain + } else { + host = document.domain + ':' + prot + } + baseURL = baseURL.split('/')[0] + '//' + baseURL.split('/')[1] + host + '/' + param + } else { + baseURL = location.protocol + '//' + location.hostname + (location.port ? ':' : '') + location.port + baseURL + } + } else if (param !== '' || baseURL.startsWith('/')) { + baseURL = (location.protocol === 'https' ? 'wss://' : 'ws://') + location.hostname + (location.port ? ':' : '') + location.port + baseURL + } + if (!baseURL.endsWith('/')) { + baseURL += '/' + } + if (baseURL.startsWith('http')) { + baseURL = baseURL.replace('http', 'ws') + } else if (baseURL.startsWith('https')) { + baseURL = baseURL.replace('https', 'wss') + } + return baseURL +} /** * 自动生成ID */