112 lines
2.5 KiB
TypeScript
112 lines
2.5 KiB
TypeScript
import type { User } from "./user"
|
||
|
||
export type Coupon = {
|
||
id: number
|
||
name: string
|
||
user_id: number
|
||
code: string
|
||
remark: string
|
||
amount: number
|
||
min_amount: number
|
||
count: number
|
||
status: number
|
||
expire_type: number
|
||
created_at: Date
|
||
updated_at: Date
|
||
expire_at: Date
|
||
expire_in: number
|
||
coupon: useCoupon
|
||
user: User
|
||
}
|
||
type useCoupon = {
|
||
id: number
|
||
name: string
|
||
expire_type: number
|
||
status: number
|
||
created_at: Date
|
||
}
|
||
|
||
// 优惠券使用状态
|
||
export const couponUseStatusMap = {
|
||
0: { text: "未使用", color: "text-green-600" },
|
||
1: { text: "已使用", color: "text-blue-600" },
|
||
2: { text: "已禁用", color: "text-green-600" },
|
||
} as const
|
||
|
||
// 优惠券状态
|
||
export const couponStatusMap = {
|
||
0: { text: "禁用", color: "text-yellow-600" },
|
||
1: { text: "正常", color: "text-green-600" },
|
||
} as const
|
||
|
||
// 优惠券过期类型
|
||
export const expireTypeMap = {
|
||
0: "不过期",
|
||
1: "固定日期",
|
||
2: "相对日期",
|
||
} as const
|
||
|
||
// 优惠券状态 & 使用状态
|
||
export const getStatus = (status: number, type: "coupon" | "use") => {
|
||
if (type === "coupon") {
|
||
return (
|
||
couponStatusMap[status as keyof typeof couponStatusMap] || {
|
||
text: "",
|
||
color: "text-gray-400",
|
||
}
|
||
)
|
||
}
|
||
return (
|
||
couponUseStatusMap[status as keyof typeof couponUseStatusMap] || {
|
||
text: "",
|
||
color: "text-gray-400",
|
||
}
|
||
)
|
||
}
|
||
|
||
export const getDaysToExpire = (expireAt: Date | string): number => {
|
||
if (!expireAt) return 0
|
||
|
||
const targetDate = new Date(expireAt)
|
||
const now = new Date()
|
||
|
||
const targetDay = new Date(
|
||
targetDate.getFullYear(),
|
||
targetDate.getMonth(),
|
||
targetDate.getDate(),
|
||
)
|
||
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
|
||
|
||
const diffTime = targetDay.getTime() - today.getTime()
|
||
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
|
||
|
||
return diffDays
|
||
}
|
||
|
||
// 过期类型
|
||
export const getExpireType = (expireType: number): string => {
|
||
return expireTypeMap[expireType as keyof typeof expireTypeMap] || ""
|
||
}
|
||
|
||
// 获取过期类型显示(带天数)
|
||
export const getExpireTypeText = (
|
||
expireType: number,
|
||
expireAt: Date,
|
||
): string => {
|
||
const typeText = getExpireType(expireType)
|
||
console.log(typeText, "typeText")
|
||
|
||
if (expireType === 0) return typeText
|
||
|
||
const days = getDaysToExpire(expireAt)
|
||
console.log(days, "days")
|
||
|
||
if (days === 0) {
|
||
return `${typeText}`
|
||
} else if (days > 0) {
|
||
return `${typeText}(${days}天后)`
|
||
} else {
|
||
return `${typeText}(已过期${Math.abs(days)}天)`
|
||
}
|
||
}
|