| | |
| | | // 员工管理API |
| | | |
| | | import { API_CONFIG, graphqlRequest } from '@/config/api' |
| | | |
| | | // 员工相关的 GraphQL 查询和变更 |
| | | export const EMPLOYEE_QUERIES = { |
| | | // 获取所有员工 |
| | |
| | | |
| | | // API 函数 |
| | | export const employeeApi = { |
| | | // 获取所有员工 |
| | | // 获取员工列表 |
| | | async getEmployees(): Promise<Employee[]> { |
| | | try { |
| | | const response = await fetch('http://localhost:8080/api/graphql', { |
| | | method: 'POST', |
| | | headers: { |
| | | 'Content-Type': 'application/json', |
| | | }, |
| | | body: JSON.stringify({ |
| | | query: EMPLOYEE_QUERIES.GET_EMPLOYEES |
| | | }) |
| | | }) |
| | | const result = await response.json() |
| | | if (result.errors) { |
| | | throw new Error(result.errors[0].message) |
| | | } |
| | | return result.data?.employees || [] |
| | | const data = await graphqlRequest(EMPLOYEE_QUERIES.GET_EMPLOYEES) |
| | | return data?.employees || [] |
| | | } catch (error: any) { |
| | | throw new Error(error.message || '获取员工列表失败') |
| | | } |
| | | }, |
| | | |
| | | // 根据名称搜索员工 |
| | | async searchEmployees(name?: string): Promise<Employee[]> { |
| | | // 搜索员工 |
| | | async searchEmployees(keyword: string): Promise<Employee[]> { |
| | | try { |
| | | const response = await fetch('http://localhost:8080/api/graphql', { |
| | | method: 'POST', |
| | | headers: { |
| | | 'Content-Type': 'application/json', |
| | | }, |
| | | body: JSON.stringify({ |
| | | query: EMPLOYEE_QUERIES.SEARCH_EMPLOYEES, |
| | | variables: { name } |
| | | }) |
| | | }) |
| | | const result = await response.json() |
| | | if (result.errors) { |
| | | throw new Error(result.errors[0].message) |
| | | } |
| | | return result.data?.employeesByName || [] |
| | | const data = await graphqlRequest(EMPLOYEE_QUERIES.SEARCH_EMPLOYEES, { keyword }) |
| | | return data?.searchEmployees || [] |
| | | } catch (error: any) { |
| | | throw new Error(error.message || '搜索员工失败') |
| | | } |
| | | }, |
| | | |
| | | // 获取员工详情 |
| | | // 根据ID获取员工详情 |
| | | async getEmployee(id: string): Promise<Employee | null> { |
| | | try { |
| | | const response = await fetch('http://localhost:8080/api/graphql', { |
| | | method: 'POST', |
| | | headers: { |
| | | 'Content-Type': 'application/json', |
| | | }, |
| | | body: JSON.stringify({ |
| | | query: EMPLOYEE_QUERIES.GET_EMPLOYEE, |
| | | variables: { id } |
| | | }) |
| | | }) |
| | | const result = await response.json() |
| | | if (result.errors) { |
| | | throw new Error(result.errors[0].message) |
| | | } |
| | | return result.data?.employee || null |
| | | const data = await graphqlRequest(EMPLOYEE_QUERIES.GET_EMPLOYEE, { id }) |
| | | return data?.employee || null |
| | | } catch (error: any) { |
| | | throw new Error(error.message || '获取员工详情失败') |
| | | } |
| | | }, |
| | | |
| | | // 保存员工 |
| | | async saveEmployee(input: EmployeeInput): Promise<Employee> { |
| | | // 保存员工(新增或更新) |
| | | async saveEmployee(employee: EmployeeInput): Promise<Employee> { |
| | | try { |
| | | const response = await fetch('http://localhost:8080/api/graphql', { |
| | | method: 'POST', |
| | | headers: { |
| | | 'Content-Type': 'application/json', |
| | | }, |
| | | body: JSON.stringify({ |
| | | query: EMPLOYEE_MUTATIONS.SAVE_EMPLOYEE, |
| | | variables: { input } |
| | | }) |
| | | }) |
| | | const result = await response.json() |
| | | if (result.errors) { |
| | | throw new Error(result.errors[0].message) |
| | | } |
| | | return result.data?.saveEmployee |
| | | const data = await graphqlRequest(EMPLOYEE_MUTATIONS.SAVE_EMPLOYEE, { input: employee }) |
| | | return data?.saveEmployee |
| | | } catch (error: any) { |
| | | throw new Error(error.message || '保存员工失败') |
| | | } |
| | |
| | | // 删除员工 |
| | | async deleteEmployee(id: string): Promise<boolean> { |
| | | try { |
| | | const response = await fetch('http://localhost:8080/api/graphql', { |
| | | method: 'POST', |
| | | headers: { |
| | | 'Content-Type': 'application/json', |
| | | }, |
| | | body: JSON.stringify({ |
| | | query: EMPLOYEE_MUTATIONS.DELETE_EMPLOYEE, |
| | | variables: { id } |
| | | }) |
| | | }) |
| | | const result = await response.json() |
| | | if (result.errors) { |
| | | throw new Error(result.errors[0].message) |
| | | } |
| | | return result.data?.deleteEmployee || false |
| | | const data = await graphqlRequest(EMPLOYEE_MUTATIONS.DELETE_EMPLOYEE, { id }) |
| | | return data?.deleteEmployee || false |
| | | } catch (error: any) { |
| | | throw new Error(error.message || '删除员工失败') |
| | | } |