This commit is contained in:
Yvan
2026-09-04 10:51:45 +08:00
parent d94e2ed225
commit af1e194396
17 changed files with 1015 additions and 61 deletions
@@ -170,7 +170,7 @@
<el-button
v-if="isWebSocket(selected)"
:icon="DocumentCopy"
:disabled="isBusy(selected)"
:disabled="isBusy(selected) || !canTestConnectionInfo(selected)"
@click="openConnectionInfo"
>
连接信息
@@ -326,9 +326,18 @@ const selected = computed(
const integrationKey = (item) => `${item.kind}/${item.provider}`
const providerMeta = (item) => TARGETS[integrationKey(item)] || TARGETS[TARGET_ORDER[0]]
const isWebSocket = (item) => integrationKey(item) === 'websocket/melody'
const websocketBackendIsExplicit = () =>
Boolean(
String(import.meta.env.VITE_WEBSOCKET_URL || '').trim() ||
String(import.meta.env.VITE_SERVER_PORT || '').trim()
)
const websocketOrigin = () => {
if (import.meta.env.DEV && import.meta.env.VITE_SERVER_PORT) {
const configuredBase = String(import.meta.env.VITE_BASE_PATH || window.location.origin)
if (websocketBackendIsExplicit()) {
const configuredBase = String(
import.meta.env.VITE_WEBSOCKET_URL ||
import.meta.env.VITE_BASE_PATH ||
window.location.origin
)
const backend = new URL(configuredBase, window.location.origin)
if (
['127.0.0.1', 'localhost'].includes(backend.hostname) &&
@@ -336,18 +345,27 @@ const websocketOrigin = () => {
) {
backend.hostname = window.location.hostname
}
backend.port = String(import.meta.env.VITE_SERVER_PORT)
if (import.meta.env.VITE_SERVER_PORT) {
backend.port = String(import.meta.env.VITE_SERVER_PORT)
}
backend.protocol = backend.protocol === 'https:' ? 'wss:' : 'ws:'
return `${backend.protocol}//${backend.host}`
}
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
return `${protocol}//${window.location.host}`
}
const websocketPathPrefix = () => {
if (websocketBackendIsExplicit()) return ''
const base = String(import.meta.env.VITE_BASE_API || '').trim()
if (!base || /^https?:\/\//i.test(base)) return ''
return `/${base.replace(/^\/+|\/+$/g, '')}`
}
const connectionInfo = computed(() => {
const path = String(selected.value?.config?.path || '/ws').trim() || '/ws'
const normalizedPath = path.startsWith('/') ? path : `/${path}`
const routePath = `${websocketPathPrefix()}${normalizedPath}`
const token = String(userStore.token || '').trim()
const url = `${websocketOrigin()}${normalizedPath}${token ? `?token=${encodeURIComponent(token)}` : ''}`
const url = `${websocketOrigin()}${routePath}${token ? `?token=${encodeURIComponent(token)}` : ''}`
return {
url,
example: `const socket = new WebSocket(${JSON.stringify(url)})\n\nsocket.onopen = () => console.log('connected')\nsocket.onmessage = (event) => console.log(event.data)`
@@ -357,7 +375,7 @@ const connectionTestMessage = computed(() => {
if (connectionTestStatus.value === 'testing') return '等待服务器握手包...'
if (connectionTestStatus.value === 'success') return '握手成功,WebSocket 已连接。'
if (connectionTestStatus.value === 'error') return '握手失败,请查看 websocket/websocket.log。'
return '点击“测试此地址”验证浏览器到服务器的真实握手。'
return '“测试连接”仅校验当前参数;保存并启用后可测试正式地址。'
})
const connectionTestAlertType = computed(() => {
if (connectionTestStatus.value === 'success') return 'success'
@@ -469,6 +487,8 @@ const isBusy = (item) => Boolean(pending[operationKey(item)])
const isSaving = (item) => pending[operationKey(item)] === 'save'
const isToggling = (item) => pending[operationKey(item)] === 'toggle'
const isTesting = (item) => pending[operationKey(item)] === 'test'
const canTestConnectionInfo = (item) =>
Boolean(item?.configured && item.enabled && !isDirty(item))
const numberConstraint = (fieldKey) =>
NUMBER_CONSTRAINTS[fieldKey] || { min: undefined, max: undefined }
@@ -601,7 +621,7 @@ const testSelected = async () => {
if (res.code !== 0) {
return
}
ElMessage.success(`${item.name || providerMeta(item).name} 连接测试成功`)
ElMessage.success(`${item.name || providerMeta(item).name} 配置测试成功`)
} catch {
// The request layer owns transport and server error presentation.
} finally {