mirror of
https://github.com/Buriburizaem0n/nezha-dash-v1.git
synced 2026-02-05 21:20:08 +00:00
feat: refactor public note
This commit is contained in:
109
src/lib/utils.ts
109
src/lib/utils.ts
@@ -1,5 +1,6 @@
|
||||
import { NezhaServer } from "@/types/nezha-api"
|
||||
import { type ClassValue, clsx } from "clsx"
|
||||
import dayjs from "dayjs"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
@@ -46,6 +47,112 @@ export function formatNezhaInfo(now: number, serverInfo: NezhaServer) {
|
||||
}
|
||||
}
|
||||
|
||||
export function getDaysBetweenDatesWithAutoRenewal({
|
||||
autoRenewal,
|
||||
cycle,
|
||||
startDate,
|
||||
endDate,
|
||||
}: BillingData): { days: number; cycleLabel: string; remainingPercentage: number } {
|
||||
let months = 1
|
||||
// 套餐资费
|
||||
let cycleLabel = cycle
|
||||
|
||||
switch (cycle.toLowerCase()) {
|
||||
case "月":
|
||||
case "m":
|
||||
case "mo":
|
||||
case "month":
|
||||
case "monthly":
|
||||
cycleLabel = "月"
|
||||
months = 1
|
||||
break
|
||||
case "年":
|
||||
case "y":
|
||||
case "yr":
|
||||
case "year":
|
||||
case "annual":
|
||||
cycleLabel = "年"
|
||||
months = 12
|
||||
break
|
||||
case "季":
|
||||
case "quarterly":
|
||||
cycleLabel = "季"
|
||||
months = 3
|
||||
break
|
||||
case "半":
|
||||
case "半年":
|
||||
case "h":
|
||||
case "half":
|
||||
case "semi-annually":
|
||||
cycleLabel = "半年"
|
||||
months = 6
|
||||
break
|
||||
default:
|
||||
cycleLabel = cycle
|
||||
break
|
||||
}
|
||||
|
||||
const nowTime = new Date().getTime()
|
||||
const endTime = dayjs(endDate).valueOf()
|
||||
|
||||
if (autoRenewal !== "1") {
|
||||
return {
|
||||
days: getDaysBetweenDates(endDate, new Date(nowTime).toISOString()),
|
||||
cycleLabel: cycleLabel,
|
||||
remainingPercentage:
|
||||
getDaysBetweenDates(endDate, new Date(nowTime).toISOString()) /
|
||||
dayjs(endDate).diff(startDate, "day") >
|
||||
1
|
||||
? 1
|
||||
: getDaysBetweenDates(endDate, new Date(nowTime).toISOString()) /
|
||||
dayjs(endDate).diff(startDate, "day"),
|
||||
}
|
||||
}
|
||||
|
||||
const nextTime = getNextCycleTime(endTime, months, nowTime)
|
||||
const diff = dayjs(nextTime).diff(dayjs(), "day") + 1
|
||||
const remainingPercentage = diff / (30 * months) > 1 ? 1 : diff / (30 * months)
|
||||
|
||||
console.log(
|
||||
"nextTime",
|
||||
nextTime,
|
||||
"diff",
|
||||
diff,
|
||||
"month",
|
||||
months,
|
||||
"remainingPercentage",
|
||||
remainingPercentage,
|
||||
)
|
||||
|
||||
return {
|
||||
days: diff,
|
||||
cycleLabel: cycleLabel,
|
||||
remainingPercentage: remainingPercentage,
|
||||
}
|
||||
}
|
||||
|
||||
// Thanks to hi2shark for the code
|
||||
// https://github.com/hi2shark/nazhua/blob/main/src/utils/date.js#L86
|
||||
export function getNextCycleTime(startDate: number, months: number, specifiedDate: number): number {
|
||||
const start = dayjs(startDate)
|
||||
const checkDate = dayjs(specifiedDate)
|
||||
|
||||
if (!start.isValid() || months <= 0) {
|
||||
throw new Error("参数无效:请检查起始日期、周期月份数和指定日期。")
|
||||
}
|
||||
|
||||
let nextDate = start
|
||||
|
||||
// 循环增加周期直到大于当前日期
|
||||
let whileStatus = true
|
||||
while (whileStatus) {
|
||||
nextDate = nextDate.add(months, "month")
|
||||
whileStatus = nextDate.valueOf() <= checkDate.valueOf()
|
||||
}
|
||||
|
||||
return nextDate.valueOf() // 返回时间毫秒数
|
||||
}
|
||||
|
||||
export function getDaysBetweenDates(date1: string, date2: string): number {
|
||||
const oneDay = 24 * 60 * 60 * 1000 // 一天的毫秒数
|
||||
const firstDate = new Date(date1)
|
||||
@@ -137,7 +244,7 @@ interface PlanData {
|
||||
extra: string
|
||||
}
|
||||
|
||||
interface PublicNoteData {
|
||||
export interface PublicNoteData {
|
||||
billingDataMod?: BillingData
|
||||
planDataMod?: PlanData
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user