feat(license): 免费试用改为每24小时30分钟窗口,额度在用户进入时才开启

- 前端 startTrial/remainingMs: 距窗口起点满24h即刷新起点,额度不因用户缺席空耗
- 服务端 trial_allowed 同步同算法(TRIAL_CYCLE_H可配),前后端窗口一致,避免前端显示有额度而服务端403
- 锁定弹窗提示「明天 HH:MM 可再试」,Pricing/About/README/403文案统一为「每天/今日」
- 验证: 服务端 /check 6/6 + 前端 5/5 通过
This commit is contained in:
personal82555 committed 2026-10-14 21:17:45 +08:00
1 parent aacdd0e560
commit 1c53e2b188
10 files changed
+321 -43

No files matched your search

+1 -1
View File
@@ -204,7 +204,7 @@ async function submit() {
body: JSON.stringify(body),
});
if (res.status === 403) {
throw new Error('授权校验未通过:免费试用已结束,请购买授权或输入卡密');
throw new Error('授权校验未通过:今日免费试用已用完,请购买授权或输入卡密');
}
if (!res.ok) {
throw new Error(`处理失败 HTTP ${res.status}`);
+1 -1
View File
@@ -127,7 +127,7 @@ async function execute() {
fd.append('format', targetFmt.value);
const res = await fetch('/relay/convert', { method: 'POST', body: fd, headers: licenseHeaders('doc-convert') });
if (res.status === 403) {
throw new Error('授权校验未通过:免费试用已结束,请购买授权或输入卡密');
throw new Error('授权校验未通过:今日免费试用已用完,请购买授权或输入卡密');
}
if (!res.ok) {
let msg = `转换失败 HTTP ${res.status}`;
+7 -3
View File
@@ -10,6 +10,7 @@ import {
licenseState,
remainingMs,
startTrial,
trialResetHint,
} from './license';
const props = defineProps<{ path: string }>();
@@ -44,6 +45,9 @@ const urgency = computed(() => {
return 'normal';
});
/** 本轮已用完时的下一轮重置时刻提示(如「明天 14:23」) */
const resetHint = computed(() => (protectedTool.value ? trialResetHint(props.path, now.value) : ''));
async function submitCard() {
if (licenseState.authorized
&& !window.confirm('将撤销本机当前授权并用新卡密激活,确定换卡?')) {
@@ -145,7 +149,7 @@ onUnmounted(() => {
免费试用 {{ displayName }} 剩余 <b>{{ mmss }}</b>
</template>
<template v-else>
{{ displayName }} · 免费试用 30 分钟
{{ displayName }} · 今日免费试用 30 分钟
</template>
<template v-if="!licenseState.authorized">
@@ -179,10 +183,10 @@ onUnmounted(() => {
<div v-if="expired" class="lg-overlay">
<div class="lg-modal">
<div class="lg-modal-title">
🔒 {{ displayName }} 免费试用已结束
🔒 {{ displayName }} 今日免费试用已用完
</div>
<p class="lg-modal-desc">
该功能 30 分钟免费试用时间已用完。购买授权后全部功能立即解锁,或输入卡密激活。
该功能今日 30 分钟免费试用已用完<template v-if="resetHint">,{{ resetHint }} 可再试</template>。购买授权后全部功能立即解锁,或输入卡密激活。
</p>
<div class="lg-modal-actions">
+36 -5
View File
@@ -23,7 +23,8 @@ export const PROTECTED_TOOLS: Record<string, string> = {
'/ai-gen': 'AI 生图',
};
export const TRIAL_MS = 30 * 60 * 1000; // 每个功能免费 30 分钟
export const TRIAL_MS = 30 * 60 * 1000; // 每个功能每轮免费 30 分钟
export const TRIAL_CYCLE_MS = 24 * 60 * 60 * 1000; // 每 24 小时重置一轮(买断制下给用户第二次触达机会)
const MACHINE_KEY = 'it-tools-machine-id';
const TRIAL_KEY = 'it-tools-trials';
@@ -39,7 +40,7 @@ export function getMachineId(): string {
return id;
}
// ====== 免费试用计时(每个功能独立,挂钟倒计时) ======
// ====== 免费试用计时(每个功能独立;以首次进入为锚点,每 24 小时滚一轮) ======
function readTrials(): Record<string, number> {
try {
return JSON.parse(localStorage.getItem(TRIAL_KEY) ?? '{}');
@@ -59,18 +60,48 @@ export function getTrialStart(path: string): number | null {
export function startTrial(path: string): void {
const t = readTrials();
if (!t[path]) {
t[path] = Date.now();
const now = Date.now();
const start = t[path];
// 首次进入,或距本窗口起点已满一个周期 → 立刻开新窗口
// (否则额度会从上一轮起点空耗掉,用户不在场也算用过)
if (!start || now - start >= TRIAL_CYCLE_MS) {
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 - start));
return Math.max(0, TRIAL_MS - (now - cycleStartOf(start, now)));
}
/** 本轮已用完时,下一轮免费试用的重置时刻(ms 时间戳);未开始计时返回 null */
export function nextTrialResetAt(path: string, now: number): number | null {
const start = getTrialStart(path);
if (start === null) return null;
if (remainingMs(path, now) > 0) return null; // 本轮还有剩余,无需提示重置
return cycleStartOf(start, now) + TRIAL_CYCLE_MS;
}
/** 人类可读的重置提示,如「明天 14:23」/「今天 21:05」 */
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}`;
}
// ====== 授权状态 ======