feat: 更新签到天数计算方法
This commit is contained in:
@@ -26,7 +26,7 @@ export function getTodayStr(): string {
|
||||
export function isChecked(taskId: number): boolean {
|
||||
const all = loadCheckIns()
|
||||
const today = getTodayStr()
|
||||
return !!(all[today] && all[today][taskId])
|
||||
return (all[today] && all[today][taskId])
|
||||
}
|
||||
|
||||
// 切换签到状态
|
||||
@@ -48,32 +48,21 @@ export function getStreak(taskId: number): number {
|
||||
let streak = 0
|
||||
const d = new Date()
|
||||
|
||||
// 如果今天没签到,从昨天开始算
|
||||
if (!all[getTodayStr()]?.[taskId]) {
|
||||
d.setDate(d.getDate() - 1)
|
||||
}
|
||||
|
||||
while (true) {
|
||||
const key = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
|
||||
if (all[key] && all[key][taskId]) {
|
||||
|
||||
if (all[key]?.[taskId]) {
|
||||
streak++
|
||||
d.setDate(d.getDate() - 1)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return streak
|
||||
}
|
||||
|
||||
// 今日已签到数量
|
||||
export function getTodayCheckedCount(): number {
|
||||
const all = loadCheckIns()
|
||||
const today = getTodayStr()
|
||||
if (!all[today]) return 0
|
||||
return Object.values(all[today]).filter(Boolean).length
|
||||
}
|
||||
|
||||
// 所有任务中最长连续天数
|
||||
export function getMaxStreak(taskList: { id: number }[]): number {
|
||||
let max = 0
|
||||
taskList.forEach(t => {
|
||||
const s = getStreak(t.id)
|
||||
if (s > max) max = s
|
||||
})
|
||||
return max
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user