eef91a6c83
前端: - 在 API 请求中添加 Accept-Language 头,传递当前语言设置 后端: - 创建 LanguageMiddleware 中间件,自动检测并设置语言 - 为所有 API 路由添加语言中间件 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
111 lines
3.1 KiB
TypeScript
111 lines
3.1 KiB
TypeScript
import axios from 'axios'
|
|
|
|
const API_BASE = '/api/v1'
|
|
|
|
// 创建 axios 实例
|
|
const api = axios.create({
|
|
baseURL: API_BASE,
|
|
timeout: 30000,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
})
|
|
|
|
// 请求拦截器:添加 token 和语言
|
|
api.interceptors.request.use(
|
|
(config) => {
|
|
const token = localStorage.getItem('token')
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`
|
|
}
|
|
// 添加语言头
|
|
const lang = localStorage.getItem('i18nextLng') || 'zh'
|
|
config.headers['Accept-Language'] = lang
|
|
return config
|
|
},
|
|
(error) => {
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
|
|
// 响应拦截器:处理错误
|
|
api.interceptors.response.use(
|
|
(response) => {
|
|
const { data } = response
|
|
if (data.code === 0) {
|
|
return data.data
|
|
}
|
|
return Promise.reject(new Error(data.message || '请求失败'))
|
|
},
|
|
(error) => {
|
|
const message = error.response?.data?.message || error.message || '网络错误'
|
|
return Promise.reject(new Error(message))
|
|
}
|
|
)
|
|
|
|
// ========== 认证 API ==========
|
|
export const authApi = {
|
|
login: (username: string, password: string) =>
|
|
api.post('/auth/login', { username, password }),
|
|
|
|
feishuLogin: (authCode: string, tenantKey: string) =>
|
|
api.post('/auth/feishu-login', { auth_code: authCode, tenant_key: tenantKey }),
|
|
}
|
|
|
|
// ========== 用户 API ==========
|
|
export const userApi = {
|
|
getInfo: () => api.get('/user/info'),
|
|
getMenu: () => api.get('/user/menu'),
|
|
getCompanies: () => api.get('/user/companies'),
|
|
switchCompany: (companyId: number) =>
|
|
api.post('/user/switch-company', { company_id: companyId }),
|
|
|
|
// 获取用户列表
|
|
getUsers: (params: { keyword?: string; page?: number; page_size?: number }) =>
|
|
api.get('/user/list', { params }),
|
|
}
|
|
|
|
// ========== 出差申请 API ==========
|
|
export const businessTripApi = {
|
|
getList: (params: { type?: string; page?: number; page_size?: number }) =>
|
|
api.get('/business-trip/list', { params }),
|
|
|
|
getDetail: (id: number) => api.get(`/business-trip/detail/${id}`),
|
|
|
|
create: (data: any) => api.post('/business-trip/create', data),
|
|
|
|
update: (id: number, data: any) =>
|
|
api.post(`/business-trip/update/${id}`, data),
|
|
|
|
submit: (id: number, approvers: number[], ccUsers: number[] = []) =>
|
|
api.post(`/business-trip/submit/${id}`, {
|
|
approvers,
|
|
cc_users: ccUsers,
|
|
}),
|
|
|
|
cancel: (id: number) => api.post(`/business-trip/cancel/${id}`),
|
|
|
|
deleteDraft: (id: number) => api.delete(`/business-trip/${id}`),
|
|
|
|
approve: (id: number, action: 'approve' | 'reject', comment?: string) =>
|
|
api.post(`/business-trip/approve/${id}`, { action, comment }),
|
|
}
|
|
|
|
// ========== 审批 API ==========
|
|
export const approvalApi = {
|
|
getList: (params: { type?: string; page?: number; page_size?: number }) =>
|
|
api.get('/approval/list', { params }),
|
|
|
|
getDetail: (id: number) => api.get(`/approval/detail/${id}`),
|
|
|
|
approve: (id: number, comment?: string) =>
|
|
api.post(`/approval/approve/${id}`, { comment }),
|
|
|
|
reject: (id: number, comment?: string) =>
|
|
api.post(`/approval/reject/${id}`, { comment }),
|
|
|
|
cancel: (id: number) => api.post(`/approval/cancel/${id}`),
|
|
}
|
|
|
|
export default api
|