feat: 授权关联用户账号 — 登录记录设备, 激活时卡密存账号, 换设备登录自动授权(最多10台), 用户中心显示授权状态
This commit is contained in:
1 parent
3cd73d7f62
commit
70e03feca6
4 files changed
+133
-4
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { computed, ref } from 'vue';
|
||||
import { licenseHeaders } from '@/tools/license/license';
|
||||
|
||||
const TOKEN_KEY = 'it-tools-auth';
|
||||
|
||||
@@ -52,7 +53,7 @@ export function useUserAuth() {
|
||||
async function login(u: string, p: string, cfToken = '') {
|
||||
const { status, body } = await apiFetch('/user/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
headers: { 'Content-Type': 'application/json', ...licenseHeaders() },
|
||||
body: JSON.stringify({ username: u, password: p, cf_token: cfToken }),
|
||||
});
|
||||
if (status !== 200) {
|
||||
@@ -66,7 +67,7 @@ export function useUserAuth() {
|
||||
async function register(u: string, p: string, cfToken = '') {
|
||||
const { status, body } = await apiFetch('/user/register', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
headers: { 'Content-Type': 'application/json', ...licenseHeaders() },
|
||||
body: JSON.stringify({ username: u, password: p, cf_token: cfToken }),
|
||||
});
|
||||
if (status !== 200) {
|
||||
|
||||
@@ -9,6 +9,23 @@ useHead({ title: '用户中心 - IT-TOOLS-PRO' });
|
||||
|
||||
const { isLoggedIn, username } = useUserAuth();
|
||||
|
||||
// ---- 授权状态 ----
|
||||
const licensed = ref<boolean | null>(null);
|
||||
async function loadLicense() {
|
||||
try {
|
||||
const { getMachineId } = await import('@/tools/license/license');
|
||||
const res = await fetch(`/relay/auth-status?machine_id=${getMachineId()}`);
|
||||
const b = await res.json();
|
||||
licensed.value = !!b?.data?.authorized;
|
||||
}
|
||||
catch {
|
||||
licensed.value = null;
|
||||
}
|
||||
}
|
||||
if (isLoggedIn.value) {
|
||||
loadLicense();
|
||||
}
|
||||
|
||||
// ============ 数据(优先云端,回退本地) ============
|
||||
const favTools = ref<string[]>([]);
|
||||
const recentTools = ref<{ path: string; name: string; at: number }[]>([]);
|
||||
@@ -144,6 +161,9 @@ function removeBookmark(i: number) {
|
||||
<div class="uc-sync" v-if="syncAt">
|
||||
云端已同步 {{ fmtTime(syncAt) }}
|
||||
</div>
|
||||
<div class="uc-license" :class="licensed ? 'ok' : 'none'">
|
||||
{{ licensed ? '✓ 授权已关联账号' : '未授权(试用中)' }}
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
v-for="m in MENUS"
|
||||
@@ -295,6 +315,24 @@ function removeBookmark(i: number) {
|
||||
opacity: 0.5;
|
||||
margin-top: 3px;
|
||||
}
|
||||
|
||||
.uc-license {
|
||||
display: inline-block;
|
||||
margin-top: 7px;
|
||||
font-size: 11.5px;
|
||||
border-radius: 999px;
|
||||
padding: 2px 12px;
|
||||
|
||||
&.ok {
|
||||
color: #18a058;
|
||||
background: rgba(24, 160, 88, 0.12);
|
||||
}
|
||||
|
||||
&.none {
|
||||
opacity: 0.55;
|
||||
background: rgba(128, 128, 128, 0.12);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.uc-menu-item {
|
||||
|
||||
@@ -162,9 +162,22 @@ export async function activateCard(cardKey: string, contactQq: string, replace =
|
||||
licenseState.activateSuccess = false;
|
||||
try {
|
||||
// 走本站 relay:API Key 只存在服务端,且 relay 先用 /verify 预检卡密
|
||||
// attach the logged-in account token so the card is bound to the account (follows across devices)
|
||||
let authHeader = '';
|
||||
try {
|
||||
const auth = JSON.parse(localStorage.getItem('it-tools-auth') ?? 'null');
|
||||
if (auth?.token) {
|
||||
authHeader = `Bearer ${auth.token}`;
|
||||
}
|
||||
}
|
||||
catch { /* ignore */ }
|
||||
const actHeaders: Record<string, string> = { 'Content-Type': 'application/json' };
|
||||
if (authHeader) {
|
||||
actHeaders['Authorization'] = authHeader;
|
||||
}
|
||||
const res = await fetch('/relay/activate', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
headers: actHeaders,
|
||||
body: JSON.stringify({
|
||||
card_key: key,
|
||||
machine_id: getMachineId(),
|
||||
|
||||
Reference in new issue
Block a user