|
|
@@ -1,14 +1,13 @@
|
|
|
-import React, { useState, useEffect } from 'react'
|
|
|
+import React, { useEffect } from 'react'
|
|
|
import { View, Picker, Text } from '@tarojs/components'
|
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
-import { cityClient } from '@/api'
|
|
|
-import { InferResponseType } from 'hono'
|
|
|
+import { areaClient } from '@/api'
|
|
|
|
|
|
-interface City {
|
|
|
+interface Area {
|
|
|
id: number
|
|
|
name: string
|
|
|
level: number
|
|
|
- parentId: number
|
|
|
+ parentId: number | null
|
|
|
}
|
|
|
|
|
|
interface CitySelectorProps {
|
|
|
@@ -36,81 +35,75 @@ export const CitySelector: React.FC<CitySelectorProps> = ({
|
|
|
disabled = false,
|
|
|
showLabels = true,
|
|
|
}) => {
|
|
|
- // 获取省份数据 (level=1)
|
|
|
+ // 获取省份数据
|
|
|
const { data: provinces } = useQuery({
|
|
|
- queryKey: ['cities', 'provinces'],
|
|
|
+ queryKey: ['areas', 'provinces'],
|
|
|
queryFn: async () => {
|
|
|
- const res = await cityClient.$get({
|
|
|
+ const res = await areaClient.provinces.$get({
|
|
|
query: {
|
|
|
page: 1,
|
|
|
- pageSize: 100,
|
|
|
- filters: JSON.stringify({ level: 1 }),
|
|
|
- sortOrder: 'ASC',
|
|
|
- sortBy: 'id'
|
|
|
+ pageSize: 100
|
|
|
},
|
|
|
})
|
|
|
if (res.status !== 200) throw new Error('获取省份数据失败')
|
|
|
const data = await res.json()
|
|
|
- return data.data as City[]
|
|
|
+ return data.data.provinces as Area[]
|
|
|
},
|
|
|
})
|
|
|
|
|
|
- // 获取城市数据 (level=2)
|
|
|
+ // 获取城市数据
|
|
|
const { data: cities } = useQuery({
|
|
|
- queryKey: ['cities', 'cities', provinceValue],
|
|
|
+ queryKey: ['areas', 'cities', provinceValue],
|
|
|
queryFn: async () => {
|
|
|
if (!provinceValue) return []
|
|
|
- const res = await cityClient.$get({
|
|
|
+ const res = await areaClient.cities.$get({
|
|
|
query: {
|
|
|
+ provinceId: provinceValue,
|
|
|
page: 1,
|
|
|
- pageSize: 100,
|
|
|
- filters: JSON.stringify({ level: 2, parentId: provinceValue }),
|
|
|
- sortOrder: 'ASC',
|
|
|
+ pageSize: 100
|
|
|
},
|
|
|
})
|
|
|
if (res.status !== 200) throw new Error('获取城市数据失败')
|
|
|
const data = await res.json()
|
|
|
- return data.data as City[]
|
|
|
+ return data.data.cities as Area[]
|
|
|
},
|
|
|
enabled: !!provinceValue,
|
|
|
})
|
|
|
|
|
|
- // 获取区县数据 (level=3)
|
|
|
+ // 获取区县数据
|
|
|
const { data: districts } = useQuery({
|
|
|
- queryKey: ['cities', 'districts', cityValue],
|
|
|
+ queryKey: ['areas', 'districts', cityValue],
|
|
|
queryFn: async () => {
|
|
|
if (!cityValue) return []
|
|
|
- const res = await cityClient.$get({
|
|
|
+ const res = await areaClient.districts.$get({
|
|
|
query: {
|
|
|
+ cityId: cityValue,
|
|
|
page: 1,
|
|
|
- pageSize: 100,
|
|
|
- filters: JSON.stringify({ level: 3, parentId: cityValue }),
|
|
|
- sortOrder: 'ASC',
|
|
|
+ pageSize: 100
|
|
|
},
|
|
|
})
|
|
|
if (res.status !== 200) throw new Error('获取区县数据失败')
|
|
|
const data = await res.json()
|
|
|
- return data.data as City[]
|
|
|
+ return data.data.districts as Area[]
|
|
|
},
|
|
|
enabled: !!cityValue,
|
|
|
})
|
|
|
|
|
|
- // 获取街道数据 (level=4)
|
|
|
+ // 获取街道数据
|
|
|
const { data: towns } = useQuery({
|
|
|
- queryKey: ['cities', 'towns', districtValue],
|
|
|
+ queryKey: ['areas', 'towns', districtValue],
|
|
|
queryFn: async () => {
|
|
|
if (!districtValue) return []
|
|
|
- const res = await cityClient.$get({
|
|
|
+ const res = await areaClient.towns.$get({
|
|
|
query: {
|
|
|
+ districtId: districtValue,
|
|
|
page: 1,
|
|
|
- pageSize: 100,
|
|
|
- filters: JSON.stringify({ level: 4, parentId: districtValue }),
|
|
|
- sortOrder: 'ASC',
|
|
|
+ pageSize: 100
|
|
|
},
|
|
|
})
|
|
|
if (res.status !== 200) throw new Error('获取街道数据失败')
|
|
|
const data = await res.json()
|
|
|
- return data.data as City[]
|
|
|
+ return data.data.towns as Area[]
|
|
|
},
|
|
|
enabled: !!districtValue,
|
|
|
})
|
|
|
@@ -205,7 +198,7 @@ export const CitySelector: React.FC<CitySelectorProps> = ({
|
|
|
{showLabels && (
|
|
|
<View className="text-sm font-medium text-gray-700">所在地区</View>
|
|
|
)}
|
|
|
-
|
|
|
+
|
|
|
<View className="space-y-3">
|
|
|
{/* 省份选择器 */}
|
|
|
<View>
|