feat: 新增license

This commit is contained in:
小蟑螂
2026-07-06 19:34:53 +08:00
parent 8a6cb23c07
commit ac890dcc5c
110 changed files with 10678 additions and 1368 deletions
@@ -0,0 +1,85 @@
<script setup lang="ts">
import type { LicenseDTO } from '#/api/system/license';
import { reactive, ref } from 'vue';
import { Button, Form, Input, message } from 'ant-design-vue';
import { createInitAdminApi } from '#/api/system/license';
defineOptions({ name: 'InitStepAdmin' });
const emit = defineEmits<{
next: [];
}>();
const submitting = ref(false);
const formState = reactive<LicenseDTO.InitAdmin>({
loginCode: '',
phone: '',
password: '',
realName: '',
});
const rules = {
loginCode: [{ required: true, message: '请输入登录账号' }],
password: [{ required: true, message: '请输入密码' }],
realName: [{ required: true, message: '请输入姓名' }],
};
async function handleSubmit() {
submitting.value = true;
const hide = message.loading('正在创建超级管理员...', 0);
try {
await createInitAdminApi({
...formState,
phone: formState.phone || undefined,
});
message.success('超级管理员创建成功');
emit('next');
} finally {
hide();
submitting.value = false;
}
}
</script>
<template>
<Form
layout="vertical"
:model="formState"
:rules="rules"
@finish="handleSubmit"
>
<Form.Item label="登录账号" name="loginCode">
<Input
v-model:value="formState.loginCode"
placeholder="超级管理员登录账号"
autocomplete="username"
/>
</Form.Item>
<Form.Item label="手机号码" name="phone">
<Input
v-model:value="formState.phone"
placeholder="选填"
autocomplete="tel"
/>
</Form.Item>
<Form.Item label="姓名" name="realName">
<Input v-model:value="formState.realName" placeholder="管理员姓名" />
</Form.Item>
<Form.Item label="登录密码" name="password">
<Input.Password
v-model:value="formState.password"
placeholder="设置登录密码"
autocomplete="new-password"
/>
</Form.Item>
<Form.Item>
<Button type="primary" html-type="submit" :loading="submitting" block>
下一步
</Button>
</Form.Item>
</Form>
</template>
@@ -0,0 +1,77 @@
<script setup lang="ts">
import type { LicenseDTO } from '#/api/system/license';
import { reactive, ref } from 'vue';
import { Button, Form, Input, message } from 'ant-design-vue';
import { saveInitCompanyApi } from '#/api/system/license';
defineOptions({ name: 'InitStepCompany' });
const emit = defineEmits<{
back: [];
next: [];
}>();
const submitting = ref(false);
const formState = reactive<LicenseDTO.InitCompany>({
companyName: '',
contactPhone: '',
contactEmail: '',
});
const rules = {
companyName: [{ required: true, message: '请输入公司名称' }],
};
async function handleSubmit() {
submitting.value = true;
const hide = message.loading('正在保存公司信息...', 0);
try {
await saveInitCompanyApi({
...formState,
contactPhone: formState.contactPhone || undefined,
contactEmail: formState.contactEmail || undefined,
});
message.success('公司信息保存成功');
emit('next');
} finally {
hide();
submitting.value = false;
}
}
</script>
<template>
<Form
layout="vertical"
:model="formState"
:rules="rules"
@finish="handleSubmit"
>
<Form.Item label="公司名称" name="companyName">
<Input v-model:value="formState.companyName" placeholder="公司全称" />
</Form.Item>
<Form.Item label="联系电话" name="contactPhone">
<Input v-model:value="formState.contactPhone" placeholder="选填" />
</Form.Item>
<Form.Item label="联系邮箱" name="contactEmail">
<Input v-model:value="formState.contactEmail" placeholder="选填" />
</Form.Item>
<Form.Item>
<div class="flex gap-3">
<Button class="flex-1" @click="emit('back')">上一步</Button>
<Button
type="primary"
html-type="submit"
class="flex-1"
:loading="submitting"
>
下一步
</Button>
</div>
</Form.Item>
</Form>
</template>
@@ -0,0 +1,103 @@
<script setup lang="ts">
import type { LicenseVO } from '#/api/system/license';
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import { LOGIN_PATH } from '@vben/constants';
import { Button, Form, Input, message, Typography } from 'ant-design-vue';
import {
activateLicenseApi,
getInitFingerprintApi,
} from '#/api/system/license';
import { resetInitStatusCache } from '#/router/guard';
import FingerprintDisplay from './components/FingerprintDisplay.vue';
defineOptions({ name: 'InitStepLicense' });
const emit = defineEmits<{
back: [];
}>();
const router = useRouter();
const submitting = ref(false);
const fingerprintLoading = ref(false);
const fingerprint = ref<LicenseVO.Fingerprint | null>(null);
const licenseKey = ref('');
async function loadFingerprint() {
fingerprintLoading.value = true;
try {
fingerprint.value = await getInitFingerprintApi();
} finally {
fingerprintLoading.value = false;
}
}
async function handleActivate() {
if (!licenseKey.value.trim()) {
message.warning('请输入 License Key');
return;
}
submitting.value = true;
const hide = message.loading('正在激活 License...', 0);
try {
await activateLicenseApi({ licenseKey: licenseKey.value.trim() });
resetInitStatusCache();
message.success('系统初始化完成,请登录');
await router.replace(LOGIN_PATH);
} finally {
hide();
submitting.value = false;
}
}
onMounted(() => {
loadFingerprint();
});
</script>
<template>
<div class="space-y-4">
<Typography.Paragraph type="secondary" class="!mb-0 text-sm">
将机器指纹提供给运营方,获取 License Key 后粘贴到下方完成激活。
</Typography.Paragraph>
<div v-if="fingerprintLoading" class="py-6 text-center text-gray-400">
正在获取机器指纹...
</div>
<FingerprintDisplay
v-else-if="fingerprint"
:fingerprint="fingerprint.fingerprint"
:hostname="fingerprint.hostname"
:machine-id="fingerprint.machineId"
/>
<Form layout="vertical" @finish="handleActivate">
<Form.Item label="License Key" required>
<Input.TextArea
v-model:value="licenseKey"
:rows="4"
placeholder="粘贴运营方签发的 License JWT"
class="font-mono text-xs"
/>
</Form.Item>
<Form.Item>
<div class="flex gap-3">
<Button class="flex-1" @click="emit('back')">上一步</Button>
<Button
type="primary"
html-type="submit"
class="flex-1"
:loading="submitting"
>
激活并完成
</Button>
</div>
</Form.Item>
</Form>
</div>
</template>
@@ -0,0 +1,91 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import { LOGIN_PATH } from '@vben/constants';
import { preferences } from '@vben/preferences';
import { Card, Spin, Steps, Typography } from 'ant-design-vue';
import { getInitStatusApi } from '#/api/system/license';
import InitStepAdmin from './InitStepAdmin.vue';
import InitStepCompany from './InitStepCompany.vue';
import InitStepLicense from './InitStepLicense.vue';
defineOptions({ name: 'InitWizard' });
const router = useRouter();
const currentStep = ref(0);
const statusLoading = ref(true);
const stepItems = [
{ title: '超级管理员' },
{ title: '公司信息' },
{ title: 'License 激活' },
];
async function loadStatus() {
statusLoading.value = true;
try {
const status = await getInitStatusApi();
if (status.initialized) {
await router.replace(LOGIN_PATH);
return;
}
// 根据后端进度定位到对应步骤
if (status.licenseActivated || status.currentStep >= 3) {
currentStep.value = 2;
} else if (status.companySaved || status.currentStep >= 2) {
currentStep.value = 2;
} else if (status.adminCreated || status.currentStep >= 1) {
currentStep.value = 1;
} else {
currentStep.value = 0;
}
} finally {
statusLoading.value = false;
}
}
function goNext() {
currentStep.value = Math.min(currentStep.value + 1, 2);
}
function goBack() {
currentStep.value = Math.max(currentStep.value - 1, 0);
}
onMounted(() => {
loadStatus();
});
</script>
<template>
<div
class="flex min-h-screen flex-col items-center justify-center bg-gradient-to-b from-background to-muted/30 px-4 py-10"
>
<div class="mb-8 text-center">
<Typography.Title :level="3" class="!mb-1">
{{ preferences.app.name }} 系统初始化
</Typography.Title>
<Typography.Text type="secondary">
首次部署请按步骤完成配置
</Typography.Text>
</div>
<Card class="w-full max-w-lg shadow-sm">
<Steps :current="currentStep" :items="stepItems" class="mb-8" />
<Spin :spinning="statusLoading">
<InitStepAdmin v-if="currentStep === 0" @next="goNext" />
<InitStepCompany
v-else-if="currentStep === 1"
@back="goBack"
@next="goNext"
/>
<InitStepLicense v-else @back="goBack" />
</Spin>
</Card>
</div>
</template>
@@ -0,0 +1,56 @@
<script setup lang="ts">
import { ref } from 'vue';
import { createIconifyIcon } from '@vben/icons';
import { Button, Input, message, Space, Typography } from 'ant-design-vue';
defineOptions({ name: 'FingerprintDisplay' });
const props = defineProps<{
fingerprint: string;
hostname?: string;
machineId?: string;
}>();
const CopyIcon = createIconifyIcon('lucide:copy');
const copying = ref(false);
async function copyFingerprint() {
if (!props.fingerprint) return;
copying.value = true;
try {
await navigator.clipboard.writeText(props.fingerprint);
message.success('机器指纹已复制');
} catch {
message.error('复制失败,请手动选择复制');
} finally {
copying.value = false;
}
}
</script>
<template>
<div class="rounded-lg border border-dashed border-primary/30 bg-primary/5 p-4">
<Typography.Text type="secondary" class="mb-2 block text-sm">
请将以下机器指纹发送给运营方签发 License
</Typography.Text>
<Input :value="fingerprint" readonly class="font-mono text-xs" />
<Space class="mt-3" wrap>
<Button
type="primary"
ghost
size="small"
:loading="copying"
@click="copyFingerprint"
>
<CopyIcon class="mr-1 size-4" />
复制指纹
</Button>
<Typography.Text v-if="hostname" type="secondary" class="text-xs">
主机名:{{ hostname }}
</Typography.Text>
</Space>
</div>
</template>
@@ -0,0 +1,46 @@
<script setup lang="ts">
import type { LicenseVO } from '#/api/system/license';
import { onMounted, ref } from 'vue';
import { Card, Spin } from 'ant-design-vue';
import { getSystemFingerprintApi } from '#/api/system/license';
import FingerprintDisplay from '#/views/init/components/FingerprintDisplay.vue';
defineOptions({ name: 'FingerprintInfo' });
const loading = ref(false);
const fingerprint = ref<LicenseVO.Fingerprint | null>(null);
async function load() {
loading.value = true;
try {
fingerprint.value = await getSystemFingerprintApi();
} finally {
loading.value = false;
}
}
onMounted(() => {
load();
});
</script>
<template>
<Card title="机器指纹" :bordered="false">
<Spin :spinning="loading">
<FingerprintDisplay
v-if="fingerprint"
:fingerprint="fingerprint.fingerprint"
:hostname="fingerprint.hostname"
:machine-id="fingerprint.machineId"
/>
<div v-else-if="!loading" class="text-gray-400">暂无指纹信息</div>
</Spin>
<p class="mt-4 text-sm text-gray-500">
迁机或重新签发 License 时,请将新环境的机器指纹提供给运营方。
</p>
</Card>
</template>
@@ -0,0 +1,160 @@
<script setup lang="ts">
import type { LicenseVO } from '#/api/system/license';
import { computed, onMounted, ref } from 'vue';
import { Page } from '@vben/common-ui';
import {
Button,
Card,
Descriptions,
Form,
Input,
message,
Space,
Tag,
Typography,
} from 'ant-design-vue';
import dayjs from 'dayjs';
import {
getLicenseInfoApi,
updateLicenseApi,
} from '#/api/system/license';
import FingerprintInfo from './FingerprintInfo.vue';
defineOptions({ name: 'LicenseInfo' });
const loading = ref(false);
const updating = ref(false);
const license = ref<LicenseVO.LicenseSummary | null>(null);
const renewKey = ref('');
const statusTag = computed(() => {
const status = license.value?.status;
if (status === 'active') return { color: 'success', text: '有效' };
if (status === 'expiring') return { color: 'warning', text: '即将过期' };
if (status === 'expired') return { color: 'error', text: '已过期' };
if (status === 'dev-disabled') return { color: 'default', text: '开发模式' };
return { color: 'default', text: status || '未知' };
});
const editionLabel: Record<string, string> = {
trial: '试用版',
enterprise: '企业版',
unlimited: '无限版',
};
async function loadLicense() {
loading.value = true;
try {
license.value = await getLicenseInfoApi();
} catch (error: any) {
license.value = null;
if (!error?.message?.includes('无有效')) {
message.error(error?.message || '获取 License 信息失败');
}
} finally {
loading.value = false;
}
}
async function handleRenew() {
if (!renewKey.value.trim()) {
message.warning('请输入新的 License Key');
return;
}
updating.value = true;
const hide = message.loading('正在更新 License...', 0);
try {
license.value = await updateLicenseApi({ licenseKey: renewKey.value.trim() });
renewKey.value = '';
message.success('License 更新成功');
} finally {
hide();
updating.value = false;
}
}
function formatDate(value?: string) {
return value ? dayjs(value).format('YYYY-MM-DD HH:mm') : '-';
}
onMounted(() => {
loadLicense();
});
</script>
<template>
<Page title="License 管理" description="查看授权信息与续期更新">
<div class="grid gap-4 lg:grid-cols-2">
<Card title="授权信息" :loading="loading">
<template v-if="license">
<Descriptions :column="1" bordered size="small">
<Descriptions.Item label="状态">
<Tag :color="statusTag.color">{{ statusTag.text }}</Tag>
</Descriptions.Item>
<Descriptions.Item label="授权公司">
{{ license.companyName || '-' }}
</Descriptions.Item>
<Descriptions.Item label="版本">
{{ editionLabel[license.edition || ''] || license.edition || '-' }}
</Descriptions.Item>
<Descriptions.Item label="机器指纹">
<Typography.Text code class="text-xs break-all">
{{ license.machineFingerprint || '-' }}
</Typography.Text>
</Descriptions.Item>
<Descriptions.Item label="授权功能">
<Space wrap>
<Tag v-for="f in license.features" :key="f">{{ f }}</Tag>
<span v-if="!license.features?.length">-</span>
</Space>
</Descriptions.Item>
<Descriptions.Item label="签发时间">
{{ formatDate(license.issuedAt) }}
</Descriptions.Item>
<Descriptions.Item label="过期时间">
{{ formatDate(license.expiresAt) }}
</Descriptions.Item>
<Descriptions.Item
v-if="license.daysRemaining != null"
label="剩余天数"
>
{{ license.daysRemaining }} 天
</Descriptions.Item>
</Descriptions>
</template>
<Typography.Text v-else type="secondary">
暂无有效 License(本地开发可能未启用校验)
</Typography.Text>
<Button class="mt-4" size="small" @click="loadLicense">刷新</Button>
</Card>
<Card title="续期 / 更新 License">
<Typography.Paragraph type="secondary" class="text-sm">
运营方签发新 License 后,将完整 JWT 粘贴到下方并提交。
</Typography.Paragraph>
<Form layout="vertical" @finish="handleRenew">
<Form.Item label="新 License Key" required>
<Input.TextArea
v-model:value="renewKey"
:rows="5"
placeholder="粘贴新的 License JWT"
class="font-mono text-xs"
/>
</Form.Item>
<Button type="primary" html-type="submit" :loading="updating">
更新 License
</Button>
</Form>
</Card>
</div>
<div class="mt-4">
<FingerprintInfo />
</div>
</Page>
</template>