530 lines
13 KiB
Vue
530 lines
13 KiB
Vue
<script setup lang="ts">
|
||
import { computed, ref } from 'vue';
|
||
import { useHead } from '@vueuse/head';
|
||
import { useUserAuth, apiFetch } from '@/composable/user-auth';
|
||
import { useToolStore } from '@/tools/tools.store';
|
||
import { RouterLink } from 'vue-router';
|
||
|
||
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 }[]>([]);
|
||
const bookmarks = ref<{ name: string; url: string; category: string }[]>([]);
|
||
const loading = ref(true);
|
||
const syncAt = ref(0);
|
||
|
||
function parseJson<T>(s: string | null, def: T): T {
|
||
try {
|
||
return s ? JSON.parse(s) : def;
|
||
}
|
||
catch {
|
||
return def;
|
||
}
|
||
}
|
||
|
||
function loadLocal() {
|
||
favTools.value = parseJson<string[]>(localStorage.getItem('favoriteToolsName'), []);
|
||
recentTools.value = parseJson<{ path: string; name: string; at: number }[]>(localStorage.getItem('it-tools-recent-tools'), []);
|
||
bookmarks.value = parseJson<{ name: string; url: string; category: string }[]>(localStorage.getItem('it-tools-bookmarks:sites'), []);
|
||
}
|
||
|
||
async function loadCloud() {
|
||
loading.value = true;
|
||
try {
|
||
const { status, body } = await apiFetch('/user/data');
|
||
if (status === 200 && body.data?.data) {
|
||
const d = body.data.data;
|
||
favTools.value = parseJson<string[]>(d.favoriteToolsName ?? null, []);
|
||
recentTools.value = parseJson<{ path: string; name: string; at: number }[]>(d['it-tools-recent-tools'] ?? null, []);
|
||
bookmarks.value = parseJson<{ name: string; url: string; category: string }[]>(d['it-tools-bookmarks:sites'] ?? null, []);
|
||
syncAt.value = body.data.updated ? Math.round(body.data.updated * 1000) : Date.now();
|
||
}
|
||
else {
|
||
loadLocal();
|
||
}
|
||
}
|
||
catch {
|
||
loadLocal();
|
||
}
|
||
finally {
|
||
loading.value = false;
|
||
}
|
||
}
|
||
|
||
if (isLoggedIn.value) {
|
||
loadCloud();
|
||
}
|
||
|
||
// ============ 菜单 ============
|
||
type MenuId = 'favorites' | 'recent' | 'bookmarks';
|
||
const menu = ref<MenuId>('favorites');
|
||
const MENUS: { id: MenuId; name: string; icon: string }[] = [
|
||
{ id: 'favorites', name: '我的收藏', icon: '⭐' },
|
||
{ id: 'recent', name: '最近使用', icon: '🕘' },
|
||
{ id: 'bookmarks', name: '网站收藏', icon: '🗂️' },
|
||
];
|
||
|
||
// ============ 列表加工 ============
|
||
const toolStore = useToolStore();
|
||
const allTools = computed(() => toolStore.tools as any[]);
|
||
|
||
const favToolList = computed(() =>
|
||
favTools.value
|
||
.map(p => allTools.value.find((t: any) => t.path === p))
|
||
.filter(Boolean)
|
||
.slice(0, 60),
|
||
);
|
||
|
||
const recentList = computed(() => recentTools.value.slice(0, 30));
|
||
|
||
const bookmarkGroups = computed(() => {
|
||
const m = new Map<string, typeof bookmarks.value>();
|
||
for (const b of bookmarks.value) {
|
||
const c = b.category || '其他';
|
||
if (!m.has(c)) {
|
||
m.set(c, []);
|
||
}
|
||
m.get(c)!.push(b);
|
||
}
|
||
return [...m.entries()];
|
||
});
|
||
|
||
const menuCounts: Record<MenuId, number> = computed(() => ({
|
||
favorites: favToolList.value.length,
|
||
recent: recentList.value.length,
|
||
bookmarks: bookmarks.value.length,
|
||
})) as any;
|
||
|
||
function fmtTime(t: number) {
|
||
const d = new Date(t);
|
||
const pad = (n: number) => String(n).padStart(2, '0');
|
||
return `${d.getMonth() + 1}/${d.getDate()} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
||
}
|
||
|
||
function removeBookmark(i: number) {
|
||
// remove from grouped view: recompute flat index
|
||
let idx = 0;
|
||
for (const [, list] of bookmarkGroups.value) {
|
||
for (let j = 0; j < list.length; j++) {
|
||
if (idx === i) {
|
||
list.splice(j, 1);
|
||
bookmarks.value = [...bookmarks.value]; // trigger reactivity
|
||
localStorage.setItem('it-tools-bookmarks:sites', JSON.stringify(bookmarks.value));
|
||
return;
|
||
}
|
||
idx++;
|
||
}
|
||
}
|
||
}
|
||
|
||
</script>
|
||
|
||
<template>
|
||
<div class="uc-root">
|
||
<!-- not logged in -->
|
||
<div v-if="!isLoggedIn" class="uc-empty">
|
||
<div class="uc-empty-icon">🔐</div>
|
||
<div class="uc-empty-title">请先登录</div>
|
||
<div class="uc-empty-desc">登录后可查看你的云端收藏、最近使用记录和网站收藏</div>
|
||
<RouterLink to="/">
|
||
<c-button type="primary" mt-12px>返回首页登录</c-button>
|
||
</RouterLink>
|
||
</div>
|
||
|
||
<!-- logged in -->
|
||
<div v-else class="uc-layout">
|
||
<!-- left menu -->
|
||
<div class="uc-menu">
|
||
<div class="uc-user">
|
||
<div class="uc-avatar">{{ username.slice(0, 1).toUpperCase() }}</div>
|
||
<div class="uc-username">{{ username }}</div>
|
||
<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"
|
||
:key="m.id"
|
||
class="uc-menu-item"
|
||
:class="{ active: menu === m.id }"
|
||
@click="menu = m.id"
|
||
>
|
||
<span class="uc-menu-icon">{{ m.icon }}</span>
|
||
<span>{{ m.name }}</span>
|
||
<span class="uc-menu-count">{{ menuCounts[m.id] }}</span>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- right content -->
|
||
<div class="uc-content">
|
||
<div v-if="loading" class="uc-loading">
|
||
⏳ 正在加载云端数据…
|
||
</div>
|
||
|
||
<template v-else>
|
||
<!-- favorites -->
|
||
<div v-if="menu === 'favorites'">
|
||
<h3 class="uc-title">⭐ 我的收藏 <span class="uc-sub">点击工具卡片直接使用</span></h3>
|
||
<div v-if="favToolList.length" class="uc-tool-grid">
|
||
<RouterLink v-for="t in favToolList" :key="t.path" :to="t.path" class="uc-tool">
|
||
<span class="uc-tool-name">{{ t.name }}</span>
|
||
<span class="uc-tool-desc">{{ String(t.description || '').slice(0, 40) }}</span>
|
||
</RouterLink>
|
||
</div>
|
||
<div v-else class="uc-none">
|
||
还没有收藏工具——在工具卡片上点「加入收藏」即可
|
||
</div>
|
||
</div>
|
||
|
||
<!-- recent -->
|
||
<div v-if="menu === 'recent'">
|
||
<h3 class="uc-title">🕘 最近使用 <span class="uc-sub">按访问时间倒序,保留 30 条</span></h3>
|
||
<div v-if="recentList.length" class="uc-recent-list">
|
||
<RouterLink v-for="r in recentList" :key="r.path" :to="r.path" class="uc-recent">
|
||
<span class="uc-recent-name">{{ r.name }}</span>
|
||
<span class="uc-recent-path">{{ r.path }}</span>
|
||
<span class="uc-recent-time">{{ fmtTime(r.at) }}</span>
|
||
</RouterLink>
|
||
</div>
|
||
<div v-else class="uc-none">
|
||
还没有使用记录——用过的工具会自动出现在这里
|
||
</div>
|
||
</div>
|
||
|
||
<!-- bookmarks -->
|
||
<div v-if="menu === 'bookmarks'">
|
||
<h3 class="uc-title">🗂️ 网站收藏 <span class="uc-sub">共 {{ bookmarks.length }} 个自定义站点</span></h3>
|
||
<template v-if="bookmarks.length">
|
||
<div v-for="[cat, list] in bookmarkGroups" :key="cat" class="uc-bm-group">
|
||
<div class="uc-bm-cat">
|
||
{{ cat }} ({{ list.length }})
|
||
</div>
|
||
<div class="uc-bm-list">
|
||
<div v-for="(b, j) in list" :key="b.url" class="uc-bm">
|
||
<a :href="b.url" target="_blank" rel="noopener" class="uc-bm-name">{{ b.name }}</a>
|
||
<span class="uc-bm-url">{{ b.url }}</span>
|
||
<a href="javascript:;" class="uc-bm-del" title="删除" @click="removeBookmark(bookmarks.indexOf(b))">✕</a>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
<div v-else class="uc-none">
|
||
还没有网站收藏——在「IT网站收藏」工具里添加
|
||
</div>
|
||
</div>
|
||
</template>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<style lang="less" scoped>
|
||
.uc-root {
|
||
max-width: 1000px;
|
||
margin: 0 auto;
|
||
padding: 20px 16px 60px;
|
||
}
|
||
|
||
.uc-empty {
|
||
text-align: center;
|
||
padding: 70px 20px;
|
||
|
||
.uc-empty-icon {
|
||
font-size: 40px;
|
||
}
|
||
|
||
.uc-empty-title {
|
||
font-size: 18px;
|
||
font-weight: 800;
|
||
margin-top: 10px;
|
||
}
|
||
|
||
.uc-empty-desc {
|
||
font-size: 13px;
|
||
opacity: 0.6;
|
||
margin-top: 6px;
|
||
}
|
||
}
|
||
|
||
.uc-layout {
|
||
display: flex;
|
||
gap: 16px;
|
||
align-items: flex-start;
|
||
}
|
||
|
||
.uc-menu {
|
||
width: 210px;
|
||
flex-shrink: 0;
|
||
border: 1px solid rgba(128, 128, 128, 0.2);
|
||
border-radius: 12px;
|
||
padding: 14px;
|
||
position: sticky;
|
||
top: 70px;
|
||
|
||
.uc-user {
|
||
text-align: center;
|
||
padding-bottom: 12px;
|
||
border-bottom: 1px solid rgba(128, 128, 128, 0.15);
|
||
margin-bottom: 10px;
|
||
|
||
.uc-avatar {
|
||
width: 44px;
|
||
height: 44px;
|
||
border-radius: 50%;
|
||
background: linear-gradient(135deg, #25636c, #14a058);
|
||
color: #fff;
|
||
font-size: 19px;
|
||
font-weight: 800;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
margin: 0 auto 8px;
|
||
}
|
||
|
||
.uc-username {
|
||
font-size: 14px;
|
||
font-weight: 700;
|
||
word-break: break-all;
|
||
}
|
||
|
||
.uc-sync {
|
||
font-size: 11px;
|
||
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 {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
padding: 9px 10px;
|
||
border-radius: 8px;
|
||
cursor: pointer;
|
||
font-size: 13.5px;
|
||
transition: background 0.15s;
|
||
|
||
&:hover {
|
||
background: rgba(128, 128, 128, 0.08);
|
||
}
|
||
|
||
&.active {
|
||
background: rgba(24, 160, 88, 0.12);
|
||
color: #18a058;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.uc-menu-count {
|
||
margin-left: auto;
|
||
font-size: 11.5px;
|
||
opacity: 0.5;
|
||
}
|
||
}
|
||
}
|
||
|
||
.uc-content {
|
||
flex: 1;
|
||
min-width: 0;
|
||
border: 1px solid rgba(128, 128, 128, 0.2);
|
||
border-radius: 12px;
|
||
padding: 18px 20px;
|
||
min-height: 400px;
|
||
}
|
||
|
||
.uc-loading {
|
||
text-align: center;
|
||
padding: 50px 0;
|
||
opacity: 0.6;
|
||
}
|
||
|
||
.uc-title {
|
||
font-size: 16px;
|
||
font-weight: 800;
|
||
margin: 0 0 14px;
|
||
|
||
.uc-sub {
|
||
font-size: 12px;
|
||
font-weight: 400;
|
||
opacity: 0.5;
|
||
margin-left: 8px;
|
||
}
|
||
}
|
||
|
||
.uc-none {
|
||
text-align: center;
|
||
padding: 40px 10px;
|
||
font-size: 13px;
|
||
opacity: 0.5;
|
||
}
|
||
|
||
.uc-tool-grid {
|
||
display: grid;
|
||
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
|
||
gap: 10px;
|
||
|
||
.uc-tool {
|
||
border: 1px solid rgba(128, 128, 128, 0.22);
|
||
border-radius: 10px;
|
||
padding: 12px 14px;
|
||
text-decoration: none;
|
||
color: inherit;
|
||
transition: border-color 0.15s;
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 4px;
|
||
|
||
&:hover {
|
||
border-color: #18a058;
|
||
}
|
||
|
||
.uc-tool-name {
|
||
font-size: 13.5px;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.uc-tool-desc {
|
||
font-size: 11.5px;
|
||
opacity: 0.55;
|
||
}
|
||
}
|
||
}
|
||
|
||
.uc-recent-list {
|
||
.uc-recent {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 12px;
|
||
padding: 9px 12px;
|
||
border-radius: 8px;
|
||
text-decoration: none;
|
||
color: inherit;
|
||
transition: background 0.15s;
|
||
|
||
&:hover {
|
||
background: rgba(128, 128, 128, 0.07);
|
||
}
|
||
|
||
.uc-recent-name {
|
||
font-size: 13.5px;
|
||
font-weight: 600;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.uc-recent-path {
|
||
font-size: 11.5px;
|
||
opacity: 0.45;
|
||
flex: 1;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.uc-recent-time {
|
||
font-size: 11.5px;
|
||
opacity: 0.5;
|
||
font-variant-numeric: tabular-nums;
|
||
flex-shrink: 0;
|
||
}
|
||
}
|
||
}
|
||
|
||
.uc-bm-group {
|
||
margin-bottom: 16px;
|
||
|
||
.uc-bm-cat {
|
||
font-size: 13px;
|
||
font-weight: 700;
|
||
opacity: 0.75;
|
||
margin-bottom: 6px;
|
||
}
|
||
|
||
.uc-bm-list {
|
||
.uc-bm {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
padding: 7px 10px;
|
||
border-radius: 8px;
|
||
|
||
&:hover {
|
||
background: rgba(128, 128, 128, 0.06);
|
||
|
||
.uc-bm-del {
|
||
opacity: 1;
|
||
}
|
||
}
|
||
|
||
.uc-bm-name {
|
||
font-size: 13px;
|
||
font-weight: 600;
|
||
color: #18a058;
|
||
text-decoration: none;
|
||
flex-shrink: 0;
|
||
|
||
&:hover {
|
||
text-decoration: underline;
|
||
}
|
||
}
|
||
|
||
.uc-bm-url {
|
||
font-size: 11.5px;
|
||
opacity: 0.45;
|
||
flex: 1;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.uc-bm-del {
|
||
opacity: 0;
|
||
color: #d03050;
|
||
font-size: 12px;
|
||
transition: opacity 0.15s;
|
||
flex-shrink: 0;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
</style>
|