diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..08eb07b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +.git +.idea +node_modules +**/node_modules +dist +**/dist +docs-site/docs/.vitepress/dist +**/.DS_Store +**/*.log +*.iml diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..a2dc70e --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +@xuqm:registry=https://nexus.xuqinmin.com/repository/npm-hosted/ diff --git a/tenant-platform/src/services/storeReviewRealtime.ts b/tenant-platform/src/services/storeReviewRealtime.ts new file mode 100644 index 0000000..1e68557 --- /dev/null +++ b/tenant-platform/src/services/storeReviewRealtime.ts @@ -0,0 +1,91 @@ +import { ElMessage } from 'element-plus' +import { init, login, ImClient, type ImMessage } from '@xuqm/vue3-sdk' +import client from '@/api/client' + +export interface StoreReviewRefreshEvent { + event: string + appKey: string + versionId?: string + storeType?: string + reviewState?: string + reviewReason?: string + stage?: string + batchId?: string + publishStatus?: string + source?: string + timestamp?: number +} + +interface PlatformEventTokenResponse { + userId: string + token: string +} + +let imClient: ImClient | null = null +let activeAppKey = '' + +function sdkBaseUrl() { + return import.meta.env.VITE_IM_API_BASE_URL ?? '' +} + +function sdkWsUrl() { + return import.meta.env.VITE_IM_WS_URL ?? '' +} + +function parseEvent(message: ImMessage): StoreReviewRefreshEvent | null { + if (!['NOTIFY', 'CUSTOM'].includes(message.msgType)) return null + try { + const payload = JSON.parse(message.content) as Partial + if (!payload || payload.event !== 'store_review_update') return null + if (!payload.appKey) return null + return payload as StoreReviewRefreshEvent + } catch { + return null + } +} + +export async function connectStoreReviewRealtime(appKey: string, onEvent: (event: StoreReviewRefreshEvent) => void) { + if (!appKey) return + disconnectStoreReviewRealtime() + activeAppKey = appKey + + const res = await client.get<{ data: PlatformEventTokenResponse }>('/im/platform-events/token', { + params: { appKey }, + }) + const token = res.data.data + init({ + appKey, + baseUrl: sdkBaseUrl(), + wsUrl: sdkWsUrl(), + debug: import.meta.env.DEV, + }) + login(token.userId, token.token) + + const clientInstance = new ImClient() + clientInstance.on('message', (message) => { + const event = parseEvent(message) + if (!event || event.appKey !== activeAppKey) return + onEvent(event) + }) + clientInstance.on('error', (error) => { + if (import.meta.env.DEV) { + console.warn('[tenant-platform][IM] store review realtime error', error) + } + }) + clientInstance.connect() + imClient = clientInstance +} + +export function disconnectStoreReviewRealtime() { + if (imClient) { + imClient.disconnect() + imClient = null + } + activeAppKey = '' +} + +export function notifyStoreReviewRefresh(appKey: string) { + if (appKey && appKey === activeAppKey) { + ElMessage.info('检测到审核状态更新,正在刷新...') + } +}