/** * 用户端账号到期信息(V146,2026-10-05) * * app-web 无 Pinia,登录态走 localStorage;到期时间同理:登录 / check_login 的 data 里带 * expiresAt(yyyy-MM-dd HH:mm)就落键,没有就删键——换账号登录时不能残留上一个账号的到期值。 * 只反映账号自身 expires_at(续费提醒用),级联停用判定全部在服务端,这里不做任何本地推断。 */ export const ACCOUNT_EXPIRY_STORAGE_KEY = 'account_expires_at' function storage(): Storage | null { if (typeof window === 'undefined') return null try { return window.localStorage } catch { return null } } /** 从登录/check_login 的 data 段落到期时间;缺字段或空白一律清键(= 永不过期/未知)。 */ export function writeAccountExpiry(data: unknown): void { const db = storage() if (!db) return const raw = data && typeof data === 'object' ? (data as { expiresAt?: unknown; expires_at?: unknown }).expiresAt ?? (data as { expires_at?: unknown }).expires_at : undefined const value = typeof raw === 'string' ? raw.trim() : '' if (value) db.setItem(ACCOUNT_EXPIRY_STORAGE_KEY, value) else db.removeItem(ACCOUNT_EXPIRY_STORAGE_KEY) } /** 当前账号到期时间字符串(yyyy-MM-dd HH:mm);无到期/未知返回空串。 */ export function readAccountExpiry(): string { const db = storage() if (!db) return '' try { return (db.getItem(ACCOUNT_EXPIRY_STORAGE_KEY) || '').trim() } catch { return '' } } /** 登出/切号/被顶下线时清键。 */ export function clearAccountExpiry(): void { const db = storage() if (!db) return try { db.removeItem(ACCOUNT_EXPIRY_STORAGE_KEY) } catch { /* 忽略 */ } } /** * 距到期还剩整天数(floor);无到期返回 null,已过期返回负数。 * 续费提醒按 0 ≤ days < 5 判定(剩余不足 5 天开始提醒,续到 ≥5 天消失)。 */ export function daysToAccountExpiry(): number | null { const raw = readAccountExpiry() if (!raw) return null const parsed = new Date(raw.replace(' ', 'T')) if (Number.isNaN(parsed.getTime())) return null return Math.floor((parsed.getTime() - Date.now()) / 86400_000) }