|
|
@@ -6,71 +6,55 @@ import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@
|
|
|
import { DataTablePagination } from '../components/DataTablePagination';
|
|
|
import { Plus, Edit, Trash2, MapPin, DollarSign, Users } from 'lucide-react';
|
|
|
import { useState } from 'react';
|
|
|
+import { routeClient } from '@/client/api';
|
|
|
+import type { InferResponseType } from 'hono/client';
|
|
|
|
|
|
-interface Route {
|
|
|
- id: number;
|
|
|
- name: string;
|
|
|
- description?: string;
|
|
|
- startPoint: string;
|
|
|
- endPoint: string;
|
|
|
- pickupPoint: string;
|
|
|
- dropoffPoint: string;
|
|
|
- departureTime: string;
|
|
|
- vehicleType: string;
|
|
|
- price: number;
|
|
|
- seatCount: number;
|
|
|
- availableSeats: number;
|
|
|
- activityId: number;
|
|
|
- isDisabled: number;
|
|
|
- isDeleted: number;
|
|
|
- createdAt: string;
|
|
|
- updatedAt: string;
|
|
|
-}
|
|
|
+// 类型提取规范
|
|
|
+type RouteResponse = InferResponseType<typeof routeClient.$get, 200>['data'][0];
|
|
|
|
|
|
-interface RoutesResponse {
|
|
|
- data: Route[];
|
|
|
- pagination: {
|
|
|
- total: number;
|
|
|
- current: number;
|
|
|
- pageSize: number;
|
|
|
- };
|
|
|
-}
|
|
|
+// 统一操作处理函数
|
|
|
+const handleOperation = async (operation: () => Promise<any>) => {
|
|
|
+ try {
|
|
|
+ await operation();
|
|
|
+ // toast.success('操作成功');
|
|
|
+ console.log('操作成功');
|
|
|
+ } catch (error) {
|
|
|
+ console.error('操作失败:', error);
|
|
|
+ // toast.error('操作失败,请重试');
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+};
|
|
|
|
|
|
export const RoutesPage: React.FC = () => {
|
|
|
const queryClient = useQueryClient();
|
|
|
const [page, setPage] = useState(1);
|
|
|
const [pageSize, setPageSize] = useState(20);
|
|
|
|
|
|
- // 获取路线列表
|
|
|
- const { data, isLoading, error } = useQuery<RoutesResponse>({
|
|
|
+ // 获取路线列表 - 使用RPC客户端
|
|
|
+ const { data, isLoading, error } = useQuery({
|
|
|
queryKey: ['routes', page, pageSize],
|
|
|
queryFn: async () => {
|
|
|
- const response = await fetch(`/api/v1/admin/routes?page=${page}&pageSize=${pageSize}`, {
|
|
|
- headers: {
|
|
|
- 'Authorization': `Bearer ${localStorage.getItem('token')}`,
|
|
|
- 'Content-Type': 'application/json'
|
|
|
+ const res = await routeClient.$get({
|
|
|
+ query: {
|
|
|
+ page,
|
|
|
+ pageSize
|
|
|
}
|
|
|
});
|
|
|
- if (!response.ok) {
|
|
|
- throw new Error('获取路线列表失败');
|
|
|
- }
|
|
|
- return response.json();
|
|
|
+ if (res.status !== 200) throw new Error('获取路线列表失败');
|
|
|
+ return await res.json();
|
|
|
},
|
|
|
+ staleTime: 5 * 60 * 1000, // 5分钟缓存
|
|
|
});
|
|
|
|
|
|
- // 删除路线
|
|
|
+ // 删除路线 - 使用RPC客户端
|
|
|
const deleteMutation = useMutation({
|
|
|
mutationFn: async (id: number) => {
|
|
|
- const response = await fetch(`/api/v1/admin/routes/${id}`, {
|
|
|
- method: 'DELETE',
|
|
|
- headers: {
|
|
|
- 'Authorization': `Bearer ${localStorage.getItem('token')}`,
|
|
|
- 'Content-Type': 'application/json'
|
|
|
- }
|
|
|
+ await handleOperation(async () => {
|
|
|
+ const res = await routeClient[':id'].$delete({
|
|
|
+ param: { id }
|
|
|
+ });
|
|
|
+ if (res.status !== 204) throw new Error('删除路线失败');
|
|
|
});
|
|
|
- if (!response.ok) {
|
|
|
- throw new Error('删除路线失败');
|
|
|
- }
|
|
|
},
|
|
|
onSuccess: () => {
|
|
|
queryClient.invalidateQueries({ queryKey: ['routes'] });
|
|
|
@@ -138,7 +122,7 @@ export const RoutesPage: React.FC = () => {
|
|
|
</TableCell>
|
|
|
</TableRow>
|
|
|
) : data?.data && data.data.length > 0 ? (
|
|
|
- data.data.map((route) => (
|
|
|
+ data.data.map((route: RouteResponse) => (
|
|
|
<TableRow key={route.id}>
|
|
|
<TableCell>
|
|
|
<div className="flex items-center gap-2">
|