guoyu/fronted_uniapp/App.vue
2025-12-03 18:58:36 +08:00

106 lines
3.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>
import config from '@/utils/config.js'
const APP_DEV_HOST = '192.168.0.106'
const APP_DEV_PORT = 30091
export default {
onLaunch: function() {
console.log('App Launch')
// 设置状态栏高度 CSS 变量
this.setStatusBarHeight()
// App环境配置服务器地址生产环境使用生产服务器
// #ifdef APP-PLUS
const { serverHost, serverPort } = config.getServerConfig()
// 生产环境:强制使用生产服务器地址
const PROD_HOST = '192.168.0.106'
const PROD_PORT = 30091
if (serverHost !== PROD_HOST || serverPort !== PROD_PORT) {
// 强制设置为生产服务器地址
console.log(`✅ 配置生产服务器地址:${PROD_HOST}:${PROD_PORT}`)
config.setServerConfig(PROD_HOST, PROD_PORT)
} else {
console.log(`当前服务器地址:${serverHost}:${serverPort}`)
}
// 等待 plus 对象初始化
this.waitForPlusReady()
// #endif
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
},
methods: {
setStatusBarHeight() {
try {
const systemInfo = uni.getSystemInfoSync()
const statusBarHeight = systemInfo.statusBarHeight || 0
// 将状态栏高度转换为 rpx假设设计稿宽度为 750rpx
const statusBarHeightRpx = (statusBarHeight * 750) / systemInfo.windowWidth
// #ifdef H5
// H5 环境:设置 CSS 变量
if (typeof document !== 'undefined') {
document.documentElement.style.setProperty('--status-bar-height', statusBarHeightRpx + 'rpx')
}
// #endif
// #ifdef APP-PLUS
// APP 环境uni-app 会自动处理,但也可以手动设置
// 这里主要确保变量有默认值
// #endif
console.log('状态栏高度:', statusBarHeight, 'rpx:', statusBarHeightRpx)
} catch (e) {
console.error('获取状态栏高度失败:', e)
}
},
// #ifdef APP-PLUS
waitForPlusReady() {
// 检查 plus 对象是否已可用
if (typeof plus !== 'undefined') {
console.log('✅ App启动时 plus 对象已可用')
return
}
// 监听 plusready 事件
// 注意:在 App 环境中,可能需要使用 window.addEventListener
const checkPlus = () => {
if (typeof plus !== 'undefined') {
console.log('✅ plus 对象已初始化')
return
}
// 继续等待
setTimeout(checkPlus, 100)
}
// 开始检查
setTimeout(checkPlus, 100)
// 如果 5 秒后仍未初始化,输出警告
setTimeout(() => {
if (typeof plus === 'undefined') {
console.warn('⚠️ App启动5秒后 plus 对象仍未初始化')
console.warn('⚠️ 这可能会影响截图功能')
}
}, 5000)
}
// #endif
}
}
</script>
<style>
/*每个页面公共css */
page {
/* 默认状态栏高度,如果 JavaScript 设置失败则使用此值 */
/* iOS 默认 44pxAndroid 默认 24px转换为 rpx 约为 44rpx */
--status-bar-height: 44rpx;
}
</style>