| | |
| | | const GRAPHQL_ENDPOINT = 'http://localhost:8080/api/graphql' |
| | | import { API_CONFIG, graphqlRequest } from '@/config/api' |
| | | |
| | | // GraphQL请求函数 |
| | | async function graphqlRequest(query, variables = {}) { |
| | | const response = await fetch(GRAPHQL_ENDPOINT, { |
| | | method: 'POST', |
| | | headers: { |
| | | 'Content-Type': 'application/json', |
| | | }, |
| | | body: JSON.stringify({ |
| | | query, |
| | | variables, |
| | | }), |
| | | }) |
| | | // 使用统一的GraphQL请求函数 |
| | | |
| | | if (!response.ok) { |
| | | throw new Error(`HTTP error! status: ${response.status}`) |
| | | } |
| | | |
| | | const result = await response.json() |
| | | |
| | | if (result.errors) { |
| | | throw new Error(result.errors[0].message) |
| | | } |
| | | |
| | | return result.data |
| | | } |
| | | |
| | | // 分页查询评分模板列表 |
| | | export const getRatingSchemes = async (page = 0, size = 10, name = '') => { |
| | | const query = ` |
| | | query GetRatingSchemes($page: Int, $size: Int, $name: String) { |
| | | // GraphQL查询语句 |
| | | const GET_RATING_SCHEMES_QUERY = ` |
| | | query GetRatingSchemes($page: Int!, $size: Int!, $name: String) { |
| | | ratingSchemes(page: $page, size: $size, name: $name) { |
| | | content { |
| | | id |
| | | name |
| | | description |
| | | totalScore |
| | | state |
| | | stateName |
| | | createTime |
| | | updateTime |
| | | } |
| | | totalElements |
| | | totalPages |
| | | number |
| | | page |
| | | size |
| | | first |
| | | last |
| | | } |
| | | } |
| | | ` |
| | | |
| | | const variables = { page, size } |
| | | if (name) { |
| | | variables.name = name |
| | | } |
| | | |
| | | const data = await graphqlRequest(query, variables) |
| | | return data.ratingSchemes |
| | | } |
| | | |
| | | // 根据ID获取评分模板详情 |
| | | export const getRatingScheme = async (id) => { |
| | | const query = ` |
| | | query GetRatingScheme($id: ID!) { |
| | | ratingScheme(id: $id) { |
| | | id |
| | | name |
| | | description |
| | | totalScore |
| | | items { |
| | | id |
| | | name |
| | | maxScore |
| | | orderNo |
| | | } |
| | | createTime |
| | | updateTime |
| | | } |
| | | } |
| | | ` |
| | | |
| | | const data = await graphqlRequest(query, { id }) |
| | | return data.ratingScheme |
| | | } |
| | | |
| | | // 获取所有评分模板(用于下拉选择) |
| | | export const getAllRatingSchemes = async () => { |
| | | const query = ` |
| | | const GET_ALL_RATING_SCHEMES_QUERY = ` |
| | | query GetAllRatingSchemes { |
| | | allRatingSchemes { |
| | | id |
| | | name |
| | | description |
| | | totalScore |
| | | state |
| | | stateName |
| | | } |
| | | } |
| | | ` |
| | | |
| | | const data = await graphqlRequest(query) |
| | | return data.allRatingSchemes |
| | | } |
| | | |
| | | // 保存评分模板 |
| | | export const saveRatingScheme = async (input) => { |
| | | const mutation = ` |
| | | mutation SaveRatingScheme($input: RatingSchemeInput!) { |
| | | saveRatingScheme(input: $input) { |
| | | const GET_RATING_SCHEME_QUERY = ` |
| | | query GetRatingScheme($id: ID!) { |
| | | ratingScheme(id: $id) { |
| | | id |
| | | name |
| | | description |
| | | totalScore |
| | | state |
| | | stateName |
| | | createTime |
| | | updateTime |
| | | items { |
| | | id |
| | | name |
| | | maxScore |
| | | orderNo |
| | | } |
| | | } |
| | | } |
| | | ` |
| | | |
| | | const SAVE_RATING_SCHEME_MUTATION = ` |
| | | mutation SaveRatingScheme($input: RatingSchemeInput!) { |
| | | saveRatingScheme(input: $input) { |
| | | id |
| | | name |
| | | description |
| | | totalScore |
| | | state |
| | | stateName |
| | | createTime |
| | | updateTime |
| | | } |
| | | } |
| | | ` |
| | | |
| | | const data = await graphqlRequest(mutation, { input }) |
| | | return data.saveRatingScheme |
| | | } |
| | | |
| | | // 删除评分模板 |
| | | export const deleteRatingScheme = async (id) => { |
| | | const mutation = ` |
| | | const DELETE_RATING_SCHEME_MUTATION = ` |
| | | mutation DeleteRatingScheme($id: ID!) { |
| | | deleteRatingScheme(id: $id) |
| | | } |
| | | ` |
| | | |
| | | const data = await graphqlRequest(mutation, { id }) |
| | | // API函数 |
| | | export const getRatingSchemes = async (page = 0, size = 10, name = '') => { |
| | | try { |
| | | const data = await graphqlRequest(GET_RATING_SCHEMES_QUERY, { page, size, name }) |
| | | return data.ratingSchemes |
| | | } catch (error) { |
| | | throw new Error(error.message || '获取评分方案列表失败') |
| | | } |
| | | } |
| | | |
| | | export const getAllRatingSchemes = async () => { |
| | | try { |
| | | const data = await graphqlRequest(GET_ALL_RATING_SCHEMES_QUERY) |
| | | return data.allRatingSchemes || [] |
| | | } catch (error) { |
| | | throw new Error(error.message || '获取所有评分方案失败') |
| | | } |
| | | } |
| | | |
| | | export const getRatingScheme = async (id) => { |
| | | try { |
| | | const data = await graphqlRequest(GET_RATING_SCHEME_QUERY, { id }) |
| | | return data.ratingScheme |
| | | } catch (error) { |
| | | throw new Error(error.message || '获取评分方案详情失败') |
| | | } |
| | | } |
| | | |
| | | export const saveRatingScheme = async (ratingSchemeData) => { |
| | | try { |
| | | const data = await graphqlRequest(SAVE_RATING_SCHEME_MUTATION, { input: ratingSchemeData }) |
| | | return data.saveRatingScheme |
| | | } catch (error) { |
| | | throw new Error(error.message || '保存评分方案失败') |
| | | } |
| | | } |
| | | |
| | | export const deleteRatingScheme = async (id) => { |
| | | try { |
| | | const data = await graphqlRequest(DELETE_RATING_SCHEME_MUTATION, { id }) |
| | | return data.deleteRatingScheme |
| | | } catch (error) { |
| | | throw new Error(error.message || '删除评分方案失败') |
| | | } |
| | | } |