From 407332c938ea57c67747895bc30a0cfa86506ddb Mon Sep 17 00:00:00 2001
From: hamster1963 <1410514192@qq.com>
Date: Thu, 19 Dec 2024 13:48:13 +0800
Subject: [PATCH] feat: refactor public note
---
bun.lockb | Bin 184880 -> 185228 bytes
package.json | 1 +
src/components/PlanInfo.tsx | 69 +++++++++++++
src/components/RemainPercentBar.tsx | 21 ++++
src/components/ServerCard.tsx | 144 +++-------------------------
src/components/ServerCardInline.tsx | 142 +++------------------------
src/components/billingInfo.tsx | 61 ++++++++++++
src/lib/utils.ts | 109 ++++++++++++++++++++-
8 files changed, 285 insertions(+), 262 deletions(-)
create mode 100644 src/components/PlanInfo.tsx
create mode 100644 src/components/RemainPercentBar.tsx
create mode 100644 src/components/billingInfo.tsx
diff --git a/bun.lockb b/bun.lockb
index 9041504de22748fc6a89cdc58f84cfbd88651c31..3438f771eefb1477cb98b21eafeb28d71c40572e 100755
GIT binary patch
delta 23284
zcmeHvc|aEB-uB!hk1`-CiUJ}5iYtl&!UKXlSZ;t=nug{UDk>_u;6|n3R$yV_WVfc4
zwj3qX_Q=ZI>L?W@wrQo=qPb>jmYB sI_HI)!x8rLo
z%uYKArhX92&MN}@W53E
+ {parsedData.planDataMod.bandwidth}
+
+ {parsedData.planDataMod.trafficVol}
+
+ IPv4
+
+ IPv6
+
+ {parsedData.planDataMod.networkRoute.split(",").map((route) => {
+ return route
+ })}
+
+ {parsedData.planDataMod.extra.split(",").map((extra) => {
+ return extra
+ })}
+
- 剩余时间: {isNeverExpire ? "永久" : daysLeft + "天"}
-
- 价格: {parsedData.billingDataMod.amount}/{parsedData.billingDataMod.cycle}
- 免费 按量收费
- 已过期: {daysLeft * -1} 天
-
- 价格: {parsedData.billingDataMod.amount}/{parsedData.billingDataMod.cycle}
- 免费 按量收费
- {parsedData.planDataMod.bandwidth}
-
- {parsedData.planDataMod.trafficVol}
-
+
{name}
- 剩余时间: {isNeverExpire ? "永久" : daysLeft + "天"}
-
- 价格: {parsedData.billingDataMod.amount}/{parsedData.billingDataMod.cycle}
- 免费 按量收费
- 已过期: {daysLeft * -1} 天
-
- 价格: {parsedData.billingDataMod.amount}/{parsedData.billingDataMod.cycle}
- 免费 按量收费
- {parsedData.planDataMod.bandwidth}
-
- {parsedData.planDataMod.trafficVol}
-
z;lq;3VYB3MlQU+_$Q-Lo%FLa`l5?|7zZLM~dd$q3IU{Q_JAB&A%;~wx4Ajr4an97~
znVE{R7=CQ;8R)EG#+WJ7*#w(&5s&rmk)J%(bjZ|&r*B@rY37`skde~~RT!T!(I{Q<
zpROp)k&Oe)ahW+itJB0BMVUJD?%Xj`#%A7*?AYxtFylkOoH#yU&X0^4Cfo6<6NBiDSI
zc`Ab^_KZ7W^)%v`%M-BJJ7$leoartt3SB=O8a7Mx%EPdjGiKTJcn5nP)okl@G{JRi
ztnSr1UZm>DIWDajHcm>`{MZKzJBm4iYTFsQ$BaaAx1K!1rEQo|<=EP(9f!s0Ap3Ss
zwbM*J1v78-OtYo99zY#;z+(5n!pVJ>*~e%MDt9|9Rv%L|)TuoPiyef)MVI_wSw_>e
z(AoChPR}W@*m>qOD1qgK)xelGhxNkQF4b?2{{HMlZPpxfjTu|H*sdqf!8qiaCoEJU
z$*IkP#i7)2E#uT)gJsTU*8Hp$=V7#?%{}%ov+6TonWG6yysB&WxEw!0>!z2_j@R1V
zTh%SdXELnnGS@X#;Jd+0mv8C+hV%Za
{AnZYCYL;v@|tmwyJ`jvpWrJuCo%fQU
- 剩余时间: {isNeverExpire ? "永久" : daysLeft + "天"} -
- {parsedData.billingDataMod.amount && - parsedData.billingDataMod.amount !== "0" && - parsedData.billingDataMod.amount !== "-1" ? ( -- 价格: {parsedData.billingDataMod.amount}/{parsedData.billingDataMod.cycle} -
- ) : parsedData.billingDataMod.amount === "0" ? ( -免费
- ) : parsedData.billingDataMod.amount === "-1" ? ( -按量收费
- ) : null} - > - ) : ( - <> -- 已过期: {daysLeft * -1} 天 -
- {parsedData.billingDataMod.amount && - parsedData.billingDataMod.amount !== "0" && - parsedData.billingDataMod.amount !== "-1" ? ( -- 价格: {parsedData.billingDataMod.amount}/{parsedData.billingDataMod.cycle} -
- ) : parsedData.billingDataMod.amount === "0" ? ( -免费
- ) : parsedData.billingDataMod.amount === "-1" ? ( -按量收费
- ) : null} - > - ))} - {parsedData?.planDataMod && ( -- {parsedData.planDataMod.bandwidth} -
- )} - {parsedData.planDataMod.trafficVol !== "" && ( -- {parsedData.planDataMod.trafficVol} -
- )} -- 剩余时间: {isNeverExpire ? "永久" : daysLeft + "天"} -
- {parsedData.billingDataMod.amount && - parsedData.billingDataMod.amount !== "0" && - parsedData.billingDataMod.amount !== "-1" ? ( -- 价格: {parsedData.billingDataMod.amount}/{parsedData.billingDataMod.cycle} -
- ) : parsedData.billingDataMod.amount === "0" ? ( -免费
- ) : parsedData.billingDataMod.amount === "-1" ? ( -按量收费
- ) : null} - > - ) : ( - <> -- 已过期: {daysLeft * -1} 天 -
- {parsedData.billingDataMod.amount && - parsedData.billingDataMod.amount !== "0" && - parsedData.billingDataMod.amount !== "-1" ? ( -- 价格: {parsedData.billingDataMod.amount}/{parsedData.billingDataMod.cycle} -
- ) : parsedData.billingDataMod.amount === "0" ? ( -免费
- ) : parsedData.billingDataMod.amount === "-1" ? ( -按量收费
- ) : null} - > - ))} - {parsedData?.planDataMod && ( -- {parsedData.planDataMod.bandwidth} -
- )} - {parsedData.planDataMod.trafficVol !== "" && ( -- {parsedData.planDataMod.trafficVol} -
- )} -+ 价格: {parsedData.billingDataMod.amount}/{parsedData.billingDataMod.cycle} +
+ ) : parsedData.billingDataMod.amount === "0" ? ( +免费
+ ) : parsedData.billingDataMod.amount === "-1" ? ( +按量收费
+ ) : null} ++ 已过期: {daysLeftObject.days * -1} 天 +
+ {parsedData.billingDataMod.amount && + parsedData.billingDataMod.amount !== "0" && + parsedData.billingDataMod.amount !== "-1" ? ( ++ 价格: {parsedData.billingDataMod.amount}/{parsedData.billingDataMod.cycle} +
+ ) : parsedData.billingDataMod.amount === "0" ? ( +免费
+ ) : parsedData.billingDataMod.amount === "-1" ? ( +按量收费
+ ) : null} + > + ) +} diff --git a/src/lib/utils.ts b/src/lib/utils.ts index d04c9be..3777f5b 100644 --- a/src/lib/utils.ts +++ b/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 }