164 lines
4.5 KiB
Vue
164 lines
4.5 KiB
Vue
<script setup>
|
||
import { onLaunch,onShow,onHide } from '@dcloudio/uni-app'
|
||
import { isPublicPath } 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
|
||
|
||
// 路由分发(同步):所有人先进首页,点击功能再提示登录
|
||
const token = uni.getStorageSync('token') || ''
|
||
const hasShowGuide = uni.getStorageSync('hasShowGuide')
|
||
|
||
// 首次使用:标记引导已看过(不再强制引导页),直接进首页
|
||
if (!token && !hasShowGuide) {
|
||
console.log('首次启动,标记引导已看过,进入首页')
|
||
uni.setStorageSync('hasShowGuide', true)
|
||
}
|
||
|
||
// 所有人统一进首页
|
||
console.log('进入首页(hasToken=' + !!token + ')')
|
||
|
||
// 全局 navigateTo 拦截器:未登录用户访问需登录页面时自动跳转登录页
|
||
uni.addInterceptor('navigateTo', {
|
||
invoke(args) {
|
||
const t = uni.getStorageSync('token')
|
||
if (!t && !isPublicPath(args.url)) {
|
||
uni.showToast({ title: '请先登录', icon: 'none' })
|
||
setTimeout(() => {
|
||
uni.navigateTo({ url: '/pageSubPack/loginSub/login?mode=pwd' })
|
||
}, 1000)
|
||
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('推送 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) => {
|
||
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>
|