生产管理系统 - 禁用、启用接口联调

This commit is contained in:
2025-11-03 17:49:39 +08:00
parent 0305bd64a7
commit c690d50baa
2 changed files with 120 additions and 19 deletions

View File

@@ -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<Ten
if (!params.size) queryParams.size = 10;
if (!params.sort_order) queryParams.sort_order = 'desc';
// 使用SDK API调用添加缓存破坏器和认证头部
// 使用SDK API调用企业查询接口,添加缓存破坏器和认证头部
const token = getAuthToken();
const response = await listTenantsApiV1TenantsGet({
query: {
@@ -152,6 +156,78 @@ export async function fetchTenants(params: TenantsQueryParams = {}): Promise<Ten
}
}
/**
* 启用企业
*/
export async function enableTenant(tenantId: string): Promise<TenantData> {
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<TenantData> {
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数据转换为页面所需的企业数据格式
*/

View File

@@ -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 });
const tenantId = state.selectedEnterprise.id;
let updatedTenant;
if (state.statusAction === 'enable') {
updatedTenant = await enableTenant(tenantId);
toast.success('企业已启用');
} else {
updatedTenant = await disableTenant(tenantId);
toast.success('企业已禁用');
}
// 验证返回的数据是否正确更新了状态
console.log('API返回的更新数据:', updatedTenant);
// 更新本地状态
const updatedEnterprise = transformTenantData(updatedTenant);
dispatch({
type: 'SET_ENTERPRISES',
payload: {
data: state.enterprises.map(ent =>
ent.id === state.selectedEnterprise?.id
? { ...ent, status: newStatus }
: ent
ent.id === tenantId ? updatedEnterprise : ent
),
pagination: state.pagination
}
});
dispatch({ type: 'TOGGLE_STATUS_DIALOG', payload: false });
toast.success(state.statusAction === 'enable' ? '企业已启用' : '企业已禁用');
// 不需要立即刷新,因为本地状态已经更新
// 如果用户需要看到最新数据,可以手动点击刷新按钮
} 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 (