refactor: price

This commit is contained in:
Tristan Yang
2023-01-09 09:41:58 +08:00
parent b9ba2b523d
commit 77e4edab9b
6 changed files with 36 additions and 20 deletions
@@ -9,19 +9,19 @@ import { useGetLicensePaymentUrlMutation } from "../../../app/services/server";
import { LicensePriceList } from "../../../app/config";
import { useTranslation } from "react-i18next";
import dayjs from "dayjs";
import { PriceSubscriptionDuration, PriceType } from "../../../types/common";
// import { LicenseMetadata, RenewLicense } from "../../types/server";
interface Props {
closeModal: () => void;
// domain: string;
}
type Mode = "subscription" | "payment";
type SubType = "month" | "quarter" | "year";
const getExpireDay = (sub_type: SubType) => {
const getExpireDay = (sub_dur: PriceSubscriptionDuration) => {
const currDate = dayjs();
// 默认10年
let res = currDate;
switch (sub_type) {
switch (sub_dur) {
case "year":
res = currDate.add(1, "year");
break;
@@ -31,7 +31,6 @@ const getExpireDay = (sub_type: SubType) => {
case "quarter":
res = currDate.add(3, "month");
break;
default:
break;
};
@@ -42,16 +41,16 @@ const LicensePriceListModal: FC<Props> = ({ closeModal }) => {
const { t: ct } = useTranslation();
const [getUrl, { isLoading, isSuccess }] = useGetLicensePaymentUrlMutation();
const [selectPrice, setSelectPrice] = useState(
`${LicensePriceList[0].pid}|${LicensePriceList[0].limit}|${LicensePriceList[0].mode || "payment"}|${LicensePriceList[0].sub_type || ""}`
`${LicensePriceList[0].pid}|${LicensePriceList[0].limit}|${LicensePriceList[0].type}|${LicensePriceList[0].sub_dur || ""}`
);
const handleRenew = async () => {
const [priceId, user_limit, mode, sub_type = "month"] = selectPrice.split("|") as [string, string, Mode, SubType];
const [priceId, user_limit, type, sub_dur = "month"] = selectPrice.split("|") as [string, string, PriceType, PriceSubscriptionDuration];
const resp = await getUrl({
mode: mode,
type,
priceId,
metadata: {
user_limit: Number(user_limit),
expire: mode == "subscription" ? getExpireDay(sub_type) : "2035-01-01",
expire: type == "subscription" ? getExpireDay(sub_dur) : getExpireDay("year"),
// 本地,则*通配符
domain: location.hostname.startsWith("localhost") ? "*" : location.hostname
},
@@ -87,7 +86,7 @@ const LicensePriceListModal: FC<Props> = ({ closeModal }) => {
>
<StyledRadio
options={LicensePriceList.map(({ title, desc }) => `${title} [${desc}]`)}
values={LicensePriceList.map(({ pid, limit, mode = "payment", sub_type = "month" }) => `${pid}|${limit}|${mode}|${sub_type}`)}
values={LicensePriceList.map(({ pid, limit, type = "payment", sub_dur = "month" }) => `${pid}|${limit}|${type}|${sub_dur}`)}
value={selectPrice}
onChange={(v) => {
console.log("wtff", v);