Files
property-resident-side/property-uniapp-project/App.vue
zhouyanli 92dd39cf4b 修复bug
2026-08-07 18:05:16 +08:00

153 lines
4.2 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'
import { isPublicPath, requireLogin } from '@/common/checkLogin.js'
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
// 路由分发:全局拦截器注册(首页 onLoad 中处理首次引导页跳转)
const token = uni.getStorageSync('token') || ''
console.log('App 启动hasToken=' + !!token + '')
// 全局 navigateTo 拦截器:未登录用户访问需登录页面时自动跳转登录页
uni.addInterceptor('navigateTo', {
invoke(args) {
const t = uni.getStorageSync('token')
if (!t && !isPublicPath(args.url)) {
// 使用 requireLogin 统一处理,自带防重复锁
requireLogin({ message: '请先登录', redirect: true })
return false
}
}
})
})
onShow((options) => {
console.log('App Show', options)
// 处理从小程序返回的支付结果
if (options && options.referrerInfo && options.referrerInfo.extraData) {
const extraData = options.referrerInfo.extraData
console.log('小程序返回数据:', extraData)
if (extraData.retCode === 'SUCCESS') {
// 支付成功:返回缴费列表页并刷新数据
uni.showToast({ title: '支付成功', icon: 'success' })
setTimeout(() => {
uni.navigateTo({
url: '/pageSubPack/payment-list/paymentIndex'
})
}, 1500)
return
} else if (extraData.retCode === 'FAIL') {
uni.showToast({ title: extraData.retMsg || '支付失败', icon: 'none' })
return
}
}
// #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>