Refactor Date Calculator UI to Morandi theme and fix logic; fix Index page interaction

This commit is contained in:
2026-02-07 20:25:50 +08:00
parent a21716e772
commit 19c762b3fa
11 changed files with 1636 additions and 479 deletions

View File

@@ -1,3 +1,5 @@
{ {
"navigationBarTitleText": "计算器" "navigationBarTitleText": "科学计算器",
"navigationBarBackgroundColor": "#1c1c1e",
"navigationBarTextStyle": "white"
} }

View File

@@ -6,14 +6,25 @@ Page({
operator: null as string | null, operator: null as string | null,
firstOperand: null as number | null, firstOperand: null as number | null,
waitingForSecondOperand: false, waitingForSecondOperand: false,
isScientific: false isScientific: false,
showHistory: false,
historyList: [] as Array<{expression: string, result: string}>
}, },
onLoad() { onLoad() {
// 从本地存储恢复历史记录
const saved = wx.getStorageSync('CALC_HISTORY');
if (saved && Array.isArray(saved)) {
this.setData({ historyList: saved });
}
}, },
vibrate() { vibrate() {
// wx.vibrateShort({ type: 'light' }); wx.vibrateShort({ type: 'light' }).catch(() => {});
},
preventTouchMove() {
// 阻止历史面板底层滚动
}, },
toggleMode() { toggleMode() {
@@ -157,6 +168,8 @@ Page({
history: desc, history: desc,
waitingForSecondOperand: true // 计算完后,下次输入数字是新数字 waitingForSecondOperand: true // 计算完后,下次输入数字是新数字
}); });
this.addToHistory(desc, String(formatted));
}, },
onOperator(e: any) { onOperator(e: any) {
@@ -201,6 +214,7 @@ Page({
if (operator && firstOperand != null) { if (operator && firstOperand != null) {
const result = this.performCalculation(operator, firstOperand, inputValue); const result = this.performCalculation(operator, firstOperand, inputValue);
const expression = `${firstOperand} ${operator} ${inputValue}`;
this.setData({ this.setData({
displayValue: String(result), displayValue: String(result),
@@ -209,6 +223,8 @@ Page({
waitingForSecondOperand: true, waitingForSecondOperand: true,
history: `${firstOperand} ${operator} ${inputValue} =` history: `${firstOperand} ${operator} ${inputValue} =`
}); });
this.addToHistory(expression, String(result));
} }
}, },
@@ -223,5 +239,42 @@ Page({
} }
const precision = 10000000000; const precision = 10000000000;
return Math.round(result * precision) / precision; return Math.round(result * precision) / precision;
},
// 历史记录相关方法
addToHistory(expression: string, result: string) {
const list = this.data.historyList.slice();
list.unshift({ expression, result });
// 最多保留50条
if (list.length > 50) list.length = 50;
this.setData({ historyList: list });
wx.setStorageSync('CALC_HISTORY', list);
},
toggleHistory() {
this.setData({ showHistory: !this.data.showHistory });
},
clearHistory() {
this.setData({ historyList: [] });
wx.removeStorageSync('CALC_HISTORY');
},
useHistoryResult(e: any) {
const result = e.currentTarget.dataset.result;
this.setData({
displayValue: String(result),
showHistory: false,
waitingForSecondOperand: true
});
},
copyResult() {
wx.setClipboardData({
data: this.data.displayValue,
success: () => {
wx.showToast({ title: '已复制', icon: 'success', duration: 1000 });
}
});
} }
}) })

View File

@@ -1,9 +1,44 @@
<!--pages/calculator/calculator.wxml--> <!--pages/calculator/calculator.wxml-->
<view class="calculator-container"> <view class="calculator-container">
<!-- 历史记录面板 -->
<view class="history-panel {{showHistory ? 'show' : ''}}" catchtouchmove="preventTouchMove">
<view class="history-mask" bindtap="toggleHistory"></view>
<view class="history-content">
<view class="history-header">
<text class="history-title">计算历史</text>
<text class="history-clear" bindtap="clearHistory">清空</text>
</view>
<scroll-view scroll-y class="history-list" enhanced show-scrollbar="{{false}}">
<block wx:if="{{historyList.length > 0}}">
<view class="history-item" wx:for="{{historyList}}" wx:key="index"
bindtap="useHistoryResult" data-result="{{item.result}}"
hover-class="history-item-hover" hover-stay-time="100">
<text class="history-expr">{{item.expression}}</text>
<text class="history-result">= {{item.result}}</text>
</view>
</block>
<view wx:else class="history-empty">
<text class="history-empty-icon">📭</text>
<text>暂无计算记录</text>
</view>
</scroll-view>
</view>
</view>
<!-- 显示区域 --> <!-- 显示区域 -->
<view class="display-area"> <view class="display-area">
<view class="mode-indicator" bindtap="toggleMode"> <view class="display-top-bar">
<text class="{{isScientific ? 'active' : ''}}">⚗️ 科学模式</text> <view class="mode-indicator" bindtap="toggleMode">
<text class="{{isScientific ? 'active' : ''}}">⚗️ 科学</text>
</view>
<view class="top-actions">
<view class="action-btn" bindtap="toggleHistory" hover-class="action-btn-hover" hover-stay-time="100">
<text class="action-icon">🕐</text>
</view>
<view class="action-btn" bindtap="copyResult" hover-class="action-btn-hover" hover-stay-time="100">
<text class="action-icon">📋</text>
</view>
</view>
</view> </view>
<view class="history-text">{{history}}</view> <view class="history-text">{{history}}</view>
<view class="current-value {{displayValue.length > 9 ? 'shrink' : ''}} {{displayValue.length > 15 ? 'shrink-more' : ''}}">{{displayValue}}</view> <view class="current-value {{displayValue.length > 9 ? 'shrink' : ''}} {{displayValue.length > 15 ? 'shrink-more' : ''}}">{{displayValue}}</view>

View File

@@ -1,7 +1,9 @@
/* pages/calculator/calculator.wxss */ /* pages/calculator/calculator.wxss */
/* ========== 全局 ========== */
page { page {
height: 100%; height: 100%;
background-color: #000000; background-color: #1c1c1e;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
} }
@@ -14,168 +16,330 @@ page {
padding-bottom: env(safe-area-inset-bottom); padding-bottom: env(safe-area-inset-bottom);
} }
/* 显示区域 */ /* ========== 显示区域 ========== */
.display-area { .display-area {
flex: 1; flex: 1;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content: flex-end; justify-content: flex-end;
align-items: flex-end; align-items: flex-end;
padding: 40rpx; padding: 30rpx 40rpx 20rpx;
background-color: #000; background: linear-gradient(180deg, #1c1c1e 0%, #2c2c2e 100%);
position: relative; position: relative;
overflow: hidden;
} }
.mode-indicator { /* 顶部工具栏 */
position: absolute; .display-top-bar {
top: 40rpx; position: absolute;
left: 40rpx; top: 20rpx;
left: 30rpx;
right: 30rpx;
display: flex;
justify-content: space-between;
align-items: center;
} }
.top-actions {
display: flex;
gap: 16rpx;
}
.action-btn {
width: 64rpx;
height: 64rpx;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
background: rgba(255,255,255,0.08);
}
.action-icon {
font-size: 30rpx;
}
/* 模式切换 */
.mode-indicator text { .mode-indicator text {
color: #444; color: rgba(255,255,255,0.4);
font-size: 24rpx; font-size: 24rpx;
border: 1px solid #444; padding: 10rpx 24rpx;
padding: 10rpx 20rpx; border-radius: 30rpx;
border-radius: 30rpx; background: rgba(255,255,255,0.06);
border: none;
} }
.mode-indicator text.active { .mode-indicator text.active {
color: #ff9f0a; color: #ff9f0a;
border-color: #ff9f0a; background: rgba(255, 159, 10, 0.15);
background: rgba(255, 159, 10, 0.1);
} }
/* 表达式行 */
.history-text { .history-text {
font-size: 36rpx; font-size: 32rpx;
color: #666; color: rgba(255,255,255,0.35);
min-height: 50rpx; min-height: 44rpx;
margin-bottom: 20rpx; margin-bottom: 12rpx;
font-family: monospace; font-family: -apple-system, 'SF Pro Display', 'Helvetica Neue', sans-serif;
text-align: right; text-align: right;
width: 100%; width: 100%;
letter-spacing: 2rpx;
} }
/* 当前值 */
.current-value { .current-value {
font-size: 160rpx; font-size: 130rpx;
color: #fff; color: #ffffff;
line-height: 1; line-height: 1.1;
font-weight: 200; font-weight: 200;
word-break: break-all; word-break: break-all;
text-align: right; text-align: right;
width: 100%; width: 100%;
font-family: -apple-system, 'SF Pro Display', 'Helvetica Neue', sans-serif;
} }
.current-value.shrink { .current-value.shrink {
font-size: 100rpx; font-size: 90rpx;
}
.current-value.shrink-more {
font-size: 70rpx;
} }
/* 键盘区域 */ .current-value.shrink-more {
font-size: 62rpx;
}
/* ========== 键盘区域 ========== */
.keypad-area { .keypad-area {
background-color: #000; background: #1c1c1e;
padding: 0 30rpx 30rpx 30rpx; padding: 16rpx 24rpx 24rpx;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 30rpx; gap: 20rpx;
} }
/* 科学模式适配 */
.keypad-area.scientific-pad { .keypad-area.scientific-pad {
gap: 20rpx; gap: 14rpx;
padding-bottom: 20rpx; padding-bottom: 20rpx;
}
/* 统一圆形按钮 */
.keypad-area.scientific-pad .btn {
width: 130rpx;
height: 130rpx;
font-size: 50rpx;
border-radius: 50%; /* 保持圆形 */
}
.keypad-area.scientific-pad .zero {
width: 280rpx; /* 适配新的宽度 */
border-radius: 65rpx; /* 圆角为高度一半 */
}
/* 科学功能键行 */
.keypad-area.scientific-pad .sci-btn {
width: 120rpx; /* 稍微大一点,填满宽度 */
height: 120rpx; /* 正圆 */
font-size: 36rpx;
background: #222;
color: #fff;
border-radius: 50%;
}
.small-row {
justify-content: space-around; /* 均匀分布 */
gap: 10rpx;
} }
/* 行 */
.row { .row {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
gap: 30rpx; gap: 20rpx;
} }
.keypad-area.scientific-pad .row { .keypad-area.scientific-pad .row {
gap: 20rpx; gap: 14rpx;
} }
.small-row {
justify-content: space-around;
gap: 8rpx;
}
/* ========== 按钮通用 ========== */
.btn { .btn {
width: 150rpx; width: 152rpx;
height: 150rpx; height: 152rpx;
border-radius: 75rpx; /* 圆形按钮 */ border-radius: 50%;
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
font-size: 60rpx; font-size: 56rpx;
font-weight: 500; font-weight: 400;
font-family: -apple-system, 'SF Pro Display', 'Helvetica Neue', sans-serif;
position: relative;
overflow: hidden;
} }
/* 数字键 */ /* 数字键 */
.digit { .digit {
background-color: #333333; background-color: #3a3a3c;
color: #fff; color: #ffffff;
} }
/* 功能键 (AC, Del, +/-) */ /* 功能键 */
.func { .func {
background-color: #a5a5a5; background-color: #636366;
color: #000; color: #ffffff;
font-size: 50rpx; font-size: 46rpx;
font-weight: 600; font-weight: 500;
} }
/* 运算符 */ /* 运算符 */
.operator { .operator {
background-color: #ff9f0a; background-color: #ff9f0a;
color: #fff; color: #ffffff;
font-size: 70rpx; font-size: 64rpx;
padding-bottom: 10rpx; font-weight: 400;
padding: 0;
} }
/* 0键特殊处理 */ /* 等号键:渐变突出 */
.equal {
background: linear-gradient(135deg, #ff9f0a 0%, #ff6723 100%);
box-shadow: 0 6rpx 20rpx rgba(255, 159, 10, 0.35);
}
/* 0键 */
.zero { .zero {
width: 330rpx; width: 324rpx;
border-radius: 75rpx; border-radius: 76rpx;
justify-content: flex-start; justify-content: flex-start;
padding-left: 60rpx; padding-left: 62rpx;
box-sizing: border-box; box-sizing: border-box;
} }
/* 点击反馈 */ /* 点击反馈 */
.btn-hover { .btn-hover {
opacity: 0.7; transform: scale(0.92);
opacity: 0.75;
} }
/* 平板适配或小屏适配 */ /* 科学模式缩放 */
.keypad-area.scientific-pad .btn {
width: 130rpx;
height: 130rpx;
font-size: 48rpx;
}
.keypad-area.scientific-pad .zero {
width: 274rpx;
border-radius: 65rpx;
}
.keypad-area.scientific-pad .func {
font-size: 40rpx;
}
.keypad-area.scientific-pad .operator {
font-size: 54rpx;
}
/* 科学功能键 */
.sci-btn {
width: 120rpx !important;
height: 120rpx !important;
font-size: 32rpx !important;
background: rgba(255,255,255,0.08) !important;
color: #64d2ff !important;
border-radius: 50% !important;
font-weight: 500 !important;
letter-spacing: 1rpx;
}
/* ========== 历史记录面板 ========== */
.history-panel {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 100;
pointer-events: none;
opacity: 0;
}
.history-panel.show {
pointer-events: auto;
opacity: 1;
}
.history-mask {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.55);
}
.history-content {
position: relative;
z-index: 101;
background: #2c2c2e;
border-radius: 0 0 36rpx 36rpx;
max-height: 70vh;
display: flex;
flex-direction: column;
box-shadow: 0 16rpx 48rpx rgba(0,0,0,0.5);
}
.history-panel.show .history-content {
animation: histSlideDown 0.3s cubic-bezier(0.32, 0.72, 0, 1) forwards;
}
@keyframes histSlideDown {
from { transform: translateY(-100%); }
to { transform: translateY(0); }
}
.history-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 36rpx 40rpx;
border-bottom: 1rpx solid rgba(255,255,255,0.08);
}
.history-title {
color: #ffffff;
font-size: 34rpx;
font-weight: 600;
letter-spacing: 1rpx;
}
.history-clear {
color: #ff9f0a;
font-size: 28rpx;
padding: 8rpx 24rpx;
border-radius: 20rpx;
background: rgba(255, 159, 10, 0.1);
}
.history-list {
max-height: 58vh;
padding: 10rpx 0;
}
.history-item {
padding: 28rpx 40rpx;
border-bottom: 1rpx solid rgba(255,255,255,0.05);
display: flex;
flex-direction: column;
align-items: flex-end;
}
.history-item:active {
background: rgba(255,255,255,0.06);
}
.history-expr {
color: rgba(255,255,255,0.45);
font-size: 26rpx;
margin-bottom: 8rpx;
font-family: -apple-system, 'SF Pro Display', monospace;
}
.history-result {
color: #ffffff;
font-size: 42rpx;
font-weight: 500;
font-family: -apple-system, 'SF Pro Display', 'Helvetica Neue', sans-serif;
}
.history-empty {
text-align: center;
padding: 100rpx 0;
color: rgba(255,255,255,0.25);
font-size: 28rpx;
}
.history-empty-icon {
font-size: 64rpx;
display: block;
margin-bottom: 16rpx;
}
/* ========== 小屏适配 ========== */
@media (max-width: 360px) { @media (max-width: 360px) {
.btn { .btn {
width: 140rpx; width: 140rpx;
@@ -183,6 +347,9 @@ page {
font-size: 50rpx; font-size: 50rpx;
} }
.zero { .zero {
width: 310rpx; width: 300rpx;
}
.current-value {
font-size: 110rpx;
} }
} }

View File

@@ -1,4 +1,6 @@
{ {
"usingComponents": {}, "usingComponents": {},
"navigationBarTitleText": "日期计算器" "navigationBarTitleText": "日期计算与农历转换",
"navigationBarBackgroundColor": "#f5f6fa",
"navigationBarTextStyle": "black"
} }

View File

@@ -1,16 +1,40 @@
// pages/date-calc/date-calc.ts // pages/date-calc/date-calc.ts
import { solarToLunar, lunarToSolar, getSolarTerm, getFestival, getLunarFestival } from '../../utils/lunar';
Page({ Page({
data: { data: {
currentTab: 0, currentTab: 0,
startDate: '', startDate: '',
startWeekday: '', // 新增:开始日期的星期 startWeekday: '',
endDate: '', endDate: '',
days: 0, // 可以为空字符串以便清空输入框,但 input type number 会处理 days: 0,
resultDate: '', resultDate: '',
resultWeekday: '', resultWeekday: '',
resultLunar: '',
intervalDays: 0, intervalDays: 0,
intervalWeeks: 0, intervalWeeks: 0,
intervalRemainingDays: 0 intervalRemainingDays: 0,
intervalMonths: 0,
intervalExtraDays: 0,
// 农历转换相关
lunarDirection: 'toL', // 'toL' 公历→农历, 'toS' 农历→公历
lunarSolarDate: '',
lunarResult: null as any,
lunarFestivalText: '',
solarTermText: '',
solarFestivalText: '',
// 农历→公历
lunarPickerRange: [[] as string[], [] as string[], [] as string[]],
lunarPickerValue: [0, 0, 0],
lunarPickerDisplay: '',
lunarToSolarResult: '',
lunarToSolarWeekday: '',
// 今日信息
todaySolar: '',
todayLunar: '',
todayGanZhi: '',
todayAnimal: '',
todayTerm: ''
}, },
onLoad() { onLoad() {
@@ -21,15 +45,23 @@ Page({
endDate: dateStr, endDate: dateStr,
resultDate: dateStr, resultDate: dateStr,
resultWeekday: this.getWeekday(today), resultWeekday: this.getWeekday(today),
startWeekday: this.getWeekday(today) startWeekday: this.getWeekday(today),
lunarSolarDate: dateStr
}); });
// 初始化农历相关
this.initLunarPicker();
this.updateTodayInfo();
this.convertSolarToLunar(dateStr);
this.updateResultLunar(today);
}, },
switchTab(e: any) { switchTab(e: any) {
wx.vibrateShort({ type: 'light' }).catch(() => {});
this.setData({ this.setData({
currentTab: parseFloat(e.currentTarget.dataset.index) currentTab: parseFloat(e.currentTarget.dataset.index)
}); });
this.calculate(); // 切换时重新计算 this.calculate();
}, },
bindDateChange(e: any) { bindDateChange(e: any) {
@@ -38,9 +70,8 @@ Page({
[field]: e.detail.value [field]: e.detail.value
}); });
// 如果修改的是开始日期,同步更新星期显示
if (field === 'startDate') { if (field === 'startDate') {
const d = new Date(e.detail.value); const d = new Date(e.detail.value.replace(/-/g, '/'));
this.setData({ startWeekday: this.getWeekday(d) }); this.setData({ startWeekday: this.getWeekday(d) });
} }
@@ -49,7 +80,6 @@ Page({
bindDaysInput(e: any) { bindDaysInput(e: any) {
const val = e.detail.value; const val = e.detail.value;
// 允许输入负号
if (val === '-' || val === '') { if (val === '-' || val === '') {
this.setData({ days: val }); this.setData({ days: val });
return; return;
@@ -61,6 +91,7 @@ Page({
}, },
setDays(e: any) { setDays(e: any) {
wx.vibrateShort({ type: 'light' }).catch(() => {});
const days = parseInt(e.currentTarget.dataset.days); const days = parseInt(e.currentTarget.dataset.days);
this.setData({ days: days }); this.setData({ days: days });
this.calculate(); this.calculate();
@@ -68,20 +99,16 @@ Page({
calculate() { calculate() {
if (this.data.currentTab === 0) { if (this.data.currentTab === 0) {
// 日期推算
// 注意:直接使用 new Date('2023-10-01') 在 iOS 上可能不兼容,应替换为 '/'
const start = new Date(this.data.startDate.replace(/-/g, '/')); const start = new Date(this.data.startDate.replace(/-/g, '/'));
// days 可能是字符串或数字
const dayOffset = typeof(this.data.days) === 'number' ? this.data.days : parseInt(this.data.days || '0'); const dayOffset = typeof(this.data.days) === 'number' ? this.data.days : parseInt(this.data.days || '0');
const target = new Date(start.getTime() + dayOffset * 24 * 60 * 60 * 1000); const target = new Date(start.getTime() + dayOffset * 24 * 60 * 60 * 1000);
this.setData({ this.setData({
resultDate: this.formatDate(target), resultDate: this.formatDate(target),
resultWeekday: this.getWeekday(target) resultWeekday: this.getWeekday(target)
}); });
} else { this.updateResultLunar(target);
// 日期通过 (Interval) } else if (this.data.currentTab === 1) {
const start = new Date(this.data.startDate.replace(/-/g, '/')); const start = new Date(this.data.startDate.replace(/-/g, '/'));
const end = new Date(this.data.endDate.replace(/-/g, '/')); const end = new Date(this.data.endDate.replace(/-/g, '/'));
const diffTime = Math.abs(end.getTime() - start.getTime()); const diffTime = Math.abs(end.getTime() - start.getTime());
@@ -89,15 +116,184 @@ Page({
const weeks = Math.floor(diffDays / 7); const weeks = Math.floor(diffDays / 7);
const remainingDays = diffDays % 7; const remainingDays = diffDays % 7;
const months = Math.floor(diffDays / 30);
const extraDays = diffDays % 30;
this.setData({ this.setData({
intervalDays: diffDays, intervalDays: diffDays,
intervalWeeks: weeks, intervalWeeks: weeks,
intervalRemainingDays: remainingDays intervalRemainingDays: remainingDays,
intervalMonths: months,
intervalExtraDays: extraDays
}); });
} }
}, },
// 更新推算结果的农历信息
updateResultLunar(date: Date) {
try {
const lunar = solarToLunar(date.getFullYear(), date.getMonth() + 1, date.getDate());
this.setData({
resultLunar: lunar.monthName + lunar.dayName
});
} catch (e) {
this.setData({ resultLunar: '' });
}
},
// === 农历转换相关 ===
setLunarDirection(e: any) {
wx.vibrateShort({ type: 'light' }).catch(() => {});
this.setData({ lunarDirection: e.currentTarget.dataset.dir });
},
toggleLunarDirection() {
wx.vibrateShort({ type: 'light' }).catch(() => {});
this.setData({
lunarDirection: this.data.lunarDirection === 'toL' ? 'toS' : 'toL'
});
},
onLunarSolarDateChange(e: any) {
const dateStr = e.detail.value;
this.setData({ lunarSolarDate: dateStr });
this.convertSolarToLunar(dateStr);
},
convertSolarToLunar(dateStr: string) {
try {
const parts = dateStr.split('-');
const year = parseInt(parts[0]);
const month = parseInt(parts[1]);
const day = parseInt(parts[2]);
const result = solarToLunar(year, month, day);
const term = getSolarTerm(year, month, day);
const solarFest = getFestival(month, day);
const lunarFest = getLunarFestival(result.month, result.day);
this.setData({
lunarResult: result,
solarTermText: term || '',
solarFestivalText: solarFest || '',
lunarFestivalText: lunarFest || ''
});
} catch (e) {
console.error('农历转换出错:', e);
}
},
// 初始化农历选择器
initLunarPicker() {
const years: string[] = [];
for (let y = 1901; y <= 2099; y++) {
years.push(y + '年');
}
const months: string[] = [];
for (let m = 1; m <= 12; m++) {
months.push(this.getLunarMonthLabel(m));
}
const days: string[] = [];
for (let d = 1; d <= 30; d++) {
days.push(this.getLunarDayLabel(d));
}
const today = new Date();
// Calculate today's lunar date to set default picker value
const result = solarToLunar(today.getFullYear(), today.getMonth() + 1, today.getDate());
const yearIdx = result.year - 1901;
const monthIdx = result.month - 1;
const dayIdx = result.day - 1;
const display = `${result.year}${result.monthName} ${result.dayName}`;
this.setData({
lunarPickerRange: [years, months, days],
lunarPickerValue: [yearIdx >= 0 ? yearIdx : 0, monthIdx, dayIdx],
lunarPickerDisplay: display
});
// Also init the reverse calculation result
this.calcLunarToSolar(result.year, result.month, result.day);
},
getLunarMonthLabel(m: number): string {
const names = ['正月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '冬月', '腊月'];
return names[m - 1];
},
getLunarDayLabel(d: number): string {
const names = [
'初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十',
'十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十',
'廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '三十'
];
return names[d - 1];
},
onLunarColumnChange(e: any) {
// 列变化时可以联动更新(简化处理)
},
onLunarPickerChange(e: any) {
const val = e.detail.value;
const year = 1901 + val[0];
const month = val[1] + 1;
const day = val[2] + 1;
const display = `${year}${this.getLunarMonthLabel(month)} ${this.getLunarDayLabel(day)}`;
this.setData({
lunarPickerValue: val,
lunarPickerDisplay: display,
});
this.calcLunarToSolar(year, month, day);
},
calcLunarToSolar(year: number, month: number, day: number) {
try {
const result = lunarToSolar(year, month, day, false);
const solarDate = new Date(result.year, result.month - 1, result.day);
const solarStr = this.formatDate(solarDate);
this.setData({
lunarToSolarResult: solarStr,
lunarToSolarWeekday: this.getWeekday(solarDate)
});
} catch (e) {
this.setData({
lunarToSolarResult: '无效日期',
lunarToSolarWeekday: ''
});
}
},
// 今日信息
updateTodayInfo() {
const today = new Date();
const y = today.getFullYear();
const m = today.getMonth() + 1;
const d = today.getDate();
try {
const lunar = solarToLunar(y, m, d);
const term = getSolarTerm(y, m, d);
this.setData({
todaySolar: `${y}${m}${d}${this.getWeekday(today)}`,
todayLunar: lunar.monthName + lunar.dayName,
todayGanZhi: lunar.ganZhi + '年',
todayAnimal: lunar.animal,
todayTerm: term || ''
});
} catch (e) {
console.error('获取今日信息出错:', e);
}
},
formatDate(date: Date): string { formatDate(date: Date): string {
const y = date.getFullYear(); const y = date.getFullYear();
const m = (date.getMonth() + 1).toString().padStart(2, '0'); const m = (date.getMonth() + 1).toString().padStart(2, '0');

View File

@@ -3,94 +3,181 @@
<!-- 顶部 Tab 切换 --> <!-- 顶部 Tab 切换 -->
<view class="tab-header"> <view class="tab-header">
<view class="tab-pill {{currentTab === 0 ? 'active' : ''}}" bindtap="switchTab" data-index="{{0}}"> <view class="tab-pill {{currentTab === 0 ? 'active' : ''}}" hover-class="tab-hover" bindtap="switchTab" data-index="{{0}}">日期推算</view>
<text class="tab-icon">📅</text> <text>日期推算</text> <view class="tab-pill {{currentTab === 1 ? 'active' : ''}}" hover-class="tab-hover" bindtap="switchTab" data-index="{{1}}">间隔计算</view>
</view> <view class="tab-pill {{currentTab === 2 ? 'active' : ''}}" hover-class="tab-hover" bindtap="switchTab" data-index="{{2}}">农历转换</view>
<view class="tab-pill {{currentTab === 1 ? 'active' : ''}}" bindtap="switchTab" data-index="{{1}}">
<text class="tab-icon">⏳</text> <text>间隔计算</text>
</view>
</view> </view>
<!-- 内容区 --> <!-- ========== 模式1: 日期推算 ========== -->
<view class="content-wrapper"> <block wx:if="{{currentTab === 0}}">
<!-- 日期选择 -->
<!-- 模式1: 日期推算 --> <view class="card input-card">
<block wx:if="{{currentTab === 0}}"> <view class="card-label">开始日期</view>
<view class="card input-card"> <picker mode="date" value="{{startDate}}" bindchange="bindDateChange" data-field="startDate">
<view class="card-title">开始日期</view> <view class="date-picker-bar" hover-class="picker-bar-hover">
<view class="picker-left">
<text class="picker-date">{{startDate || '请选择'}}</text>
<text class="picker-weekday">{{startWeekday}}</text>
</view>
<view class="picker-arrow">✎</view>
</view>
</picker>
</view>
<!-- 推算天数 -->
<view class="card input-card">
<view class="card-label">推算天数</view>
<view class="days-input-group">
<input class="days-input" type="number" placeholder="输入天数" placeholder-style="color:#ccc;font-size:36rpx;font-weight:400" bindinput="bindDaysInput" value="{{days}}" />
<text class="days-suffix">天</text>
</view>
<view class="quick-tags">
<view class="tag tag-plus" hover-class="tag-hover" bindtap="setDays" data-days="7">+1周</view>
<view class="tag tag-plus" hover-class="tag-hover" bindtap="setDays" data-days="30">+1月</view>
<view class="tag tag-plus" hover-class="tag-hover" bindtap="setDays" data-days="100">+百日</view>
<view class="tag tag-plus" hover-class="tag-hover" bindtap="setDays" data-days="365">+1年</view>
<view class="tag tag-minus" hover-class="tag-minus-hover" bindtap="setDays" data-days="-1">-1天</view>
<view class="tag tag-minus" hover-class="tag-minus-hover" bindtap="setDays" data-days="-7">-1周</view>
</view>
</view>
<!-- 推算结果 -->
<view class="result-card">
<view class="result-label">推算结果</view>
<view class="result-main">{{resultDate}}</view>
<view class="result-extra">{{resultWeekday}}</view>
<view class="result-tag" wx:if="{{resultLunar}}">🌙 农历 {{resultLunar}}</view>
</view>
</block>
<!-- ========== 模式2: 日期间隔 ========== -->
<block wx:if="{{currentTab === 1}}">
<view class="card input-card interval-card-compact">
<!-- 起始 -->
<view class="iv-col">
<view class="iv-small-label">起始日期</view>
<picker mode="date" value="{{startDate}}" bindchange="bindDateChange" data-field="startDate"> <picker mode="date" value="{{startDate}}" bindchange="bindDateChange" data-field="startDate">
<view class="date-display"> <view class="iv-pill" hover-class="iv-pill-hover">
<text class="date-text">{{startDate || '请选择'}}</text> {{startDate}}
<text class="date-week">{{startWeekday}}</text>
<view class="edit-icon">✎</view>
</view> </view>
</picker> </picker>
</view>
<view class="divider"></view>
<!-- 连接符 -->
<view class="iv-connector">
<view class="iv-arrow-icon">→</view>
</view>
<view class="card-title">推算天数</view> <!-- 结束 -->
<view class="input-row"> <view class="iv-col">
<input class="days-input" type="number" placeholder="0" bindinput="bindDaysInput" value="{{days}}" /> <view class="iv-small-label">结束日期</view>
<text class="input-unit">天后</text> <picker mode="date" value="{{endDate}}" bindchange="bindDateChange" data-field="endDate">
</view> <view class="iv-pill" hover-class="iv-pill-hover">
{{endDate}}
<!-- 快捷标签 --> </view>
<view class="tags-row"> </picker>
<view class="tag" bindtap="setDays" data-days="7">+1周</view> </view>
<view class="tag" bindtap="setDays" data-days="30">+1月</view> </view>
<view class="tag" bindtap="setDays" data-days="100">+百日</view>
<view class="tag tag-red" bindtap="setDays" data-days="-1">-1天</view> <!-- 间隔结果 -->
<view class="result-card">
<view class="result-label">时间跨度</view>
<view class="interval-num-row">
<text class="interval-big-num">{{intervalDays}}</text>
<text class="interval-big-unit">天</text>
</view>
<view class="result-detail-row" wx:if="{{intervalWeeks > 0 || intervalMonths > 0}}">
<text class="result-detail" wx:if="{{intervalWeeks > 0}}">≈ {{intervalWeeks}} 周{{intervalRemainingDays > 0 ? ' ' + intervalRemainingDays + ' 天' : ''}}</text>
<text class="result-detail-sep" wx:if="{{intervalWeeks > 0 && intervalMonths > 0}}">|</text>
<text class="result-detail" wx:if="{{intervalMonths > 0}}">≈ {{intervalMonths}} 个月{{intervalExtraDays > 0 ? ' ' + intervalExtraDays + ' 天' : ''}}</text>
</view>
</view>
</block>
<!-- ========== 模式3: 农历转换 ========== -->
<block wx:if="{{currentTab === 2}}">
<!-- 输入卡片(固定结构) -->
<view class="card input-card">
<view class="lunar-header-row">
<view class="card-label" style="margin-bottom:0">{{lunarDirection === 'toL' ? '选择公历日期' : '选择农历日期'}}</view>
<view class="dir-switch" hover-class="dir-switch-hover" bindtap="toggleLunarDirection">
<text class="dir-switch-text">切换为 {{lunarDirection === 'toL' ? '农历→公历' : '公历→农历'}}</text>
<text class="dir-switch-icon">⇄</text>
</view> </view>
</view> </view>
<view class="card result-card"> <!-- 公历日期选择 -->
<view class="result-header">计算结果</view> <picker wx:if="{{lunarDirection === 'toL'}}" mode="date" value="{{lunarSolarDate}}" bindchange="onLunarSolarDateChange">
<view class="result-date">{{resultDate}}</view> <view class="date-picker-bar" hover-class="picker-bar-hover">
<view class="result-week">{{resultWeekday}}</view> <view class="picker-left">
</view> <text class="picker-date">{{lunarSolarDate || '请选择'}}</text>
</block> </view>
<view class="picker-arrow">✎</view>
<!-- 模式2: 日期间隔 -->
<block wx:if="{{currentTab === 1}}">
<view class="card input-card">
<!-- 开始日期 -->
<view class="date-row">
<view class="row-label">开始</view>
<picker mode="date" value="{{startDate}}" bindchange="bindDateChange" data-field="startDate">
<view class="date-pill">
{{startDate}} <text class="pill-icon">▼</text>
</view>
</picker>
</view> </view>
</picker>
<view class="timeline-connect"> <!-- 农历日期选择 -->
<view class="dots"></view> <picker wx:if="{{lunarDirection === 'toS'}}" mode="multiSelector" range="{{lunarPickerRange}}" value="{{lunarPickerValue}}" bindchange="onLunarPickerChange" bindcolumnchange="onLunarColumnChange">
<view class="duration-badge">相隔</view> <view class="date-picker-bar" hover-class="picker-bar-hover">
<view class="dots"></view> <view class="picker-left">
<text class="picker-date">{{lunarPickerDisplay || '请选择'}}</text>
</view>
<view class="picker-arrow">✎</view>
</view> </view>
</picker>
</view>
<!-- 结束日期 --> <!-- 结果卡片 -->
<view class="date-row"> <view class="result-card" wx:if="{{(lunarDirection === 'toL' && lunarResult) || (lunarDirection === 'toS' && lunarToSolarResult)}}">
<view class="row-label">结束</view>
<picker mode="date" value="{{endDate}}" bindchange="bindDateChange" data-field="endDate"> <!-- 公历 -> 农历结果 -->
<view class="date-pill"> <block wx:if="{{lunarDirection === 'toL'}}">
{{endDate}} <text class="pill-icon">▼</text> <view class="result-label">对应农历</view>
</view> <view class="lunar-hero">{{lunarResult.monthName}}{{lunarResult.dayName}}</view>
</picker> <view class="lunar-sub">{{lunarResult.ganZhi}}年 · {{lunarResult.animal}}年</view>
<view class="lunar-tags" wx:if="{{lunarFestivalText || solarTermText || solarFestivalText}}">
<view class="lunar-tag festival" wx:if="{{lunarFestivalText}}">🎊 {{lunarFestivalText}}</view>
<view class="lunar-tag term" wx:if="{{solarTermText}}">🌿 {{solarTermText}}</view>
<view class="lunar-tag solar" wx:if="{{solarFestivalText}}">🎉 {{solarFestivalText}}</view>
</view>
</block>
<!-- 农历 -> 公历结果 -->
<block wx:if="{{lunarDirection === 'toS'}}">
<view class="result-label">对应公历</view>
<view class="result-main">{{lunarToSolarResult}}</view>
<view class="result-extra">{{lunarToSolarWeekday}}</view>
</block>
</view>
<!-- 今日信息 -->
<view class="today-card">
<view class="today-title">📌 今日</view>
<view class="today-grid">
<view class="today-cell">
<text class="today-cell-label">公历</text>
<text class="today-cell-value">{{todaySolar}}</text>
</view>
<view class="today-cell">
<text class="today-cell-label">农历</text>
<text class="today-cell-value accent">{{todayLunar}}</text>
</view>
<view class="today-cell">
<text class="today-cell-label">干支</text>
<text class="today-cell-value">{{todayGanZhi}}</text>
</view>
<view class="today-cell">
<text class="today-cell-label">生肖</text>
<text class="today-cell-value">{{todayAnimal}}</text>
</view> </view>
</view> </view>
<view class="today-term-bar" wx:if="{{todayTerm}}">
<view class="card result-card interval-result"> <text class="term-icon">🌿</text>
<view class="result-header">时间跨度</view> <text class="term-text">今日节气:{{todayTerm}}</text>
<view class="result-huge">
<text class="num">{{intervalDays}}</text>
<text class="unit">天</text>
</view>
<view class="result-sub" wx:if="{{intervalWeeks > 0}}">
约合 {{intervalWeeks}} 周 {{intervalRemainingDays > 0 ? '+ ' + intervalRemainingDays + ' 天' : ''}}
</view>
</view> </view>
</block> </view>
</view> </block>
</view> </view>

View File

@@ -1,276 +1,520 @@
/* pages/date-calc/date-calc.wxss */ /* pages/date-calc/date-calc.wxss */
/* ========== 全局变量 ========== */
page { page {
background-color: #f0f2f5; background-color: #fcfbfd; /* 极淡的紫灰色背景 */
color: #333; color: #594f6d; /* 莫兰迪深紫灰文字 */
--primary-color: #5b73e8;
--primary-light: #e0e6fc; /* 莫兰迪紫色系定义 */
--primary: #9b8ea9; /* 莫兰迪紫/烟熏紫 */
--primary-light: #dcd6e4;
--primary-bg: #f4f1f7;
--card-shadow: 0 4rpx 16rpx rgba(155, 142, 169, 0.1);
--card-radius: 24rpx;
/* 移除渐变,使用莫兰迪纯色 */
--accent-color: #9b8ea9;
} }
/* ========== 容器 ========== */
.container { .container {
padding: 30rpx; padding: 30rpx 32rpx;
padding-bottom: 80rpx;
min-height: 100vh; min-height: 100vh;
box-sizing: border-box; box-sizing: border-box;
} }
/* Tab 切换 */ /* ========== Tab 切换 ========== */
.tab-header { .tab-header {
display: flex; display: grid; /* 使用 Grid 均分三列,杜绝挤压 */
background: #ffffff; grid-template-columns: 1fr 1fr 1fr;
background: #f1f2f6;
padding: 8rpx; padding: 8rpx;
border-radius: 20rpx; border-radius: 60rpx;
margin-bottom: 30rpx; margin-bottom: 40rpx;
box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.03); align-items: center;
gap: 8rpx; /* 增加列间距 */
} }
.tab-pill { .tab-pill {
flex: 1;
text-align: center; text-align: center;
padding: 20rpx 0; padding: 24rpx 0; /* 增加高度 */
border-radius: 16rpx; border-radius: 50rpx;
font-size: 28rpx; font-size: 28rpx;
color: #666; color: #a4b0be; /* 莫兰迪浅灰未选中态 */
font-weight: 500; font-weight: 500;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
gap: 10rpx;
white-space: nowrap; white-space: nowrap;
transition: all 0.2s ease;
position: relative;
z-index: 1;
} }
.tab-pill.active { .tab-pill.active {
background-color: var(--primary-light); background: var(--accent-color); /* 纯色背景 */
color: var(--primary-color); color: #ffffff;
font-weight: bold; font-weight: 600;
box-shadow: 0 4rpx 12rpx rgba(155, 142, 169, 0.4); /* 匹配主色的柔和阴影 */
transform: none; /* 移除缩放,防止文字抖动 */
} }
.tab-icon { .tab-hover {
font-size: 32rpx; background: rgba(0, 0, 0, 0.05); /* 简单的灰色点击态 */
} }
/* 卡片通用样式 */ .tab-pill.active.tab-hover {
opacity: 0.9;
background: var(--accent-gradient); /* 保持渐变 */
}
/* ========== 通用卡片 ========== */
.card { .card {
background: #ffffff; background: #fff;
border-radius: 24rpx; border-radius: var(--card-radius);
padding: 40rpx; padding: 32rpx;
margin-bottom: 30rpx; margin-bottom: 24rpx;
box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.04); box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.04); /* 更柔和的弥散阴影 */
position: relative;
transition: transform 0.2s;
} }
.card-title { /* 移除旧的顶部边框,使用更现代的左侧强调条 */
font-size: 24rpx; .input-card {
color: #999; border-left: 8rpx solid var(--primary);
margin-bottom: 20rpx; }
text-transform: uppercase;
.card-label {
font-size: 23rpx;
color: #b2bec3;
margin-bottom: 14rpx;
letter-spacing: 1rpx; letter-spacing: 1rpx;
font-weight: 600;
} }
/* 模式1日期推算 */ .card-divider {
.date-display { height: 1rpx;
background: #f0f0f5;
margin: 20rpx 0;
}
/* ========== 日期选择栏(通用) ========== */
.date-picker-bar {
display: flex;
align-items: center;
justify-content: space-between;
background: #f8f9ff;
border-radius: 16rpx;
padding: 20rpx 24rpx;
border: 1rpx solid #eef0f8;
}
.picker-bar-hover {
background: var(--primary-bg);
border-color: var(--primary-light);
}
.picker-left {
display: flex; display: flex;
align-items: baseline; align-items: baseline;
padding-bottom: 20rpx; gap: 14rpx;
} }
.date-text { .picker-date {
font-size: 48rpx;
font-weight: bold;
color: #333;
margin-right: 20rpx;
}
.date-week {
font-size: 28rpx;
color: #666;
}
.edit-icon {
margin-left: auto;
color: var(--primary-color);
font-size: 36rpx; font-size: 36rpx;
opacity: 0.8; font-weight: 700;
color: #2d3436;
} }
.divider { .picker-weekday {
height: 1px; font-size: 24rpx;
background: #f0f0f0; color: var(--primary);
margin: 30rpx 0; background: var(--primary-bg);
padding: 4rpx 14rpx;
border-radius: 10rpx;
font-weight: 600;
} }
.input-row { .picker-arrow {
display: flex; color: var(--primary-light);
align-items: center; font-size: 30rpx;
border-bottom: 2px solid var(--primary-light); width: 48rpx;
padding-bottom: 10rpx; height: 48rpx;
margin-bottom: 30rpx; display: flex;
align-items: center;
justify-content: center;
background: var(--primary-bg);
border-radius: 50%;
flex-shrink: 0;
}
/* ========== 天数输入模式1 ========== */
.days-input-group {
display: flex;
align-items: center;
background: #f8f9ff;
border-radius: 16rpx;
padding: 8rpx 24rpx;
margin-bottom: 20rpx;
border: 1rpx solid #eef0f8;
} }
.days-input { .days-input {
flex: 1; flex: 1;
font-size: 56rpx; font-size: 42rpx;
font-weight: bold; font-weight: 700;
color: var(--primary-color); color: var(--primary);
height: 80rpx; height: 72rpx;
font-family: -apple-system, 'SF Pro Display', 'Helvetica Neue', sans-serif;
} }
.input-unit { .days-suffix {
font-size: 32rpx; font-size: 26rpx;
color: #999; color: #b2bec3;
font-weight: 500;
flex-shrink: 0;
} }
.tags-row { /* ========== 快捷标签 ========== */
display: flex; .quick-tags {
gap: 20rpx; display: flex;
flex-wrap: wrap;
gap: 14rpx;
} }
.tag { .tag {
background: #f5f7fa; padding: 12rpx 26rpx;
color: #666; border-radius: 28rpx;
padding: 12rpx 24rpx; font-size: 24rpx;
border-radius: 30rpx; font-weight: 600;
font-size: 24rpx; border: 1rpx solid transparent;
font-weight: 500;
} }
.tag-red { .tag-plus {
background: #fff0f0; background: #f1f2f6; /* 莫兰迪浅灰 */
color: #ff4d4f; color: #747d8c;
border-color: #dfe4ea;
} }
/* 结果卡片 */ .tag-minus {
background: #fae1dd; /* 莫兰迪淡粉红 */
color: #c08081; /* 莫兰迪深砖红 */
border-color: #fceceb;
}
.tag-hover {
background: #ced6e0 !important;
color: #57606f !important;
border-color: #a4b0be !important;
transform: scale(0.95);
}
.tag-minus-hover {
background: #f5cdc9 !important;
color: #b37172 !important;
transform: scale(0.95);
}
/* ========== 结果卡片(通用) ========== */
.result-card { .result-card {
background: var(--primary-color); /* 渐变色更好吗?先用纯色突出 */ background: var(--accent-color); /* 莫兰迪色纯色背景 */
background: linear-gradient(135deg, #5b73e8 0%, #455ac0 100%); color: #fff;
color: #fff; text-align: center;
text-align: center; padding: 40rpx 32rpx;
padding: 50rpx; border-radius: var(--card-radius);
margin-bottom: 24rpx;
box-shadow: 0 8rpx 24rpx rgba(155, 142, 169, 0.35); /* 匹配紫色的阴影 */
position: relative;
overflow: hidden;
} }
.result-header { .result-card::after {
font-size: 24rpx; content: '';
opacity: 0.8; position: absolute;
margin-bottom: 20rpx; top: -50rpx;
right: -50rpx;
width: 160rpx;
height: 160rpx;
border-radius: 50%;
background: rgba(255,255,255,0.1); /* 稍微提高透明度 */
} }
.result-date { .result-label {
font-size: 64rpx; font-size: 21rpx;
font-weight: bold; opacity: 0.7;
margin-bottom: 10rpx; margin-bottom: 14rpx;
letter-spacing: 2rpx;
font-weight: 600;
} }
.result-week { .result-main {
font-size: 32rpx; font-size: 48rpx;
opacity: 0.9; font-weight: 700;
margin-bottom: 6rpx;
letter-spacing: 1rpx;
} }
/* 模式2日期间隔 */ .result-extra {
.date-row { font-size: 28rpx;
display: flex; opacity: 0.85;
justify-content: space-between; font-weight: 500;
align-items: center;
} }
.row-label { .result-tag {
font-size: 30rpx; font-size: 24rpx;
color: #666; opacity: 0.8;
font-weight: bold; margin-top: 12rpx;
background: rgba(255,255,255,0.12);
display: inline-block;
padding: 6rpx 18rpx;
border-radius: 14rpx;
} }
.date-pill { /* ========== 模式2日期间隔 (Compact) ========== */
background: #f5f7fa; .interval-card-compact {
padding: 16rpx 30rpx; display: flex;
border-radius: 12rpx; align-items: center;
font-size: 32rpx; justify-content: space-between;
color: #333; padding: 40rpx 24rpx; /* Increase vertical padding */
font-weight: 500;
display: flex;
align-items: center;
gap: 10rpx;
} }
.pill-icon { .iv-col {
font-size: 20rpx; flex: 1;
color: #999; display: flex;
flex-direction: column;
align-items: center;
} }
.timeline-connect { .iv-small-label {
display: flex; font-size: 24rpx;
align-items: center; /* 居中对齐 */ color: #b2bec3;
margin: 30rpx 0; margin-bottom: 16rpx;
padding-left: 20rpx; /* 微调对齐 */ font-weight: 500;
} }
.dots { .iv-pill {
width: 2px; background: #f8f9ff;
height: 40rpx; padding: 16rpx 32rpx; /* Wider padding */
border-left: 2px dashed #ddd; /* 使用左边框模拟竖线 */ border-radius: 20rpx;
margin: 0 auto; /* 水平居中 */ font-size: 34rpx;
font-weight: 600;
color: #2d3436;
border: 1rpx solid #eef0f8;
white-space: nowrap;
box-shadow: 0 2rpx 6rpx rgba(0,0,0,0.02);
transition: all 0.2s;
} }
/* 实际上我想做竖向的时间轴,但在 input-card 里横向可能更好看,但这设计是竖向列表? .iv-pill-hover {
重新设计 timeline 为竖向的连接线 background: var(--primary-bg);
*/ border-color: var(--primary-light);
.input-card { color: var(--primary);
position: relative; transform: translateY(-2rpx);
} }
/* 覆盖上面的 timeline-connect 为竖向视觉连接 */ .iv-connector {
.timeline-connect { width: 80rpx;
display: flex; display: flex;
flex-direction: column; /* 竖向 */ justify-content: center;
align-items: flex-start; /* 靠左对齐,或者根据布局 */ align-items: center;
margin: 30rpx 0; padding-top: 40rpx; /* Align with text */
padding-left: 0;
position: relative;
height: 60rpx; /* 高度 */
justify-content: center;
} }
.dots { .iv-arrow-icon {
/* 复用为无形占位或改用伪元素连线 */ font-size: 32rpx;
display: none; color: #b2bec3;
} font-weight: 700;
.timeline-connect::before {
content: '';
position: absolute;
left: 40rpx; /* 连接线的位置,约等于 row-label 的中心 */
top: -20rpx;
bottom: -20rpx;
width: 2rpx;
background: #e0e0e0;
z-index: 0;
}
.duration-badge {
position: relative;
z-index: 1;
background: #e6f7ff;
color: #1890ff;
font-size: 22rpx;
padding: 6rpx 20rpx;
border-radius: 20rpx;
margin-left: 20rpx; /* 避开左侧文字 */
align-self: center; /* 居中 */
} }
/* 间隔结果 */ /* 间隔结果 */
.interval-result .result-huge { .interval-num-row {
font-size: 80rpx; display: flex;
font-weight: bold; align-items: baseline;
margin-bottom: 10rpx; justify-content: center;
margin-bottom: 8rpx;
} }
.interval-result .unit { .interval-big-num {
font-size: 32rpx; font-size: 72rpx;
font-weight: normal; font-weight: 700;
margin-left: 10rpx; letter-spacing: 2rpx;
} }
.interval-result .result-sub { .interval-big-unit {
font-size: 28rpx; font-size: 28rpx;
opacity: 0.9; font-weight: 400;
border-top: 1px solid rgba(255,255,255,0.2); margin-left: 8rpx;
padding-top: 20rpx; opacity: 0.75;
display: inline-block; }
.result-detail-row {
display: flex;
align-items: center;
justify-content: center;
gap: 16rpx;
margin-top: 8rpx;
opacity: 0.8;
font-size: 24rpx;
}
.result-detail {
font-size: 24rpx;
}
.result-detail-sep {
opacity: 0.4;
font-size: 20rpx;
}
/* ========== 模式3农历转换 ========== */
/* 方向切换(行内文字链接样式 -> 按钮样式) */
.lunar-header-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 24rpx;
}
.dir-switch {
display: flex;
align-items: center;
gap: 8rpx;
padding: 12rpx 24rpx;
border-radius: 30rpx;
background: var(--primary-bg);
transition: all 0.2s;
border: 1rpx solid transparent; /* 防止抖动 */
}
.dir-switch-hover {
background: #e2e0ff; /* slightly darker primary-bg */
transform: scale(0.98);
}
.dir-switch-text {
font-size: 24rpx;
color: var(--primary);
font-weight: 700;
}
.dir-switch-icon {
font-size: 24rpx;
color: var(--primary);
font-weight: 700;
}
/* 农历结果卡片 */
.lunar-result-card {
text-align: center;
}
.lunar-hero {
font-size: 48rpx;
font-weight: 700;
margin-bottom: 6rpx;
letter-spacing: 3rpx;
}
.lunar-sub {
font-size: 26rpx;
opacity: 0.8;
margin-bottom: 16rpx;
font-weight: 500;
}
.lunar-tags {
display: flex;
flex-wrap: wrap;
gap: 10rpx;
justify-content: center;
border-top: 1rpx solid rgba(255,255,255,0.12);
padding-top: 16rpx;
}
.lunar-tag {
background: rgba(255,255,255,0.12);
border-radius: 12rpx;
padding: 8rpx 20rpx;
font-size: 24rpx;
font-weight: 600;
}
.lunar-tag.festival {
color: #ffeaa7;
}
.lunar-tag.term {
color: #55efc4;
}
.lunar-tag.solar {
color: #fff;
}
/* ========== 今日信息 ========== */
.today-card {
background: #fff;
border-radius: var(--card-radius);
padding: 28rpx 32rpx;
margin-bottom: 20rpx;
box-shadow: var(--card-shadow);
border-left: 5rpx solid var(--primary);
}
.today-title {
font-size: 28rpx;
font-weight: 700;
color: #2d3436;
margin-bottom: 16rpx;
}
.today-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 14rpx;
margin-bottom: 4rpx;
}
.today-cell {
background: #f8f9ff;
border-radius: 14rpx;
padding: 16rpx 20rpx;
display: flex;
flex-direction: column;
gap: 6rpx;
}
.today-cell-label {
font-size: 21rpx;
color: #b2bec3;
font-weight: 600;
}
.today-cell-value {
font-size: 28rpx;
color: #2d3436;
font-weight: 700;
}
.today-cell-value.accent {
color: var(--primary);
}
.today-term-bar {
display: flex;
align-items: center;
gap: 8rpx;
margin-top: 14rpx;
background: var(--primary-bg);
padding: 14rpx 20rpx;
border-radius: 14rpx;
}
.term-icon {
font-size: 24rpx;
}
.term-text {
font-size: 25rpx;
color: var(--primary);
font-weight: 600;
} }

View File

@@ -27,42 +27,10 @@
<text class="icon">🧮</text> <text class="icon">🧮</text>
</view> </view>
<view class="text-box"> <view class="text-box">
<text class="label">计算器</text> <text class="label">科学计算器</text>
<text class="sub-label">日常计算</text> <text class="sub-label">支持基础运算与科学函数</text>
</view>
</view>
<!-- 单位换算 -->
<view class="grid-item" bindtap="goToUnitConverter" hover-class="grid-item-hover">
<view class="icon-box bg-gradient-green">
<text class="icon">⚖️</text>
</view>
<view class="text-box">
<text class="label">单位换算</text>
<text class="sub-label">长度/重量...</text>
</view>
</view>
<!-- 谁去拿外卖 -->
<view class="grid-item" bindtap="goToRandomDraw" hover-class="grid-item-hover">
<view class="icon-box bg-gradient-purple">
<text class="icon">🎲</text>
</view>
<view class="text-box">
<text class="label">谁去拿外卖</text>
<text class="sub-label">随机抽取幸运儿</text>
</view>
</view>
<!-- 记分板 -->
<view class="grid-item" bindtap="goToScoreboard" hover-class="grid-item-hover">
<view class="icon-box bg-gradient-orange">
<text class="icon">📝</text>
</view>
<view class="text-box">
<text class="label">万能记分板</text>
<text class="sub-label">桌游/比赛记分</text>
</view> </view>
<view class="arrow-icon"></view>
</view> </view>
<!-- 日期计算 --> <!-- 日期计算 -->
@@ -71,8 +39,32 @@
<text class="icon">📅</text> <text class="icon">📅</text>
</view> </view>
<view class="text-box"> <view class="text-box">
<text class="label">日期计算</text> <text class="label">日期计算</text>
<text class="sub-label">天数推算</text> <text class="sub-label">日期推算 · 间隔计算 · 农历转换</text>
</view>
<view class="arrow-icon"></view>
</view>
</view>
<!-- 功能亮点 -->
<view class="feature-highlights">
<view class="highlight-title">功能亮点</view>
<view class="highlight-list">
<view class="highlight-item">
<text class="highlight-dot">●</text>
<text>计算器支持基础四则运算和科学模式</text>
</view>
<view class="highlight-item">
<text class="highlight-dot">●</text>
<text>计算历史记录,随时回看</text>
</view>
<view class="highlight-item">
<text class="highlight-dot">●</text>
<text>日期推算与天数间隔计算</text>
</view>
<view class="highlight-item">
<text class="highlight-dot">●</text>
<text>公历农历互转,节日速查</text>
</view> </view>
</view> </view>
</view> </view>

View File

@@ -3,7 +3,7 @@ page {
height: 100vh; height: 100vh;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
background-color: #f6f7f9; background-color: #f5f6fa;
} }
.scrollarea { .scrollarea {
flex: 1; flex: 1;
@@ -19,24 +19,39 @@ page {
.header-bg { .header-bg {
width: 100%; width: 100%;
height: 380rpx; height: 380rpx;
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); background: linear-gradient(135deg, #6c5ce7 0%, #a29bfe 100%);
border-bottom-left-radius: 40rpx; border-bottom-left-radius: 48rpx;
border-bottom-right-radius: 40rpx; border-bottom-right-radius: 48rpx;
padding: 40rpx; padding: 40rpx;
box-sizing: border-box; box-sizing: border-box;
display: flex; display: flex;
justify-content: center; justify-content: center;
/* 加上一点阴影 */ box-shadow: 0 12rpx 32rpx rgba(108, 92, 231, 0.25);
box-shadow: 0 10rpx 20rpx rgba(79, 172, 254, 0.2); position: relative;
overflow: hidden;
}
.header-bg::after {
content: '';
position: absolute;
top: -80rpx;
right: -60rpx;
width: 240rpx;
height: 240rpx;
border-radius: 50%;
background: rgba(255,255,255,0.08);
pointer-events: none; /* Prevent blocking clicks */
} }
.header-content { .header-content {
width: 100%; width: 100%;
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; /* 垂直居中 */ align-items: center;
margin-top: 20rpx; margin-top: 20rpx;
height: 140rpx; /* 固定高度确保对齐 */ height: 140rpx;
position: relative; /* Ensure z-index works */
z-index: 2; /* Ensure it is above the background decoration */
} }
.text-area { .text-area {
@@ -46,148 +61,202 @@ page {
} }
.greeting { .greeting {
color: rgba(255, 255, 255, 0.9); color: rgba(255, 255, 255, 0.85);
font-size: 28rpx; font-size: 26rpx;
margin-bottom: 10rpx; margin-bottom: 10rpx;
font-weight: 500;
} }
.nickname { .nickname {
color: #ffffff; color: #ffffff;
font-size: 48rpx; font-size: 44rpx;
font-weight: bold; font-weight: 700;
letter-spacing: 2rpx; letter-spacing: 2rpx;
} }
.avatar-wrapper { .avatar-wrapper {
padding: 6rpx; padding: 4rpx;
background: rgba(255, 255, 255, 0.3); background: rgba(255, 255, 255, 0.25);
border-radius: 50%; border-radius: 50%;
} }
.avatar { .avatar {
width: 120rpx; width: 110rpx;
height: 120rpx; height: 110rpx;
border-radius: 50%; border-radius: 50%;
background-color: #fff; background-color: #fff;
border: 4rpx solid #ffffff; border: 3rpx solid #ffffff;
} }
/* 主要内容区 */ /* 主要内容区 */
.main-content { .main-content {
flex: 1; flex: 1;
width: 100%; width: 100%;
padding: 0 30rpx; padding: 0 28rpx;
box-sizing: border-box; box-sizing: border-box;
margin-top: -80rpx; /* 向上浮动覆盖 Header */ margin-top: -80rpx;
z-index: 10; z-index: 10;
padding-bottom: 40rpx; padding-bottom: 60rpx;
} }
.section-title { .section-title {
font-size: 32rpx; font-size: 32rpx;
font-weight: bold; font-weight: 700;
color: #333; color: #2d3436;
margin-left: 10rpx; margin-left: 10rpx;
margin-bottom: 20rpx; margin-bottom: 20rpx;
display: none; /* 卡片式设计不需要标题也行,或保留 */ display: none;
} }
/* 一行两列或三列的卡片 */ /* 卡片列表 */
.grid-container { .grid-container {
display: flex; display: flex;
flex-direction: column; /* 垂直排列的卡片列表看起来更像菜单 */ flex-direction: column;
gap: 24rpx; gap: 20rpx;
} }
/* 单个卡片样式 */ /* 单个卡片 */
.grid-item { .grid-item {
background-color: #ffffff; background-color: #ffffff;
border-radius: 24rpx; border-radius: 28rpx;
padding: 30rpx; padding: 32rpx;
display: flex; display: flex;
align-items: center; align-items: center;
box-shadow: 0 4rpx 16rpx rgba(0,0,0,0.04); box-shadow: 0 8rpx 32rpx rgba(108, 92, 231, 0.08);
transition: all 0.2s ease;
} }
.grid-item-hover { .grid-item-hover {
transform: scale(0.98); transform: scale(0.97);
background-color: #fafafa; background-color: #f8f7ff;
} }
.icon-box { .icon-box {
width: 100rpx; width: 96rpx;
height: 100rpx; height: 96rpx;
border-radius: 20rpx; border-radius: 24rpx;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
margin-right: 30rpx; margin-right: 28rpx;
flex-shrink: 0;
} }
.icon { .icon {
font-size: 50rpx; font-size: 46rpx;
} }
.bg-gradient-blue { .bg-gradient-blue {
background: linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%); background: linear-gradient(135deg, #e0e6ff 0%, #c7d2fe 100%);
}
.bg-gradient-pink {
background: linear-gradient(135deg, #fce4ec 0%, #f8bbd0 100%);
} }
.bg-gradient-green { .bg-gradient-green {
background: linear-gradient(135deg, #d299c2 0%, #fef9d7 100%); background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
} }
/* 替换为柔和的莫兰迪色或渐变 */ .bg-gradient-orange {
.bg-gradient-blue { background: #e3f2fd; color: #2196f3; } background: linear-gradient(135deg, #fff3e0 0%, #ffe0b2 100%);
.bg-gradient-green { background: #e8f5e9; color: #4caf50; } }
.bg-gradient-orange { background: #fff3e0; color: #ff9800; } .bg-gradient-purple {
.bg-gradient-purple { background: #f3e5f5; color: #9c27b0; } background: linear-gradient(135deg, #f3e5f5 0%, #e1bee7 100%);
.bg-gradient-pink { background: #ffebee; color: #e91e63; } }
.text-box { .text-box {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
flex: 1;
min-width: 0;
} }
.label { .label {
font-size: 32rpx; font-size: 32rpx;
color: #333; color: #2d3436;
font-weight: 600; font-weight: 700;
margin-bottom: 6rpx; margin-bottom: 6rpx;
} }
.sub-label { .sub-label {
font-size: 24rpx; font-size: 23rpx;
color: #999; color: #b2bec3;
font-weight: 500;
} }
/* Banner */ /* Banner */
.banner { .banner {
margin-top: 40rpx; margin-top: 30rpx;
background: linear-gradient(to right, #4facfe, #00f2fe); background: linear-gradient(135deg, #6c5ce7 0%, #a29bfe 100%);
border-radius: 24rpx; border-radius: 28rpx;
padding: 30rpx 40rpx; padding: 28rpx 36rpx;
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
box-shadow: 0 8rpx 20rpx rgba(79, 172, 254, 0.3); box-shadow: 0 12rpx 32rpx rgba(108, 92, 231, 0.3);
} }
.banner-text { .banner-text {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
} }
.banner-title { .banner-title {
color: #fff; color: #fff;
font-weight: bold; font-weight: 700;
font-size: 30rpx; font-size: 30rpx;
margin-bottom: 8rpx; margin-bottom: 8rpx;
} }
.banner-desc { .banner-desc {
color: rgba(255,255,255,0.8); color: rgba(255,255,255,0.8);
font-size: 24rpx; font-size: 24rpx;
} }
.banner-icon { .banner-icon {
font-size: 40rpx; font-size: 40rpx;
}
/* 箭头 */
.arrow-icon {
margin-left: auto;
font-size: 40rpx;
color: #ddd;
font-weight: 300;
flex-shrink: 0;
padding-left: 12rpx;
}
/* 功能亮点区 */
.feature-highlights {
margin-top: 24rpx;
background: #ffffff;
border-radius: 28rpx;
padding: 32rpx 36rpx;
box-shadow: 0 8rpx 32rpx rgba(108, 92, 231, 0.08);
}
.highlight-title {
font-size: 28rpx;
font-weight: 700;
color: #2d3436;
margin-bottom: 20rpx;
letter-spacing: 1rpx;
}
.highlight-list {
display: flex;
flex-direction: column;
gap: 18rpx;
}
.highlight-item {
display: flex;
align-items: center;
gap: 14rpx;
font-size: 25rpx;
color: #636e72;
font-weight: 500;
}
.highlight-dot {
font-size: 14rpx;
color: #6c5ce7;
} }

View File

@@ -0,0 +1,310 @@
/**
* 农历(阴历)工具库
* 支持 1900-2100 年的公历/农历互转
*/
// 农历数据表 (1900-2100)
// 每个元素编码该年的农历信息:
// - 低 12 bit: 各月大小 (1=30天, 0=29天)
// - 第 13-16 bit: 闰月月份 (0=无闰月)
// - 第 17-20 bit: 闰月大小 (0=29天, 1=30天)
const lunarInfo: number[] = [
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0,
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6,
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x05ac0, 0x0ab60, 0x096d5, 0x092e0,
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
0x05aa0, 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0,
0x14b63, 0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0,
0x092e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4,
0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, 0x052b0,
0x0b273, 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, 0x04b60, 0x0a570, 0x054e4, 0x0d160,
0x0e968, 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a4d0, 0x0d150, 0x0f252,
0x0d520
];
// 天干
const tianGan = ['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸'];
// 地支
const diZhi = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'];
// 生肖
const shengXiao = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
// 农历月份名
const lunarMonthNames = ['正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊'];
// 农历日名
const lunarDayNames = [
'初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十',
'十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十',
'廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '三十'
];
// 24节气
const solarTermNames = [
'小寒', '大寒', '立春', '雨水', '惊蛰', '春分',
'清明', '谷雨', '立夏', '小满', '芒种', '夏至',
'小暑', '大暑', '立秋', '处暑', '白露', '秋分',
'寒露', '霜降', '立冬', '小雪', '大雪', '冬至'
];
// 节气数据 (1900-2100) 用 delta 编码
const sTermInfo = [
0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551,
218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447,
419210, 440795, 462224, 483532, 504758
];
/**
* 返回农历 year 年的总天数
*/
function lunarYearDays(year: number): number {
let sum = 348; // 12 * 29
for (let i = 0x8000; i > 0x8; i >>= 1) {
sum += (lunarInfo[year - 1900] & i) ? 1 : 0;
}
return sum + leapDays(year);
}
/**
* 返回农历 year 年闰月的天数,无闰月返回 0
*/
function leapDays(year: number): number {
if (leapMonth(year)) {
return (lunarInfo[year - 1900] & 0x10000) ? 30 : 29;
}
return 0;
}
/**
* 返回农历 year 年闰月月份,无闰月返回 0
*/
function leapMonth(year: number): number {
return lunarInfo[year - 1900] & 0xf;
}
/**
* 返回农历 year 年 month 月的天数
*/
function monthDays(year: number, month: number): number {
return (lunarInfo[year - 1900] & (0x10000 >> month)) ? 30 : 29;
}
/**
* 公历转农历
*/
export function solarToLunar(year: number, month: number, day: number) {
// 基准日期: 1900年1月31日 = 农历1900年正月初一
const baseDate = new Date(1900, 0, 31);
const targetDate = new Date(year, month - 1, day);
let offset = Math.floor((targetDate.getTime() - baseDate.getTime()) / 86400000);
// 计算农历年
let lunarYear = 1900;
let temp = 0;
for (lunarYear = 1900; lunarYear < 2101 && offset > 0; lunarYear++) {
temp = lunarYearDays(lunarYear);
offset -= temp;
}
if (offset < 0) {
offset += temp;
lunarYear--;
}
// 闰月
const leap = leapMonth(lunarYear);
let isLeap = false;
let lunarMonth = 1;
for (let i = 1; i < 13 && offset > 0; i++) {
// 闰月
if (leap > 0 && i === (leap + 1) && !isLeap) {
--i;
isLeap = true;
temp = leapDays(lunarYear);
} else {
temp = monthDays(lunarYear, i);
}
if (isLeap && i === (leap + 1)) {
isLeap = false;
}
offset -= temp;
if (!isLeap) {
lunarMonth = i;
}
}
if (offset === 0 && leap > 0 && lunarMonth === leap + 1) {
if (isLeap) {
isLeap = false;
} else {
isLeap = true;
--lunarMonth;
}
}
if (offset < 0) {
offset += temp;
--lunarMonth;
if (lunarMonth <= 0) {
lunarMonth = 12;
lunarYear--;
}
}
const lunarDay = offset + 1;
// 天干地支
const ganIndex = (lunarYear - 4) % 10;
const zhiIndex = (lunarYear - 4) % 12;
const ganZhi = tianGan[ganIndex] + diZhi[zhiIndex];
const animal = shengXiao[zhiIndex];
return {
year: lunarYear,
month: lunarMonth,
day: lunarDay,
isLeap: isLeap,
monthName: (isLeap ? '闰' : '') + lunarMonthNames[lunarMonth - 1] + '月',
dayName: lunarDayNames[lunarDay - 1],
ganZhi: ganZhi,
animal: animal,
// 完整中文表示
fullText: `${ganZhi}年(${animal}年)${isLeap ? '闰' : ''}${lunarMonthNames[lunarMonth - 1]}${lunarDayNames[lunarDay - 1]}`
};
}
/**
* 农历转公历
*/
export function lunarToSolar(lunarYear: number, lunarMonth: number, lunarDay: number, isLeapMonth: boolean = false) {
// 检查闰月是否有效
const leap = leapMonth(lunarYear);
if (isLeapMonth && leap !== lunarMonth) {
// 该年该月不是闰月,回退为非闰
isLeapMonth = false;
}
// 计算从基准日偏移天数
let offset = 0;
for (let y = 1900; y < lunarYear; y++) {
offset += lunarYearDays(y);
}
// 加上本年各月天数
let leapM = leapMonth(lunarYear);
let isAfterLeap = false;
for (let m = 1; m < lunarMonth; m++) {
// 如果有闰月且在目标月之前
if (leapM > 0 && m === leapM && !isAfterLeap) {
offset += leapDays(lunarYear);
isAfterLeap = true;
}
offset += monthDays(lunarYear, m);
}
// 如果目标就是闰月
if (isLeapMonth) {
offset += monthDays(lunarYear, lunarMonth);
}
// 如果闰月在目标月之前或等于目标月且不是闰月
if (!isLeapMonth && leapM > 0 && leapM === lunarMonth && !isAfterLeap) {
// 不需要额外操作
}
offset += lunarDay - 1;
// 基准日期: 1900年1月31日
const baseDate = new Date(1900, 0, 31);
const targetDate = new Date(baseDate.getTime() + offset * 86400000);
return {
year: targetDate.getFullYear(),
month: targetDate.getMonth() + 1,
day: targetDate.getDate()
};
}
/**
* 获取某年某月某日的节气(如果有的话)
*/
export function getSolarTerm(year: number, month: number, day: number): string | null {
// 每个月有两个节气
const termIndex1 = (month - 1) * 2; // 本月第一个节气
const termIndex2 = termIndex1 + 1; // 本月第二个节气
const d1 = getSolarTermDate(year, termIndex1);
const d2 = getSolarTermDate(year, termIndex2);
if (day === d1) return solarTermNames[termIndex1];
if (day === d2) return solarTermNames[termIndex2];
return null;
}
/**
* 计算某年第 n 个节气的日期(返回日)
*/
function getSolarTermDate(year: number, n: number): number {
const offDate = new Date((31556925974.7 * (year - 1900) + sTermInfo[n] * 60000) + Date.UTC(1900, 0, 6, 2, 5));
return offDate.getUTCDate();
}
/**
* 获取农历年份的天干地支和生肖
*/
export function getYearInfo(year: number) {
const ganIndex = (year - 4) % 10;
const zhiIndex = (year - 4) % 12;
return {
ganZhi: tianGan[ganIndex] + diZhi[zhiIndex],
animal: shengXiao[zhiIndex]
};
}
/**
* 获取农历月份名
*/
export function getLunarMonthName(month: number, isLeap: boolean = false): string {
return (isLeap ? '闰' : '') + lunarMonthNames[month - 1] + '月';
}
/**
* 获取农历日名
*/
export function getLunarDayName(day: number): string {
return lunarDayNames[day - 1];
}
/**
* 获取常见节日
*/
export function getFestival(month: number, day: number): string | null {
const solarFestivals: Record<string, string> = {
'1-1': '元旦', '2-14': '情人节', '3-8': '妇女节', '3-12': '植树节',
'4-1': '愚人节', '5-1': '劳动节', '5-4': '青年节', '6-1': '儿童节',
'7-1': '建党节', '8-1': '建军节', '9-10': '教师节', '10-1': '国庆节',
'10-31': '万圣节', '12-24': '平安夜', '12-25': '圣诞节'
};
return solarFestivals[`${month}-${day}`] || null;
}
/**
* 获取农历节日
*/
export function getLunarFestival(month: number, day: number): string | null {
const lunarFestivals: Record<string, string> = {
'1-1': '春节', '1-15': '元宵节', '2-2': '龙抬头',
'5-5': '端午节', '7-7': '七夕', '7-15': '中元节',
'8-15': '中秋节', '9-9': '重阳节', '12-8': '腊八节',
'12-23': '小年', '12-30': '除夕'
};
return lunarFestivals[`${month}-${day}`] || null;
}