From c690d50baaf2f9fd3d95a26d0e665153f145dfce Mon Sep 17 00:00:00 2001 From: peng Date: Mon, 3 Nov 2025 17:49:39 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E4=BA=A7=E7=AE=A1=E7=90=86=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=20-=20=E7=A6=81=E7=94=A8=E3=80=81=E5=90=AF=E7=94=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=81=94=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/enterpriseApi.ts | 80 ++++++++++++++++++- .../tenant/enterprise-management/page.tsx | 59 ++++++++++---- 2 files changed, 120 insertions(+), 19 deletions(-) diff --git a/crop-x/src/app/(app)/central-config/tenant/enterprise-management/components/enterpriseApi.ts b/crop-x/src/app/(app)/central-config/tenant/enterprise-management/components/enterpriseApi.ts index fe8ea73..7bbe519 100644 --- a/crop-x/src/app/(app)/central-config/tenant/enterprise-management/components/enterpriseApi.ts +++ b/crop-x/src/app/(app)/central-config/tenant/enterprise-management/components/enterpriseApi.ts @@ -7,7 +7,11 @@ // API响应数据类型定义 import { getAuthToken } from "@/utils/token.ts"; -import { listTenantsApiV1TenantsGet, getTenantAuditLogsApiV1TenantsAuditLogsGet } from "@/lib/api/sdk.gen"; +import { + listTenantsApiV1TenantsGet, + enableTenantApiV1TenantsTenantIdEnablePatch, + disableTenantApiV1TenantsTenantIdDisablePatch +} from "@/lib/api/sdk.gen"; export interface TenantData { id: string; tenant_code: string; @@ -116,7 +120,7 @@ export async function fetchTenants(params: TenantsQueryParams = {}): Promise { + try { + const token = getAuthToken(); + console.log('启用企业API调用:', tenantId); + + const response = await enableTenantApiV1TenantsTenantIdEnablePatch({ + path: { + tenant_id: tenantId, + }, + headers: token ? { + 'Authorization': `Bearer ${token}`, + } : undefined, + }); + + if (response.error) { + throw new Error(`启用企业失败: ${response.error.message || 'Unknown error'}`); + } + + const data = response.data as TenantData; + console.log('启用企业API响应:', data); + + // 验证返回的数据中is_active是否为true + if (data.is_active !== true) { + throw new Error('启用企业失败:返回数据状态不正确'); + } + + return data; + } catch (error) { + console.error('Failed to enable tenant:', error); + throw error; + } +} + +/** + * 禁用企业 + */ +export async function disableTenant(tenantId: string): Promise { + try { + const token = getAuthToken(); + console.log('禁用企业API调用:', tenantId); + + const response = await disableTenantApiV1TenantsTenantIdDisablePatch({ + path: { + tenant_id: tenantId, + }, + headers: token ? { + 'Authorization': `Bearer ${token}`, + } : undefined, + }); + + if (response.error) { + throw new Error(`禁用企业失败: ${response.error.message || 'Unknown error'}`); + } + + const data = response.data as TenantData; + console.log('禁用企业API响应:', data); + + // 验证返回的数据中is_active是否为false + if (data.is_active !== false) { + throw new Error('禁用企业失败:返回数据状态不正确'); + } + + return data; + } catch (error) { + console.error('Failed to disable tenant:', error); + throw error; + } +} + /** * 将API数据转换为页面所需的企业数据格式 */ diff --git a/crop-x/src/app/(app)/central-config/tenant/enterprise-management/page.tsx b/crop-x/src/app/(app)/central-config/tenant/enterprise-management/page.tsx index face2d1..7b62f81 100644 --- a/crop-x/src/app/(app)/central-config/tenant/enterprise-management/page.tsx +++ b/crop-x/src/app/(app)/central-config/tenant/enterprise-management/page.tsx @@ -22,7 +22,7 @@ import { Building2, Eye, Power, PowerOff, Search, FileText, CreditCard, User, Re import { toast } from 'sonner'; import { enterpriseReducer, initialState, EnterpriseState, EnterpriseAction } from './components/enterpriseReducer'; -import { fetchTenants, transformTenantData, TenantsQueryParams, Enterprise } from './components/enterpriseApi'; +import { fetchTenants, transformTenantData, enableTenant, disableTenant, TenantsQueryParams, Enterprise } from './components/enterpriseApi'; // Utility functions const getStatusBadge = (status: 'active' | 'inactive') => { @@ -67,6 +67,9 @@ export default function EnterpriseManagement() { const response = await fetchTenants(params); const transformedData = response.data.map(transformTenantData); + console.log('API Response:', response); + console.log('Transformed Data:', transformedData); + dispatch({ type: 'SET_ENTERPRISES', payload: { @@ -143,27 +146,49 @@ export default function EnterpriseManagement() { dispatch({ type: 'TOGGLE_STATUS_DIALOG', payload: true }); }; - const confirmStatusChange = () => { + const confirmStatusChange = async () => { if (!state.selectedEnterprise) return; - // 这里应该调用API来更新企业状态 - // 暂时更新本地状态 - const newStatus = state.statusAction === 'enable' ? 'active' : 'inactive'; + try { + dispatch({ type: 'SET_LOADING', payload: true }); - dispatch({ - type: 'SET_ENTERPRISES', - payload: { - data: state.enterprises.map(ent => - ent.id === state.selectedEnterprise?.id - ? { ...ent, status: newStatus } - : ent - ), - pagination: state.pagination + const tenantId = state.selectedEnterprise.id; + let updatedTenant; + + if (state.statusAction === 'enable') { + updatedTenant = await enableTenant(tenantId); + toast.success('企业已启用'); + } else { + updatedTenant = await disableTenant(tenantId); + toast.success('企业已禁用'); } - }); - dispatch({ type: 'TOGGLE_STATUS_DIALOG', payload: false }); - toast.success(state.statusAction === 'enable' ? '企业已启用' : '企业已禁用'); + // 验证返回的数据是否正确更新了状态 + console.log('API返回的更新数据:', updatedTenant); + + // 更新本地状态 + const updatedEnterprise = transformTenantData(updatedTenant); + dispatch({ + type: 'SET_ENTERPRISES', + payload: { + data: state.enterprises.map(ent => + ent.id === tenantId ? updatedEnterprise : ent + ), + pagination: state.pagination + } + }); + + dispatch({ type: 'TOGGLE_STATUS_DIALOG', payload: false }); + + // 不需要立即刷新,因为本地状态已经更新 + // 如果用户需要看到最新数据,可以手动点击刷新按钮 + } catch (error) { + console.error('Status change failed:', error); + const errorMessage = error instanceof Error ? error.message : '状态更新失败'; + toast.error(errorMessage); + } finally { + dispatch({ type: 'SET_LOADING', payload: false }); + } }; return (