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:
personal82555 committed 2026-10-14 22:50:45 +08:00
1 parent 2e090903c2
commit e1ae8a7bd8
4 files changed
+43 -34

No files matched your search

+12 -10
View File
@@ -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]: