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
@@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. See [standa
|
||||
|
||||
## Version 2026.10.14-it-tools-pro
|
||||
|
||||
### 🕛 试用额度改为按自然日 0 点重置(北京时间)
|
||||
- **之前**:以"用户首次进入"为锚点的滚动 24 小时窗口 → 重置时刻是 `明天 22:12` 这种**跟首用时间绑定的任意时刻**,用户无法预期,也不符合"每天免费 30 分钟"的直觉
|
||||
- **现在**:额度按**北京时间自然日**结算
|
||||
- `todayStartOf()` = `floor((now + 8h) / 24h) * 24h - 8h`,epoch 计算、与运行环境时区无关
|
||||
- 起点不早于今天 00:00(昨天的起点跨天后自动失效)
|
||||
- **重置时刻恒为次日 00:00**
|
||||
- **额度仍在用户当天首次使用时才起算**,不会从 0 点空耗:`startTrial()` 只在"无记录"或"记录早于今天 00:00"时把起点写成当前时间
|
||||
- 服务端 `trial_allowed()` 同步同算法(`TRIAL_CYCLE_MS`/`TRIAL_CYCLE_H` 已移除,日界硬编 UTC+8)
|
||||
- 浮条文案:默认 `(每日 0 点重置)`,临近耗尽(≤5 分钟)显示具体时刻 `(明天 00:00 重置)`
|
||||
|
||||
### 🩹 修复:面单导入后仍有改不了的字、删条码后仍有痕迹
|
||||
- **问题 A(有些字改不了)**:文字层用「近白低饱和」判断是否擦除底图,但那个判断**同时门控了"是否生成可编辑元素"**——底色不在阈值内(面单常见的灰条、蓝条、色带)时整段文字被静默丢弃:既不生成元素、也不擦除,永久留在底图上
|
||||
- 实测靶子:蓝条 (184,212,240) 亮度 207 达标但**色差 56 > 40** → 直接被丢弃
|
||||
|
||||
@@ -44,7 +44,8 @@ AUTH_PASS = os.environ.get("AUTH_PASS", "")
|
||||
PROJECT_NAME = os.environ.get("PROJECT_NAME", "IT-Tools")
|
||||
TRIAL_FILE = os.environ.get("TRIAL_FILE", "/app/data/trials.json")
|
||||
TRIAL_MS = int(os.environ.get("TRIAL_MS_MIN", "30")) * 60 * 1000
|
||||
TRIAL_CYCLE_MS = int(os.environ.get("TRIAL_CYCLE_H", "24")) * 60 * 60 * 1000
|
||||
DAY_MS = 24 * 60 * 60 * 1000
|
||||
TZ_OFFSET_MS = 8 * 60 * 60 * 1000 # 北京时间 UTC+8 —— 重置对齐自然日 0 点
|
||||
PROTECTED_TOOLS = {"translator", "image-ocr", "pdf-ocr", "ai-retouch", "label-designer", "doc-convert", "image-tools", "pdf-tools", "video-tools", "audio-tools", "text-to-image", "ai-image-tools", "ai-chat", "copywriting", "screenshot-qa", "video-subtitle", "ai-gen"}
|
||||
MACHINE_RE = re.compile(r"^\d{10,16}$")
|
||||
|
||||
@@ -140,24 +141,25 @@ def _save_trials(trials: dict) -> None:
|
||||
os.replace(tmp, TRIAL_FILE)
|
||||
|
||||
|
||||
def _today_start(now_ms: int) -> int:
|
||||
"""当天 00:00(北京时间)对应的时间戳;epoch 计算,与时区环境变量无关"""
|
||||
return (now_ms + TZ_OFFSET_MS) // DAY_MS * DAY_MS - TZ_OFFSET_MS
|
||||
|
||||
|
||||
def trial_allowed(machine: str, tool: str) -> bool:
|
||||
key = f"{machine}|{tool}"
|
||||
now_ms = int(time.time() * 1000)
|
||||
with _lock:
|
||||
trials = _load_trials()
|
||||
start = trials.get(key)
|
||||
# 首次进入,或距本窗口起点已满一个周期 -> 立刻开新窗口
|
||||
# (否则额度会从上一轮起点空耗掉,用户不在场也算用过)
|
||||
if start is None or (now_ms - start) >= TRIAL_CYCLE_MS:
|
||||
today = _today_start(now_ms)
|
||||
# 首次进入,或已跨到新的一天 -> 立刻开新一天的额度
|
||||
# (额度只在用户当天真的进来时才起算,不会从 0 点空耗)
|
||||
if start is None or start < today:
|
||||
trials[key] = now_ms
|
||||
_save_trials(trials)
|
||||
return True
|
||||
# 同窗口内:以窗口起点为准(处理页面长期未重载的跨轮情况)
|
||||
if now_ms <= start:
|
||||
cycle_start = start
|
||||
else:
|
||||
cycle_start = start + ((now_ms - start) // TRIAL_CYCLE_MS) * TRIAL_CYCLE_MS
|
||||
return (now_ms - cycle_start) < TRIAL_MS
|
||||
return (now_ms - start) < TRIAL_MS
|
||||
|
||||
|
||||
def deactivate_card(machine_id: str) -> tuple[int, dict]:
|
||||
|
||||
@@ -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