File size: 4,723 Bytes
ef785b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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