| | |
| | | data() { |
| | | return { |
| | | locationTimer: null, |
| | | jpushModule: null |
| | | jpushModule: null, |
| | | jpushListenersBound: false, |
| | | jpushRegistrationTimer: null, |
| | | jpushAliasRetryTimer: null, |
| | | jpushRegistrationRetryCount: 0, |
| | | jpushAliasRetryCount: 0 |
| | | } |
| | | }, |
| | | computed: { |
| | |
| | | token(newVal) { |
| | | if (newVal) { |
| | | this.startLocationPolling() |
| | | this.bindJPushAlias() |
| | | this.bindJPushAlias(true) |
| | | } else { |
| | | this.stopLocationPolling() |
| | | this.clearJPushRetryTimers() |
| | | this.deleteJPushAlias() |
| | | } |
| | | } |
| | | }, |
| | | onLaunch: function() { |
| | | this.initJPush() |
| | | this.ensureJPushAliasOnLaunch() |
| | | this.checkAndStartLocationPolling() |
| | | this.checkAppUpdate() |
| | | }, |
| | |
| | | }, |
| | | methods: { |
| | | initJPush() { |
| | | const jpushModule = uni.requireNativePlugin('JPush') |
| | | // #ifdef APP-PLUS |
| | | if (this.jpushModule) { |
| | | this.retryGetRegistrationID() |
| | | return |
| | | } |
| | | |
| | | const jpushModule = uni.requireNativePlugin('JG-JPush') |
| | | if (!jpushModule) { |
| | | console.log('极光推送插件未找到,请先安装插件') |
| | | return |
| | | } |
| | | this.jpushModule = jpushModule |
| | | |
| | | jpushModule.init() |
| | | try { |
| | | jpushModule.setLoggerEnable(false) |
| | | jpushModule.init() |
| | | } catch (error) { |
| | | console.log('极光推送初始化失败:', error) |
| | | return |
| | | } |
| | | console.log('极光推送初始化成功') |
| | | |
| | | jpushModule.setLoggerEnable(true) |
| | | 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.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 = 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.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.addReceiveMessageListener((result) => { |
| | | console.log('收到透传消息:', JSON.stringify(result)) |
| | | }) |
| | | this.jpushListenersBound = true |
| | | } |
| | | |
| | | jpushModule.getRegistrationID((res) => { |
| | | console.log('JPush RegistrationID:', res) |
| | | if (res && res.registerID) { |
| | | uni.setStorageSync('jpush_registration_id', res.registerID) |
| | | } |
| | | }) |
| | | this.retryGetRegistrationID() |
| | | if (this.token) { |
| | | this.bindJPushAlias(true) |
| | | } |
| | | // #endif |
| | | }, |
| | | |
| | | 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)) |
| | | }) |
| | | 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 |
| | | } |
| | | }, |
| | | |
| | | deleteJPushAlias() { |
| | | 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) => { |
| | | console.log('删除极光别名成功:', JSON.stringify(result)) |
| | | if (this.isJPushActionSuccess(result)) { |
| | | console.log('删除极光别名成功:', JSON.stringify(result)) |
| | | } else { |
| | | console.log('删除极光别名失败:', JSON.stringify(result)) |
| | | } |
| | | }) |
| | | // #endif |
| | | }, |
| | | |
| | | checkAndStartLocationPolling() { |
| | |
| | | }, |
| | | |
| | | checkAppUpdate() { |
| | | // #ifdef APP-PLUS |
| | | this.$u.api.getApiVersion({ type: 0 }).then(res => { |
| | | if (res.code === 200 && res.data) { |
| | | const localVersionCode = plus.runtime.versionCode |
| | |
| | | } |
| | | } |
| | | }) |
| | | // #endif |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | <style lang="scss"> |
| | | @import "uview-ui/index.scss"; |
| | | </style> |
| | | </style> |