Compare commits
15 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
bf8375d345 | 1 month ago |
|
|
16201d567b | 1 month ago |
|
|
47e4236392 | 1 month ago |
|
|
9f24d1fb94 | 1 month ago |
|
|
4822e06a64 | 1 month ago |
|
|
9e8306be3b | 1 month ago |
|
|
76a98d3b95 | 1 month ago |
|
|
b43fa24165 | 1 month ago |
|
|
27d65ea08f | 1 month ago |
|
|
9409a2c5e8 | 1 month ago |
|
|
4f674154b4 | 1 month ago |
|
|
4de216d00c | 1 month ago |
|
|
c85cb97eed | 1 month ago |
|
|
f94177c6ed | 1 month ago |
|
|
91cf8f5d44 | 1 month ago |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,9 @@
|
|||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
.data
|
||||||
|
.config
|
||||||
|
.git
|
||||||
|
.vscode
|
||||||
|
*.md
|
||||||
|
design/
|
||||||
|
*.log
|
||||||
@ -0,0 +1,32 @@
|
|||||||
|
# Build stage
|
||||||
|
FROM node:20-alpine AS builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY package*.json ./
|
||||||
|
|
||||||
|
RUN npm ci
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# Production stage
|
||||||
|
FROM node:20-alpine
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY package*.json ./
|
||||||
|
|
||||||
|
RUN npm ci --only=production
|
||||||
|
|
||||||
|
COPY --from=builder /app/dist ./dist
|
||||||
|
COPY --from=builder /app/server.js ./
|
||||||
|
COPY --from=builder /app/src ./src
|
||||||
|
|
||||||
|
ENV NODE_ENV=production
|
||||||
|
ENV PORT=3001
|
||||||
|
|
||||||
|
EXPOSE 3001
|
||||||
|
|
||||||
|
CMD ["node", "server.js"]
|
||||||
File diff suppressed because one or more lines are too long
@ -0,0 +1,15 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
trip-planner:
|
||||||
|
build: .
|
||||||
|
container_name: trip-planner
|
||||||
|
ports:
|
||||||
|
- "3001:3001"
|
||||||
|
volumes:
|
||||||
|
- E:\docker_workspace\trip-planner_datas:/app/.data
|
||||||
|
environment:
|
||||||
|
- NODE_ENV=production
|
||||||
|
- PORT=3001
|
||||||
|
- DATA_DIR=/app/.data
|
||||||
|
restart: unless-stopped
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,416 @@
|
|||||||
|
<template>
|
||||||
|
<div v-if="props.visible" class="user-panel">
|
||||||
|
<div class="panel-content">
|
||||||
|
<!-- 用户信息 -->
|
||||||
|
<div class="user-section">
|
||||||
|
<div class="user-avatar">{{ authStore.user?.username?.charAt(0)?.toUpperCase() || 'U' }}</div>
|
||||||
|
<div class="user-details">
|
||||||
|
<div class="user-name">{{ authStore.user?.username }}</div>
|
||||||
|
<div class="user-role" :class="authStore.user?.role">
|
||||||
|
{{ authStore.user?.role === 'admin' ? '管理员' : '普通用户' }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button class="close-btn" @click="handleClose">✕</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 用户统计 -->
|
||||||
|
<div class="user-stats">
|
||||||
|
<div class="stat-item">
|
||||||
|
<span class="stat-num">{{ plans.length }}</span>
|
||||||
|
<span class="stat-label">行程规划</span>
|
||||||
|
</div>
|
||||||
|
<div class="stat-item">
|
||||||
|
<span class="stat-num">0</span>
|
||||||
|
<span class="stat-label">行程收藏</span>
|
||||||
|
</div>
|
||||||
|
<div class="stat-item">
|
||||||
|
<span class="stat-num">0</span>
|
||||||
|
<span class="stat-label">地点收藏</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Tab 导航 -->
|
||||||
|
<div class="tab-nav">
|
||||||
|
<button :class="{ active: activeTab === 'plans' }" @click="activeTab = 'plans'">📋 规划记录</button>
|
||||||
|
<button :class="{ active: activeTab === 'favorites' }" @click="activeTab = 'favorites'">⭐ 行程收藏</button>
|
||||||
|
<button :class="{ active: activeTab === 'locations' }" @click="activeTab = 'locations'">📍 地点收藏</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 规划记录 -->
|
||||||
|
<div v-if="activeTab === 'plans'" class="tab-content">
|
||||||
|
<div v-if="loadingPlans" class="loading">加载中...</div>
|
||||||
|
<div v-else-if="plans.length === 0" class="empty">暂无规划记录</div>
|
||||||
|
<div v-else class="plan-list">
|
||||||
|
<div v-for="plan in plans" :key="plan.id" class="plan-item" @click="loadPlan(plan)">
|
||||||
|
<div class="plan-name">{{ plan.name }}</div>
|
||||||
|
<div v-if="plan.description" class="plan-desc">{{ plan.description }}</div>
|
||||||
|
<div class="plan-date">{{ formatDate(plan.created_at) }}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 行程收藏 -->
|
||||||
|
<div v-if="activeTab === 'favorites'" class="tab-content">
|
||||||
|
<div class="empty-placeholder">
|
||||||
|
<div class="empty-icon">⭐</div>
|
||||||
|
<p>暂无收藏行程</p>
|
||||||
|
<p class="hint">功能开发中...</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 地点收藏 -->
|
||||||
|
<div v-if="activeTab === 'locations'" class="tab-content">
|
||||||
|
<div class="empty-placeholder">
|
||||||
|
<div class="empty-icon">📍</div>
|
||||||
|
<p>暂无收藏地点</p>
|
||||||
|
<p class="hint">功能开发中...</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 退出登录按钮 -->
|
||||||
|
<div class="logout-section">
|
||||||
|
<button class="logout-panel-btn" @click="handleLogout">🚪 退出登录</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { ref, onMounted } from 'vue'
|
||||||
|
import { useRouter } from 'vue-router'
|
||||||
|
import { useAuthStore } from '../stores/auth'
|
||||||
|
import { useItineraryStore } from '../stores/itinerary'
|
||||||
|
import { trackPlanLoad } from '../services/statsService'
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
visible: { type: Boolean, default: false }
|
||||||
|
})
|
||||||
|
|
||||||
|
const emit = defineEmits(['update:visible', 'load', 'close'])
|
||||||
|
|
||||||
|
const router = useRouter()
|
||||||
|
const authStore = useAuthStore()
|
||||||
|
const itineraryStore = useItineraryStore()
|
||||||
|
|
||||||
|
const activeTab = ref('plans')
|
||||||
|
const plans = ref([])
|
||||||
|
const loadingPlans = ref(false)
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
if (authStore.isAuthenticated) {
|
||||||
|
await loadPlans()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
function handleClose() {
|
||||||
|
emit('update:visible', false)
|
||||||
|
emit('close')
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleLogout() {
|
||||||
|
if (confirm('确定要退出登录吗?')) {
|
||||||
|
authStore.logout()
|
||||||
|
emit('update:visible', false)
|
||||||
|
router.push('/')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadPlans() {
|
||||||
|
loadingPlans.value = true
|
||||||
|
try {
|
||||||
|
const API_BASE = 'http://localhost:3001/api'
|
||||||
|
const headers = { 'Authorization': `Bearer ${authStore.token}` }
|
||||||
|
|
||||||
|
const res = await fetch(`${API_BASE}/plans`, { headers })
|
||||||
|
const data = await res.json()
|
||||||
|
plans.value = data.plans || []
|
||||||
|
} catch (err) {
|
||||||
|
console.error('加载规划记录失败:', err)
|
||||||
|
} finally {
|
||||||
|
loadingPlans.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadPlan(plan) {
|
||||||
|
loadingPlans.value = true
|
||||||
|
try {
|
||||||
|
const API_BASE = 'http://localhost:3001/api'
|
||||||
|
const headers = { 'Authorization': `Bearer ${authStore.token}` }
|
||||||
|
|
||||||
|
const res = await fetch(`${API_BASE}/plans/${plan.id}`, { headers })
|
||||||
|
const data = await res.json()
|
||||||
|
|
||||||
|
if (data.plan) {
|
||||||
|
const planData = data.plan.data
|
||||||
|
if (planData.points) {
|
||||||
|
itineraryStore.loadFromAI({ points: planData.points })
|
||||||
|
itineraryStore.setPhase('workbench')
|
||||||
|
trackPlanLoad(plan.id)
|
||||||
|
emit('load', plan)
|
||||||
|
router.push('/')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('加载行程失败:', err)
|
||||||
|
alert('加载行程失败')
|
||||||
|
} finally {
|
||||||
|
loadingPlans.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatDate(dateStr) {
|
||||||
|
const date = new Date(dateStr)
|
||||||
|
return date.toLocaleDateString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' })
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.user-panel {
|
||||||
|
position: fixed;
|
||||||
|
top: 60px;
|
||||||
|
right: 0;
|
||||||
|
width: 320px;
|
||||||
|
height: calc(100vh - 60px);
|
||||||
|
background: #fff;
|
||||||
|
box-shadow: -4px 0 20px rgba(0, 0, 0, 0.1);
|
||||||
|
z-index: 9998;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-content {
|
||||||
|
flex: 1;
|
||||||
|
overflow: hidden;
|
||||||
|
padding: 20px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 用户信息 */
|
||||||
|
.user-section {
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 12px;
|
||||||
|
padding-bottom: 16px;
|
||||||
|
border-bottom: 1px solid #e9ecef;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-avatar {
|
||||||
|
width: 48px;
|
||||||
|
height: 48px;
|
||||||
|
border-radius: 50%;
|
||||||
|
background: linear-gradient(135deg, #6c5ce7, #a29bfe);
|
||||||
|
color: #fff;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
font-size: 20px;
|
||||||
|
font-weight: 700;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-details {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-name {
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: #2d3436;
|
||||||
|
white-space: nowrap;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-role {
|
||||||
|
font-size: 12px;
|
||||||
|
padding: 2px 8px;
|
||||||
|
border-radius: 10px;
|
||||||
|
display: inline-block;
|
||||||
|
margin-top: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-role.admin {
|
||||||
|
background: #ffe0e0;
|
||||||
|
color: #d63031;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-role.user {
|
||||||
|
background: #e8e4ff;
|
||||||
|
color: #6c5ce7;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 关闭按钮 */
|
||||||
|
.close-btn {
|
||||||
|
flex-shrink: 0;
|
||||||
|
width: 28px;
|
||||||
|
height: 28px;
|
||||||
|
border-radius: 50%;
|
||||||
|
background: #f5f7fa;
|
||||||
|
color: #636e72;
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 16px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
transition: all 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.close-btn:hover {
|
||||||
|
background: #e8e4ff;
|
||||||
|
color: #6c5ce7;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Tab 导航 */
|
||||||
|
.tab-nav {
|
||||||
|
display: flex;
|
||||||
|
gap: 4px;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab-nav button {
|
||||||
|
flex: 1;
|
||||||
|
padding: 8px 4px;
|
||||||
|
border: none;
|
||||||
|
background: #f5f7fa;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-size: 12px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.2s;
|
||||||
|
color: #636e72;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab-nav button.active {
|
||||||
|
background: #6c5ce7;
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab-nav button:hover:not(.active) {
|
||||||
|
background: #e8e4ff;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Tab 内容 - 可滚动区域 */
|
||||||
|
.tab-content {
|
||||||
|
flex: 1;
|
||||||
|
overflow-y: auto;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 用户统计 */
|
||||||
|
.user-stats {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-around;
|
||||||
|
padding: 16px 0;
|
||||||
|
border-bottom: 1px solid #e9ecef;
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-item {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-num {
|
||||||
|
font-size: 20px;
|
||||||
|
font-weight: 700;
|
||||||
|
color: #6c5ce7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-label {
|
||||||
|
font-size: 11px;
|
||||||
|
color: #636e72;
|
||||||
|
}
|
||||||
|
|
||||||
|
.loading,
|
||||||
|
.empty {
|
||||||
|
text-align: center;
|
||||||
|
color: #b2bec3;
|
||||||
|
padding: 40px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty-placeholder {
|
||||||
|
text-align: center;
|
||||||
|
padding: 40px 0;
|
||||||
|
color: #b2bec3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty-icon {
|
||||||
|
font-size: 48px;
|
||||||
|
margin-bottom: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty-placeholder .hint {
|
||||||
|
font-size: 12px;
|
||||||
|
color: #dfe6e9;
|
||||||
|
margin-top: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 规划列表 */
|
||||||
|
.plan-list {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.plan-item {
|
||||||
|
padding: 12px;
|
||||||
|
background: #f5f7fa;
|
||||||
|
border-radius: 8px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.plan-item:hover {
|
||||||
|
background: #e8e4ff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.plan-name {
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: #2d3436;
|
||||||
|
margin-bottom: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.plan-desc {
|
||||||
|
font-size: 11px;
|
||||||
|
color: #636e72;
|
||||||
|
margin-bottom: 6px;
|
||||||
|
line-height: 1.3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.plan-date {
|
||||||
|
font-size: 10px;
|
||||||
|
color: #b2bec3;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 退出登录区域 - 固定在底部,不随列表滚动 */
|
||||||
|
.logout-section {
|
||||||
|
flex-shrink: 0;
|
||||||
|
padding-top: 16px;
|
||||||
|
border-top: 1px solid #e9ecef;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logout-panel-btn {
|
||||||
|
width: 100%;
|
||||||
|
padding: 12px;
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 2px solid #d63031;
|
||||||
|
background: #fff;
|
||||||
|
color: #d63031;
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 600;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logout-panel-btn:hover {
|
||||||
|
background: #d63031;
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,52 @@
|
|||||||
|
// 使用统计服务
|
||||||
|
import { useAuthStore } from '../stores/auth'
|
||||||
|
|
||||||
|
const API_BASE = 'http://localhost:3001/api'
|
||||||
|
|
||||||
|
export function trackEvent(eventType, eventData = {}) {
|
||||||
|
const authStore = useAuthStore()
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
userId: authStore.user?.id || null,
|
||||||
|
guestId: authStore.guestId || null,
|
||||||
|
eventType,
|
||||||
|
eventData
|
||||||
|
}
|
||||||
|
|
||||||
|
// 异步发送,不阻塞主流程
|
||||||
|
fetch(`${API_BASE}/stats/event`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(payload)
|
||||||
|
}).catch(err => console.error('统计记录失败:', err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 页面访问统计
|
||||||
|
export function trackPageView(pageName, extra = {}) {
|
||||||
|
trackEvent('page_view', { page: pageName, ...extra })
|
||||||
|
}
|
||||||
|
|
||||||
|
// 规划创建统计
|
||||||
|
export function trackPlanCreate(planName, mode) {
|
||||||
|
trackEvent('plan_create', { planName, mode })
|
||||||
|
}
|
||||||
|
|
||||||
|
// 规划加载统计
|
||||||
|
export function trackPlanLoad(planId) {
|
||||||
|
trackEvent('plan_load', { planId })
|
||||||
|
}
|
||||||
|
|
||||||
|
// 规划导出统计
|
||||||
|
export function trackPlanExport(planId, format) {
|
||||||
|
trackEvent('plan_export', { planId, format })
|
||||||
|
}
|
||||||
|
|
||||||
|
// 聊天开始统计
|
||||||
|
export function trackChatStart(mode) {
|
||||||
|
trackEvent('chat_start', { mode })
|
||||||
|
}
|
||||||
|
|
||||||
|
// 方案选择统计
|
||||||
|
export function trackSchemeSelect(schemeName, schemeIndex) {
|
||||||
|
trackEvent('scheme_select', { schemeName, schemeIndex })
|
||||||
|
}
|
||||||
@ -0,0 +1,218 @@
|
|||||||
|
// 用户认证状态管理
|
||||||
|
import { defineStore } from 'pinia'
|
||||||
|
import { ref, computed } from 'vue'
|
||||||
|
|
||||||
|
const API_BASE = 'http://localhost:3001/api'
|
||||||
|
|
||||||
|
export const useAuthStore = defineStore('auth', () => {
|
||||||
|
// 状态
|
||||||
|
const user = ref(null)
|
||||||
|
const token = ref(localStorage.getItem('auth_token') || null)
|
||||||
|
const guestId = ref(localStorage.getItem('guest_id') || null)
|
||||||
|
const guestExpiresAt = ref(localStorage.getItem('guest_expires_at') || null)
|
||||||
|
const loading = ref(false)
|
||||||
|
const error = ref(null)
|
||||||
|
|
||||||
|
// 计算属性
|
||||||
|
const isAuthenticated = computed(() => !!user.value && !!token.value)
|
||||||
|
const isGuest = computed(() => !!guestId.value && !user.value)
|
||||||
|
const isAdmin = computed(() => user.value?.role === 'admin')
|
||||||
|
const isGuestDataExpiring = computed(() => {
|
||||||
|
if (!guestExpiresAt.value) return false
|
||||||
|
const expires = new Date(guestExpiresAt.value)
|
||||||
|
const now = new Date()
|
||||||
|
const hoursLeft = (expires - now) / (1000 * 60 * 60)
|
||||||
|
return hoursLeft < 2 // 少于2小时提醒
|
||||||
|
})
|
||||||
|
const isGuestDataExpired = computed(() => {
|
||||||
|
if (!guestExpiresAt.value) return false
|
||||||
|
return new Date(guestExpiresAt.value) < new Date()
|
||||||
|
})
|
||||||
|
|
||||||
|
// 方法
|
||||||
|
function setAuthHeader() {
|
||||||
|
if (token.value) {
|
||||||
|
return { 'Authorization': `Bearer ${token.value}` }
|
||||||
|
}
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function login(username, password) {
|
||||||
|
loading.value = true
|
||||||
|
error.value = null
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_BASE}/auth/login`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ username, password })
|
||||||
|
})
|
||||||
|
const data = await res.json()
|
||||||
|
if (!res.ok) throw new Error(data.error || '登录失败')
|
||||||
|
|
||||||
|
user.value = data.user
|
||||||
|
token.value = data.token
|
||||||
|
localStorage.setItem('auth_token', data.token)
|
||||||
|
|
||||||
|
// 如果有游客数据,提示迁移
|
||||||
|
return { success: true, user: data.user }
|
||||||
|
} catch (err) {
|
||||||
|
error.value = err.message
|
||||||
|
return { success: false, error: err.message }
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function register(username, email, password) {
|
||||||
|
loading.value = true
|
||||||
|
error.value = null
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_BASE}/auth/register`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ username, email, password })
|
||||||
|
})
|
||||||
|
const data = await res.json()
|
||||||
|
if (!res.ok) throw new Error(data.error || '注册失败')
|
||||||
|
|
||||||
|
user.value = data.user
|
||||||
|
token.value = data.token
|
||||||
|
localStorage.setItem('auth_token', data.token)
|
||||||
|
|
||||||
|
return { success: true, user: data.user }
|
||||||
|
} catch (err) {
|
||||||
|
error.value = err.message
|
||||||
|
return { success: false, error: err.message }
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function logout() {
|
||||||
|
user.value = null
|
||||||
|
token.value = null
|
||||||
|
localStorage.removeItem('auth_token')
|
||||||
|
}
|
||||||
|
|
||||||
|
async function initGuestSession() {
|
||||||
|
// 检查现有游客会话是否过期
|
||||||
|
if (guestId.value && guestExpiresAt.value) {
|
||||||
|
const expires = new Date(guestExpiresAt.value)
|
||||||
|
if (expires > new Date()) {
|
||||||
|
return guestId.value // 会话仍有效
|
||||||
|
}
|
||||||
|
// 过期了,清除
|
||||||
|
clearGuestSession()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建新游客会话
|
||||||
|
loading.value = true
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_BASE}/auth/guest`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' }
|
||||||
|
})
|
||||||
|
const data = await res.json()
|
||||||
|
if (!res.ok) throw new Error(data.error || '创建游客会话失败')
|
||||||
|
|
||||||
|
guestId.value = data.guestId
|
||||||
|
guestExpiresAt.value = data.expiresAt
|
||||||
|
localStorage.setItem('guest_id', data.guestId)
|
||||||
|
localStorage.setItem('guest_expires_at', data.expiresAt)
|
||||||
|
|
||||||
|
return data.guestId
|
||||||
|
} catch (err) {
|
||||||
|
error.value = err.message
|
||||||
|
throw err
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearGuestSession() {
|
||||||
|
guestId.value = null
|
||||||
|
guestExpiresAt.value = null
|
||||||
|
localStorage.removeItem('guest_id')
|
||||||
|
localStorage.removeItem('guest_expires_at')
|
||||||
|
}
|
||||||
|
|
||||||
|
async function migrateGuestData() {
|
||||||
|
if (!token.value || !guestId.value) return
|
||||||
|
|
||||||
|
loading.value = true
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_BASE}/auth/migrate-guest-data`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...setAuthHeader()
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ guestId: guestId.value })
|
||||||
|
})
|
||||||
|
const data = await res.json()
|
||||||
|
if (!res.ok) throw new Error(data.error || '数据迁移失败')
|
||||||
|
|
||||||
|
clearGuestSession()
|
||||||
|
return data
|
||||||
|
} catch (err) {
|
||||||
|
error.value = err.message
|
||||||
|
throw err
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadCurrentUser() {
|
||||||
|
if (!token.value) return
|
||||||
|
|
||||||
|
loading.value = true
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_BASE}/auth/me`, {
|
||||||
|
headers: setAuthHeader()
|
||||||
|
})
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json()
|
||||||
|
user.value = data.user
|
||||||
|
} else {
|
||||||
|
// Token 无效,清除
|
||||||
|
logout()
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('加载用户信息失败:', err)
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化时加载用户信息
|
||||||
|
async function init() {
|
||||||
|
if (token.value) {
|
||||||
|
await loadCurrentUser()
|
||||||
|
} else {
|
||||||
|
await initGuestSession()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
user,
|
||||||
|
token,
|
||||||
|
guestId,
|
||||||
|
guestExpiresAt,
|
||||||
|
loading,
|
||||||
|
error,
|
||||||
|
isAuthenticated,
|
||||||
|
isGuest,
|
||||||
|
isAdmin,
|
||||||
|
isGuestDataExpiring,
|
||||||
|
isGuestDataExpired,
|
||||||
|
login,
|
||||||
|
register,
|
||||||
|
logout,
|
||||||
|
initGuestSession,
|
||||||
|
clearGuestSession,
|
||||||
|
migrateGuestData,
|
||||||
|
loadCurrentUser,
|
||||||
|
init,
|
||||||
|
setAuthHeader
|
||||||
|
}
|
||||||
|
})
|
||||||
Loading…
Reference in new issue