Files
property-manager-side/wuye-manager-uniapp/App.vue
2026-07-14 10:06:31 +08:00

239 lines
5.0 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 pushCIDRetryCount = 0
const MAX_RETRY = 5
let retryTimer = null
// 应用启动时执行
onLaunch(() => {
console.log('App 启动')
uni.onPushMessage((res) => {
console.log('收到推送消息:', res) // 监听推送消息
})
// #ifdef APP-PLUS
// 只有 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
}
})
// 刷新 CID(每次 onShow 调用,比对更新)
function refreshPushCID() {
const oldCid = uni.getStorageSync('push_cid') || ''
uni.getPushClientId({
success: res => {
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">
@import "@/uni_modules/uview-plus/index.scss";
</style>
<style>
/*每个页面公共css */
/* 全局清除默认样式 */
/* 通用flex布局 */
.flex {
display: flex;
}
.flex-center {
display: flex;
align-items: center;
justify-content: center;
}
.flex-between {
display: flex;
align-items: center;
justify-content: space-between;
}
.flex-around {
display: flex;
align-items: center;
justify-content: space-around;
}
.flex-col {
flex-direction: column;
}
.items-center {
align-items: center;
}
.justify-center {
justify-content: center;
}
.justify-around {
justify-content: space-around;
}
.justify-between {
justify-content: space-between;
}
.justify-evenly {
justify-content: space-evenly;
}
.flex-1 {
flex: 1;
}
.h-full {
height: 100%;
}
.w-full {
width: 100%;
}
.overflow-hidden {
overflow: hidden;
}
.overflow-auto {
overflow: auto;
}
.nodata {
color: #ccc;
text-align: center;
margin-top: 200rpx;
letter-spacing: 1rpx;
}
/* 自定义导航栏专用 */
.custom-header {
height: calc(var(--status-bar-height) + 88rpx);
padding-top: var(--status-bar-height);
}
/* 全局安全区样式 - 微信小程序完美支持 */
.safe-area-top {
padding-top: constant(safe-area-inset-top);
padding-top: env(safe-area-inset-top);
}
.safe-area-bottom {
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
}
.page_container {
min-height: 100vh;
max-height: 100vh;
height: 100%;
background: repeating-linear-gradient(180deg, #e2f0ff, #f6faff);
overflow: hidden;
box-sizing: border-box;
padding: 0 40rpx;
/* 先写 constant() 兼容 iOS < 11.2,再写 env() */
padding-top: calc(constant(safe-area-inset-top) + 20rpx);
padding-top: calc(env(safe-area-inset-top) + 20rpx);
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
}
.page_padding {
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
display: flex;
flex-direction: column;
width: 100%;
min-height: 100vh;
max-height: 100vh;
overflow: hidden;
box-sizing: border-box;
}
/* 单行省略 */
.ellipsis {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
/* 2行省略 */
.ellipsis-2 {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
/* 1 电表 2水表 */
</style>