feat(frontend): 添加基础设施模块

工具函数:
- utils/auth.ts,Token 存储、解析、过期校验

API 模块:
- api/request.ts,Axios 实例封装,请求/响应拦截器

状态管理:
- stores/auth.ts,认证状态管理(Pinia)

组合式函数:
- composables/useAuth.ts,认证逻辑封装

路由配置:
- router/index.ts,路由表定义、路由守卫(未登录重定向)
This commit is contained in:
2026-07-14 15:39:19 +08:00
parent fba7210fd1
commit 84394f4674
5 changed files with 261 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { authApi } from '@/api/auth'
export function useAuth() {
const router = useRouter()
const authStore = useAuthStore()
const login = async (username: string, password: string) => {
const response = await authApi.login({ username, password })
const { token, user } = response.data
authStore.setAuth(token, user)
return response
}
const logout = async () => {
await authApi.logout()
authStore.clearAuth()
router.push('/login')
}
const checkAuth = () => {
if (!authStore.isLoggedIn()) {
router.push('/login')
return false
}
return true
}
return {
login,
logout,
checkAuth,
}
}