2025-07-22 14:39:45 +08:00
|
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
|
import { ref, computed } from 'vue'
|
2025-07-28 09:51:21 +08:00
|
|
|
|
import { AuthApi, type User as ApiUser } from '@/api'
|
2025-07-22 14:39:45 +08:00
|
|
|
|
|
2025-07-28 09:51:21 +08:00
|
|
|
|
// 扩展API用户类型以保持兼容性
|
|
|
|
|
export interface User extends ApiUser {}
|
2025-07-22 14:39:45 +08:00
|
|
|
|
|
|
|
|
|
export const useUserStore = defineStore('user', () => {
|
|
|
|
|
// 状态
|
|
|
|
|
const user = ref<User | null>(null)
|
|
|
|
|
const token = ref<string | null>(localStorage.getItem('token'))
|
|
|
|
|
const isLoading = ref(false)
|
|
|
|
|
|
|
|
|
|
// 计算属性
|
|
|
|
|
const isLoggedIn = computed(() => !!user.value && !!token.value)
|
|
|
|
|
const isStudent = computed(() => user.value?.role === 'student')
|
|
|
|
|
const isTeacher = computed(() => user.value?.role === 'teacher')
|
|
|
|
|
const isAdmin = computed(() => user.value?.role === 'admin')
|
|
|
|
|
|
2025-07-28 09:51:21 +08:00
|
|
|
|
// 方法 - 简化版本,主要用于状态管理
|
2025-07-22 14:39:45 +08:00
|
|
|
|
const login = async (credentials: { email: string; password: string }) => {
|
2025-07-28 09:51:21 +08:00
|
|
|
|
// 这个方法现在主要用于兼容性,实际登录逻辑在组件中处理
|
|
|
|
|
return { success: true, message: '请使用登录模态框进行登录' }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const register = async (userData: any) => {
|
|
|
|
|
// 这个方法现在主要用于兼容性,实际注册逻辑在组件中处理
|
|
|
|
|
return { success: true, message: '请使用注册模态框进行注册' }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const logout = async () => {
|
2025-07-22 14:39:45 +08:00
|
|
|
|
try {
|
2025-07-28 09:51:21 +08:00
|
|
|
|
// 调用登出API
|
|
|
|
|
await AuthApi.logout()
|
2025-07-22 14:39:45 +08:00
|
|
|
|
} catch (error) {
|
2025-07-28 09:51:21 +08:00
|
|
|
|
console.error('登出API调用失败:', error)
|
2025-07-22 14:39:45 +08:00
|
|
|
|
} finally {
|
2025-07-28 09:51:21 +08:00
|
|
|
|
// 无论API调用是否成功,都清除本地数据
|
|
|
|
|
user.value = null
|
|
|
|
|
token.value = null
|
|
|
|
|
localStorage.removeItem('token')
|
|
|
|
|
localStorage.removeItem('refreshToken')
|
|
|
|
|
localStorage.removeItem('user')
|
|
|
|
|
localStorage.removeItem('rememberMe')
|
2025-07-22 14:39:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 09:51:21 +08:00
|
|
|
|
// 获取当前用户信息
|
|
|
|
|
const getCurrentUser = async () => {
|
|
|
|
|
if (!token.value) {
|
|
|
|
|
return { success: false, message: '未登录' }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果已经有用户信息,直接返回成功
|
|
|
|
|
if (user.value) {
|
|
|
|
|
return { success: true, message: '用户信息已存在' }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 尝试从localStorage恢复用户信息
|
|
|
|
|
const savedUser = localStorage.getItem('user')
|
|
|
|
|
if (savedUser) {
|
|
|
|
|
try {
|
|
|
|
|
user.value = JSON.parse(savedUser)
|
|
|
|
|
return { success: true, message: '用户信息已恢复' }
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('解析用户信息失败:', error)
|
2025-07-22 14:39:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 09:51:21 +08:00
|
|
|
|
// 暂时注释掉API调用,因为后端可能没有这个接口
|
|
|
|
|
// isLoading.value = true
|
|
|
|
|
// try {
|
|
|
|
|
// const response = await AuthApi.getCurrentUser()
|
|
|
|
|
|
|
|
|
|
// if (response.code === 200 || response.code === 0) {
|
|
|
|
|
// user.value = response.data
|
|
|
|
|
// localStorage.setItem('user', JSON.stringify(response.data))
|
|
|
|
|
// return { success: true, message: '获取用户信息成功' }
|
|
|
|
|
// } else {
|
|
|
|
|
// return { success: false, message: response.message || '获取用户信息失败' }
|
|
|
|
|
// }
|
|
|
|
|
// } catch (error: any) {
|
|
|
|
|
// console.error('获取用户信息失败:', error)
|
|
|
|
|
|
|
|
|
|
// // 如果是401错误,说明token已过期,自动登出
|
|
|
|
|
// if (error.response?.status === 401) {
|
|
|
|
|
// await logout()
|
|
|
|
|
// return { success: false, message: '登录已过期,请重新登录' }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return { success: false, message: '获取用户信息失败' }
|
|
|
|
|
// } finally {
|
|
|
|
|
// isLoading.value = false
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
return { success: false, message: '无法获取用户信息' }
|
2025-07-22 14:39:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 09:51:21 +08:00
|
|
|
|
const updateProfile = async (profileData: any) => {
|
2025-07-22 14:39:45 +08:00
|
|
|
|
isLoading.value = true
|
|
|
|
|
try {
|
2025-07-28 09:51:21 +08:00
|
|
|
|
const response = await AuthApi.updateProfile(profileData)
|
|
|
|
|
|
|
|
|
|
if (response.code === 200) {
|
|
|
|
|
user.value = response.data
|
|
|
|
|
localStorage.setItem('user', JSON.stringify(response.data))
|
|
|
|
|
return { success: true, message: '更新成功' }
|
|
|
|
|
} else {
|
|
|
|
|
return { success: false, message: response.message || '更新失败' }
|
2025-07-22 14:39:45 +08:00
|
|
|
|
}
|
2025-07-28 09:51:21 +08:00
|
|
|
|
} catch (error: any) {
|
|
|
|
|
console.error('更新用户资料失败:', error)
|
|
|
|
|
|
|
|
|
|
let message = '更新失败'
|
|
|
|
|
if (error.response?.data?.message) {
|
|
|
|
|
message = error.response.data.message
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { success: false, message }
|
2025-07-22 14:39:45 +08:00
|
|
|
|
} finally {
|
|
|
|
|
isLoading.value = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 09:51:21 +08:00
|
|
|
|
const initializeAuth = async () => {
|
2025-07-22 14:39:45 +08:00
|
|
|
|
const savedUser = localStorage.getItem('user')
|
|
|
|
|
const savedToken = localStorage.getItem('token')
|
2025-07-28 09:51:21 +08:00
|
|
|
|
|
2025-07-22 14:39:45 +08:00
|
|
|
|
if (savedUser && savedToken) {
|
|
|
|
|
try {
|
|
|
|
|
user.value = JSON.parse(savedUser)
|
|
|
|
|
token.value = savedToken
|
2025-07-28 09:51:21 +08:00
|
|
|
|
|
|
|
|
|
// 验证token是否仍然有效
|
|
|
|
|
await getCurrentUser()
|
2025-07-22 14:39:45 +08:00
|
|
|
|
} catch (error) {
|
2025-07-28 09:51:21 +08:00
|
|
|
|
console.error('Failed to parse saved user data or token expired:', error)
|
|
|
|
|
await logout()
|
2025-07-22 14:39:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 09:51:21 +08:00
|
|
|
|
|
|
|
|
|
|
2025-07-22 14:39:45 +08:00
|
|
|
|
return {
|
|
|
|
|
// 状态
|
|
|
|
|
user,
|
|
|
|
|
token,
|
|
|
|
|
isLoading,
|
|
|
|
|
// 计算属性
|
|
|
|
|
isLoggedIn,
|
|
|
|
|
isStudent,
|
|
|
|
|
isTeacher,
|
|
|
|
|
isAdmin,
|
|
|
|
|
// 方法
|
|
|
|
|
login,
|
|
|
|
|
register,
|
|
|
|
|
logout,
|
2025-07-28 09:51:21 +08:00
|
|
|
|
getCurrentUser,
|
2025-07-22 14:39:45 +08:00
|
|
|
|
updateProfile,
|
|
|
|
|
initializeAuth
|
|
|
|
|
}
|
|
|
|
|
})
|