Commit bac20d86 by liimin

feat(adapter): 支持平板竖屏模式并优化横竖屏切换策略

- 新增平板竖屏分辨率适配配置(768x1174)及对应样式
- 修改 manifest 允许横竖屏切换,解锁平板设备方向限制
- 重构方向锁定策略:手机强制竖屏,平板/大屏设备允许自由旋转
- 优化布局检测逻辑,根据设备类型和当前方向动态选择布局模式
- 改进分辨率匹配机制,支持近似分辨率匹配
- 在 LayoutRoot 组件中增加轮询以实时同步分辨率变化
- 调整首页框架组件样式,适配平板竖屏下的底部导航栏
1 parent cc1177ba
<script lang="uts">
import { updateResolutionConfig } from "@/services/adapter/index.uts";
// #ifdef APP
import { layoutService } from "@/services/LayoutService.uts";
import { lockOrientation } from "@/utils/plus-screen.uts";
import { applyScreenOrientationLock } from "@/utils/apply-orientation-lock.uts";
import ws from "@/ws";
// #endif
......@@ -11,18 +10,9 @@ let firstBackTime = 0;
export default {
onLaunch () {
// #ifdef APP
// 必须先 init:未初始化时 currentMode 默认为 pad,会误判手机为平板并先锁横屏
layoutService.init();
const sys = uni.getSystemInfoSync();
console.log(`[App] 当前分辨率:${ sys.screenWidth } x ${ sys.screenHeight}`);
// 默认竖屏:锁定后延迟刷新屏幕尺寸,并重试锁定避免首屏被框架改向
lockOrientation("portrait-primary");
setTimeout(() => {
layoutService.refreshScreenInfo();
updateResolutionConfig();
}, 400);
setTimeout(() => { lockOrientation("portrait-primary"); }, 600);
setTimeout(() => { lockOrientation("portrait-primary"); }, 1200);
applyScreenOrientationLock();
// #endif
console.log("App Launch");
// 启动时仅做分辨率配置,避免后台定时器/录音模拟导致对象被长时间持有
......
......@@ -40,7 +40,8 @@
<!-- 竖屏手机:uView 底部 TabBar,图标沿用 static/images/tabbar -->
<view v-else
class="left-side left-side-portrait">
class="left-side left-side-portrait"
:class="[isPadPortraitMode ? 'left-side-portrait-pad' : '']">
<un-tabbar :model-value="currentTabIndex"
class="portrait-un-tabbar"
:fixed="false"
......@@ -49,20 +50,22 @@
active-color="#3773f8"
inactive-color="#8a98b5"
bg-color="#ffffff"
height="56"
:height="tabbarHeight"
@change="onPortraitTabChange(($event as string | number))">
<un-tabbar-item v-for="(item, index) in tabs"
:key="index"
:name="index"
:text="item.label"
icon-size="24">
:icon-size="tabbarIconSize">
<template #icon="{ active }">
<image v-if="active === true"
class="tabbar-icon-img"
:class="[isPadPortraitMode ? 'tabbar-icon-img-pad' : '']"
mode="aspectFit"
:src="getTabIconSrc(item.icon, true)" />
<image v-else
class="tabbar-icon-img"
:class="[isPadPortraitMode ? 'tabbar-icon-img-pad' : '']"
mode="aspectFit"
:src="getTabIconSrc(item.icon, false)" />
</template>
......@@ -81,6 +84,7 @@ import SettingBox from "@/pages/person/SettingBox.uvue";
const props = defineProps<{
isPortrait?: boolean
isPadPortrait?: boolean
tabIndex?: number
userInfo?: null | UTSJSONObject
}>();
......@@ -95,6 +99,18 @@ const isPortraitMode = computed((): boolean => {
return props.isPortrait === true;
});
const isPadPortraitMode = computed((): boolean => {
return props.isPadPortrait === true;
});
const tabbarHeight = computed((): string => {
return isPadPortraitMode.value ? "76" : "56";
});
const tabbarIconSize = computed((): string => {
return isPadPortraitMode.value ? "30" : "24";
});
const currentTabIndex = computed((): number => {
const idx = props.tabIndex as null | number;
return idx != null ? idx : 0;
......@@ -311,6 +327,15 @@ function jumpPersonCenter(): void {
height: 24px;
}
.tabbar-icon-img-pad {
width: 30px;
height: 30px;
}
.left-side-portrait-pad {
padding-bottom: 6px;
}
.portrait-top-row {
width: 100%;
display: flex;
......
<template>
<view class="home-frame-container"
:class="[modeClass, resolutionClass]">
:class="[modeClass, resolutionClass, adapterClass]">
<!-- 首页内容区 -->
<view v-if="!showDetail"
class="home-content"
......@@ -135,6 +135,7 @@
<!-- 竖屏:底部 TabBar;横屏:左侧栏 -->
<HomeFrameLeftSide v-if="isPortraitMode"
:is-portrait="true"
:is-pad-portrait="isPadPortraitMode"
:tab-index="tabIndex"
:user-info="userInfo"
@change-tab="onChangeTab" />
......@@ -163,6 +164,7 @@
import { computed, defineExpose, ref } from "vue";
import Entrance from "@/components/Entrance.uvue";
import { resolutionState, useResolutionConfig } from "@/services/adapter/index.uts";
import { layoutService } from "@/services/LayoutService.uts";
import { requestRecordPermission } from "@/utils/permission.uts";
import { storage } from "@/utils/storage.uts";
......@@ -186,6 +188,13 @@ const useCardLayout = computed((): boolean => {
return props.swiperCustom === true;
});
useResolutionConfig();
const adapterClass = computed((): string => {
const cls = resolutionState.cssClass;
return cls != null ? cls : "";
});
const modeClass = computed((): string => {
const modeRef = layoutService.getCurrentMode();
const m = modeRef.value;
......@@ -202,6 +211,10 @@ const isPortraitMode = computed((): boolean => {
return layoutService.isMode("portrait");
});
const isPadPortraitMode = computed((): boolean => {
return isPortraitMode.value && layoutService.isPadLikeDevice();
});
const showDetail = ref(false);
const userInfo = ref<UTSJSONObject>({});
const sceneType = ref<number>(1);
......@@ -489,6 +502,64 @@ defineExpose({
min-height: var(--l-tool-box-min-height);
}
.home-frame-container.layout-landscape-1174x768 .right-content-box {
padding: 6px 0 10px;
}
.home-frame-container.layout-landscape-1174x768 .module-box {
height: 42%;
min-height: 216px;
max-height: 260px;
margin-bottom: 14px;
}
.home-frame-container.layout-landscape-1174x768 .tool-box {
flex: 1;
min-height: 286px;
}
.home-frame-container.layout-landscape-1174x768 .tool-box .content-box {
min-height: 0;
padding-bottom: 14px;
}
.home-frame-container.layout-portrait-768x1174 .right-content-box {
padding: 10px 0 0;
}
.home-frame-container.layout-portrait-768x1174 .module-box {
height: 56%;
min-height: 380px;
max-height: 560px;
margin-bottom: 12px;
}
.home-frame-container.layout-portrait-768x1174 .content-left {
justify-content: flex-start;
}
.home-frame-container.layout-portrait-768x1174 .tool-box {
flex: 1;
min-height: 180px;
max-height: 260px;
}
.home-frame-container.layout-portrait-768x1174 .tool-box .content-box {
flex: 1;
min-height: 0;
overflow-y: auto;
padding: 6px 12px 2px;
display: flex;
flex-direction: column;
justify-content: center;
}
.home-frame-container.layout-portrait-768x1174 .tool-box .content-box .entrance-list {
min-height: 100%;
align-content: center;
justify-content: center;
}
.common-title {
height: var(--l-common-title-height);
display: flex;
......
......@@ -5,9 +5,9 @@
</template>
<script setup lang="uts">
import { computed, type Ref } from 'vue'
import { computed, onMounted, onUnmounted, type Ref } from 'vue'
import { useLayout } from '@/composables/useLayout'
import { resolutionState, useResolutionConfig } from '@/services/adapter/index.uts'
import { resolutionState, updateResolutionConfig, useResolutionConfig } from '@/services/adapter/index.uts'
import { layoutService } from '@/services/LayoutService.uts'
const layout = useLayout()
......@@ -17,12 +17,38 @@ const styleObj = computed((): UTSJSONObject => {
const r = rawCssVars
if (r == null) return {}
const v = r.value
console.log(11111111111,v != null ? v : {})
return v != null ? v : {}
})
useResolutionConfig()
const LAYOUT_ROOT_POLL_MS = 350
let layoutRootPollId: number | null = null
let lastLayoutW = 0
let lastLayoutH = 0
function syncLayoutRootResolution(): void {
const info = uni.getWindowInfo()
let w = info.windowWidth
let h = info.windowHeight
if (w == 0 && info.screenWidth > 0) {
w = info.screenWidth
if (info.screenHeight > 0) {
h = info.screenHeight
}
}
if (w == 0 || h == 0) {
return
}
if (w == lastLayoutW && h == lastLayoutH) {
return
}
lastLayoutW = w
lastLayoutH = h
layoutService.refreshScreenInfo()
updateResolutionConfig()
}
const adapterClass = computed((): string => {
const cls = resolutionState.cssClass
return cls != null ? cls : ''
......@@ -31,15 +57,26 @@ const adapterClass = computed((): string => {
const modeClass = computed((): string => {
const modeRef = layoutService.getCurrentMode()
const m = modeRef.value
console.log(22222222222,m == 'portrait' ? 'mode-portrait' : 'mode-landscape')
return m == 'portrait' ? 'mode-portrait' : 'mode-landscape'
})
const resolutionClass = computed((): string => {
const bucket = layoutService.getResolutionBucket()
console.log(333333333333333,`res-${bucket}`)
return `res-${bucket}`
})
onMounted(() => {
syncLayoutRootResolution()
layoutRootPollId = setInterval(syncLayoutRootResolution, LAYOUT_ROOT_POLL_MS)
})
onUnmounted(() => {
const tid = layoutRootPollId
if (tid != null) {
clearInterval(tid)
layoutRootPollId = null
}
})
</script>
<style scoped>
......
......@@ -40,7 +40,8 @@
}
},
"orientation": [
"portrait-primary"
"portrait-primary",
"landscape-primary"
]
}
},
......@@ -67,7 +68,8 @@
"default": {}
},
"orientation": [
"portrait-primary"
"portrait-primary",
"landscape-primary"
],
"permissions": [
"<uses-feature android:name=\"android.hardware.camera\"/>",
......@@ -94,7 +96,8 @@
"icons": {},
"splashScreens": {},
"orientation": [
"portrait-primary"
"portrait-primary",
"landscape-primary"
]
}
},
......
.layout-landscape-1920x1080 .equity-page {
:deep(.layout-landscape-1920x1080) .equity-page {
.swiper-custom-content {
border-radius: 24px;
}
......@@ -101,7 +101,7 @@
}
}
.layout-landscape-1174x768 .equity-page {
:deep(.layout-landscape-1174x768) .equity-page {
.swiper-custom-content {
border-radius: 18px;
background: rgba(255, 255, 255, 0.9);
......@@ -168,10 +168,6 @@
font-size: 14px;
}
:deep(.right-content-box) {
padding-top: 6px;
}
:deep(.module-box .common-title),
:deep(.tool-box .common-title) {
height: 52px;
......@@ -183,10 +179,6 @@
font-size: 17px;
}
:deep(.module-box) {
margin-bottom: 12px;
}
:deep(.tool-box .content-box) {
padding: 8px 12px 10px;
}
......@@ -206,7 +198,7 @@
}
}
.layout-landscape-common .equity-page {
:deep(.layout-landscape-common) .equity-page {
.swiper-custom-content {
border-radius: 16px;
background: rgba(255, 255, 255, 0.88);
......
.layout-portrait-common .equity-page {
:deep(.layout-portrait-768x1174) .equity-page {
:deep(.start) {
height: 128px;
border-radius: 24px;
margin-bottom: 4px;
}
.equity-hero-head {
height: 52px;
padding: 0 14px;
}
.hero-title {
font-size: 18px;
line-height: 24px;
}
:deep(.module-box .common-title) {
height: 56px;
padding-left: 18px;
}
:deep(.module-box .title-text) {
font-size: 18px;
}
:deep(.tool-box .content-box .entrance-item) {
margin-bottom: 14px;
}
}
:deep(.layout-portrait-common) .equity-page {
.swiper-custom-content {
border-radius: 14px;
background: rgba(255, 255, 255, 0.92);
}
.equity-hero-head {
height: 44px;
padding: 0 12px;
}
.hero-eyebrow {
font-size: 11px;
line-height: 13px;
}
.hero-title {
font-size: 16px;
line-height: 20px;
}
.hero-indicator-pill {
height: 24px;
min-width: 48px;
padding: 0 10px;
border-radius: 14px;
}
.hero-indicator-text {
font-size: 12px;
}
.equity-swiper,
.equity-swiper-overlay {
border-radius: 10px;
}
.dot {
width: 6px;
height: 6px;
margin: 0 3px;
}
.dot.active {
width: 16px;
}
.empty-card {
width: 78%;
min-height: 126px;
border-radius: 16px;
}
.empty-title {
font-size: 18px;
}
.empty-text {
font-size: 13px;
}
:deep(.right-content-box) {
padding: 6px 0 calc(var(--l-spacing-sm) + 8px);
}
:deep(.module-box .common-title),
:deep(.tool-box .common-title) {
height: 48px;
padding-left: 16px;
}
:deep(.module-box .title-text),
:deep(.tool-box .title-text) {
font-size: 16px;
}
:deep(.module-box) {
margin-bottom: 10px;
}
:deep(.tool-box .content-box) {
padding: 8px 10px 10px;
}
:deep(.tool-box .content-box .entrance-item) {
width: 31.5%;
margin-bottom: 12px;
}
:deep(.tool-box .content-box .entrance-icon-compact) {
width: 42px;
height: 42px;
}
:deep(.tool-box .content-box .entrance-name-compact) {
font-size: 12px;
margin-top: 4px;
}
}
......@@ -109,6 +109,15 @@ class LayoutService {
}
/**
* 是否为 Pad/大屏设备(与当前横竖方向无关)
* - 用于方向锁定策略判断:Pad 允许旋转,手机强制竖屏
*/
isPadLikeDevice(): boolean {
const maxDim = Math.max(this.screenWidth.value, this.screenHeight.value);
return maxDim >= 1024;
}
/**
* 刷新屏幕信息并重新计算(如锁定横屏后需延迟调用以拿到旋转后的尺寸)
*/
refreshScreenInfo(): void {
......@@ -130,21 +139,23 @@ class LayoutService {
}
/**
* 检测布局模式(按设备物理尺寸区分)
* - 大屏:物理长边 >= 1440,用 desktop
* - 平板:物理长边 >= 1024,用 pad 布局(横屏);用长边判断避免 pad 竖屏时被误判为手机
* - 手机:物理长边 < 1024,用 portrait 布局(竖屏)
* 检测布局模式(按设备尺寸 + 当前方向)
* - 手机:物理长边 < 1024,始终 portrait
* - Pad/大屏:竖向时使用 portrait,横向时按尺寸落到 pad/desktop
*/
private detectMode(): LayoutMode {
const maxDim = Math.max(this.screenWidth.value, this.screenHeight.value);
const w = this.screenWidth.value;
const h = this.screenHeight.value;
const maxDim = Math.max(w, h);
const isPortraitNow = h >= w;
if (maxDim >= 1440) {
return "desktop";
if (maxDim < 1024) {
return "portrait";
}
if (maxDim >= 1024) {
return "pad";
if (isPortraitNow) {
return "portrait";
}
return "portrait";
return maxDim >= 1440 ? "desktop" : "pad";
}
/**
......
......@@ -4,6 +4,16 @@ import type { AppResolutionConfig, ResponsiveImage } from "./types/index.uts";
import { initRegistry, landscapeMap, portraitMap } from "./registry.uts";
import { resolutionState } from "./store.uts";
function isNearResolution(
w: number,
h: number,
tw: number,
th: number,
tolerance: number
): boolean {
return Math.abs(w - tw) <= tolerance && Math.abs(h - th) <= tolerance;
}
// ========================================================================
// 核心适配逻辑
// ========================================================================
......@@ -37,6 +47,45 @@ export function updateResolutionConfig(): void {
let matchType = "exact";
if (target == null) {
if (isLandscape) {
const near1174 = "1174x768";
if (isNearResolution(Math.round(w), Math.round(h), 1174, 768, 64)) {
const cfg1174 = configMap.get(near1174);
if (cfg1174 != null) {
target = cfg1174;
usedKey = near1174;
cssClass = `layout-${prefix}-${near1174}`;
matchType = "near-1174x768";
}
}
if (target == null) {
const near1920 = "1920x1080";
if (isNearResolution(Math.round(w), Math.round(h), 1920, 1080, 96)) {
const cfg1920 = configMap.get(near1920);
if (cfg1920 != null) {
target = cfg1920;
usedKey = near1920;
cssClass = `layout-${prefix}-${near1920}`;
matchType = "near-1920x1080";
}
}
}
} else {
const nearPortrait = "768x1174";
if (isNearResolution(Math.round(w), Math.round(h), 768, 1174, 64)) {
const cfg768 = configMap.get(nearPortrait);
if (cfg768 != null) {
target = cfg768;
usedKey = nearPortrait;
cssClass = `layout-${prefix}-${nearPortrait}`;
matchType = "near-768x1174";
}
}
}
}
if (target == null) {
target = configMap.get('common');
usedKey = `${prefix}/common`;
cssClass = `layout-${prefix}-common`;
......
import { DemoPageConfig, ResponsiveImage } from "../../types/index.uts";
export const demoConfig : DemoPageConfig = {
paddingLeft: "24rpx",
paddingRight: "24rpx",
heroImageWidth: "100%",
heroImageSrc: {
x1: "/static/images/portrait/common/study-hero.png",
x2: "/static/images/portrait/common/study-hero@2x.png"
} as ResponsiveImage,
titleFontSize: "32rpx"
};
import { AppResolutionConfig } from "../../types/index.uts";
import { demoConfig } from "./demo.uts";
export const config : AppResolutionConfig = {
demo: demoConfig
};
......@@ -5,6 +5,7 @@
import { config as ls_1174 } from "./landscape/1174x768/index.uts";
import { config as ls_1920 } from "./landscape/1920x1080/index.uts";
import { config as ls_common } from "./landscape/common/index.uts";
import { config as pt_768x1174 } from "./portrait/768x1174/index.uts";
import { config as pt_common } from "./portrait/common/index.uts";
import type { AppResolutionConfig } from "./types/index.uts";
......@@ -23,6 +24,7 @@ const landscapeConfigs = {
} as UTSJSONObject;
const portraitConfigs = {
"768x1174": pt_768x1174,
"common": pt_common
} as UTSJSONObject;
......
/**
* 统一应用竖屏锁定
* 供 App.onShow 与全局 mixin onShow 调用;锁向后延迟刷新屏幕尺寸以同步分辨率适配
* 统一应用屏幕方向策略
* - pad/desktop:允许横竖自由切换(不强制锁向)
* - 手机:强制竖屏
* 供 App.onLaunch 与全局 mixin onShow 调用;方向策略应用后延迟刷新屏幕尺寸并同步分辨率适配
*/
// #ifdef APP
import { layoutService } from '@/services/LayoutService.uts'
import { lockOrientation } from '@/utils/plus-screen.uts'
import { lockOrientation, unlockOrientation } from '@/utils/plus-screen.uts'
import { updateResolutionConfig } from '@/services/adapter/index.uts'
// #endif
// 防抖:仅保留最后一次延迟刷新,避免快速切页时多次刷新
var refreshTimer: number = 0
var retryTimer: number = 0
function getCurrentMaxDim(): number {
const systemInfo = uni.getSystemInfoSync()
let w = systemInfo.screenWidth
let h = systemInfo.screenHeight
if (w == 0 || h == 0) {
const winInfo = uni.getWindowInfo()
if (w == 0) {
w = winInfo.windowWidth
}
if (h == 0) {
h = winInfo.windowHeight
}
}
return Math.max(w, h)
}
function scheduleRefresh(delayMs: number): void {
if (refreshTimer != 0) {
clearTimeout(refreshTimer)
}
refreshTimer = setTimeout((): void => {
layoutService.refreshScreenInfo()
updateResolutionConfig()
refreshTimer = 0
}, delayMs)
}
export function applyScreenOrientationLock(): void {
// #ifdef APP
if (!layoutService.isInitialized()) {
if (layoutService.isInitialized() == false) {
layoutService.init()
}
if (layoutService.isMode('pad') || layoutService.isMode('desktop')) {
lockOrientation('landscape-primary')
// 某些机型/框架在页面切换后会把方向“抖回去”,这里再补一次锁定
if (retryTimer != 0) {
clearTimeout(retryTimer)
}
retryTimer = setTimeout((): void => {
lockOrientation('landscape-primary')
retryTimer = 0
}, 600)
if (refreshTimer != 0) {
clearTimeout(refreshTimer)
}
refreshTimer = setTimeout((): void => {
layoutService.refreshScreenInfo()
updateResolutionConfig() // 同步更新分辨率适配配置
refreshTimer = 0
}, 800)
const maxDim = getCurrentMaxDim()
const isPadLike = maxDim >= 1024 || layoutService.isPadLikeDevice()
console.log(`[orientation-lock] maxDim=${maxDim}, isPadLike=${isPadLike}`)
if (isPadLike) {
unlockOrientation()
scheduleRefresh(250)
scheduleRefresh(750)
} else {
lockOrientation('portrait-primary')
scheduleRefresh(250)
scheduleRefresh(650)
}
// #endif
}
......@@ -37,9 +37,16 @@ export function lockOrientation(orientation: string): void {
* 解锁屏幕方向
*/
export function unlockOrientation(): void {
// #ifdef APP
console.log("[plus-screen] 解锁屏幕方向");
console.warn("[plus-screen] uni-app x 中屏幕方向建议在 manifest.json 或页面配置中设置");
// #ifdef APP-ANDROID
const activity = UTSAndroid.getUniActivity()
if (activity != null) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
console.log("[plus-screen] 已解锁屏幕方向")
}
// #endif
// #ifdef APP-IOS
console.log("[plus-screen] iOS 解锁屏幕方向需在 manifest 或页面配置中设置")
// #endif
// #ifndef APP
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!