hejianhao
2026-08-28 cc31d303d12f9c7cd2c4574351b67db04069ae0d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<script setup>
    import {
        onLaunch,
        onShow
    } from '@dcloudio/uni-app'
    import {
        extractBindTicket,
        extractInviteCode
    } from '@/utils/utils.js'
    import { hasCompletedAuthSession } from '@/utils/auth-session.js'
    import { cacheInviteCode } from '@/utils/invite-cache.js'
    import { initJPushLifecycle } from '@/utils/push.js'
    import {
        getOpeninstallInstallData,
        initOpeninstall,
        registerOpeninstallWakeUp
    } from '@/utils/openinstall.js'
    let lastHandledInvitePayload = ''
 
    /**
     * 应用启动时初始化极光与 openinstall,承接推广关系参数。
     */
    onLaunch(() => {
        // #ifdef APP-PLUS
        initJPushLifecycle()
        initOpeninstall()
        registerOpeninstallWakeUp(handleOpeninstallPayload)
        getOpeninstallInstallData(handleOpeninstallPayload)
        // #endif
    })
 
    /**
     * App 被外部 Scheme 拉起时,解析 bindTicket 或 inviteCode 并跳转注册页。
     */
    onShow(() => {
        handleOpenByScheme()
    })
 
    /**
     * 读取原生启动参数中的绑定票据或邀请码,并在命中时跳转注册页。
     */
    function handleOpenByScheme() {
        // #ifdef APP-PLUS
        const runtimeArguments = typeof plus !== 'undefined' && plus.runtime ? plus.runtime.arguments : ''
        handleInvitePayload(runtimeArguments)
        // #endif
    }
 
    /**
     * 处理 openinstall 安装参数或唤醒参数。
     */
    function handleOpeninstallPayload(payload) {
        handleInvitePayload(payload)
    }
 
    /**
     * 统一解析外部推广参数,并跳转到注册页回填。 
     */
    function handleInvitePayload(payload) {
        const bindTicket = extractBindTicket(payload)
        const inviteCode = extractInviteCode(payload)
        if (!bindTicket && !inviteCode) {
            return
        }
        if (inviteCode) {
            cacheInviteCode(inviteCode)
        }
        const payloadKey = `${bindTicket || ''}_${inviteCode || ''}`
        if (payloadKey === lastHandledInvitePayload) {
            return
        }
        lastHandledInvitePayload = payloadKey
        if (hasCompletedAuthSession()) {
            return
        }
        const currentPages = getCurrentPages()
        const currentRoute = currentPages && currentPages.length > 0 ? currentPages[currentPages.length - 1].route : ''
        const targetParams = []
        if (bindTicket) {
            targetParams.push(`bindTicket=${encodeURIComponent(bindTicket)}`)
        }
        if (inviteCode) {
            targetParams.push(`inviteCode=${encodeURIComponent(inviteCode)}`)
        }
        const targetUrl = `/pages/register/register?${targetParams.join('&')}`
        if (currentRoute === 'pages/register/register') {
            uni.redirectTo({
                url: targetUrl
            })
            return
        }
        uni.navigateTo({
            url: targetUrl
        })
    }
</script>
 
<style lang="scss">
    /*每个页面公共css */
    @import "@/uni_modules/uview-pro/index.scss";
    @import "@/static/style/index.scss";
</style>