Initial commit: ERP system with advance verification fixes

This commit is contained in:
System Administrator
2026-03-25 23:55:36 +07:00
commit 563ca12d76
5920 changed files with 828689 additions and 0 deletions
@@ -0,0 +1,96 @@
import { create } from 'zustand'
import { persist } from 'zustand/middleware'
import { API_CONFIG, API_ENDPOINTS } from '../config/api'
export interface User {
id: number
username: string
name: string
email?: string
role: 'admin' | 'manager' | 'user' | 'finance'
department?: string
avatar?: string
}
export interface AuthState {
user: User | null
token: string | null
isAuthenticated: boolean
isLoading: boolean
// Actions
login: (username: string, password: string) => Promise<void>
logout: () => void
setUser: (user: User) => void
setToken: (token: string) => void
clearAuth: () => void
}
export const useAuthStore = create<AuthState>()(
persist(
(set, get) => ({
user: null,
token: null,
isAuthenticated: false,
isLoading: false,
login: async (username: string, password: string) => {
set({ isLoading: true })
try {
// 调用真实后端API
const response = await fetch(`${API_CONFIG.baseURL}${API_ENDPOINTS.auth.login}`, {
method: 'POST',
headers: API_CONFIG.headers,
body: JSON.stringify({ username, password }),
})
if (!response.ok) {
const error = await response.json()
throw new Error(error.message || '登录失败')
}
const data = await response.json()
set({
user: data.data,
token: 'mock-token', // 后端没有返回token,使用模拟值
isAuthenticated: true,
isLoading: false
})
} catch (error) {
set({ isLoading: false })
throw error
}
},
logout: () => {
set({
user: null,
token: null,
isAuthenticated: false
})
},
setUser: (user: User) => {
set({ user })
},
setToken: (token: string) => {
set({ token })
},
clearAuth: () => {
set({
user: null,
token: null,
isAuthenticated: false
})
}
}),
{
name: 'auth-storage',
}
)
)