Files
property-manager-side/wuye-manager-uniapp/App.vue
2026-08-07 16:36:05 +08:00

253 lines
5.4 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
let pushCIDFetching = false // 防止并发请求
// 应用启动时执行
onLaunch(() => {
console.log('App 启动')
uni.onPushMessage((res) => {
console.log('收到推送消息:', res) // 监听推送消息
})
// #ifdef APP-PLUS
// 延迟 2 秒等推送服务完全就绪后再获取 CID
setTimeout(() => {
getPushCID()
}, 2000)
// #endif
})
onShow(() => {
console.log('App Show')
// #ifdef APP-PLUS
// 从后台恢复时,如果 CID 已经获取过且未过期,不再重复请求
const existingCid = uni.getStorageSync('push_cid')
if (existingCid) {
console.log('已有缓存 CID,跳过重复获取')
return
}
pushCIDRetryCount = 0
setTimeout(() => {
refreshPushCID()
}, 1000)
// #endif
})
onHide(() => {
console.log('App Hide')
pushCIDFetching = false
if (retryTimer) {
clearTimeout(retryTimer)
retryTimer = null
}
})
// 刷新 CID(每次 onShow 调用,比对更新)
function refreshPushCID() {
if (pushCIDFetching) return
const oldCid = uni.getStorageSync('push_cid') || ''
pushCIDFetching = true
uni.getPushClientId({
success: res => {
pushCIDFetching = false
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 => {
pushCIDFetching = false
console.error('获取推送 CID 失败', err)
scheduleRetry()
}
})
}
// 启动时首次获取 CID(重试更积极)
function getPushCID() {
if (pushCIDFetching) return
pushCIDFetching = true
uni.getPushClientId({
success: res => {
pushCIDFetching = false
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 => {
pushCIDFetching = false
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>