screen-adapt.uts 4.72 KB
// utils/screen-adapt.uts
// 屏幕自适应布局工具函数
// 用于替代 CSS @media 查询,实现响应式布局

/**
 * 屏幕信息接口
 */
export interface ScreenInfo {
	width: number
	height: number
	pixelRatio: number
	screenWidth: number
	screenHeight: number
}

/**
 * 获取屏幕信息
 */
export function getScreenInfo(): ScreenInfo {
	const windowInfo = uni.getWindowInfo()
	return {
		width: windowInfo.windowWidth,
		height: windowInfo.windowHeight,
		pixelRatio: windowInfo.pixelRatio,
		screenWidth: windowInfo.screenWidth,
		screenHeight: windowInfo.screenHeight
	}
}

/**
 * 屏幕尺寸分类
 */
export type ScreenSize = 'small' | 'medium' | 'large' | 'xlarge'

/**
 * 获取屏幕尺寸分类
 * - small: < 480px(小屏手机)
 * - medium: 480px - 768px(常规手机)
 * - large: 768px - 1024px(大屏手机/小平板)
 * - xlarge: >= 1024px(平板、大屏设备)
 */
export function getScreenSize(): ScreenSize {
	const width = getScreenInfo().width
	if (width < 480) {
		return 'small'
	} else if (width < 768) {
		return 'medium'
	} else if (width < 1024) {
		return 'large'
	} else {
		return 'xlarge'
	}
}

/**
 * 响应式字体大小
 * @param baseSize 基础字体大小(设计稿 375px 下的值)
 * @param minSize 最小字体大小(px),默认 12
 * @param maxSize 最大字体大小(px),默认 48
 * @returns 计算后的字体大小(px)
 * 
 * @example
 * // 设计稿 375px,字体 14px
 * // 小屏(320px):12px(最小值)
 * // 常规(375px):14px(基准值)
 * // 大屏(768px):18px(按比例缩放)
 * // 超大屏(1024px):20px(最大值)
 * const fontSize = responsiveFontSize(14, 12, 20)
 */
export function responsiveFontSize(baseSize: number, minSize: number = 12, maxSize: number = 48): number {
	const screenInfo = getScreenInfo()
	const screenWidth = screenInfo.width
	
	// 设计稿基准宽度(通常为 375px 或 750px)
	// 如果设计稿是 750px,这里改为 750,baseSize 也需要相应调整
	const designWidth = 375
	
	// 计算缩放比例(限制在合理范围内)
	const scale = screenWidth / designWidth
	const scaledSize = baseSize * scale
	
	// 限制在最小值和最大值之间
	if (scaledSize < minSize) {
		return minSize
	} else if (scaledSize > maxSize) {
		return maxSize
	}
	
	return Math.round(scaledSize)
}

/**
 * 响应式间距
 * @param baseSpacing 基础间距(设计稿 375px 下的值)
 * @param minSpacing 最小间距(px),默认 4
 * @param maxSpacing 最大间距(px),默认 64
 * @returns 计算后的间距(px)
 * 
 * @example
 * // 设计稿 375px,间距 16px
 * // 小屏(320px):12px(最小值)
 * // 常规(375px):16px(基准值)
 * // 大屏(768px):24px(按比例缩放)
 * const padding = responsiveSpacing(16, 12, 24)
 */
export function responsiveSpacing(baseSpacing: number, minSpacing: number = 4, maxSpacing: number = 64): number {
	const screenInfo = getScreenInfo()
	const screenWidth = screenInfo.width
	const designWidth = 375
	
	const scale = screenWidth / designWidth
	const scaledSpacing = baseSpacing * scale
	
	if (scaledSpacing < minSpacing) {
		return minSpacing
	} else if (scaledSpacing > maxSpacing) {
		return maxSpacing
	}
	
	return Math.round(scaledSpacing)
}

/**
 * 响应式宽度(用于固定宽度元素)
 * @param baseWidth 基础宽度(设计稿 375px 下的值)
 * @returns 计算后的宽度(px)
 * 
 * @example
 * // 设计稿 375px,按钮宽度 200px
 * // 小屏(320px):171px
 * // 常规(375px):200px
 * // 大屏(768px):410px
 * const buttonWidth = responsiveWidth(200)
 */
export function responsiveWidth(baseWidth: number): number {
	const screenInfo = getScreenInfo()
	const screenWidth = screenInfo.width
	const designWidth = 375
	
	return Math.round(baseWidth * (screenWidth / designWidth))
}

/**
 * 判断是否为横屏
 */
export function isLandscape(): boolean {
	const screenInfo = getScreenInfo()
	return screenInfo.width > screenInfo.height
}

/**
 * 判断是否为平板尺寸
 */
export function isTablet(): boolean {
	const screenSize = getScreenSize()
	return screenSize === 'large' || screenSize === 'xlarge'
}

/**
 * 判断是否为手机尺寸
 */
export function isMobile(): boolean {
	const screenSize = getScreenSize()
	return screenSize === 'small' || screenSize === 'medium'
}

/**
 * 获取列数(用于列表布局)
 * @param mobileColumns 手机端列数,默认 1
 * @param tabletColumns 平板端列数,默认 2
 * @returns 当前屏幕对应的列数
 * 
 * @example
 * // 手机单列,平板两列
 * const columns = getColumns(1, 2)
 */
export function getColumns(mobileColumns: number = 1, tabletColumns: number = 2): number {
	return isTablet() ? tabletColumns : mobileColumns
}