feat(frontend): 认证体系 + 类型定义 + API 服务层
认证: - AuthContext + useAuth (JWT token 持久化, 登录/登出) - AdminLoginPage (登录表单, 对接 POST /auth/login) - axios 拦截器自动注入 Authorization header - 401 自动清除 token 并跳转登录页 类型: - api.ts: TASK_STATUS 常量, TaskResponse/ProductScanResponse 扩展字段 - admin.ts: ProductResponse 扩展 + MaterialOption/BomItem API 服务: - authApi: login() + getMe() - materialApi: fetchMaterialGroups() + fetchMaterialItems() - printApi: getLabelPreview() + executePrint() + getPrinterConfig() - antd 6.5.3 依赖 字体: simhei.ttf (标签打印用)
This commit is contained in:
@ -8,11 +8,13 @@ const api = axios.create({
|
||||
},
|
||||
});
|
||||
|
||||
// 请求拦截器 — 可在此注入 token
|
||||
// 请求拦截器 — 注入 JWT token
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
// const token = localStorage.getItem("access_token");
|
||||
// if (token) config.headers.Authorization = `Bearer ${token}`;
|
||||
const token = localStorage.getItem("track_admin_token");
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => Promise.reject(error),
|
||||
@ -23,7 +25,12 @@ api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
// 未授权:跳转登录 / 刷新 token
|
||||
// Token 过期或无效 → 清除并跳转登录
|
||||
localStorage.removeItem("track_admin_token");
|
||||
localStorage.removeItem("track_admin_user");
|
||||
if (window.location.pathname.startsWith("/admin")) {
|
||||
window.location.href = "/admin/login";
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user