Commit 3f2bbaa5 by liimin

refactor(screnVars): 重构屏幕变量处理逻辑

- 使用 OptionalVars 类型替代 UTSJSONObject | null
- 引入 getAsObject 和 merge 工具函数简化对象操作
- 优化全局和页面变量的合并逻辑,提升代码可读性
- 移除调试用的 console.log 语句
- 精简 watch 监听器的写法
1 parent 95707f52
import { type Ref, watch, ref } from 'vue' import { type Ref, watch, ref } from 'vue'
import { useOrientation, updateOrientation } from './useOrientation' import { useOrientation, updateOrientation } from './useOrientation'
import { globalScreenVars } from '../constants/screen.vars.global' import { globalScreenVars } from '../constants/screen.vars.global'
import { merge, getAsObject } from '@/utils/common'
import type { OptionalVars } from '@/types'
export function useScreenVars(pageVars: UTSJSONObject | null) { export function useScreenVars(pageVars: OptionalVars) {
// 确保方向信息已更新 // 确保方向信息已更新
updateOrientation() updateOrientation()
...@@ -13,113 +15,49 @@ export function useScreenVars(pageVars: UTSJSONObject | null) { ...@@ -13,113 +15,49 @@ export function useScreenVars(pageVars: UTSJSONObject | null) {
// 使用 ref 包装,每次更新时替换整个对象 // 使用 ref 包装,每次更新时替换整个对象
const screenVarsValue = ref<UTSJSONObject>({}) const screenVarsValue = ref<UTSJSONObject>({})
/**
* 更新屏幕变量
*/
const updateScreenVars = (): void => { const updateScreenVars = (): void => {
const orient = orientationKey.value const orient = orientationKey.value
const resKey = resolutionKey.value const resKey = resolutionKey.value
const fullResKey = `${orient}-${resKey}` const fullResKey = `${orient}-${resKey}`
console.log('[useScreenVars] orient:', orient) // 全局配置
console.log('[useScreenVars] resKey:', resKey) const globalOrient = getAsObject(
console.log('[useScreenVars] fullResKey:', fullResKey) globalScreenVars,
orient == 'portrait' ? 'portrait' : 'landscape'
// 构建新的样式对象 )
const newValue: UTSJSONObject = {} const globalResolutions = getAsObject(globalScreenVars, `resolutions.${fullResKey}`)
// 合并 globalScreenVars.common // 页面配置
if (globalScreenVars.common != null) { let pageCommon: OptionalVars = null
const common = globalScreenVars.common as UTSJSONObject let pageOrient: OptionalVars = null
for (const key in common) { let pageResolutions: OptionalVars = null
newValue[key] = common[key]
} if (pageVars != null) {
} pageCommon = getAsObject(pageVars, 'common')
pageOrient = getAsObject(pageVars, orient)
// 合并 globalScreenVars[orient] - 使用条件判断 pageResolutions = getAsObject(pageVars, `resolutions.${fullResKey}`)
let globalOrient: any | null = null
if (orient == 'portrait') {
globalOrient = globalScreenVars.portrait
} else if (orient == 'landscape') {
globalOrient = globalScreenVars.landscape
}
if (globalOrient != null) {
const orientObj = globalOrient as UTSJSONObject
for (const key in orientObj) {
newValue[key] = orientObj[key]
}
}
// 合并 globalScreenVars.resolutions[fullResKey]
if (globalScreenVars.resolutions != null) {
const resolutions = globalScreenVars.resolutions as UTSJSONObject
const globalRes = resolutions[fullResKey]
if (globalRes != null) {
const globalResObj = globalRes as UTSJSONObject
for (const key in globalResObj) {
newValue[key] = globalResObj[key]
}
}
}
// 如果 pageVars 为 null,直接赋值
if (pageVars == null) {
console.log('[useScreenVars] pageVars is null')
screenVarsValue.value = newValue
return
} }
// 将 pageVars 转换为 UTSJSONObject // 合并配置(优先级从低到高)
const pageVarsObj = pageVars as UTSJSONObject screenVarsValue.value = merge(
globalScreenVars.common,
// 合并 pageVars.common globalOrient,
const pageCommon = pageVarsObj['common'] globalResolutions,
if (pageCommon != null) { pageCommon,
const commonObj = pageCommon as UTSJSONObject pageOrient,
for (const key in commonObj) { pageResolutions
newValue[key] = commonObj[key] )
}
}
// 合并 pageVars[orient]
const pageOrient = pageVarsObj[orient]
if (pageOrient != null) {
const orientObj = pageOrient as UTSJSONObject
for (const key in orientObj) {
newValue[key] = orientObj[key]
}
}
// 合并 pageVars.resolutions[fullResKey]
const pageResolutions = pageVarsObj['resolutions']
if (pageResolutions != null) {
const resolutionsObj = pageResolutions as UTSJSONObject
const pageRes = resolutionsObj[fullResKey]
if (pageRes != null) {
const pageResObj = pageRes as UTSJSONObject
for (const key in pageResObj) {
newValue[key] = pageResObj[key]
}
}
}
console.log('[useScreenVars] final newValue --adapter-color:', newValue['--adapter-color'])
// 替换整个对象
screenVarsValue.value = newValue
console.log('[useScreenVars] updated newValue newValue:', newValue)
} }
// 初始更新 // 初始更新
updateScreenVars() updateScreenVars()
// 监听方向变化 // 监听方向和分辨率变化
watch(orientationKey, (): void => { watch(orientationKey, updateScreenVars)
console.log('[useScreenVars] orientationKey changed') watch(resolutionKey, updateScreenVars)
updateScreenVars()
})
watch(resolutionKey, (): void => {
console.log('[useScreenVars] resolutionKey changed')
updateScreenVars()
})
return { return {
screenVars: screenVarsValue screenVars: screenVarsValue
......
/**
* 可选的 UTSJSONObject 类型
* 用于表示可能为 null 的对象变量
*/
export type OptionalVars = UTSJSONObject | null
export type UserInfo = { export type UserInfo = {
unionid: string; // 用户唯一id unionid: string; // 用户唯一id
id: number; // 用户id id: number; // 用户id
......
...@@ -206,6 +206,15 @@ export function get(object: any, path: string, defaultValue: any | null = null): ...@@ -206,6 +206,15 @@ export function get(object: any, path: string, defaultValue: any | null = null):
} }
/** /**
* 获取 UTSJSONObject 类型的属性值
* @example getAsObject(obj, 'key') // UTSJSONObject | null
*/
export function getAsObject(object: any, path: string): UTSJSONObject | null {
const value = get(object, path)
return value != null ? (value as UTSJSONObject) : null
}
/**
* 设置对象的属性值 * 设置对象的属性值
* @example set({a: 1}, 'b', 2) // {a: 1, b: 2} * @example set({a: 1}, 'b', 2) // {a: 1, b: 2}
*/ */
...@@ -427,6 +436,30 @@ export function assign(...items: any[]) { ...@@ -427,6 +436,30 @@ export function assign(...items: any[]) {
} }
/** /**
* 合并多个对象(支持 null 值,自动忽略)
* @param items 要合并的对象列表
* @returns 合并后的新对象
* @example merge({a: 1}, null, {b: 2}) // {a: 1, b: 2}
*/
export function merge(...items: Array<any | null>): UTSJSONObject {
// 创建目标对象
const target: UTSJSONObject = {}
// 逐个合并源对象
for (let i = 0; i < items.length; i++) {
const source = items[i]
if (source != null) {
const sourceObj = source as UTSJSONObject
for (const key in sourceObj) {
target[key] = sourceObj[key]
}
}
}
return target
}
/**
* 获取数组中指定索引的元素 * 获取数组中指定索引的元素
* @example nth([1, 2, 3], 1) // 2 * @example nth([1, 2, 3], 1) // 2
* @example nth([1, 2, 3], -1) // 3 * @example nth([1, 2, 3], -1) // 3
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!