2026-09-20 22:57:22 +08:00

152 lines
6.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 { guardCurrentPage } from './core/nav-guard'
// rem 基准:以 1920×1280 设计稿为准,100px = 1rem,按宽高取小(contain)缩放
const DESIGN_WIDTH = 1920
const DESIGN_HEIGHT = 1280
const BASE_FONT_SIZE = 100
// 整体缩放系数:<1 整体缩小,>1 整体放大(布局偏大就调小这个值)
// 0.45 平板触摸尺寸基准偏大;桌面 H5/Electron(1920×1080)统一调整为 0.42(0.28 基础上整体放大 50%)
// 真机 APP-PLUS 的 rem 由下方全局纯 CSS 承担(2.5vw,框架锁死 16px 基础上整体放大 50% → 24px),JS 仅 H5 参与计算
const REM_SCALE = 0.42
// #ifdef APP-PLUS
// 打开应用后将系统音量调至最大(仓库环境语音播报需要;setStreamVolume 无需额外权限)
function setSystemVolumeMax () {
try {
const main = plus.android.runtimeMainActivity()
const Context = plus.android.importClass('android.content.Context')
// Context.AUDIO_SERVICE 常量值为 "audio",访问失败时兜底用字符串
const serviceName = Context.AUDIO_SERVICE || 'audio'
const audioManager = main.getSystemService(serviceName)
const streams = {
music: 3, // STREAM_MUSIC 媒体音量(语音播报 innerAudioContext 走此通道)
system: 1, // STREAM_SYSTEM 系统音量
notification: 5, // STREAM_NOTIFICATION 通知音量
alarm: 4, // STREAM_ALARM 闹钟音量
ring: 2 // STREAM_RING 铃声音量
}
for (const name in streams) {
// Native.js 桥接对象在 service 层不支持点方法调用,必须用 plus.android.invoke
const max = plus.android.invoke(audioManager, 'getStreamMaxVolume', streams[name])
plus.android.invoke(audioManager, 'setStreamVolume', streams[name], max, 0)
}
} catch (e) {
// 音量设置失败不阻断启动
}
}
// #endif
function logScreenInfo () {
try {
const info = uni.getSystemInfoSync()
console.log('[screen] uni =', JSON.stringify({
screenWidth: info.screenWidth,
screenHeight: info.screenHeight,
windowWidth: info.windowWidth,
windowHeight: info.windowHeight,
pixelRatio: info.pixelRatio,
platform: info.platform,
statusBarHeight: info.statusBarHeight,
safeArea: info.safeArea
}))
} catch (e) {
console.log('[screen] uni 失败 =', JSON.stringify(e))
}
// #ifdef APP-PLUS
try {
console.log('[screen] plus.screen =', JSON.stringify({
resolutionWidth: plus.screen.resolutionWidth,
resolutionHeight: plus.screen.resolutionHeight,
scale: plus.screen.scale,
dpiX: plus.screen.dpiX,
dpiY: plus.screen.dpiY
}))
} catch (e) {
console.log('[screen] plus.screen 失败 =', JSON.stringify(e))
}
// #endif
}
function setRemBase () {
// #ifdef H5
if (typeof document !== 'undefined' && document.documentElement) {
const docEl = document.documentElement
const width = docEl.clientWidth || window.innerWidth || DESIGN_WIDTH
const height = docEl.clientHeight || window.innerHeight || DESIGN_HEIGHT
const scale = Math.min(width / DESIGN_WIDTH, height / DESIGN_HEIGHT)
docEl.style.fontSize = (scale * BASE_FONT_SIZE * REM_SCALE) + 'px'
console.log('[rem] html fontSize =', docEl.style.fontSize, 'width =', width, 'height =', height, 'scale =', scale)
console.log('[screen-h5] screenWidth =', window.screen.width, 'screenHeight =', window.screen.height,
'availWidth =', window.screen.availWidth, 'availHeight =', window.screen.availHeight,
'devicePixelRatio =', window.devicePixelRatio)
console.log('[viewport-h5] clientWidth =', width, 'clientHeight =', height,
'innerWidth =', window.innerWidth, 'innerHeight =', window.innerHeight)
}
// #endif
logScreenInfo()
}
export default {
onLaunch: function () {
console.log('App Launch')
setRemBase()
// #ifdef APP-PLUS
setSystemVolumeMax()
// #endif
// #ifdef H5
window.addEventListener('resize', setRemBase)
// #endif
},
onShow: function () {
console.log('App Show')
setRemBase()
// 页面级守卫:覆盖直接打开/刷新进入受保护页面的场景(跳转 API 已由 installNavGuard 接管)
guardCurrentPage()
},
onHide: function () {
console.log('App Hide')
},
}
</script>
<style>
/*每个页面公共css */
/* rem 基准:设计稿 1920×1280,100px = 1rem(min(w/1920, h/1280) × 100)
H5/Electron:由 setRemBase()(JS,REM_SCALE=0.42)以内联样式运行时计算,此处仅首帧兜底;
真机 APP-PLUS:由下方 #ifdef APP-PLUS 的 !important 规则承担(见下) */
html {
font-size: min(5.2734375vw, 7.91015625vh);
}
/* #ifdef APP-PLUS */
/* 真机修复:uni-app 框架(uni-app-view.umd.js)运行时把 html font-size 以内联样式锁为
窗口宽度/23.4375;真机 innerWidth=961 刚好超过框架 rpxCalcMaxDeviceWidth=960 →
走 base 375 → 16px。内联样式优先级高于普通样式表,故上面的 CSS 对真机无效。
用 !important 让样式表反超非 important 的内联样式。
目标:在框架锁死的 16px 基础上整体放大 50% → 24px = 2.5vw。
横屏锁定 960×640(3:2) 与设计稿 1920×1280(3:2) 同比例,vw 与 min(vw,vh) 等价;
老 WebView 可能不支持 CSS min(),故直接用 vw。 */
html {
font-size: 2.5vw !important;
}
/* #endif */
/* H5/Electron 下 picker 下拉弹窗宽度与触发下拉框一致(高特异性覆盖库默认 300px),阴影调淡 */
uni-picker .uni-picker-container .uni-picker-custom,
.uni-picker-container .uni-picker-custom.uni-picker-custom {
width: 6.656rem !important;
box-shadow: 0 0 0.4rem 0.1rem rgba(0, 0, 0, 0.1) !important;
}
/* H5/Electron 下 picker 下拉选项居左显示(覆盖库默认居中 text-align:center) */
uni-picker .uni-picker-container.uni-selector-select .uni-picker-select .uni-picker-item,
.uni-picker-container.uni-selector-select .uni-picker-select .uni-picker-item,
uni-picker .uni-picker-container .uni-picker-view-content .uni-picker-item,
.uni-picker-container .uni-picker-view-content .uni-picker-item {
text-align: left;
padding-left: 0.512rem;
}
</style>