// 每日运势模块 - 基于八字日主 + 日期种子 // 移植自 Python 版本,改写为 JS 并融合八字命理 const DailyFortune = { // 运势库 overall: ["大吉", "中吉", "平", "小凶", "大凶", "半吉", "吉"], love: ["甜蜜", "稳定", "小摩擦", "桃花运旺", "平淡", "升温"], career: ["顺利", "突破", "平稳", "压力大", "有贵人", "需谨慎"], money: ["旺财", "小赚", "收支平衡", "谨慎消费", "偏财运佳", "破财预警"], // 今日吉言 quotes: [ "保持微笑,好运自来", "今日宜努力,忌焦虑", "心平气和,万事顺遂", "机会藏在细节里", "相信自己,一切皆好", "温柔待人,好运相伴", "静心养气,福气自来", "积善之家,必有余庆" ], // 幸运颜色(根据日主五行) luckyColors: { '木': ['绿色', '青色', '翠绿', '墨绿'], '火': ['红色', '紫色', '粉色', '橙色'], '土': ['黄色', '棕色', '咖啡色', '卡其色'], '金': ['白色', '金色', '银色', '米色'], '水': ['黑色', '蓝色', '深蓝', '藏青'] }, // 幸运方位 luckyDirs: { '木': '东方', '火': '南方', '土': '中央', '金': '西方', '水': '北方' }, // 宜忌事项 yiItems: ['出行', '会友', '学习', '签约', '求财', '祭祀', '动土', '搬家'], jiItems: ['争吵', '借贷', '赌博', '酗酒', '冲动消费', '熬夜', '冒险', '跳槽'], // 用日期生成固定种子 _getSeedByDate() { const today = new Date(); const dateStr = `${today.getFullYear()}-${today.getMonth()+1}-${today.getDate()}`; // 简单哈希函数 let hash = 0; for (let i = 0; i < dateStr.length; i++) { const char = dateStr.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash = hash & hash; } return Math.abs(hash); }, // 基于种子的随机数生成器 _seededRandom(seed) { let s = seed; return function() { s = (s * 9301 + 49297) % 233280; return s / 233280; }; }, // 获取今日完整运势(基于八字日主) getTodayFortune(riGan, wuXing) { const seed = this._getSeedByDate(); const rand = this._seededRandom(seed); // 日主五行 const GAN_WU_XING = { '甲': '木', '乙': '木', '丙': '火', '丁': '火', '戊': '土', '己': '土', '庚': '金', '辛': '金', '壬': '水', '癸': '水' }; const riWx = GAN_WU_XING[riGan] || '木'; // 根据日主五行调整运势 let baseOffset = 0; const dayGZ = window.getDayGanZhi ? window.getDayGanZhi(new Date().getFullYear(), new Date().getMonth()+1, new Date().getDate()) : { gan: '甲' }; const dayWx = GAN_WU_XING[dayGZ.gan] || '木'; // 日主生日元 -> 加分 const wxSheng = { '木': '火', '火': '土', '土': '金', '金': '水', '水': '木' }; if (wxSheng[riWx] === dayWx) baseOffset += 2; if (riWx === dayWx) baseOffset += 1; // 生成各维度运势 const overallMap = { 0: '大凶', 1: '小凶', 2: '平', 3: '小吉', 4: '中吉', 5: '吉', 6: '大吉' }; const overallIdx = Math.min(6, Math.max(0, Math.floor(rand() * 7) + baseOffset)); const loveMap = ['平淡', '小摩擦', '稳定', '升温', '甜蜜', '桃花运旺']; const careerMap = ['压力大', '需谨慎', '平稳', '顺利', '有贵人', '突破']; const moneyMap = ['破财预警', '谨慎消费', '收支平衡', '小赚', '旺财', '偏财运佳']; const overall = overallMap[overallIdx]; const love = loveMap[Math.floor(rand() * loveMap.length)]; const career = careerMap[Math.floor(rand() * careerMap.length)]; const money = moneyMap[Math.floor(rand() * moneyMap.length)]; const quote = this.quotes[Math.floor(rand() * this.quotes.length)]; // 幸运信息 const luckyColor = this.luckyColors[riWx] ? this.luckyColors[riWx][Math.floor(rand() * this.luckyColors[riWx].length)] : '红色'; const luckyDir = this.luckyDirs[riWx] || '东方'; const luckyNum = Math.floor(rand() * 9) + 1; const luckyNum2 = Math.floor(rand() * 9) + 1; // 宜忌 let yi = ['出行', '会友']; let ji = ['熬夜']; if (['旺财', '偏财运佳'].includes(money)) { yi.push('求财', '投资'); ji.push('借贷'); } if (['顺利', '突破', '有贵人'].includes(career)) { yi.push('签约', '面试'); ji.push('跳槽'); } if (['甜蜜', '升温', '桃花运旺'].includes(love)) { yi.push('约会'); ji.push('争吵'); } return { 星座: riGan + '日主', 今日日期: new Date().toISOString().split('T')[0], 综合运势: overall, 爱情运势: love, 事业运势: career, 财运运势: money, 今日吉言: quote, 幸运颜色: luckyColor, 幸运方位: luckyDir, 幸运数字: luckyNum + '、' + luckyNum2, 宜: yi.slice(0, 4), 忌: ji.slice(0, 3) }; } }; // 如果全局有 getDayGanZhi 函数就使用,否则提供备用 if (typeof getDayGanZhi === 'undefined') { window.getDayGanZhi = function(y, m, d) { const baseDate = new Date(1900, 0, 31); const targetDate = new Date(y, m-1, d); const diffDays = Math.floor((targetDate - baseDate) / 86400000); const ganIdx = (0 + diffDays) % 10; const zhiIdx = (4 + diffDays) % 12; const TG = ['甲','乙','丙','丁','戊','己','庚','辛','壬','癸']; const DZ = ['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥']; return { gan: TG[ganIdx], zhi: DZ[zhiIdx] }; }; } // ========== 兼容接口:calculateFortune() ========== // 老板提供的简化版接口,调用完整版 DailyFortune 实现 function calculateFortune() { // 运势库(与老板原版一致) const overall = ["大吉", "吉", "中平", "平", "半吉", "小凶"]; const love = ["甜蜜", "稳定", "小摩擦", "桃花运旺", "平淡"]; const career = ["顺利", "突破", "平稳", "压力大", "有贵人"]; const money = ["旺财", "小赚", "收支平衡", "谨慎消费", "偏财运佳"]; const quotes = [ "保持微笑,好运自来", "今日宜努力,忌焦虑", "心平气和,万事顺遂", "机会藏在细节里", "相信自己,一切皆好", "温柔待人,好运相伴" ]; // 用今天日期做种子,保证同一天运势不变 const today = new Date().toDateString(); let seed = 0; for (let i = 0; i < today.length; i++) { seed = (seed << 5) - seed + today.charCodeAt(i); } const random = (min, max) => { seed = Math.sin(seed) * 10000; seed = seed - Math.floor(seed); return Math.floor(seed * (max - min)) + min; }; // 随机抽取运势 const res = { 综合运势: overall[random(0, overall.length)], 爱情运势: love[random(0, love.length)], 事业运势: career[random(0, career.length)], 财运运势: money[random(0, money.length)], 今日吉言: quotes[random(0, quotes.length)] }; return res; } // 同时挂载到 window,确保全局可调用 window.calculateFortune = calculateFortune;