2
2

api-adapter.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // API基础URL
  2. const API_BASE_URL = 'http://localhost:3000/api';
  3. // 岗位相关API
  4. const JobAPI = {
  5. // 获取所有岗位
  6. getAllJobs: async () => {
  7. try {
  8. const response = await fetch(`${API_BASE_URL}/jobs`);
  9. if (!response.ok) throw new Error('获取岗位列表失败');
  10. return await response.json();
  11. } catch (error) {
  12. console.error('获取岗位列表错误:', error);
  13. return [];
  14. }
  15. },
  16. // 获取单个岗位详情
  17. getJobById: async (id) => {
  18. try {
  19. const response = await fetch(`${API_BASE_URL}/jobs/${id}`);
  20. if (!response.ok) throw new Error('获取岗位详情失败');
  21. return await response.json();
  22. } catch (error) {
  23. console.error('获取岗位详情错误:', error);
  24. return null;
  25. }
  26. },
  27. // 创建新岗位
  28. createJob: async (jobData) => {
  29. try {
  30. const response = await fetch(`${API_BASE_URL}/jobs`, {
  31. method: 'POST',
  32. headers: {
  33. 'Content-Type': 'application/json'
  34. },
  35. body: JSON.stringify(jobData)
  36. });
  37. if (!response.ok) throw new Error('创建岗位失败');
  38. return await response.json();
  39. } catch (error) {
  40. console.error('创建岗位错误:', error);
  41. throw error;
  42. }
  43. },
  44. // 更新岗位
  45. updateJob: async (id, jobData) => {
  46. try {
  47. const response = await fetch(`${API_BASE_URL}/jobs/${id}`, {
  48. method: 'PUT',
  49. headers: {
  50. 'Content-Type': 'application/json'
  51. },
  52. body: JSON.stringify(jobData)
  53. });
  54. if (!response.ok) throw new Error('更新岗位失败');
  55. return await response.json();
  56. } catch (error) {
  57. console.error('更新岗位错误:', error);
  58. throw error;
  59. }
  60. },
  61. // 删除岗位
  62. deleteJob: async (id) => {
  63. try {
  64. const response = await fetch(`${API_BASE_URL}/jobs/${id}`, {
  65. method: 'DELETE'
  66. });
  67. if (!response.ok) throw new Error('删除岗位失败');
  68. return await response.json();
  69. } catch (error) {
  70. console.error('删除岗位错误:', error);
  71. throw error;
  72. }
  73. },
  74. // 启动智能筛选
  75. startScreening: async (id) => {
  76. try {
  77. const response = await fetch(`${API_BASE_URL}/jobs/${id}/screen`, {
  78. method: 'POST'
  79. });
  80. if (!response.ok) throw new Error('启动筛选失败');
  81. return await response.json();
  82. } catch (error) {
  83. console.error('启动筛选错误:', error);
  84. throw error;
  85. }
  86. },
  87. // 获取岗位候选人列表
  88. getJobCandidates: async (id) => {
  89. try {
  90. const response = await fetch(`${API_BASE_URL}/jobs/${id}/candidates`);
  91. if (!response.ok) throw new Error('获取候选人列表失败');
  92. return await response.json();
  93. } catch (error) {
  94. console.error('获取候选人列表错误:', error);
  95. return { candidates: [] };
  96. }
  97. },
  98. // 发布草稿岗位
  99. publishDraft: async (id) => {
  100. try {
  101. const response = await fetch(`${API_BASE_URL}/jobs/${id}/publish`, {
  102. method: 'POST',
  103. headers: {
  104. 'Content-Type': 'application/json'
  105. },
  106. body: JSON.stringify({})
  107. });
  108. if (!response.ok) throw new Error('发布岗位失败');
  109. return await response.json();
  110. } catch (error) {
  111. console.error('发布岗位错误:', error);
  112. throw error;
  113. }
  114. }
  115. };
  116. // 候选人相关API
  117. const CandidateAPI = {
  118. // 获取所有候选人
  119. getAllCandidates: async () => {
  120. try {
  121. const response = await fetch(`${API_BASE_URL}/candidates`);
  122. if (!response.ok) throw new Error('获取候选人列表失败');
  123. return await response.json();
  124. } catch (error) {
  125. console.error('获取候选人列表错误:', error);
  126. return [];
  127. }
  128. },
  129. // 获取单个候选人详情
  130. getCandidateDetails: async (id) => {
  131. try {
  132. const response = await fetch(`${API_BASE_URL}/candidates/${id}`);
  133. if (!response.ok) throw new Error('获取候选人详情失败');
  134. return await response.json();
  135. } catch (error) {
  136. console.error('获取候选人详情错误:', error);
  137. return null;
  138. }
  139. },
  140. // 更新候选人状态
  141. updateCandidateStatus: async (id, status) => {
  142. try {
  143. const response = await fetch(`${API_BASE_URL}/candidates/${id}/status`, {
  144. method: 'PUT',
  145. headers: {
  146. 'Content-Type': 'application/json'
  147. },
  148. body: JSON.stringify({ status })
  149. });
  150. if (!response.ok) throw new Error('更新候选人状态失败');
  151. return await response.json();
  152. } catch (error) {
  153. console.error('更新候选人状态错误:', error);
  154. throw error;
  155. }
  156. },
  157. // 添加候选人
  158. addCandidate: async (candidateData) => {
  159. try {
  160. const response = await fetch(`${API_BASE_URL}/candidates`, {
  161. method: 'POST',
  162. headers: {
  163. 'Content-Type': 'application/json'
  164. },
  165. body: JSON.stringify(candidateData)
  166. });
  167. if (!response.ok) throw new Error('添加候选人失败');
  168. return await response.json();
  169. } catch (error) {
  170. console.error('添加候选人错误:', error);
  171. throw error;
  172. }
  173. }
  174. };
  175. // 统计数据相关API
  176. const StatisticsAPI = {
  177. // 获取招聘总览统计
  178. getOverviewStats: async () => {
  179. try {
  180. const response = await fetch(`${API_BASE_URL}/statistics/overview`);
  181. if (!response.ok) throw new Error('获取招聘总览统计失败');
  182. return await response.json();
  183. } catch (error) {
  184. console.error('获取招聘总览统计错误:', error);
  185. return {
  186. totalJobs: 0,
  187. totalCandidates: 0,
  188. pendingResumes: 0,
  189. passedResumes: 0
  190. };
  191. }
  192. },
  193. // 获取部门招聘统计
  194. getDepartmentStats: async () => {
  195. try {
  196. const response = await fetch(`${API_BASE_URL}/statistics/departments`);
  197. if (!response.ok) throw new Error('获取部门招聘统计失败');
  198. return await response.json();
  199. } catch (error) {
  200. console.error('获取部门招聘统计错误:', error);
  201. return [];
  202. }
  203. },
  204. // 获取筛选效率统计
  205. getScreeningEfficiencyStats: async () => {
  206. try {
  207. const response = await fetch(`${API_BASE_URL}/statistics/screening-efficiency`);
  208. if (!response.ok) throw new Error('获取筛选效率统计失败');
  209. return await response.json();
  210. } catch (error) {
  211. console.error('获取筛选效率统计错误:', error);
  212. return [];
  213. }
  214. },
  215. // 获取候选人评分分布
  216. getScoreDistribution: async () => {
  217. try {
  218. const response = await fetch(`${API_BASE_URL}/statistics/score-distribution`);
  219. if (!response.ok) throw new Error('获取候选人评分分布失败');
  220. return await response.json();
  221. } catch (error) {
  222. console.error('获取候选人评分分布错误:', error);
  223. return [];
  224. }
  225. }
  226. };
  227. // 导出API
  228. const API = {
  229. Job: JobAPI,
  230. Candidate: CandidateAPI,
  231. Statistics: StatisticsAPI
  232. };
  233. // 全局挂载API
  234. window.API = API;