From d751fc10a7ca727733e7c756b70623d90b9986c9 Mon Sep 17 00:00:00 2001 From: peng Date: Wed, 5 Nov 2025 09:09:28 +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=E6=8F=90=E4=BA=A4=E5=AE=A1=E6=A0=B8=E5=BC=80?= =?UTF-8?q?=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/EnterpriseInfoHeader.tsx | 8 +- .../components/enterpriseInfoApi.ts | 160 ++++++++++++++++++ .../tenant/enterprise-info/page.tsx | 152 ++++++++++------- 3 files changed, 252 insertions(+), 68 deletions(-) create mode 100644 crop-x/src/app/(app)/central-config/tenant/enterprise-info/components/enterpriseInfoApi.ts diff --git a/crop-x/src/app/(app)/central-config/tenant/enterprise-info/components/EnterpriseInfoHeader.tsx b/crop-x/src/app/(app)/central-config/tenant/enterprise-info/components/EnterpriseInfoHeader.tsx index 26e591b..d589834 100644 --- a/crop-x/src/app/(app)/central-config/tenant/enterprise-info/components/EnterpriseInfoHeader.tsx +++ b/crop-x/src/app/(app)/central-config/tenant/enterprise-info/components/EnterpriseInfoHeader.tsx @@ -6,6 +6,7 @@ import { Edit, Save, X } from 'lucide-react'; interface EnterpriseInfoHeaderProps { isEditing: boolean; + loading?: boolean; onEdit: () => void; onCancel: () => void; onSave: () => void; @@ -13,6 +14,7 @@ interface EnterpriseInfoHeaderProps { export function EnterpriseInfoHeader({ isEditing, + loading = false, onEdit, onCancel, onSave @@ -30,13 +32,13 @@ export function EnterpriseInfoHeader({ ) : (
- -
)} diff --git a/crop-x/src/app/(app)/central-config/tenant/enterprise-info/components/enterpriseInfoApi.ts b/crop-x/src/app/(app)/central-config/tenant/enterprise-info/components/enterpriseInfoApi.ts new file mode 100644 index 0000000..90ba877 --- /dev/null +++ b/crop-x/src/app/(app)/central-config/tenant/enterprise-info/components/enterpriseInfoApi.ts @@ -0,0 +1,160 @@ +/** + * filekorolheader: 企业信息API接口 - 企业详细信息获取和更新接口服务 + * 功能:API请求封装、数据转换、错误处理、企业信息管理 + * 路径:/central-config/tenant/enterprise-info/components/enterpriseInfoApi + * 规范:遵循crop-x/docs/开发项目规范.md,使用SDK API调用,TypeScript类型安全 + */ + +import { getAuthToken } from "@/utils/token"; +import { getTenantApiV1TenantsTenantIdGet } from "@/lib/api/sdk.gen"; +import { Enterprise } from '../types'; + +// API返回的租户数据类型(根据实际API返回定义) +export interface TenantApiData { + id: string; + tenant_code: string; + is_active: boolean; + company_name: string; + company_type: string | null; + province: string | null; + city: string | null; + district: string | null; + detailed_address: string | null; + registrant: string | null; + contact_phone: string | null; + bank_account: string | null; + bank_name: string | null; + bank_full_name: string | null; + bank_address: string | null; + social_credit_code: string | null; + legal_person_name: string | null; + company_scale: string | null; + registered_capital: string | null; + established_date: string | null; + invoice_type: string | null; + business_scope: string | null; + submit_time: string | null; + audit_time: string | null; + auditor: string | null; + audit_status: string; + audit_comment: string | null; + created_at: string; + updated_at: string; +} + +/** + * 获取企业详细信息 + */ +export async function fetchEnterpriseInfo(tenantId: string): Promise { + try { + const token = getAuthToken(); + console.log('🏢 获取企业信息API调用,租户ID:', tenantId); + + const response = await getTenantApiV1TenantsTenantIdGet({ + path: { + tenant_id: tenantId, + }, + headers: token ? { + 'Authorization': `Bearer ${token}`, + } : undefined, + }); + + if (response.error) { + throw new Error(`API error: ${response.error.message || 'Unknown error'}`); + } + + const data = response.data as TenantApiData; + console.log('🏢 获取企业信息API响应:', data); + + return transformTenantData(data); + } catch (error) { + console.error('🏢 获取企业信息失败:', error); + throw error; + } +} + +/** + * 更新企业信息 + * 注意:此功能待开发,当前不做任何操作 + */ +export async function updateEnterpriseInfo(tenantId: string, formData: Partial): Promise { + console.log('🏢 更新企业信息功能待开发,租户ID:', tenantId, '数据:', formData); + + // 暂时返回null,等待更新API接口开发完成 + return null; +} + +/** + * 将API数据转换为页面所需的企业数据格式 + */ +export function transformTenantData(tenant: TenantApiData): Enterprise { + return { + id: tenant.id, + name: tenant.company_name || '', + type: tenant.company_type || '未分类', + province: tenant.province || '', + city: tenant.city || '', + district: tenant.district || undefined, + companySize: tenant.company_scale || undefined, + registeredCapital: tenant.registered_capital || undefined, + establishmentDate: tenant.established_date ? + new Date(tenant.established_date).toLocaleDateString('zh-CN') : undefined, + invoiceType: tenant.invoice_type || undefined, + socialCreditCode: tenant.social_credit_code || '', + businessScope: tenant.business_scope || undefined, + bankAccount: tenant.bank_account || undefined, + bankName: tenant.bank_name || undefined, + bankFullName: tenant.bank_full_name || undefined, + bankAddress: tenant.bank_address || undefined, + legalPerson: tenant.legal_person_name || undefined, + registrant: tenant.registrant || '', + contactPhone: tenant.contact_phone || '', + address: tenant.detailed_address || '', + status: tenant.is_active ? 'active' : 'inactive', + auditStatus: mapAuditStatus(tenant.audit_status), + auditReason: tenant.audit_comment || undefined, + auditTime: tenant.audit_time ? formatDate(tenant.audit_time) : undefined, + auditor: tenant.auditor || undefined, + createdAt: formatDate(tenant.created_at), + updatedAt: formatDate(tenant.updated_at), + }; +} + +/** + * 映射审核状态 + */ +function mapAuditStatus(status: string): Enterprise['auditStatus'] { + switch (status) { + case '未提交': + case '草稿': + return 'pending'; + case '待审核': + return 'pending'; + case '已通过': + case '审核通过': + return 'approved'; + case '已拒绝': + case '已驳回': + return 'rejected'; + default: + return 'pending'; + } +} + +/** + * 格式化日期 + */ +function formatDate(dateString: string): string { + try { + const date = new Date(dateString); + return date.toLocaleString('zh-CN', { + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + }).replace(/\//g, '-'); + } catch (error) { + return dateString; + } +} \ No newline at end of file diff --git a/crop-x/src/app/(app)/central-config/tenant/enterprise-info/page.tsx b/crop-x/src/app/(app)/central-config/tenant/enterprise-info/page.tsx index 44cb648..143bf49 100644 --- a/crop-x/src/app/(app)/central-config/tenant/enterprise-info/page.tsx +++ b/crop-x/src/app/(app)/central-config/tenant/enterprise-info/page.tsx @@ -15,68 +15,59 @@ import { LegalInfoForm } from './components/LegalInfoForm'; import { SystemInfo } from './components/SystemInfo'; import { OperationTips } from './components/OperationTips'; import { Enterprise } from './types'; +import { getAuthUser } from '@/stores/modules/auth'; +import { fetchEnterpriseInfo, updateEnterpriseInfo } from './components/enterpriseInfoApi'; export default function EnterpriseInfoPage() { const [enterprise, setEnterprise] = useState(null); const [isEditing, setIsEditing] = useState(false); const [formData, setFormData] = useState>({}); + const [loading, setLoading] = useState(false); + const [currentUser, setCurrentUser] = useState(null); useEffect(() => { - loadEnterpriseInfo(); + // 获取用户信息 + const userInfo = getAuthUser(); + console.log('🏢 从 Zustand 获取的用户信息:', userInfo); + console.log('🏢 用户租户 ID (tenant_id):', userInfo?.tenant_id); + + if (!userInfo?.tenant_id) { + toast.error('无法获取用户租户信息,请重新登录'); + return; + } + + setCurrentUser(userInfo); + + // 加载企业信息 + loadEnterpriseInfo(userInfo.tenant_id); }, []); - const loadEnterpriseInfo = () => { - const stored = localStorage.getItem('smart_agriculture_enterprise_info'); - if (stored) { - const data = JSON.parse(stored); - setEnterprise(data); - setFormData(data); - } else { - // 初始化模拟数据 - const mockData: Enterprise = { - id: 'ent-001', - // 基本信息 - name: '智慧农业科技有限公司', - type: '有限责任公司', - province: '北京市', - city: '海淀区', - district: '中关村街道', + const loadEnterpriseInfo = async (tenantId: string) => { + try { + setLoading(true); + console.log('🏢 开始加载企业信息,租户ID:', tenantId); - // 其他信息 - companySize: '50-200人', - registeredCapital: '1000万元', - establishmentDate: '2020-01-15', - invoiceType: '增值税专用发票', - socialCreditCode: '91110108MA01XXXX00', - businessScope: '农业技术开发、技术咨询、技术服务;销售机械设备、电子产品;软件开发;数据处理服务。', - businessLicense: '', + // 调用API获取企业信息 + const enterpriseData = await fetchEnterpriseInfo(tenantId); - // 开户信息 - bankAccount: '1234567890123456789', - bankName: '中国工商银行', - bankFullName: '中国工商银行股份有限公司北京中关村支行', - bankAddress: '北京市海淀区中关村大街1号', - bankLicense: '', + if (enterpriseData) { + setEnterprise(enterpriseData); + setFormData(enterpriseData); + console.log('🏢 企业信息加载成功:', enterpriseData); + } else { + console.log('🏢 企业信息为空,显示空状态'); + setEnterprise(null); + setFormData({}); + } - // 法人信息 - legalPerson: '张伟', - idCardFront: '', - idCardBack: '', - - // 联系信息 - registrant: '李明', - contactPhone: '010-88888888', - address: '北京市海淀区中关村大街1号科技大厦A座10层', - - // 系统信息 - status: 'active', - auditStatus: 'approved', - createdAt: '2024-01-15T00:00:00', - updatedAt: '2024-01-15T00:00:00', - }; - localStorage.setItem('smart_agriculture_enterprise_info', JSON.stringify(mockData)); - setEnterprise(mockData); - setFormData(mockData); + } catch (error) { + console.error('🏢 加载企业信息失败:', error); + const errorMessage = error instanceof Error ? error.message : '加载企业信息失败'; + toast.error(errorMessage); + setEnterprise(null); + setFormData({}); + } finally { + setLoading(false); } }; @@ -91,24 +82,40 @@ export default function EnterpriseInfoPage() { } }; - const handleSave = () => { + const handleSave = async () => { + if (!currentUser?.tenant_id) { + toast.error('无法获取用户租户信息,请重新登录'); + return; + } + if (!formData.name || !formData.type || !formData.socialCreditCode) { toast.error('请填写必填项'); return; } - const updatedData: Enterprise = { - ...(enterprise as Enterprise), - ...formData, - updatedAt: new Date().toISOString(), - auditStatus: 'pending', // 修改后需要重新审核 - }; + try { + setLoading(true); + console.log('🏢 开始更新企业信息'); - localStorage.setItem('smart_agriculture_enterprise_info', JSON.stringify(updatedData)); - setEnterprise(updatedData); - setFormData(updatedData); - setIsEditing(false); - toast.success('企业信息已更新,等待管理员审核'); + // 调用API更新企业信息 + const updatedData = await updateEnterpriseInfo(currentUser.tenant_id, formData); + + if (updatedData) { + setEnterprise(updatedData); + setFormData(updatedData); + setIsEditing(false); + toast.success('企业信息已更新,等待管理员审核'); + } else { + toast.info('企业信息保存功能待开发'); + } + + } catch (error) { + console.error('🏢 更新企业信息失败:', error); + const errorMessage = error instanceof Error ? error.message : '更新企业信息失败'; + toast.error(errorMessage); + } finally { + setLoading(false); + } }; const handleChange = (field: keyof Enterprise, value: any) => { @@ -116,10 +123,24 @@ export default function EnterpriseInfoPage() { }; const handleImageUpload = (field: keyof Enterprise) => { - // 模拟图片上传 - toast.success('图片上传功能需要对接后端服务'); + // 图片上传功能需要对接后端服务 + toast.info('图片上传功能正在开发中'); }; + if (loading) { + return ( +
+
+

企业信息

+

查看和管理本企业的完整注册信息

+
+ + 加载中... + +
+ ); + } + if (!enterprise) { return (
@@ -128,7 +149,7 @@ export default function EnterpriseInfoPage() {

查看和管理本企业的完整注册信息

- 暂无企业信息 + {loading ? '加载中...' : '暂无企业信息或加载失败,请刷新页面重试'} ); @@ -138,6 +159,7 @@ export default function EnterpriseInfoPage() {