From 2ed3693479e1c500456b2713983fd4648a756f90 Mon Sep 17 00:00:00 2001
From: doum <doum>
Date: 星期三, 29 四月 2026 18:45:30 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'
---
app/App.vue | 300 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 297 insertions(+), 3 deletions(-)
diff --git a/app/App.vue b/app/App.vue
index a3c7e0c..20b66e5 100644
--- a/app/App.vue
+++ b/app/App.vue
@@ -1,21 +1,315 @@
<script>
import { mapState } from 'vuex'
export default {
+ data() {
+ return {
+ locationTimer: null,
+ jpushModule: null,
+ jpushListenersBound: false,
+ jpushRegistrationTimer: null,
+ jpushAliasRetryTimer: null,
+ jpushRegistrationRetryCount: 0,
+ jpushAliasRetryCount: 0
+ }
+ },
computed: {
- ...mapState(['userInfo'])
+ ...mapState(['userInfo', 'token'])
+ },
+ watch: {
+ token(newVal) {
+ if (newVal) {
+ this.startLocationPolling()
+ this.bindJPushAlias(true)
+ } else {
+ this.stopLocationPolling()
+ this.clearJPushRetryTimers()
+ this.deleteJPushAlias()
+ }
+ }
},
onLaunch: function() {
-
+ this.initJPush()
+ this.ensureJPushAliasOnLaunch()
+ this.checkAndStartLocationPolling()
+ this.checkAppUpdate()
},
onShow: function() {
console.log('App Show')
+ this.checkAndStartLocationPolling()
},
onHide: function() {
console.log('App Hide')
+ this.stopLocationPolling()
+ },
+ methods: {
+ initJPush() {
+ // #ifdef APP-PLUS
+ if (this.jpushModule) {
+ this.retryGetRegistrationID()
+ return
+ }
+
+ const jpushModule = uni.requireNativePlugin('JG-JPush')
+ if (!jpushModule) {
+ console.log('鏋佸厜鎺ㄩ�佹彃浠舵湭鎵惧埌锛岃鍏堝畨瑁呮彃浠�')
+ return
+ }
+ this.jpushModule = jpushModule
+
+ try {
+ jpushModule.setLoggerEnable(false)
+ jpushModule.init()
+ } catch (error) {
+ console.log('鏋佸厜鎺ㄩ�佸垵濮嬪寲澶辫触:', error)
+ return
+ }
+ console.log('鏋佸厜鎺ㄩ�佸垵濮嬪寲鎴愬姛')
+
+ if (!this.jpushListenersBound) {
+ jpushModule.addReceiveNotificationListener((result) => {
+ console.log('鏀跺埌閫氱煡:', JSON.stringify(result))
+ const extras = this.normalizeJPushExtras(result && result.extras)
+ if (extras.type === 'order') {
+ uni.$emit('jpush_order_notification', extras)
+ }
+ })
+
+ jpushModule.addReceiveOpenNotificationListener((result) => {
+ console.log('鐐瑰嚮閫氱煡:', JSON.stringify(result))
+ const extras = this.normalizeJPushExtras(result && result.extras)
+ if (extras.type === 'order' && extras.orderId) {
+ 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))
+ })
+
+ this.jpushListenersBound = true
+ }
+
+ this.retryGetRegistrationID()
+ if (this.token) {
+ this.bindJPushAlias(true)
+ }
+ // #endif
+ },
+
+ ensureJPushAliasOnLaunch() {
+ // #ifdef APP-PLUS
+ if (this.token) {
+ this.bindJPushAlias(true)
+ }
+ // #endif
+ },
+
+ clearJPushRetryTimers() {
+ if (this.jpushRegistrationTimer) {
+ clearTimeout(this.jpushRegistrationTimer)
+ this.jpushRegistrationTimer = null
+ }
+ if (this.jpushAliasRetryTimer) {
+ clearTimeout(this.jpushAliasRetryTimer)
+ this.jpushAliasRetryTimer = null
+ }
+ },
+
+ normalizeJPushExtras(extras) {
+ if (!extras) return {}
+ if (typeof extras === 'string') {
+ try {
+ return JSON.parse(extras)
+ } catch (error) {
+ return {}
+ }
+ }
+ return extras
+ },
+
+ isJPushActionSuccess(result) {
+ if (!result) return false
+ const successCodes = [0, '0']
+ return successCodes.includes(result.code) || successCodes.includes(result.errCode)
+ },
+
+ retryGetRegistrationID(forceRetry = false) {
+ // #ifdef APP-PLUS
+ if (!this.jpushModule) return
+
+ if (forceRetry) {
+ this.jpushRegistrationRetryCount = 0
+ }
+
+ if (this.jpushRegistrationTimer) {
+ clearTimeout(this.jpushRegistrationTimer)
+ this.jpushRegistrationTimer = null
+ }
+
+ this.jpushModule.getRegistrationID((res) => {
+ console.log('JPush RegistrationID:', res)
+ if (res && res.registerID) {
+ uni.setStorageSync('jpush_registration_id', res.registerID)
+ this.jpushRegistrationRetryCount = 0
+ return
+ }
+
+ if (this.jpushRegistrationRetryCount >= 5) {
+ console.log('澶氭鑾峰彇 RegistrationID 澶辫触锛屽仠姝㈤噸璇�')
+ return
+ }
+
+ this.jpushRegistrationRetryCount += 1
+ this.jpushRegistrationTimer = setTimeout(() => {
+ this.retryGetRegistrationID()
+ }, 1500)
+ })
+ // #endif
+ },
+
+ bindJPushAlias(forceRetry = false) {
+ // #ifdef APP-PLUS
+ if (!this.token) return
+ if (!this.jpushModule) {
+ this.initJPush()
+ return
+ }
+ const userId = this.$store.state.userInfo?.id || ''
+ if (!userId) return
+
+ if (forceRetry) {
+ this.jpushAliasRetryCount = 0
+ }
+
+ if (this.jpushAliasRetryTimer) {
+ clearTimeout(this.jpushAliasRetryTimer)
+ this.jpushAliasRetryTimer = null
+ }
+
+ this.jpushModule.setAlias({
+ alias: String(userId),
+ sequence: Date.now()
+ }, (result) => {
+ if (this.isJPushActionSuccess(result)) {
+ console.log('璁剧疆鏋佸厜鍒悕鎴愬姛:', JSON.stringify(result))
+ this.jpushAliasRetryCount = 0
+ return
+ }
+
+ console.log('璁剧疆鏋佸厜鍒悕澶辫触:', JSON.stringify(result))
+ if (this.jpushAliasRetryCount >= 3) {
+ return
+ }
+
+ this.jpushAliasRetryCount += 1
+ this.jpushAliasRetryTimer = setTimeout(() => {
+ this.bindJPushAlias()
+ }, 2000)
+ })
+ // #endif
+ },
+
+ deleteJPushAlias() {
+ // #ifdef APP-PLUS
+ if (!this.jpushModule) return
+ this.clearJPushRetryTimers()
+ this.jpushModule.deleteAlias({
+ sequence: Date.now()
+ }, (result) => {
+ if (this.isJPushActionSuccess(result)) {
+ console.log('鍒犻櫎鏋佸厜鍒悕鎴愬姛:', JSON.stringify(result))
+ } else {
+ console.log('鍒犻櫎鏋佸厜鍒悕澶辫触:', JSON.stringify(result))
+ }
+ })
+ // #endif
+ },
+
+ 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() {
+ // #ifdef APP-PLUS
+ 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()
+ }
+ }
+ })
+ }
+ }
+ })
+ // #endif
+ }
}
}
</script>
<style lang="scss">
@import "uview-ui/index.scss";
-</style>
\ No newline at end of file
+</style>
--
Gitblit v1.9.3