修改cid无效、登录缓存问题,正在对接支付功能
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
<script setup>
|
||||
import { onLaunch,onShow,onHide } from '@dcloudio/uni-app'
|
||||
|
||||
let cidRetryTimer = null
|
||||
let cidRetryCount = 0
|
||||
const MAX_RETRY = 5 // 最多重试 5 次
|
||||
|
||||
// 应用启动时执行
|
||||
onLaunch(() => {
|
||||
console.log('App 启动')
|
||||
@@ -12,29 +16,90 @@
|
||||
// 只有 App 平台才获取 CID
|
||||
getPushCID()
|
||||
// #endif
|
||||
|
||||
// 自动登录:检查本地是否已有 token,有则直接跳转首页
|
||||
const token = uni.getStorageSync('token')
|
||||
const hasShowGuide = uni.getStorageSync('hasShowGuide')
|
||||
if (token) {
|
||||
// 已有登录凭证,直接进入首页
|
||||
console.log('检测到已登录,跳转首页')
|
||||
uni.reLaunch({ url: '/pages/index/index' })
|
||||
} else if (hasShowGuide) {
|
||||
// 已看过引导页但没有 token,直接跳转登录页
|
||||
console.log('已看过引导页,跳转登录页')
|
||||
uni.reLaunch({ url: '/pageSubPack/loginSub/pwd-login' })
|
||||
}
|
||||
// 否则停留在引导页(navigation)
|
||||
})
|
||||
onShow(() =>{
|
||||
console.log('App Show')
|
||||
})
|
||||
|
||||
onHide(() =>{
|
||||
console.log('App Hide')
|
||||
console.log('App Show')
|
||||
// #ifdef APP-PLUS
|
||||
// 每次回到前台时检查 CID 是否有效,没有则重新获取
|
||||
const savedCid = uni.getStorageSync('push_cid')
|
||||
if (!savedCid) {
|
||||
console.log('App Show 检测到 CID 为空,重新获取')
|
||||
cidRetryCount = 0
|
||||
getPushCID()
|
||||
}
|
||||
// #endif
|
||||
})
|
||||
|
||||
// 获取 uniPush 2.0 CID
|
||||
onHide(() =>{
|
||||
console.log('App Hide')
|
||||
})
|
||||
|
||||
// 获取 uniPush 2.0 CID(带重试机制,解决离线安装首次启动 CID 为空的问题)
|
||||
function getPushCID() {
|
||||
// #ifdef APP-PLUS
|
||||
// 先尝试 plus.push.getClientInfo 获取(兼容老版本)
|
||||
try {
|
||||
const clientInfo = plus.push.getClientInfo()
|
||||
if (clientInfo && clientInfo.clientid) {
|
||||
console.log('推送 CID(plus.push)=', clientInfo.clientid)
|
||||
uni.setStorageSync('push_cid', clientInfo.clientid)
|
||||
return
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('plus.push.getClientInfo 失败,改用 uni.getPushClientId')
|
||||
}
|
||||
|
||||
uni.getPushClientId({
|
||||
success: (res) => {
|
||||
console.log('推送初始化成功')
|
||||
console.log('CID =', res.cid)
|
||||
|
||||
// 存起来备用
|
||||
uni.setStorageSync('push_cid', res.cid)
|
||||
if (res.cid) {
|
||||
console.log('推送初始化成功, CID =', res.cid)
|
||||
uni.setStorageSync('push_cid', res.cid)
|
||||
cidRetryCount = 0
|
||||
if (cidRetryTimer) {
|
||||
clearTimeout(cidRetryTimer)
|
||||
cidRetryTimer = null
|
||||
}
|
||||
} else {
|
||||
// CID 返回为空(常见于离线安装首次启动)
|
||||
console.warn('获取到 CID 为空,将重试')
|
||||
scheduleRetry()
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('获取推送 CID 失败', err)
|
||||
scheduleRetry()
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
}
|
||||
|
||||
// 定时重试获取 CID(间隔递增:2s → 4s → 8s → 16s → 32s)
|
||||
function scheduleRetry() {
|
||||
if (cidRetryCount >= MAX_RETRY) {
|
||||
console.error('CID 获取重试已达上限(' + MAX_RETRY + '次),放弃重试')
|
||||
return
|
||||
}
|
||||
cidRetryCount++
|
||||
const delay = Math.pow(2, cidRetryCount) * 1000
|
||||
console.log('将在 ' + delay / 1000 + 's 后第 ' + cidRetryCount + ' 次重试获取 CID')
|
||||
if (cidRetryTimer) clearTimeout(cidRetryTimer)
|
||||
cidRetryTimer = setTimeout(() => {
|
||||
getPushCID()
|
||||
}, delay)
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user