feat: 完善前后端核心功能模块
后端: - 新增认证(auth)、任务(tasks)、映射(mappings)、对账(reconciliation)、异常(exceptions)、导出(exports) API - 新增核心模块: database, security, permissions, tenant, exceptions, error_handlers - 新增数据模型: user, company, reconciliation_task, field_mapping, uploaded_file 等 - 新增服务层: ai_recognizer, file_parser, file_storage, mapping, reconciliation 等 - 添加数据库迁移脚本 前端: - 新增登录页面和仪表盘页面 - 新增任务列表、任务详情、字段映射页面 - 新增异常处理页面和规则设置页面 - 新增 API 代理路由 /api/[...path] - 新增 UI 组件库 (button, card, dialog, input, table 等) - 新增 auth 组件 (ProtectedRoute, PermissionGate) - 新增 layout 组件 (Header, Sidebar) - 新增 mapping 组件 (FieldMappingTable, AISuggestionPanel) - 新增 API 客户端和 hooks (useAsync, useToast, usePermission 等) - 新增状态管理 (auth-store, company-store, ui-store) - 集成 Tailwind CSS 和 shadcn/ui 组件库 其他: - 添加 Alembic 数据库迁移配置 - 添加初始化示例数据脚本 - 更新项目文档
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { create } from 'zustand';
|
||||
import { persist } from 'zustand/middleware';
|
||||
|
||||
/**
|
||||
* 用户信息接口
|
||||
*/
|
||||
export interface User {
|
||||
id: number;
|
||||
email: string;
|
||||
full_name: string;
|
||||
role: string;
|
||||
permissions: string[];
|
||||
company_id: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 认证状态接口
|
||||
*/
|
||||
interface AuthState {
|
||||
user: User | null;
|
||||
token: string | null;
|
||||
isAuthenticated: boolean;
|
||||
|
||||
// Actions
|
||||
login: (user: User, token: string) => void;
|
||||
logout: () => void;
|
||||
setUser: (user: User) => void;
|
||||
updateUser: (userData: Partial<User>) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 认证状态 Store
|
||||
* 使用 localStorage 持久化
|
||||
*/
|
||||
export const useAuthStore = create<AuthState>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
user: null,
|
||||
token: null,
|
||||
isAuthenticated: false,
|
||||
|
||||
login: (user, token) => {
|
||||
// 保存到 localStorage
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('auth_token', token);
|
||||
localStorage.setItem('company_id', String(user.company_id));
|
||||
}
|
||||
|
||||
set({
|
||||
user,
|
||||
token,
|
||||
isAuthenticated: true,
|
||||
});
|
||||
},
|
||||
|
||||
logout: () => {
|
||||
// 清除 localStorage
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.removeItem('auth_token');
|
||||
localStorage.removeItem('company_id');
|
||||
// 清除 cookie
|
||||
document.cookie = 'auth_token=; path=/; max-age=0; SameSite=Lax';
|
||||
document.cookie = 'company_id=; path=/; max-age=0; SameSite=Lax';
|
||||
}
|
||||
|
||||
set({
|
||||
user: null,
|
||||
token: null,
|
||||
isAuthenticated: false,
|
||||
});
|
||||
},
|
||||
|
||||
setUser: (user) => {
|
||||
set({ user, isAuthenticated: true });
|
||||
},
|
||||
|
||||
updateUser: (userData) => {
|
||||
set((state) => ({
|
||||
user: state.user ? { ...state.user, ...userData } : null,
|
||||
}));
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: 'auth-storage',
|
||||
partialize: (state) => ({
|
||||
user: state.user,
|
||||
token: state.token,
|
||||
isAuthenticated: state.isAuthenticated,
|
||||
}),
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -0,0 +1,96 @@
|
||||
import { create } from 'zustand';
|
||||
import { persist } from 'zustand/middleware';
|
||||
|
||||
/**
|
||||
* 企业信息接口
|
||||
*/
|
||||
export interface Company {
|
||||
id: number;
|
||||
name: string;
|
||||
plan: string;
|
||||
status: string;
|
||||
max_users: number;
|
||||
is_trial: boolean;
|
||||
data_retention_months: number;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业状态接口
|
||||
*/
|
||||
interface CompanyState {
|
||||
currentCompany: Company | null;
|
||||
companies: Company[];
|
||||
|
||||
// Actions
|
||||
setCompany: (company: Company) => void;
|
||||
setCompanies: (companies: Company[]) => void;
|
||||
addCompany: (company: Company) => void;
|
||||
updateCompany: (companyId: number, companyData: Partial<Company>) => void;
|
||||
removeCompany: (companyId: number) => void;
|
||||
clearCompanies: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业状态 Store
|
||||
* 使用 localStorage 持久化
|
||||
*/
|
||||
export const useCompanyStore = create<CompanyState>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
currentCompany: null,
|
||||
companies: [],
|
||||
|
||||
setCompany: (company) => {
|
||||
// 保存当前企业 ID 到 localStorage
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('company_id', String(company.id));
|
||||
}
|
||||
|
||||
set({ currentCompany: company });
|
||||
},
|
||||
|
||||
setCompanies: (companies) => {
|
||||
set({ companies });
|
||||
},
|
||||
|
||||
addCompany: (company) => {
|
||||
set((state) => ({
|
||||
companies: [...state.companies, company],
|
||||
}));
|
||||
},
|
||||
|
||||
updateCompany: (companyId, companyData) => {
|
||||
set((state) => ({
|
||||
companies: state.companies.map((c) =>
|
||||
c.id === companyId ? { ...c, ...companyData } : c
|
||||
),
|
||||
currentCompany:
|
||||
state.currentCompany?.id === companyId
|
||||
? { ...state.currentCompany, ...companyData }
|
||||
: state.currentCompany,
|
||||
}));
|
||||
},
|
||||
|
||||
removeCompany: (companyId) => {
|
||||
set((state) => ({
|
||||
companies: state.companies.filter((c) => c.id !== companyId),
|
||||
currentCompany:
|
||||
state.currentCompany?.id === companyId ? null : state.currentCompany,
|
||||
}));
|
||||
},
|
||||
|
||||
clearCompanies: () => {
|
||||
set({ companies: [], currentCompany: null });
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: 'company-storage',
|
||||
partialize: (state) => ({
|
||||
currentCompany: state.currentCompany,
|
||||
companies: state.companies,
|
||||
}),
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -0,0 +1,76 @@
|
||||
import { create } from 'zustand';
|
||||
import { persist } from 'zustand/middleware';
|
||||
|
||||
/**
|
||||
* UI 状态接口
|
||||
*/
|
||||
interface UIState {
|
||||
// 侧边栏状态
|
||||
sidebarOpen: boolean;
|
||||
|
||||
// 主题
|
||||
theme: 'light' | 'dark' | 'system';
|
||||
|
||||
// 加载状态
|
||||
loading: boolean;
|
||||
loadingMessage: string;
|
||||
|
||||
// Actions
|
||||
toggleSidebar: () => void;
|
||||
setSidebarOpen: (open: boolean) => void;
|
||||
setTheme: (theme: 'light' | 'dark' | 'system') => void;
|
||||
setLoading: (loading: boolean, message?: string) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* UI 状态 Store
|
||||
* 使用 localStorage 持久化部分状态
|
||||
*/
|
||||
export const useUIStore = create<UIState>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
sidebarOpen: true,
|
||||
theme: 'light',
|
||||
loading: false,
|
||||
loadingMessage: '',
|
||||
|
||||
toggleSidebar: () => {
|
||||
set((state) => ({ sidebarOpen: !state.sidebarOpen }));
|
||||
},
|
||||
|
||||
setSidebarOpen: (open) => {
|
||||
set({ sidebarOpen: open });
|
||||
},
|
||||
|
||||
setTheme: (theme) => {
|
||||
// 更新 DOM 元素的主题类
|
||||
if (typeof window !== 'undefined') {
|
||||
const root = window.document.documentElement;
|
||||
root.classList.remove('light', 'dark');
|
||||
|
||||
if (theme === 'system') {
|
||||
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches
|
||||
? 'dark'
|
||||
: 'light';
|
||||
root.classList.add(systemTheme);
|
||||
} else {
|
||||
root.classList.add(theme);
|
||||
}
|
||||
}
|
||||
|
||||
set({ theme });
|
||||
},
|
||||
|
||||
setLoading: (loading, message = '') => {
|
||||
set({ loading, loadingMessage: message });
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: 'ui-storage',
|
||||
partialize: (state) => ({
|
||||
sidebarOpen: state.sidebarOpen,
|
||||
theme: state.theme,
|
||||
}),
|
||||
}
|
||||
)
|
||||
);
|
||||
Reference in New Issue
Block a user