/** * 即时多巴胺反馈触发引擎 * 判定何时触发什么反馈,协调 Toast/Milestone 组件 */ import { getRandomText, getIcon, getSound, formatText } from './feedback-presets.js' // 存储外部注册的回调 var _toastCallback = null var _celebrationCallback = null /** * 注册反馈组件回调 * @param {Function} toastFn - 接收 {text, icon, sound} 触发 Toast * @param {Function} celebrationFn - 接收 {title, subtitle} 触发全屏庆祝 */ function registerCallbacks(toastFn, celebrationFn) { _toastCallback = toastFn _celebrationCallback = celebrationFn } /** * 触发即时反馈(L1) * @param {string} scene - 场景编码: micro_action/checkin/exercise/sleep/water/milestone_7/first_use/comeback * @param {Object} vars - 模板变量,如 {minutes: 20} */ function trigger(scene, vars) { var text = getRandomText(scene) var icon = getIcon(scene) var sound = getSound(scene) var formatted = formatText(text, vars) // 判断是否为里程碑(需要全屏庆祝) if (scene && scene.indexOf('milestone_') === 0) { var days = scene.replace('milestone_', '') var titles = { '3': '连续3天!', '7': '连续一周!', '14': '连续14天!', '21': '连续21天!', '30': '连续一个月!' } if (_celebrationCallback) { _celebrationCallback({ title: titles[days] || '里程碑达成!', subtitle: formatted }) } } else { // 普通反馈:Toast if (_toastCallback) { _toastCallback({ text: formatted, icon: icon, sound: sound }) } } } export { registerCallbacks, trigger }