Files
property-resident-side/property-uniapp-project/App.vue

130 lines
3.5 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<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 启动')
uni.onPushMessage((res) => {
console.log("收到推送消息:",res) //监听推送消息
})
// #ifdef APP-PLUS
// 只有 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')
// #ifdef APP-PLUS
// 每次回到前台时检查 CID 是否有效,没有则重新获取
const savedCid = uni.getStorageSync('push_cid')
if (!savedCid) {
console.log('App Show 检测到 CID 为空,重新获取')
cidRetryCount = 0
getPushCID()
}
// #endif
})
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('推送 CIDplus.push=', clientInfo.clientid)
uni.setStorageSync('push_cid', clientInfo.clientid)
return
}
} catch (e) {
console.log('plus.push.getClientInfo 失败,改用 uni.getPushClientId')
}
uni.getPushClientId({
success: (res) => {
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>
<style lang="scss">
@import "@/uni_modules/uview-plus/index.scss";
/*每个页面公共css */
</style>
<style>
/* 全局给 uni.showToast 加内边距 */
.uni-toast {
padding: 5px !important;
/* 上下20 左右40自己改数值 */
min-height: auto !important;
box-sizing: border-box !important;
}
/* 文字也可以居中、调整行高 */
.uni-toast-content {
/* line-height: 1.6 !important; */
padding: 0 !important;
}
/* button:after{
border-radius: 50px;
} */
</style>