// API基础URL const API_BASE_URL = 'http://localhost:3000/api'; // 岗位相关API const JobAPI = { // 获取所有岗位 getAllJobs: async () => { try { const response = await fetch(`${API_BASE_URL}/jobs`); if (!response.ok) throw new Error('获取岗位列表失败'); return await response.json(); } catch (error) { console.error('获取岗位列表错误:', error); return []; } }, // 获取单个岗位详情 getJobById: async (id) => { try { const response = await fetch(`${API_BASE_URL}/jobs/${id}`); if (!response.ok) throw new Error('获取岗位详情失败'); return await response.json(); } catch (error) { console.error('获取岗位详情错误:', error); return null; } }, // 创建新岗位 createJob: async (jobData) => { try { const response = await fetch(`${API_BASE_URL}/jobs`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(jobData) }); if (!response.ok) throw new Error('创建岗位失败'); return await response.json(); } catch (error) { console.error('创建岗位错误:', error); throw error; } }, // 更新岗位 updateJob: async (id, jobData) => { try { const response = await fetch(`${API_BASE_URL}/jobs/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(jobData) }); if (!response.ok) throw new Error('更新岗位失败'); return await response.json(); } catch (error) { console.error('更新岗位错误:', error); throw error; } }, // 删除岗位 deleteJob: async (id) => { try { const response = await fetch(`${API_BASE_URL}/jobs/${id}`, { method: 'DELETE' }); if (!response.ok) throw new Error('删除岗位失败'); return await response.json(); } catch (error) { console.error('删除岗位错误:', error); throw error; } }, // 启动智能筛选 startScreening: async (id) => { try { const response = await fetch(`${API_BASE_URL}/jobs/${id}/screen`, { method: 'POST' }); if (!response.ok) throw new Error('启动筛选失败'); return await response.json(); } catch (error) { console.error('启动筛选错误:', error); throw error; } }, // 获取岗位候选人列表 getJobCandidates: async (id) => { try { const response = await fetch(`${API_BASE_URL}/jobs/${id}/candidates`); if (!response.ok) throw new Error('获取候选人列表失败'); return await response.json(); } catch (error) { console.error('获取候选人列表错误:', error); return { candidates: [] }; } }, // 发布草稿岗位 publishDraft: async (id) => { try { const response = await fetch(`${API_BASE_URL}/jobs/${id}/publish`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({}) }); if (!response.ok) throw new Error('发布岗位失败'); return await response.json(); } catch (error) { console.error('发布岗位错误:', error); throw error; } } }; // 候选人相关API const CandidateAPI = { // 获取所有候选人 getAllCandidates: async () => { try { const response = await fetch(`${API_BASE_URL}/candidates`); if (!response.ok) throw new Error('获取候选人列表失败'); return await response.json(); } catch (error) { console.error('获取候选人列表错误:', error); return []; } }, // 获取单个候选人详情 getCandidateDetails: async (id) => { try { const response = await fetch(`${API_BASE_URL}/candidates/${id}`); if (!response.ok) throw new Error('获取候选人详情失败'); return await response.json(); } catch (error) { console.error('获取候选人详情错误:', error); return null; } }, // 更新候选人状态 updateCandidateStatus: async (id, status) => { try { const response = await fetch(`${API_BASE_URL}/candidates/${id}/status`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ status }) }); if (!response.ok) throw new Error('更新候选人状态失败'); return await response.json(); } catch (error) { console.error('更新候选人状态错误:', error); throw error; } }, // 添加候选人 addCandidate: async (candidateData) => { try { const response = await fetch(`${API_BASE_URL}/candidates`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(candidateData) }); if (!response.ok) throw new Error('添加候选人失败'); return await response.json(); } catch (error) { console.error('添加候选人错误:', error); throw error; } } }; // 统计数据相关API const StatisticsAPI = { // 获取招聘总览统计 getOverviewStats: async () => { try { const response = await fetch(`${API_BASE_URL}/statistics/overview`); if (!response.ok) throw new Error('获取招聘总览统计失败'); return await response.json(); } catch (error) { console.error('获取招聘总览统计错误:', error); return { totalJobs: 0, totalCandidates: 0, pendingResumes: 0, passedResumes: 0 }; } }, // 获取部门招聘统计 getDepartmentStats: async () => { try { const response = await fetch(`${API_BASE_URL}/statistics/departments`); if (!response.ok) throw new Error('获取部门招聘统计失败'); return await response.json(); } catch (error) { console.error('获取部门招聘统计错误:', error); return []; } }, // 获取筛选效率统计 getScreeningEfficiencyStats: async () => { try { const response = await fetch(`${API_BASE_URL}/statistics/screening-efficiency`); if (!response.ok) throw new Error('获取筛选效率统计失败'); return await response.json(); } catch (error) { console.error('获取筛选效率统计错误:', error); return []; } }, // 获取候选人评分分布 getScoreDistribution: async () => { try { const response = await fetch(`${API_BASE_URL}/statistics/score-distribution`); if (!response.ok) throw new Error('获取候选人评分分布失败'); return await response.json(); } catch (error) { console.error('获取候选人评分分布错误:', error); return []; } } }; // 导出API const API = { Job: JobAPI, Candidate: CandidateAPI, Statistics: StatisticsAPI }; // 全局挂载API window.API = API;