doum
2026-04-29 59b1f0e9967902aa10f5e017d5a0bdfd1b60c9ea
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<script>
    import { mapState } from 'vuex'
    export default {
        data() {
            return {
                locationTimer: null,
                jpushModule: null
            }
        },
        computed: {
            ...mapState(['userInfo', 'token'])
        },
        watch: {
            token(newVal) {
                if (newVal) {
                    this.startLocationPolling()
                    this.bindJPushAlias()
                } else {
                    this.stopLocationPolling()
                    this.deleteJPushAlias()
                }
            }
        },
        onLaunch: function() {
            this.initJPush()
            this.checkAndStartLocationPolling()
            this.checkAppUpdate()
        },
        onShow: function() {
            console.log('App Show')
            this.checkAndStartLocationPolling()
        },
        onHide: function() {
            console.log('App Hide')
            this.stopLocationPolling()
        },
        methods: {
            initJPush() {
                const jpushModule = uni.requireNativePlugin('JPush')
                if (!jpushModule) {
                    console.log('极光推送插件未找到,请先安装插件')
                    return
                }
                this.jpushModule = jpushModule
 
                jpushModule.init()
                console.log('极光推送初始化成功')
 
                jpushModule.setLoggerEnable(true)
 
                jpushModule.addReceiveNotificationListener((result) => {
                    console.log('收到通知:', JSON.stringify(result))
                    const extras = result.extras || {}
                    if (extras.type === 'order') {
                        uni.$emit('jpush_order_notification', extras)
                    }
                })
 
                jpushModule.addReceiveOpenNotificationListener((result) => {
                    console.log('点击通知:', JSON.stringify(result))
                    const extras = result.extras || {}
                    if (extras.type === 'order') {
                        uni.navigateTo({
                            url: '/pages/order-detail/order-detail?id=' + extras.orderId
                        })
                    } else if (extras.type === 'message') {
                        uni.switchTab({
                            url: '/pages/message/message'
                        })
                    }
                })
 
                jpushModule.addReceiveMessageListener((result) => {
                    console.log('收到透传消息:', JSON.stringify(result))
                })
 
                jpushModule.getRegistrationID((res) => {
                    console.log('JPush RegistrationID:', res)
                    if (res && res.registerID) {
                        uni.setStorageSync('jpush_registration_id', res.registerID)
                    }
                })
            },
 
            bindJPushAlias() {
                if (!this.jpushModule || !this.token) return
                const userId = this.$store.state.userInfo?.id || ''
                if (userId) {
                    this.jpushModule.setAlias({
                        alias: String(userId),
                        sequence: Date.now()
                    }, (result) => {
                        console.log('设置极光别名成功:', JSON.stringify(result))
                    })
                }
            },
 
            deleteJPushAlias() {
                if (!this.jpushModule) return
                this.jpushModule.deleteAlias({
                    sequence: Date.now()
                }, (result) => {
                    console.log('删除极光别名成功:', JSON.stringify(result))
                })
            },
 
            checkAndStartLocationPolling() {
                if (this.token) {
                    this.startLocationPolling()
                }
            },
 
            startLocationPolling() {
                this.stopLocationPolling()
                this.updateLocation()
                this.locationTimer = setInterval(() => {
                    this.updateLocation()
                }, 60000)
            },
 
            stopLocationPolling() {
                if (this.locationTimer) {
                    clearInterval(this.locationTimer)
                    this.locationTimer = null
                }
            },
 
            updateLocation() {
                uni.getLocation({
                    type: 'gcj02',
                    success: (res) => {
                        this.$u.api.updateLocation({
                            latitude: res.latitude,
                            longitude: res.longitude
                        })
                    }
                })
            },
 
            compareVersion(localVersion, serverVersion) {
                const v1 = localVersion.split('.')
                const v2 = serverVersion.split('.')
                for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
                    const n1 = parseInt(v1[i] || 0)
                    const n2 = parseInt(v2[i] || 0)
                    if (n1 < n2) return -1
                    if (n1 > n2) return 1
                }
                return 0
            },
 
            checkAppUpdate() {
                this.$u.api.getApiVersion({ type: 0 }).then(res => {
                    if (res.code === 200 && res.data) {
                        const localVersionCode = plus.runtime.versionCode
                        const serverVersionCode = res.data.versionCode
                        if (serverVersionCode && localVersionCode < serverVersionCode) {
                            const isForce = res.data.isForce === 1
                            uni.showModal({
                                title: '发现新版本',
                                content: res.data.versionInfo || '有新版本可用,是否立即更新?',
                                showCancel: !isForce,
                                cancelText: isForce ? '' : '暂不更新',
                                confirmText: '立即更新',
                                success: (modalRes) => {
                                    if (modalRes.confirm) {
                                        if (res.data.fileUrl) {
                                            plus.runtime.openURL(res.data.fileUrl)
                                        }
                                    }
                                    if (isForce && modalRes.cancel) {
                                        this.checkAppUpdate()
                                    }
                                }
                            })
                        }
                    }
                })
            }
        }
    }
</script>
 
<style lang="scss">
    @import "uview-ui/index.scss"; 
</style>