优化后台收不到推送报错404
This commit is contained in:
@@ -35,6 +35,33 @@ export const getUrl = (): string => {
|
||||
return value == 'getCurrentDomain' ? window.location.protocol + '//' + window.location.host : value
|
||||
}
|
||||
|
||||
/**
|
||||
* Webman Push 在 API 上的 HTTP 根地址(私有频道 POST /plugin/webman/push/auth 等)。
|
||||
* 与 VITE_AXIOS_BASE_URL 一致;开发环境为空时用当前页面源,配合 Vite 对 /plugin 的代理。
|
||||
*/
|
||||
export function getPushHttpBase(): string {
|
||||
const api = getUrl()
|
||||
if (api) {
|
||||
return api.replace(/\/$/, '')
|
||||
}
|
||||
return window.location.protocol + '//' + window.location.host
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载 push.js 的绝对 URL:文件放在 web/public/plugin/webman/push/,随 dist 与后台同源发布,
|
||||
* 不依赖 API 站点是否暴露 /plugin/webman/push/push.js。
|
||||
*/
|
||||
export function getPushScriptUrl(): string {
|
||||
let base = import.meta.env.BASE_URL
|
||||
if (typeof base !== 'string' || base === '') {
|
||||
base = '/'
|
||||
}
|
||||
if (!base.endsWith('/')) {
|
||||
base = base + '/'
|
||||
}
|
||||
return window.location.origin + base + 'plugin/webman/push/push.js'
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据运行环境获取基础请求URL的端口
|
||||
*/
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
* 后台推送频道测试:加载官方 push.js 并订阅频道、监听文档约定事件
|
||||
*/
|
||||
|
||||
import { getPushHttpBase, getPushScriptUrl } from '/@/utils/axios'
|
||||
|
||||
const DOC_EVENTS = [
|
||||
'period.tick',
|
||||
'period.locked',
|
||||
@@ -21,7 +23,7 @@ export async function loadPushJs(): Promise<void> {
|
||||
}
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const script = document.createElement('script')
|
||||
script.src = '/plugin/webman/push/push.js'
|
||||
script.src = getPushScriptUrl()
|
||||
script.onload = () => resolve()
|
||||
script.onerror = () => reject(new Error('load push.js failed'))
|
||||
document.head.appendChild(script)
|
||||
@@ -42,7 +44,7 @@ export function startPushChannelListener(options: {
|
||||
const PushCtor = (window as any).Push
|
||||
const cfg: anyObj = { url: options.url, app_key: options.app_key }
|
||||
if (options.usePrivateAuth) {
|
||||
cfg.auth = '/plugin/webman/push/auth'
|
||||
cfg.auth = `${getPushHttpBase()}/plugin/webman/push/auth`
|
||||
}
|
||||
const client = new PushCtor(cfg)
|
||||
const ch = client.subscribe(options.channel)
|
||||
|
||||
@@ -108,7 +108,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, reactive, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import createAxios from '/@/utils/axios'
|
||||
import createAxios, { getPushScriptUrl } from '/@/utils/axios'
|
||||
|
||||
interface Snapshot {
|
||||
record: anyObj | null
|
||||
@@ -307,7 +307,7 @@ async function loadPushJs() {
|
||||
}
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const script = document.createElement('script')
|
||||
script.src = '/plugin/webman/push/push.js'
|
||||
script.src = getPushScriptUrl()
|
||||
script.onload = () => resolve()
|
||||
script.onerror = () => reject(new Error('load push.js failed'))
|
||||
document.head.appendChild(script)
|
||||
|
||||
Reference in New Issue
Block a user