Files
cmr-mini/miniprogram/game/experience/contentCard.ts

96 lines
2.9 KiB
TypeScript

export type ContentCardTemplate = 'minimal' | 'story' | 'focus'
export type ContentCardCtaType = 'detail' | 'photo' | 'audio' | 'quiz'
export interface ContentCardQuizConfig {
bonusScore: number
countdownSeconds: number
minValue: number
maxValue: number
allowSubtraction: boolean
}
export interface ContentCardCtaConfig {
type: ContentCardCtaType
label: string
quiz: ContentCardQuizConfig | null
}
export interface ContentCardCtaConfigOverride {
type?: ContentCardCtaType
label?: string
bonusScore?: number
countdownSeconds?: number
minValue?: number
maxValue?: number
allowSubtraction?: boolean
}
export interface ContentCardActionViewModel {
key: string
type: ContentCardCtaType
label: string
}
export const DEFAULT_CONTENT_CARD_QUIZ_CONFIG: ContentCardQuizConfig = {
bonusScore: 1,
countdownSeconds: 10,
minValue: 10,
maxValue: 999,
allowSubtraction: true,
}
export function buildDefaultContentCardCtaLabel(type: ContentCardCtaType): string {
if (type === 'detail') {
return '查看详情'
}
if (type === 'photo') {
return '拍照打卡'
}
if (type === 'audio') {
return '语音留言'
}
return '答题加分'
}
export function buildDefaultContentCardQuizConfig(
override?: ContentCardCtaConfigOverride | null,
): ContentCardQuizConfig {
const minValue = Number(override && override.minValue)
const maxValue = Number(override && override.maxValue)
return {
bonusScore: Number.isFinite(Number(override && override.bonusScore))
? Math.max(1, Math.round(Number(override && override.bonusScore)))
: DEFAULT_CONTENT_CARD_QUIZ_CONFIG.bonusScore,
countdownSeconds: Number.isFinite(Number(override && override.countdownSeconds))
? Math.max(5, Math.round(Number(override && override.countdownSeconds)))
: DEFAULT_CONTENT_CARD_QUIZ_CONFIG.countdownSeconds,
minValue: Number.isFinite(minValue)
? Math.max(10, Math.round(minValue))
: DEFAULT_CONTENT_CARD_QUIZ_CONFIG.minValue,
maxValue: Number.isFinite(maxValue)
? Math.max(
Number.isFinite(minValue) ? Math.max(10, Math.round(minValue)) : DEFAULT_CONTENT_CARD_QUIZ_CONFIG.minValue,
Math.round(maxValue),
)
: DEFAULT_CONTENT_CARD_QUIZ_CONFIG.maxValue,
allowSubtraction: override && typeof override.allowSubtraction === 'boolean'
? override.allowSubtraction
: DEFAULT_CONTENT_CARD_QUIZ_CONFIG.allowSubtraction,
}
}
export function resolveContentCardCtaConfig(
override: ContentCardCtaConfigOverride | null | undefined,
): ContentCardCtaConfig | null {
const type = override && override.type
if (type !== 'detail' && type !== 'photo' && type !== 'audio' && type !== 'quiz') {
return null
}
return {
type,
label: override && override.label ? override.label : buildDefaultContentCardCtaLabel(type),
quiz: type === 'quiz' ? buildDefaultContentCardQuizConfig(override) : null,
}
}