/** * 订阅对象的接口定义(根据你的后端数据结构调整) */ interface SubscriptionItem { expire_time?: string | number | null } export interface ExpireInfo { text: string highlight: boolean } /** * 格式化订阅到期信息 * @param sub 整个订阅对象 item */ export function formatExpireDate(sub?: SubscriptionItem | null): ExpireInfo { // 1. 处理对象不存在的情况 if (!sub || !sub.expire_time) { return { text: '尚未购买套餐', highlight: true } } // 2. 解析日期(兼容 ISO 字符串和时间戳) const timestamp = sub.expire_time const isMs = typeof timestamp === 'number' && timestamp > 10000000000 const expireDate = new Date( isMs ? timestamp : typeof timestamp === 'number' ? timestamp * 1000 : timestamp, ) if (isNaN(expireDate.getTime())) { return { text: '套餐信息无效', highlight: false } } // 3. 提取日期组件 const now = new Date() const isExpired = expireDate < now const y = expireDate.getFullYear() const m = String(expireDate.getMonth() + 1).padStart(2, '0') const d = String(expireDate.getDate()).padStart(2, '0') const dateStr = `${y}/${m}/${d}` // 4. 根据是否过期返回不同格式 if (isExpired) { return { text: `已于 ${dateStr} 到期`, highlight: true, } } const hh = String(expireDate.getHours()).padStart(2, '0') const mm = String(expireDate.getMinutes()).padStart(2, '0') const ss = String(expireDate.getSeconds()).padStart(2, '0') return { text: `到期时间:${dateStr} ${hh}:${mm}:${ss}`, highlight: false, } }