2
0
Эх сурвалжийг харах

♻️ refactor(membership): 重构会员套餐API调用方式

- 将会员套餐相关API调用从通用client迁移到专用membershipPlanClient
- 更新类型定义以匹配新的API客户端结构
- 调整API请求参数传递方式,使用param属性传递路径参数
- 优化代码组织结构,提高API调用的可读性和可维护性
yourname 3 сар өмнө
parent
commit
23d2cf755c

+ 13 - 8
src/client/admin/pages/MembershipPlans.tsx

@@ -41,13 +41,13 @@ import { z } from 'zod';
 import { useForm } from 'react-hook-form';
 import { zodResolver } from '@hookform/resolvers/zod';
 import { Plus, Edit, Trash2 } from 'lucide-react';
-import { client } from '@/client/api';
+import { membershipPlanClient } from '@/client/api';
 import type { InferResponseType, InferRequestType } from 'hono/client';
 import { toast } from 'sonner';
 
-type MembershipPlan = InferResponseType<typeof client.api.v1['membership-plans'].$get, 200>['data'][0];
-type CreateMembershipPlanRequest = InferRequestType<typeof client.api.v1['membership-plans'].$post>['json'];
-type UpdateMembershipPlanRequest = InferRequestType<typeof client.api.v1['membership-plans'][number]['$put']>['json'];
+type MembershipPlan = InferResponseType<typeof membershipPlanClient.$get, 200>['data'][0];
+type CreateMembershipPlanRequest = InferRequestType<typeof membershipPlanClient.$post>['json'];
+type UpdateMembershipPlanRequest = InferRequestType<typeof membershipPlanClient[':id']['$put']>['json'];
 
 const formSchema = z.object({
   name: z.string().min(1, '套餐名称不能为空'),
@@ -68,7 +68,7 @@ export default function MembershipPlans() {
   const { data: plans, isLoading } = useQuery({
     queryKey: ['membership-plans'],
     queryFn: async () => {
-      const response = await client.api.v1['membership-plans'].$get();
+      const response = await membershipPlanClient.$get();
       if (!response.ok) throw new Error('获取套餐失败');
       const data = await response.json();
       return data.data;
@@ -77,7 +77,7 @@ export default function MembershipPlans() {
 
   const createMutation = useMutation({
     mutationFn: async (data: CreateMembershipPlanRequest) => {
-      const response = await client.api.v1['membership-plans'].$post({ json: data });
+      const response = await membershipPlanClient.$post({ json: data });
       if (!response.ok) throw new Error('创建套餐失败');
       return response.json();
     },
@@ -93,7 +93,10 @@ export default function MembershipPlans() {
 
   const updateMutation = useMutation({
     mutationFn: async ({ id, data }: { id: number; data: UpdateMembershipPlanRequest }) => {
-      const response = await client.api.v1['membership-plans'][id].$put({ json: data });
+      const response = await membershipPlanClient[':id'].$put({ 
+        param: { id },
+        json: data
+       });
       if (!response.ok) throw new Error('更新套餐失败');
       return response.json();
     },
@@ -109,7 +112,9 @@ export default function MembershipPlans() {
 
   const deleteMutation = useMutation({
     mutationFn: async (id: number) => {
-      const response = await client.api.v1['membership-plans'][id].$delete();
+      const response = await membershipPlanClient[':id'].$delete({
+        param: { id }
+      });
       if (!response.ok) throw new Error('删除套餐失败');
       return response.json();
     },