feat(avatar): integrate Huihui payments

This commit is contained in:
stefanfeng
2026-08-26 14:41:21 +08:00
parent 5360cac8ad
commit c37294be17
13 changed files with 962 additions and 39 deletions
+26 -6
View File
@@ -4,6 +4,7 @@
// uni-webview bridge is bundled by main.ts; no external CDN is required.
const BRIDGE_HANDLER = '__uniBridgeHandle__'
const nativeMessageHandlers = new Set<(message: any) => void>()
export interface UniLaunchParams {
token?: string
@@ -25,7 +26,21 @@ function readParams(search: string, target: UniLaunchParams): void {
// 是否运行在 uniapp web-view 环境中
export function isInUniWebView(): boolean {
return !!(window as any).uni?.webView
const runtime = window as any
const userAgent = navigator.userAgent
const isDCloudApp = Boolean(
runtime.plus ||
runtime.__dcloud_weex_postMessage ||
runtime.__dcloud_weex_ ||
/uni-app|Html5Plus/i.test(userAgent)
)
const isMiniProgram = Boolean(
(/miniProgram/i.test(userAgent) && (runtime.wx?.miniProgram || runtime.qq?.miniProgram)) ||
runtime.my ||
runtime.swan?.webView ||
runtime.tt?.miniProgram
)
return Boolean(runtime.uni?.webView && (isDCloudApp || isMiniProgram))
}
// 解析 web-view 加载 URL 时原生注入的参数(token / 会会用户)
@@ -65,14 +80,18 @@ export function postToNative(message: Record<string, any>): boolean {
}
// 原生 → H5:注册消息处理(原生通过 web-view.evalJS 调用 window.__uniBridgeHandle__)
export function onNativeMessage(handler: (message: any) => void): void {
export function onNativeMessage(handler: (message: any) => void): () => void {
nativeMessageHandlers.add(handler)
;(window as any)[BRIDGE_HANDLER] = (message: any) => {
try {
handler(message)
} catch (e) {
console.error('[uniBridge] handler error', e)
for (const listener of nativeMessageHandlers) {
try {
listener(message)
} catch (e) {
console.error('[uniBridge] handler error', e)
}
}
}
return () => nativeMessageHandlers.delete(handler)
}
// 便捷事件
@@ -81,5 +100,6 @@ export const UniEvents = {
needLogin: () => postToNative({ type: 'needLogin' }),
setTitle: (title: string) => postToNative({ type: 'setTitle', title }),
navigate: (path: string) => postToNative({ type: 'navigate', path }),
payment: (payment: Record<string, any>) => postToNative({ type: 'payment', payment }),
back: () => postToNative({ type: 'back' })
}