lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const GRAPHQL_ENDPOINT = '/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) {
    const errorText = await response.text();
    throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`)
  }
 
  const result = await response.json()
  
  if (result.errors) {
    throw new Error(result.errors.map(e => e.message).join('\n'))
  }
 
  return result.data
}
 
// --- GraphQL Query Strings ---
 
const GET_REGIONS = `
  query GetRegions($name: String, $state: Int, $page: Int!, $size: Int!) {
    regions(name: $name, state: $state, page: $page, size: $size) {
      content { id name pid code level leafFlag fullPath state createTime createUserId updateTime updateUserId version }
      totalElements totalPages currentPage pageSize
    }
  }
`
 
const GET_ALL_REGIONS = `
  query GetAllRegions {
    allRegions { id name pid code level leafFlag fullPath state createTime }
  }
`
 
const GET_REGION = `
  query GetRegion($id: ID!) {
    region(id: $id) { id name pid code level leafFlag fullPath state createTime createUserId updateTime updateUserId version }
  }
`
 
const GET_PROVINCES = `
  query GetProvinces {
    provinces { id name pid code level state }
  }
`
 
const GET_CITIES = `
  query GetCities($provinceId: ID!) {
    cities(provinceId: $provinceId) { id name pid code level state }
  }
`
 
const GET_DISTRICTS = `
  query GetDistricts($cityId: ID!) {
    districts(cityId: $cityId) { id name pid code level state }
  }
`
 
const GET_REGION_CHILDREN = `
  query GetRegionChildren($parentId: ID!) {
    regionChildren(parentId: $parentId) { id name pid code level leafFlag state }
  }
`
 
const SAVE_REGION = `
  mutation SaveRegion($input: RegionInput!) {
    saveRegion(input: $input) { id name pid code level leafFlag fullPath state createTime updateTime version }
  }
`
 
const DELETE_REGION = `
  mutation DeleteRegion($id: ID!) {
    deleteRegion(id: $id)
  }
`
 
const TOGGLE_REGION_STATE = `
  mutation ToggleRegionState($id: ID!) {
    toggleRegionState(id: $id) { id name state updateTime }
  }
`
 
// --- API Functions ---
 
export const RegionApi = {
  getRegions: async (name, state, page, size) => {
    const data = await graphqlRequest(GET_REGIONS, { name, state, page, size });
    return data.regions;
  },
  getAllRegions: async () => {
    const data = await graphqlRequest(GET_ALL_REGIONS);
    return data.allRegions;
  },
  getRegion: async (id) => {
    const data = await graphqlRequest(GET_REGION, { id });
    return data.region;
  },
  getProvinces: async () => {
    const data = await graphqlRequest(GET_PROVINCES);
    return data.provinces || [];
  },
  getCities: async (provinceId) => {
    const data = await graphqlRequest(GET_CITIES, { provinceId });
    return data.cities || [];
  },
  getDistricts: async (cityId) => {
    const data = await graphqlRequest(GET_DISTRICTS, { cityId });
    return data.districts || [];
  },
  getChildren: async (parentId) => {
    const data = await graphqlRequest(GET_REGION_CHILDREN, { parentId });
    return data.regionChildren;
  },
  saveRegion: async (input) => {
    const data = await graphqlRequest(SAVE_REGION, { input });
    return data.saveRegion;
  },
  deleteRegion: async (id) => {
    const data = await graphqlRequest(DELETE_REGION, { id });
    return data.deleteRegion;
  },
  toggleRegionState: async (id) => {
    const data = await graphqlRequest(TOGGLE_REGION_STATE, { id });
    return data.toggleRegionState;
  }
}