import { API_CONFIG, graphqlRequest } from '@/config/api'
|
|
// GraphQL请求函数 - 使用统一的graphqlRequest
|
|
// 获取所有地区
|
export const getRegions = async () => {
|
const query = `
|
query GetRegions {
|
regions {
|
id
|
name
|
description
|
createTime
|
updateTime
|
}
|
}
|
`
|
|
try {
|
const data = await graphqlRequest(query)
|
return data.regions || []
|
} catch (error) {
|
throw new Error(error.message || '获取地区列表失败')
|
}
|
}
|
|
// 根据ID获取地区
|
export const getRegion = async (id) => {
|
const query = `
|
query GetRegion($id: ID!) {
|
region(id: $id) {
|
id
|
name
|
description
|
createTime
|
updateTime
|
}
|
}
|
`
|
|
try {
|
const data = await graphqlRequest(query, { id })
|
return data.region
|
} catch (error) {
|
throw new Error(error.message || '获取地区详情失败')
|
}
|
}
|
|
// 保存地区(新增或更新)
|
export const saveRegion = async (regionData) => {
|
const mutation = `
|
mutation SaveRegion($input: RegionInput!) {
|
saveRegion(input: $input) {
|
id
|
name
|
description
|
createTime
|
updateTime
|
}
|
}
|
`
|
|
try {
|
const data = await graphqlRequest(mutation, { input: regionData })
|
return data.saveRegion
|
} catch (error) {
|
throw new Error(error.message || '保存地区失败')
|
}
|
}
|
|
// 删除地区
|
export const deleteRegion = async (id) => {
|
const mutation = `
|
mutation DeleteRegion($id: ID!) {
|
deleteRegion(id: $id)
|
}
|
`
|
|
try {
|
const data = await graphqlRequest(mutation, { id })
|
return data.deleteRegion
|
} catch (error) {
|
throw new Error(error.message || '删除地区失败')
|
}
|
}
|
|
// 获取所有省份
|
export const getProvinces = async () => {
|
const query = `
|
query GetProvinces {
|
provinces {
|
id
|
name
|
pid
|
level
|
}
|
}
|
`
|
|
try {
|
const data = await graphqlRequest(query)
|
return data.provinces || []
|
} catch (error) {
|
throw new Error(error.message || '获取省份列表失败')
|
}
|
}
|
|
// 根据省份ID获取城市
|
export const getCities = async (provinceId) => {
|
const query = `
|
query GetCities($provinceId: ID!) {
|
cities(provinceId: $provinceId) {
|
id
|
name
|
pid
|
level
|
}
|
}
|
`
|
|
try {
|
const data = await graphqlRequest(query, { provinceId })
|
return data.cities || []
|
} catch (error) {
|
throw new Error(error.message || '获取城市列表失败')
|
}
|
}
|
|
// 根据城市ID获取区县
|
export const getDistricts = async (cityId) => {
|
const query = `
|
query GetDistricts($cityId: ID!) {
|
districts(cityId: $cityId) {
|
id
|
name
|
pid
|
level
|
}
|
}
|
`
|
|
try {
|
const data = await graphqlRequest(query, { cityId })
|
return data.districts || []
|
} catch (error) {
|
throw new Error(error.message || '获取区县列表失败')
|
}
|
}
|
|
// RegionApi 对象,包含所有区域相关的API方法
|
export const RegionApi = {
|
getRegions,
|
getRegion,
|
saveRegion,
|
deleteRegion,
|
getProvinces,
|
getCities,
|
getDistricts
|
}
|