3 changed files with 0 additions and 217 deletions
@ -1,171 +0,0 @@ |
|||
import { defineStore } from 'pinia' |
|||
import { api } from '@/api/axios' // 你的 API 配置
|
|||
|
|||
export const useAuthStore = defineStore('auth', { |
|||
state: () => ({ |
|||
// 用户信息
|
|||
user: null, |
|||
// 令牌
|
|||
token: localStorage.getItem('token') || null, |
|||
// 登录状态
|
|||
isLoggedIn: !!localStorage.getItem('token'), |
|||
// 加载状态
|
|||
isLoading: false, |
|||
// 错误信息
|
|||
error: null |
|||
}), |
|||
|
|||
getters: { |
|||
// 用户基本信息
|
|||
userInfo: (state) => state.user, |
|||
// 用户名
|
|||
userName: (state) => state.user?.username || state.user?.name || '用户', |
|||
// 用户角色
|
|||
userRole: (state) => state.user?.role || '', |
|||
// 是否有特定权限
|
|||
hasPermission: (state) => (permission) => { |
|||
return state.user?.permissions?.includes(permission) || false |
|||
}, |
|||
// 是否是管理员
|
|||
isAdmin: (state) => state.user?.role === 'admin' |
|||
}, |
|||
|
|||
actions: { |
|||
/** |
|||
* 用户登录 |
|||
*/ |
|||
async login(credentials) { |
|||
this.isLoading = true |
|||
this.error = null |
|||
|
|||
try { |
|||
const response = await api.post('/auth/login', credentials) |
|||
const { user, token } = response.data |
|||
|
|||
// 保存用户信息和令牌
|
|||
this.user = user |
|||
this.token = token |
|||
this.isLoggedIn = true |
|||
|
|||
// 保存到 localStorage
|
|||
localStorage.setItem('token', token) |
|||
localStorage.setItem('user', JSON.stringify(user)) |
|||
|
|||
// 设置 axios 默认授权头
|
|||
api.defaults.headers.common['Authorization'] = `Bearer ${token}` |
|||
|
|||
this.isLoading = false |
|||
return { success: true, user } |
|||
} catch (error) { |
|||
this.isLoading = false |
|||
this.error = error.response?.data?.message || '登录失败' |
|||
return { success: false, error: this.error } |
|||
} |
|||
}, |
|||
|
|||
/** |
|||
* 用户注册 |
|||
*/ |
|||
async register(userData) { |
|||
this.isLoading = true |
|||
this.error = null |
|||
|
|||
try { |
|||
const response = await api.post('/auth/register', userData) |
|||
const { user, token } = response.data |
|||
|
|||
// 自动登录
|
|||
this.user = user |
|||
this.token = token |
|||
this.isLoggedIn = true |
|||
|
|||
localStorage.setItem('token', token) |
|||
localStorage.setItem('user', JSON.stringify(user)) |
|||
api.defaults.headers.common['Authorization'] = `Bearer ${token}` |
|||
|
|||
this.isLoading = false |
|||
return { success: true, user } |
|||
} catch (error) { |
|||
this.isLoading = false |
|||
this.error = error.response?.data?.message || '注册失败' |
|||
return { success: false, error: this.error } |
|||
} |
|||
}, |
|||
|
|||
/** |
|||
* 用户退出登录 |
|||
*/ |
|||
logout() { |
|||
this.user = null |
|||
this.token = null |
|||
this.isLoggedIn = false |
|||
this.error = null |
|||
|
|||
// 清除 localStorage
|
|||
localStorage.removeItem('token') |
|||
localStorage.removeItem('user') |
|||
|
|||
// 移除 axios 授权头
|
|||
delete api.defaults.headers.common['Authorization'] |
|||
|
|||
// 可以在这里跳转到登录页
|
|||
if (this.router) { |
|||
this.router.push('/login') |
|||
} |
|||
}, |
|||
|
|||
/** |
|||
* 检查登录状态(页面刷新时调用) |
|||
*/ |
|||
async checkAuth() { |
|||
const token = localStorage.getItem('token') |
|||
const userData = localStorage.getItem('user') |
|||
|
|||
if (token && userData) { |
|||
try { |
|||
// 验证 token 是否有效
|
|||
const response = await api.get('/auth/me') |
|||
this.user = response.data.user |
|||
this.token = token |
|||
this.isLoggedIn = true |
|||
api.defaults.headers.common['Authorization'] = `Bearer ${token}` |
|||
} catch (error) { |
|||
// token 无效,清除状态
|
|||
this.logout() |
|||
} |
|||
} |
|||
}, |
|||
|
|||
/** |
|||
* 更新用户信息 |
|||
*/ |
|||
async updateUser(userData) { |
|||
try { |
|||
const response = await api.put('/auth/profile', userData) |
|||
this.user = { ...this.user, ...response.data.user } |
|||
|
|||
// 更新 localStorage
|
|||
localStorage.setItem('user', JSON.stringify(this.user)) |
|||
|
|||
return { success: true, user: this.user } |
|||
} catch (error) { |
|||
this.error = error.response?.data?.message || '更新失败' |
|||
return { success: false, error: this.error } |
|||
} |
|||
}, |
|||
|
|||
/** |
|||
* 清除错误信息 |
|||
*/ |
|||
clearError() { |
|||
this.error = null |
|||
}, |
|||
|
|||
/** |
|||
* 设置路由实例(用于退出登录后跳转) |
|||
*/ |
|||
setRouter(router) { |
|||
this.router = router |
|||
} |
|||
} |
|||
}) |
|||
@ -1,16 +0,0 @@ |
|||
import { defineStore } from 'pinia' |
|||
|
|||
export const useCounterStore = defineStore({ |
|||
id: 'counter', |
|||
state: () => ({ |
|||
counter: 0 |
|||
}), |
|||
getters: { |
|||
doubleCount: (state) => state.counter * 2 |
|||
}, |
|||
actions: { |
|||
increment() { |
|||
this.counter++ |
|||
} |
|||
} |
|||
}) |
|||
@ -1,30 +0,0 @@ |
|||
import { defineStore } from 'pinia'; |
|||
|
|||
export const userStStore = defineStore('user', { |
|||
state: () => { |
|||
return { |
|||
token: "ffdsasfoo", // 存储用户token
|
|||
user: null, // 存储用户对象
|
|||
isLoggedIn: false // 用户登录状态
|
|||
}; |
|||
}, |
|||
getters: { |
|||
getUser: (state) => state.user, |
|||
getIsLoggedIn: (state) => state.isLoggedIn, |
|||
token: (state) => state.token |
|||
}, |
|||
actions: { |
|||
setUser(user) { |
|||
this.user = user; |
|||
this.isLoggedIn = true; |
|||
}, |
|||
logout() { |
|||
this.token = null |
|||
this.user = null; |
|||
this.isLoggedIn = false; |
|||
}, |
|||
setToken(token) { |
|||
this.token = token; |
|||
} |
|||
}, |
|||
}); |
|||
Loading…
Reference in new issue