import axios from 'axios' import { getMockResumeData, getMockJobs, getMockReport, getMockSuggestions } from './mock' const USE_MOCK = false function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)) } const api = axios.create({ baseURL: '', timeout: 10000, }) api.interceptors.response.use( (response) => response, (error) => { if (error.response) { const status = error.response.status if (status >= 500) { console.error('服务器错误,请稍后重试') } else if (status >= 400) { console.error('请求错误,请检查输入') } } else if (error.request) { console.error('网络连接失败,请检查网络') } return Promise.reject(error) } ) export async function uploadResume(file) { if (USE_MOCK) { await delay(800) return getMockResumeData() } const formData = new FormData() formData.append('file', file) return api.post('/api/resume/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }, }) } export async function getRecommendJobs(resumeId) { if (USE_MOCK) { await delay(500) return getMockJobs() } const res = await api.get('/api/jobs/recommend', { params: { resume_id: resumeId } }) const list = (res.data?.jobs || []).map(job => ({ ...job, match_rate: Math.round((job.match_rate || 0) * 100) })) return { data: { jobs: list } } } export async function generateReport(jobId, resumeData) { if (USE_MOCK) { const encoder = new TextEncoder() const report = getMockReport() const suggestions = getMockSuggestions() const stream = new ReadableStream({ async start(controller) { const events = [ { type: 'agent_start', agent: 'agent1' }, { type: 'agent_complete', agent: 'agent1', data: { must_have: ['Python', 'Redis', '分布式系统'], nice_to_have: ['Go', '微服务'], position_summary: '后端开发实习生 - 腾讯' } }, { type: 'agent_start', agent: 'agent2' }, { type: 'agent_complete', agent: 'agent2', data: { dimensions: report.dimensions, total_score: report.total_score, strength: report.strength, gap: report.gap, hr_view: report.hr_view } }, { type: 'agent_start', agent: 'agent3' }, { type: 'agent_complete', agent: 'agent3', data: suggestions }, { type: 'report_complete', data: { jd_parsed: { must_have: ['Python', 'Redis', '分布式系统'], nice_to_have: ['Go', '微服务'], position_summary: '后端开发实习生 - 腾讯' }, match_report: { dimensions: report.dimensions, total_score: report.total_score, strength: report.strength, gap: report.gap, hr_view: report.hr_view }, advice: suggestions } }, ] for (const event of events) { await delay(600) const line = JSON.stringify(event) + '\n' controller.enqueue(encoder.encode(line)) } controller.close() } }) return { ok: true, body: { getReader: () => stream.getReader() } } } const body = { job_id: jobId } if (resumeData) { const normalizeSkills = (s) => typeof s === 'string' ? s : (s.name || s) const normalizeProjects = (p) => typeof p === 'string' ? p : (p.name || JSON.stringify(p)) const normalizeString = (v) => { if (typeof v === 'string') return v if (v && typeof v === 'object') return v.school || v.name || JSON.stringify(v) return '' } body.resume_data = { name: resumeData.name || '', school: normalizeString(resumeData.school), major: normalizeString(resumeData.major), grade: normalizeString(resumeData.grade), skills: (resumeData.skills || []).map(normalizeSkills), projects: (resumeData.projects || []).map(normalizeProjects), summary: resumeData.summary || '', experience: resumeData.experience || '', education: normalizeString(resumeData.education), } } return fetch('/api/report/generate', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) } export async function rerankReport(jobId, editedResume) { if (USE_MOCK) { await delay(800) return { data: { match_rate: Math.floor(Math.random() * 30) + 60, previous_rate: 62 } } } return api.post('/api/report/rerank', { job_id: jobId, edited_resume: editedResume, }) } export async function confirmResume(resumeData) { if (USE_MOCK) { await delay(300) return { data: { success: true, data: resumeData } } } return api.post('/api/resume/confirm', resumeData) } export default api