功能变化: wsBaseURL

pull/79/head
李强 2022-11-25 10:12:53 +08:00
parent 3c066bdd2a
commit bed7219724
4 changed files with 36 additions and 5 deletions

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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
*/