修改cid无效问题
This commit is contained in:
@@ -1,41 +1,108 @@
|
||||
<script setup>
|
||||
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
|
||||
|
||||
let pushCIDRetryCount = 0
|
||||
const MAX_RETRY = 5
|
||||
let retryTimer = null
|
||||
|
||||
// 应用启动时执行
|
||||
onLaunch(() => {
|
||||
console.log('App 启动')
|
||||
uni.onPushMessage((res) => {
|
||||
console.log("收到推送消息:",res) //监听推送消息
|
||||
console.log('收到推送消息:', res) // 监听推送消息
|
||||
})
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
// 只有 App 平台才获取 CID
|
||||
// 只有 App 平台才获取 CID,延迟 1 秒等推送服务就绪
|
||||
setTimeout(() => {
|
||||
getPushCID()
|
||||
}, 1000)
|
||||
// #endif
|
||||
})
|
||||
|
||||
onShow(() => {
|
||||
console.log('App Show')
|
||||
// #ifdef APP-PLUS
|
||||
// 每次回到前台都重新获取 CID,防止 CID 过期失效
|
||||
pushCIDRetryCount = 0
|
||||
setTimeout(() => {
|
||||
refreshPushCID()
|
||||
}, 500)
|
||||
// #endif
|
||||
})
|
||||
|
||||
onHide(() => {
|
||||
console.log('App Hide')
|
||||
// 清除重试定时器,避免后台无效重试
|
||||
if (retryTimer) {
|
||||
clearTimeout(retryTimer)
|
||||
retryTimer = null
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
// 获取 uniPush 2.0 CID
|
||||
function getPushCID() {
|
||||
// 刷新 CID(每次 onShow 调用,比对更新)
|
||||
function refreshPushCID() {
|
||||
const oldCid = uni.getStorageSync('push_cid') || ''
|
||||
uni.getPushClientId({
|
||||
success: res => {
|
||||
console.log('推送初始化成功')
|
||||
console.log('CID =', res.cid)
|
||||
// 存起来备用
|
||||
uni.setStorageSync('push_cid', res.cid)
|
||||
const newCid = res.cid
|
||||
if (!newCid) {
|
||||
console.warn('获取到的 CID 为空,视为失败')
|
||||
scheduleRetry()
|
||||
return
|
||||
}
|
||||
pushCIDRetryCount = 0
|
||||
if (oldCid === newCid) {
|
||||
console.log('CID 未变化:', newCid)
|
||||
} else {
|
||||
console.log(`CID 已更新: ${oldCid || '(无)'} → ${newCid}`)
|
||||
uni.setStorageSync('push_cid', newCid)
|
||||
// TODO: 将新 CID 上报到服务器
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
console.error('获取推送 CID 失败', err)
|
||||
scheduleRetry()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 启动时首次获取 CID(重试更积极)
|
||||
function getPushCID() {
|
||||
uni.getPushClientId({
|
||||
success: res => {
|
||||
const cid = res.cid
|
||||
if (!cid) {
|
||||
console.warn('获取到的 CID 为空,视为失败')
|
||||
scheduleRetry()
|
||||
return
|
||||
}
|
||||
console.log('推送初始化成功, CID =', cid)
|
||||
uni.setStorageSync('push_cid', cid)
|
||||
pushCIDRetryCount = 0
|
||||
// TODO: 将 CID 上报到服务器
|
||||
},
|
||||
fail: err => {
|
||||
console.error('获取推送 CID 失败', err)
|
||||
scheduleRetry()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 重试调度
|
||||
function scheduleRetry() {
|
||||
if (pushCIDRetryCount < MAX_RETRY) {
|
||||
pushCIDRetryCount++
|
||||
const delay = pushCIDRetryCount * 1500 // 1.5s, 3s, 4.5s, 6s, 7.5s
|
||||
console.log(`推送 CID 获取失败,${delay}ms 后第 ${pushCIDRetryCount} 次重试...`)
|
||||
retryTimer = setTimeout(() => {
|
||||
getPushCID()
|
||||
}, delay)
|
||||
} else {
|
||||
console.error('推送 CID 获取已达最大重试次数,请检查推送配置')
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
|
||||
Reference in New Issue
Block a user