diff --git a/src/components/logistics/LogisticsQuery.tsx b/src/components/logistics/LogisticsQuery.tsx index 6b9da53..a57f068 100644 --- a/src/components/logistics/LogisticsQuery.tsx +++ b/src/components/logistics/LogisticsQuery.tsx @@ -1,5 +1,4 @@ import React, { useEffect, useState } from 'react'; -import axios from 'axios'; import { Spin, Alert } from 'antd'; interface LogisticsDetail { @@ -28,13 +27,20 @@ const LogisticsQuery: React.FC = ({ id }) => { useEffect(() => { const fetchData = async () => { try { - const response = await axios.get(`/api/tools/logistics/detail/${id}`); - if (response.data && response.data.length > 0) { - setData(response.data[0]); + const response = await fetch(`/api/tools/logistics/detail/${id}`); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const data = await response.json(); + if (data && data.length > 0) { + setData(data[0]); } else { setError('No data found'); } - } catch (err) { + } catch (err: unknown) { + console.error('Error fetching logistics data:', err); setError('Error fetching data'); } finally { setLoading(false); diff --git a/src/pages/api/tools/parseAddress.ts b/src/pages/api/tools/parseAddress.ts index 07e7332..4a99c0b 100644 --- a/src/pages/api/tools/parseAddress.ts +++ b/src/pages/api/tools/parseAddress.ts @@ -7,7 +7,6 @@ // src/pages/api/tools/parseAddress.ts import type { NextApiRequest, NextApiResponse } from 'next'; -import axios from 'axios'; interface ExtractedInfoItem { text: string; @@ -73,13 +72,18 @@ export default async function handler( } // 第一步:提取姓名和电话 - const extractInfoResponse = await axios.post( - extractInfoUrl, - { text }, - { headers: { 'Content-Type': 'application/json' } } - ); + const extractInfoResponse = await fetch(extractInfoUrl, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ text }) + }); - const extractedInfo = extractInfoResponse.data.extracted_info[0]; + if (!extractInfoResponse.ok) { + throw new Error(`Extract info API failed: ${extractInfoResponse.status}`); + } + + const extractInfoData: ExtractInfoResponse = await extractInfoResponse.json(); + const extractedInfo = extractInfoData.extracted_info[0]; let name: string | null = null; let phone: string | null = null; @@ -104,16 +108,22 @@ export default async function handler( } // 第二步:解析地址 - const parseLocationResponse = await axios.post( - parseLocationUrl, - { text: addressText }, - { headers: { 'Content-Type': 'application/json' } } - ); + const parseLocationResponse = await fetch(parseLocationUrl, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ text: addressText }) + }); + + if (!parseLocationResponse.ok) { + throw new Error(`Parse location API failed: ${parseLocationResponse.status}`); + } + + const parseLocationData: ParseLocationResponse = await parseLocationResponse.json(); const combinedResponse: CombinedResponse = { name, phone, - address: parseLocationResponse.data, + address: parseLocationData, }; res.status(200).json(combinedResponse); @@ -122,9 +132,9 @@ export default async function handler( // 根据错误类型返回不同的错误信息 if (error.message === '服务配置错误:API地址未设置') { res.status(500).json({ error: '服务配置错误,请联系管理员' }); - } else if (axios.isAxiosError(error)) { + } else if (error instanceof TypeError && error.message.includes('fetch')) { res.status(500).json({ - error: `外部服务请求失败: ${error.response?.status || '未知错误'}` + error: `外部服务请求失败: 网络连接错误` }); } else { res.status(500).json({ error: '服务器内部错误' }); diff --git a/src/pages/api/tools/test4.ts b/src/pages/api/tools/test4.ts index f057ab4..7b9aed7 100644 --- a/src/pages/api/tools/test4.ts +++ b/src/pages/api/tools/test4.ts @@ -1,5 +1,4 @@ import { NextApiRequest, NextApiResponse } from 'next'; -import axios from 'axios'; import connectDB from '@/utils/connectDB'; //const extractInfoAPI = 'http://192.168.1.8:8006/extract_info/'; @@ -17,8 +16,20 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { const { text } = req.body; // 1. 使用 extract_info API 提取信息 - const extractResponse = await axios.post(extractInfoAPI, { text }); - const extractedInfo = extractResponse.data.extracted_info[0]; + const extractResponse = await fetch(extractInfoAPI, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ text }) + }); + + if (!extractResponse.ok) { + throw new Error(`Extract info API failed: ${extractResponse.status}`); + } + + const extractData = await extractResponse.json(); + const extractedInfo = extractData.extracted_info[0]; // 提取信息失败时抛出异常 if (!extractedInfo) { @@ -32,8 +43,19 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { const fullAddress = `${city}${county}${detailAddress}`; // 3. 使用 parse_location API 解析完整地址 - const parseResponse = await axios.post(parseLocationAPI, { text: fullAddress }); - const parsedLocation = parseResponse.data; + const parseResponse = await fetch(parseLocationAPI, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ text: fullAddress }) + }); + + if (!parseResponse.ok) { + throw new Error(`Parse location API failed: ${parseResponse.status}`); + } + + const parsedLocation = await parseResponse.json(); // 4. 返回组合后的结果 const result = { diff --git a/src/pages/team/sale/sales-record-modal.tsx b/src/pages/team/sale/sales-record-modal.tsx index f972d40..7a6e7e0 100644 --- a/src/pages/team/sale/sales-record-modal.tsx +++ b/src/pages/team/sale/sales-record-modal.tsx @@ -1,10 +1,11 @@ import React, { useEffect, useState } from 'react'; -import { Modal, Form, Input, Select, message, DatePicker } from 'antd'; +import { Modal, Form, Input, Select, DatePicker, App } from 'antd'; import { ISalesRecord } from '@/models/types'; -import axios from 'axios'; import { useUserInfo } from '@/store/userStore'; import dayjs from 'dayjs'; +const { useApp } = App; + interface SalesRecordModalProps { visible: boolean; onCancel: () => void; @@ -19,6 +20,7 @@ const SalesRecordModal: React.FC = ({ visible, onCancel, const [accounts, setAccounts] = useState([]); const [users, setUsers] = useState([]); const userInfo = useUserInfo(); // 获取当前用户信息 + const { message } = useApp(); // 使用 useApp hook 获取 message 实例 useEffect(() => { if (userInfo.团队?._id) { @@ -49,36 +51,56 @@ const SalesRecordModal: React.FC = ({ visible, onCancel, const fetchCustomers = async (teamId: string) => { try { - const { data } = await axios.get(`/api/backstage/customers?teamId=${teamId}`); + const response = await fetch(`/api/backstage/customers?teamId=${teamId}`); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); setCustomers(data.customers); - } catch (error) { + } catch (error: unknown) { + console.error('加载客户数据失败:', error); message.error('加载客户数据失败'); } }; const fetchProducts = async (teamId: string) => { try { - const { data } = await axios.get(`/api/backstage/products?teamId=${teamId}`); + const response = await fetch(`/api/backstage/products?teamId=${teamId}`); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); setProducts(data.products); - } catch (error) { + } catch (error: unknown) { + console.error('加载产品数据失败:', error); message.error('加载产品数据失败'); } }; const fetchAccounts = async (teamId: string) => { try { - const { data } = await axios.get(`/api/backstage/accounts?teamId=${teamId}`); + const response = await fetch(`/api/backstage/accounts?teamId=${teamId}`); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); setAccounts(data.accounts); - } catch (error) { + } catch (error: unknown) { + console.error('加载账户数据失败:', error); message.error('加载账户数据失败'); } }; const fetchUsers = async (teamId: string) => { try { - const { data } = await axios.get(`/api/backstage/users?teamId=${teamId}`); + const response = await fetch(`/api/backstage/users?teamId=${teamId}`); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); setUsers(data.users); - } catch (error) { + } catch (error: unknown) { + console.error('加载用户数据失败:', error); message.error('加载用户数据失败'); } }; @@ -89,18 +111,24 @@ const SalesRecordModal: React.FC = ({ visible, onCancel, const method = salesRecord ? 'PUT' : 'POST'; const url = salesRecord ? `/api/backstage/sales/${salesRecord._id}` : '/api/backstage/sales'; - await axios({ + const response = await fetch(url, { method: method, - url: url, - data: { + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ ...values, 团队: userInfo.团队?._id, // 将团队ID包含在请求数据中 - }, + }), }); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + message.success('销售记录操作成功'); onOk(); // 直接调用 onOk 通知外部重新加载 - } catch (info) { + } catch (info: unknown) { console.error('Validate Failed:', info); message.error('销售记录操作失败'); }