推送模块

This commit is contained in:
2026-04-18 15:48:31 +08:00
parent e3f26ba1f7
commit 7d0f11fe43
19 changed files with 548 additions and 5 deletions

View File

@@ -9,4 +9,8 @@ export default {
'/': ['./frontend/${lang}/index.ts'],
[adminBaseRoutePath + '/moduleStore']: ['./backend/${lang}/module.ts'],
[adminBaseRoutePath + '/crud/crud']: ['./backend/${lang}/crud/log.ts', './backend/${lang}/crud/state.ts'],
/** 推送测试三页:共享 test.push.* 文案(见 PushChannelTestPage.vue */
[adminBaseRoutePath + '/test/pushGamePeriod']: ['./backend/${lang}/test/push.ts'],
[adminBaseRoutePath + '/test/pushOperationNotice']: ['./backend/${lang}/test/push.ts'],
[adminBaseRoutePath + '/test/pushPrivateUser']: ['./backend/${lang}/test/push.ts'],
}

View File

@@ -0,0 +1,12 @@
export default {
connected: 'Push connected (subscribed)',
disconnected: 'Disconnected or idle',
user_uuid: 'User uuid',
user_uuid_placeholder: '10-char public id as in mobile profile',
btn_connect: 'Connect & subscribe',
btn_disconnect: 'Disconnect',
btn_clear: 'Clear log',
channel_label: 'Channel',
log_title: 'Event log',
log_empty: 'No messages yet. Ensure the push worker is running and the server publishes to this channel.',
}

View File

@@ -0,0 +1,3 @@
export default {
tip: 'Subscribe to public-game-period (global period channel) for period.tick / period.locked / period.opened. The server must publish to this channel.',
}

View File

@@ -0,0 +1,3 @@
export default {
tip: 'Subscribe to public-operation-notice for broadcast notices such as notice.popout. The server must publish to this channel.',
}

View File

@@ -0,0 +1,3 @@
export default {
tip: 'Subscribe to private-user-{uuid}: enter the player uuid; auth uses /plugin/webman/push/auth like the mobile client. For bet.accepted, wallet.changed, etc.',
}

View File

@@ -0,0 +1,12 @@
export default {
connected: '推送服务已连接(已订阅频道)',
disconnected: '未连接或已断开',
user_uuid: '用户 uuid',
user_uuid_placeholder: '与移动端档案一致的 10 位对外标识',
btn_connect: '连接并订阅',
btn_disconnect: '断开',
btn_clear: '清空日志',
channel_label: '当前频道',
log_title: '事件日志',
log_empty: '暂无推送,请确认 push 进程已启动且服务端会向对应频道发消息。',
}

View File

@@ -0,0 +1,3 @@
export default {
tip: '订阅文档中的「全局对局频道」public-game-period用于验证 period.tick / period.locked / period.opened 等公共事件(需服务端向该频道推送)。',
}

View File

@@ -0,0 +1,3 @@
export default {
tip: '订阅文档中的「公告广播频道」public-operation-notice用于验证 notice.popout 等全站公告类推送(需服务端向该频道推送)。',
}

View File

@@ -0,0 +1,3 @@
export default {
tip: '订阅「用户私有频道」private-user-{uuid}:请输入玩家 uuid连接后将走 /plugin/webman/push/auth 鉴权(与移动端一致)。用于验证 bet.accepted、wallet.changed 等私有事件。',
}

View File

@@ -0,0 +1,79 @@
/**
* 后台推送频道测试:加载官方 push.js 并订阅频道、监听文档约定事件
*/
const DOC_EVENTS = [
'period.tick',
'period.locked',
'period.opened',
'bet.accepted',
'wallet.changed',
'notice.popout',
'withdraw.review_required',
]
export async function loadPushJs(): Promise<void> {
if ((window as any).Push) {
return
}
await new Promise<void>((resolve, reject) => {
const script = document.createElement('script')
script.src = '/plugin/webman/push/push.js'
script.onload = () => resolve()
script.onerror = () => reject(new Error('load push.js failed'))
document.head.appendChild(script)
})
}
export type PushTestLogLine = { t: number; event: string; payload: string }
export function startPushChannelListener(options: {
url: string
app_key: string
channel: string
/** 私有频道需走 /plugin/webman/push/auth */
usePrivateAuth: boolean
onLog: (line: PushTestLogLine) => void
onConnected: (ok: boolean) => void
}): { disconnect: () => void } {
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'
}
const client = new PushCtor(cfg)
const ch = client.subscribe(options.channel)
const pushLog = (event: string, data: unknown) => {
let payload = ''
try {
payload = typeof data === 'string' ? data : JSON.stringify(data)
} catch {
payload = String(data)
}
options.onLog({ t: Date.now(), event, payload })
}
ch.on('pusher:subscription_succeeded', () => {
options.onConnected(true)
pushLog('pusher:subscription_succeeded', {})
})
for (const ev of DOC_EVENTS) {
ch.on(ev, (data: unknown) => {
options.onConnected(true)
pushLog(ev, data)
})
}
return {
disconnect: () => {
try {
client.disconnect()
} catch {
/* ignore */
}
options.onConnected(false)
},
}
}

View File

@@ -0,0 +1,142 @@
<template>
<div class="default-main">
<el-alert type="info" :title="tip" show-icon class="mb-12" />
<el-alert :type="connected ? 'success' : 'warning'" :title="connected ? t('test.push.connected') : t('test.push.disconnected')" show-icon class="mb-12" />
<el-card shadow="never" class="mb-12">
<el-form :inline="true" @submit.prevent>
<el-form-item v-if="useUuid" :label="t('test.push.user_uuid')">
<el-input v-model="userUuid" :placeholder="t('test.push.user_uuid_placeholder')" clearable style="width: 280px" />
</el-form-item>
<el-form-item>
<el-button type="primary" :loading="loading" @click="connect">{{ t('test.push.btn_connect') }}</el-button>
<el-button :disabled="!session" @click="disconnect">{{ t('test.push.btn_disconnect') }}</el-button>
<el-button @click="clearLogs">{{ t('test.push.btn_clear') }}</el-button>
</el-form-item>
</el-form>
<div class="text-muted mb-8">{{ t('test.push.channel_label') }}: {{ channelName || '-' }}</div>
</el-card>
<el-card shadow="never">
<template #header>{{ t('test.push.log_title') }}</template>
<el-scrollbar max-height="480">
<pre class="push-log">{{ logText }}</pre>
</el-scrollbar>
</el-card>
</div>
</template>
<script setup lang="ts">
import { computed, onUnmounted, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import createAxios from '/@/utils/axios'
import { loadPushJs, startPushChannelListener, type PushTestLogLine } from '/@/utils/backend/pushChannelTest'
const props = withDefaults(
defineProps<{
/** 如 /admin/test.PushGamePeriod/pushConfig */
configApi: string
useUuid?: boolean
tip: string
}>(),
{
useUuid: false,
}
)
const { t } = useI18n()
const loading = ref(false)
const connected = ref(false)
const channelName = ref('')
const userUuid = ref('')
const logs = ref<PushTestLogLine[]>([])
const session = ref<{ disconnect: () => void } | null>(null)
const logText = computed(() => {
if (!logs.value.length) return t('test.push.log_empty')
return logs.value
.map((row) => {
const time = new Date(row.t).toLocaleString()
return `[${time}] ${row.event}\n${row.payload}`
})
.join('\n\n')
})
function appendLog(line: PushTestLogLine) {
logs.value = [...logs.value, line].slice(-200)
}
function clearLogs() {
logs.value = []
}
async function connect() {
if (props.useUuid && !userUuid.value.trim()) {
return
}
loading.value = true
disconnect()
try {
await loadPushJs()
const params: anyObj = {}
if (props.useUuid) {
params.uuid = userUuid.value.trim()
}
const res = await createAxios({
url: props.configApi,
method: 'get',
params,
showCodeMessage: true,
})
if (res.code !== 1 || !res.data) {
connected.value = false
return
}
const { url, app_key, channel } = res.data
channelName.value = typeof channel === 'string' ? channel : ''
try {
const handle = startPushChannelListener({
url,
app_key,
channel: channelName.value,
usePrivateAuth: !!props.useUuid,
onLog: appendLog,
onConnected: (ok) => {
connected.value = ok
},
})
session.value = handle
} catch {
connected.value = false
}
} finally {
loading.value = false
}
}
function disconnect() {
if (session.value) {
session.value.disconnect()
session.value = null
}
connected.value = false
}
onUnmounted(() => {
disconnect()
})
</script>
<style scoped>
.push-log {
margin: 0;
font-size: 12px;
line-height: 1.5;
white-space: pre-wrap;
word-break: break-all;
}
.text-muted {
color: var(--el-text-color-secondary);
font-size: 13px;
}
</style>

View File

@@ -0,0 +1,10 @@
<template>
<PushChannelTestPage config-api="/admin/test.PushGamePeriod/pushConfig" :tip="t('test.pushGamePeriod.tip')" />
</template>
<script setup lang="ts">
import { useI18n } from 'vue-i18n'
import PushChannelTestPage from '../components/PushChannelTestPage.vue'
const { t } = useI18n()
</script>

View File

@@ -0,0 +1,10 @@
<template>
<PushChannelTestPage config-api="/admin/test.PushOperationNotice/pushConfig" :tip="t('test.pushOperationNotice.tip')" />
</template>
<script setup lang="ts">
import { useI18n } from 'vue-i18n'
import PushChannelTestPage from '../components/PushChannelTestPage.vue'
const { t } = useI18n()
</script>

View File

@@ -0,0 +1,14 @@
<template>
<PushChannelTestPage
config-api="/admin/test.PushPrivateUser/pushConfig"
use-uuid
:tip="t('test.pushPrivateUser.tip')"
/>
</template>
<script setup lang="ts">
import { useI18n } from 'vue-i18n'
import PushChannelTestPage from '../components/PushChannelTestPage.vue'
const { t } = useI18n()
</script>