Files
it-tools-pro/src/tools/license/LicenseGate.vue
T
personal82555 e1ae8a7bd8 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 + 前端五态(含两个跨天用例)全通过
2026-10-14 22:50:45 +08:00

429 lines
11 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import {
BUY_URL,
PROTECTED_TOOLS,
TRIAL_MS,
activateCard,
checkAuth,
deactivateLicense,
getMachineId,
licenseState,
remainingMs,
startTrial,
trialResetHint,
} from './license';
const props = defineProps<{ path: string }>();
const router = useRouter();
const displayName = computed(() => PROTECTED_TOOLS[props.path] ?? '');
const protectedTool = computed(() => !!PROTECTED_TOOLS[props.path]);
const now = ref(Date.now());
const showCardInput = ref(false);
const cardKey = ref('');
const contactQq = ref('');
const activateMsg = ref('');
const copied = ref(false);
let timer: ReturnType<typeof setInterval> | undefined;
const remaining = computed(() => (protectedTool.value ? remainingMs(props.path, now.value) : TRIAL_MS));
const trialUsed = computed(() => protectedTool.value && remaining.value < TRIAL_MS);
const expired = computed(() => protectedTool.value && !licenseState.authorized && remaining.value <= 0);
const mmss = computed(() => {
const s = Math.ceil(remaining.value / 1000);
const m = Math.floor(s / 60);
const sec = s % 60;
return `${String(m).padStart(2, '0')}:${String(sec).padStart(2, '0')}`;
});
const urgency = computed(() => {
if (remaining.value <= 5 * 60 * 1000) return 'danger';
if (remaining.value <= 15 * 60 * 1000) return 'warn';
return 'normal';
});
/** 本轮已用完时的下一轮重置时刻提示(如「明天 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 '每日 0 点重置';
});
async function submitCard() {
if (licenseState.authorized
&& !window.confirm('将撤销本机当前授权并用新卡密激活,确定换卡?')) {
return;
}
const replacing = licenseState.authorized;
const r = await activateCard(cardKey.value, contactQq.value, replacing);
activateMsg.value = r.msg;
if (r.ok) {
showCardInput.value = false;
cardKey.value = '';
if (replacing) {
checkAuth(); // 换卡后刷新授权状态
}
}
}
const revokeMsg = ref('');
const revoking = ref(false);
async function revokeLicense() {
if (!window.confirm('确定撤销本机授权?撤销后本机回到免费试用状态,重新使用需再输入卡密激活。')) {
return;
}
revoking.value = true;
try {
const r = await deactivateLicense();
revokeMsg.value = r.msg;
if (r.ok) {
startTrial(props.path); // 回到试用:从现在起重新计 30 分钟
now.value = Date.now();
}
setTimeout(() => (revokeMsg.value = ''), 5000);
}
finally {
revoking.value = false;
}
}
function copyMachine() {
try {
navigator.clipboard?.writeText(getMachineId());
copied.value = true;
setTimeout(() => (copied.value = false), 1500);
}
catch { /* ignore */ }
}
/** 每次进入受控功能页时检查授权并启动该功能的独立试用计时 */
async function init() {
activateMsg.value = '';
showCardInput.value = false;
if (!protectedTool.value) return;
const ok = await checkAuth();
if (!ok) {
startTrial(props.path); // 未授权:开始/续计该功能 30 分钟免费时长
}
now.value = Date.now();
}
onMounted(() => {
init();
timer = setInterval(() => {
now.value = Date.now();
}, 1000);
// 登录/登出后重新检查授权:换电脑登录已授权账号后,立即解除未授权提示
window.addEventListener('it-tools-auth-changed', init);
});
onBeforeUnmount(() => {
window.removeEventListener('it-tools-auth-changed', init);
});
// 跨工具路由复用:路径变化时重新走授权检查
watch(() => props.path, init);
onUnmounted(() => {
if (timer) clearInterval(timer);
});
</script>
<template>
<template v-if="protectedTool">
<!-- ===== floating status bar (bottom-right, no layout impact) ===== -->
<div
v-if="!expired"
class="lg-float"
:class="{ ok: licenseState.authorized, warn: !licenseState.authorized && trialUsed && urgency === 'warn', danger: !licenseState.authorized && trialUsed && urgency === 'danger' }"
>
<span class="lg-dot" />
<template v-if="licenseState.authorized">
{{ displayName }} · 已授权
<a href="javascript:;" class="lg-link" @click="showCardInput = !showCardInput">换卡密</a>
<a href="javascript:;" class="lg-link" :class="{ disabled: revoking }" @click="revokeLicense">
{{ revoking ? '撤销中…' : '撤销' }}
</a>
</template>
<template v-else-if="trialUsed">
{{ displayName }} · 今日免费 <b>{{ mmss }}</b>({{ resetTag }})
</template>
<template v-else>
{{ displayName }} · 今日免费 30 分钟({{ resetTag }})
</template>
<template v-if="!licenseState.authorized">
<a
:href="BUY_URL"
target="_blank"
rel="noopener"
class="lg-buy"
title="买断制授权,不按次不按月;一次激活,15 个高级功能全部解锁,授权跟随账号、换电脑可恢复"
>永久解锁 15 个功能</a>
<a href="javascript:;" class="lg-link" @click="showCardInput = !showCardInput">卡密</a>
</template>
</div>
<!-- ===== inline card input ===== -->
<div v-if="showCardInput && !expired" class="lg-cardbar">
<input
v-model="cardKey"
class="lg-input"
placeholder="卡密,如 CA-XXXX-XXXX-XXXX"
@keyup.enter="submitCard"
>
<input
v-model="contactQq"
class="lg-input lg-input-qq"
placeholder="联系QQ(选填)"
@keyup.enter="submitCard"
>
<c-button size="small" type="primary" :disabled="licenseState.activating" @click="submitCard">
{{ licenseState.activating ? '激活中…' : '激活' }}
</c-button>
<span v-if="activateMsg" class="lg-msg" :class="{ err: !licenseState.activateSuccess }">{{ activateMsg }}</span>
<a href="javascript:;" class="lg-link" @click="showCardInput = false">收起</a>
</div>
<!-- ===== lock overlay when trial expired ===== -->
<div v-if="expired" class="lg-overlay">
<div class="lg-modal">
<div class="lg-modal-title">
🔒 {{ displayName }} 今日免费试用已用完
</div>
<p class="lg-modal-desc">
该功能今日 30 分钟免费试用已用完<template v-if="resetHint">,{{ resetHint }} 可再试</template>。购买授权后全部功能立即解锁,或输入卡密激活。
</p>
<div class="lg-modal-actions">
<c-button type="primary" :href="BUY_URL" target="_blank" rel="noopener">
购买授权
</c-button>
<c-button @click="showCardInput = !showCardInput">
输入卡密
</c-button>
<c-button @click="router.push('/')">
返回免费功能
</c-button>
</div>
<div v-if="showCardInput" class="lg-modal-form">
<input
v-model="cardKey"
class="lg-input"
placeholder="卡密,如 CA-XXXX-XXXX-XXXX"
@keyup.enter="submitCard"
>
<input
v-model="contactQq"
class="lg-input"
placeholder="联系QQ(选填)"
@keyup.enter="submitCard"
>
<c-button type="primary" :disabled="licenseState.activating" @click="submitCard">
{{ licenseState.activating ? '激活中…' : '立即激活' }}
</c-button>
</div>
<div v-if="activateMsg" class="lg-msg modal-msg" :class="{ err: !licenseState.activateSuccess }">
{{ activateMsg }}
</div>
<div class="lg-modal-foot">
机器码:<code @click="copyMachine">{{ copied ? '已复制 ✓' : getMachineId() }}</code>
<div class="lg-hint">
购买时填 MACHINE:此机器码,支付后自动绑定;本工具集其他免费功能不受影响
</div>
</div>
</div>
</div>
</template>
</template>
<style lang="less" scoped>
.lg-float {
position: fixed;
right: 14px;
top: 70px;
bottom: auto;
z-index: 9998;
display: flex;
align-items: center;
gap: 8px;
font-size: 12.5px;
padding: 7px 14px;
border-radius: 999px;
background: rgba(30, 30, 30, 0.88);
color: #eee;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(4px);
b {
font-variant-numeric: tabular-nums;
font-size: 13.5px;
}
.lg-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #18a058;
flex-shrink: 0;
}
&.ok .lg-dot { background: #18a058; }
&.warn .lg-dot { background: #f0a000; }
&.danger .lg-dot { background: #d03050; animation: lgPulse 1s infinite; }
}
@keyframes lgPulse {
50% { opacity: 0.3; }
}
.lg-buy {
color: #63e6a0;
font-weight: 600;
text-decoration: none;
&:hover { text-decoration: underline; }
}
.lg-link {
color: #bbb;
text-decoration: underline;
cursor: pointer;
}
.lg-cardbar {
position: fixed;
right: 14px;
bottom: 56px;
z-index: 9998;
display: flex;
align-items: center;
gap: 8px;
padding: 10px 12px;
border-radius: 10px;
background: rgba(30, 30, 30, 0.94);
box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(4px);
max-width: calc(100vw - 28px);
flex-wrap: wrap;
}
.lg-input {
width: 220px;
height: 30px;
padding: 0 10px;
border: 1px solid rgba(255, 255, 255, 0.3);
border-radius: 4px;
background: rgba(255, 255, 255, 0.08);
color: #eee;
font-size: 13px;
outline: none;
&::placeholder { color: rgba(255, 255, 255, 0.45); }
&:focus { border-color: #18a058; }
}
.lg-input-qq {
width: 140px;
}
.lg-msg {
font-size: 12px;
color: #63e6a0;
&.err { color: #ff8fa0; }
}
.lg-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.62);
backdrop-filter: blur(3px);
display: flex;
align-items: center;
justify-content: center;
z-index: 10000;
}
.lg-modal {
background: #fff;
color: #333;
border-radius: 12px;
padding: 28px 30px;
width: min(460px, calc(100vw - 32px));
box-shadow: 0 12px 48px rgba(0, 0, 0, 0.3);
text-align: center;
max-height: calc(100vh - 40px);
overflow-y: auto;
}
.lg-modal-title {
font-size: 17px;
font-weight: 700;
margin-bottom: 10px;
}
.lg-modal-desc {
font-size: 13.5px;
line-height: 1.7;
opacity: 0.75;
margin: 0 0 18px;
}
.lg-modal-actions {
display: flex;
gap: 12px;
justify-content: center;
margin-bottom: 14px;
}
.lg-modal-form {
display: flex;
flex-direction: column;
gap: 8px;
margin-bottom: 10px;
.lg-input {
width: 100%;
box-sizing: border-box;
border-color: rgba(128, 128, 128, 0.45);
background: #fff;
color: #333;
&::placeholder { color: rgba(128, 128, 128, 0.7); }
}
}
.modal-msg {
color: #18a058;
&.err { color: #d03050; }
}
.lg-modal-foot {
font-size: 12px;
opacity: 0.7;
margin-top: 10px;
line-height: 1.7;
code {
background: rgba(128, 128, 128, 0.12);
padding: 1px 6px;
border-radius: 4px;
cursor: pointer;
}
.lg-hint {
opacity: 0.8;
margin-top: 4px;
}
}
</style>