feat(license): 试用额度按自然日0点重置(北京时间),额度当天首次使用才起算
- todayStartOf()=floor((now+8h)/24h)*24h-8h,epoch计算与环境时区无关 - 起点不早于今天00:00,昨天起点跨天自动失效;重置时刻恒为次日00:00 - startTrial()仅在无记录或记录早于今日0点时写起点 -> 额度不会从0点空耗 - 移除 TRIAL_CYCLE_MS/TRIAL_CYCLE_H;服务端 trial_allowed 同步同算法 - 浮条默认(每日0点重置),<=5min显示(明天00:00重置) - 验证:服务端/check 7/7 + 前端五态(含两个跨天用例)全通过
This commit is contained in:
1 parent
2e090903c2
commit
e1ae8a7bd8
4 files changed
+43
-34
No files matched your search
@@ -48,10 +48,10 @@ const urgency = computed(() => {
|
||||
/** 本轮已用完时的下一轮重置时刻提示(如「明天 14:23」) */
|
||||
const resetHint = computed(() => (protectedTool.value ? trialResetHint(props.path, now.value) : ''));
|
||||
|
||||
/** 浮条上的重置提示:临近耗尽时给出具体时刻(给出口、消除"永久失去"恐惧),平时只说每天重置 */
|
||||
/** 浮条上的重置提示:重置恒为次日 0 点;临近耗尽时把具体时刻显出来 */
|
||||
const resetTag = computed(() => {
|
||||
if (remaining.value <= 5 * 60 * 1000 && resetHint.value) return `${resetHint.value} 重置`;
|
||||
return '每天重置';
|
||||
return '每日 0 点重置';
|
||||
});
|
||||
|
||||
async function submitCard() {
|
||||
|
||||
@@ -23,8 +23,9 @@ export const PROTECTED_TOOLS: Record<string, string> = {
|
||||
'/ai-gen': 'AI 生图',
|
||||
};
|
||||
|
||||
export const TRIAL_MS = 30 * 60 * 1000; // 每个功能每轮免费 30 分钟
|
||||
export const TRIAL_CYCLE_MS = 24 * 60 * 60 * 1000; // 每 24 小时重置一轮(买断制下给用户第二次触达机会)
|
||||
export const TRIAL_MS = 30 * 60 * 1000; // 每个功能每天免费 30 分钟
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
const TZ_OFFSET_MS = 8 * 60 * 60 * 1000; // 北京时间 UTC+8 —— 重置对齐自然日 0 点
|
||||
|
||||
const MACHINE_KEY = 'it-tools-machine-id';
|
||||
const TRIAL_KEY = 'it-tools-trials';
|
||||
@@ -58,49 +59,45 @@ export function getTrialStart(path: string): number | null {
|
||||
return readTrials()[path] ?? null;
|
||||
}
|
||||
|
||||
/** 当天 00:00(北京时间)对应的时间戳 */
|
||||
function todayStartOf(now: number): number {
|
||||
return Math.floor((now + TZ_OFFSET_MS) / DAY_MS) * DAY_MS - TZ_OFFSET_MS;
|
||||
}
|
||||
|
||||
export function startTrial(path: string): void {
|
||||
const t = readTrials();
|
||||
const now = Date.now();
|
||||
const start = t[path];
|
||||
// 首次进入,或距本窗口起点已满一个周期 → 立刻开新窗口
|
||||
// (否则额度会从上一轮起点空耗掉,用户不在场也算用过)
|
||||
if (!start || now - start >= TRIAL_CYCLE_MS) {
|
||||
// 首次进入,或已跨到新的一天 → 立刻开新一天的额度
|
||||
// (额度只在用户当天真的进来时才起算,不会从 0 点空耗)
|
||||
if (!start || start < todayStartOf(now)) {
|
||||
t[path] = now;
|
||||
writeTrials(t);
|
||||
}
|
||||
}
|
||||
|
||||
/** 当前轮次起点:锚定首次进入时间,每 TRIAL_CYCLE_MS 滚动一次 */
|
||||
function cycleStartOf(start: number, now: number): number {
|
||||
if (now <= start) return start;
|
||||
return start + Math.floor((now - start) / TRIAL_CYCLE_MS) * TRIAL_CYCLE_MS;
|
||||
}
|
||||
|
||||
export function remainingMs(path: string, now: number): number {
|
||||
const start = getTrialStart(path);
|
||||
if (start === null) {
|
||||
return TRIAL_MS; // 尚未开始计时
|
||||
}
|
||||
return Math.max(0, TRIAL_MS - (now - cycleStartOf(start, now)));
|
||||
// 起点不早于今天 0 点:昨天的起点在跨天后自动失效
|
||||
const s = Math.max(start, todayStartOf(now));
|
||||
return Math.max(0, TRIAL_MS - (now - s));
|
||||
}
|
||||
|
||||
/** 本轮的下一次重置时刻(ms 时间戳);未开始计时返回 null。无论本轮是否还有剩余都返回 */
|
||||
/** 下一次重置时刻(恒为次日 00:00 北京时间);未开始计时返回 null */
|
||||
export function nextTrialResetAt(path: string, now: number): number | null {
|
||||
const start = getTrialStart(path);
|
||||
if (start === null) return null;
|
||||
return cycleStartOf(start, now) + TRIAL_CYCLE_MS;
|
||||
if (getTrialStart(path) === null) return null;
|
||||
return todayStartOf(now) + DAY_MS;
|
||||
}
|
||||
|
||||
/** 人类可读的重置提示,如「明天 14:23」/「今天 21:05」 */
|
||||
/** 人类可读的重置提示,恒为「明天 00:00」 */
|
||||
export function trialResetHint(path: string, now: number): string {
|
||||
const at = nextTrialResetAt(path, now);
|
||||
if (at === null) return '';
|
||||
const d = new Date(at);
|
||||
const hm = `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
|
||||
const start = getTrialStart(path);
|
||||
const cycleOfAt = cycleStartOf(start ?? now, at);
|
||||
const sameDay = new Date(cycleOfAt).toDateString() === new Date(now).toDateString();
|
||||
return `${sameDay ? '今天' : '明天'} ${hm}`;
|
||||
return `明天 ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
// ====== 授权状态 ======
|
||||
|
||||
Reference in new issue
Block a user