const GRAPHQL_ENDPOINT = 'http://localhost:8080/api/graphql'
|
|
// GraphQL请求函数
|
async function graphqlRequest(query, variables = {}) {
|
const response = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query,
|
variables,
|
}),
|
})
|
|
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
|
}
|
|
// GraphQL 查询和变更
|
const GET_CAROUSELS = `
|
query GetCarousels($page: Int!, $size: Int!, $title: String) {
|
carousels(page: $page, size: $size, title: $title) {
|
content {
|
id
|
title
|
content
|
sortOrder
|
mediaCount
|
createTime
|
updateTime
|
}
|
totalElements
|
page
|
size
|
}
|
}
|
`
|
|
const GET_CAROUSEL = `
|
query GetCarousel($id: ID!) {
|
carousel(id: $id) {
|
id
|
title
|
content
|
sortOrder
|
mediaCount
|
createTime
|
updateTime
|
}
|
}
|
`
|
|
const GET_CAROUSEL_PLAY_LIST = `
|
query GetCarouselPlayList {
|
carouselPlayList {
|
id
|
title
|
content
|
sortOrder
|
mediaCount
|
createTime
|
updateTime
|
}
|
}
|
`
|
|
const SAVE_CAROUSEL = `
|
mutation SaveCarousel($carousel: CarouselInput!) {
|
saveCarousel(carousel: $carousel) {
|
id
|
title
|
content
|
sortOrder
|
createTime
|
updateTime
|
}
|
}
|
`
|
|
const DELETE_CAROUSEL = `
|
mutation DeleteCarousel($id: ID!) {
|
deleteCarousel(id: $id)
|
}
|
`
|
|
const UPDATE_CAROUSEL_SORT_ORDERS = `
|
mutation UpdateCarouselSortOrders($sortOrders: [CarouselSortOrderInput!]!) {
|
updateCarouselSortOrders(sortOrders: $sortOrders)
|
}
|
`
|
|
// API 函数
|
export const CarouselApi = {
|
// 分页查询轮播图
|
getCarousels: async (page = 0, size = 10, title) => {
|
const data = await graphqlRequest(GET_CAROUSELS, { page, size, title })
|
return data.carousels
|
},
|
|
// 根据ID查询轮播图
|
getCarousel: async (id) => {
|
const data = await graphqlRequest(GET_CAROUSEL, { id })
|
return data.carousel
|
},
|
|
// 获取播放列表
|
getPlayList: async () => {
|
const data = await graphqlRequest(GET_CAROUSEL_PLAY_LIST)
|
return data.carouselPlayList
|
},
|
|
// 保存轮播图
|
saveCarousel: async (carousel) => {
|
const data = await graphqlRequest(SAVE_CAROUSEL, { carousel })
|
return data.saveCarousel
|
},
|
|
// 删除轮播图
|
deleteCarousel: async (id) => {
|
const data = await graphqlRequest(DELETE_CAROUSEL, { id })
|
return data.deleteCarousel
|
},
|
|
// 批量更新排序
|
updateSortOrders: async (sortOrders) => {
|
const data = await graphqlRequest(UPDATE_CAROUSEL_SORT_ORDERS, { sortOrders })
|
return data.updateCarouselSortOrders
|
}
|
}
|
|
export default CarouselApi
|