show-rule-context.js 1.01 KB
/** childrenShowRules 保留上下文 key 解析。 */

function normalizeContextArray(values) {
	const list = Array.isArray(values) ? values : [values]
	return [...new Set(
		list
			.filter(value => value != null && value !== '')
			.map(value => String(value))
	)]
}

export function resolveShowRuleSceneCodes(pageContext = {}) {
	const sceneCodes = normalizeContextArray(
		(pageContext.sceneList || []).map(item => item?.baseSceneCode)
	)
	if (sceneCodes.length) {
		return sceneCodes
	}
	return normalizeContextArray(pageContext.baseSceneCode)
}

export function resolveShowRuleBrandIds(pageContext = {}) {
	return normalizeContextArray(pageContext.brandIds || [])
}

export function resolveShowRuleContextValue(key = '', pageContext = {}) {
	if (key === '$sceneCodes') {
		const sceneCodes = resolveShowRuleSceneCodes(pageContext)
		return sceneCodes.length ? sceneCodes : null
	}
	if (key === '$brandIds') {
		const brandIds = resolveShowRuleBrandIds(pageContext)
		return brandIds.length ? brandIds : null
	}
	return undefined
}