ソースを参照

refactor(api): 优化 API 请求和分页功能

- 更新 API URL 为 http://192.168.1.112:19801
- 移除 package.json 中的 tsc 命令
- 在请求头中添加 Ip 信息,用于区分中心端和普通用户
- 优化请求拦截器,避免重复设置 Ip 头
- 修复文件上传逻辑,添加错误处理
- 完善分页功能,添加总数据数和当前页码
wzg 5 ヶ月 前
コミット
bf30f1d365
6 ファイル変更28 行追加12 行削除
  1. 1 1
      .env
  2. 2 2
      package.json
  3. 2 1
      src/api/index.ts
  4. 9 5
      src/api/request.ts
  5. 9 3
      src/page/audio/drawer.tsx
  6. 5 0
      src/page/audio/index.tsx

+ 1 - 1
.env

@@ -1,5 +1,5 @@
 # API配置
-VITE_API_URL=http://192.168.1.120:19801
+VITE_API_URL=http://192.168.1.112:19801
 # VITE_API_URL=http://localhost:8080/api
 # VITE_API_URL=http://192.168.1.52:8080/api
 

+ 2 - 2
package.json

@@ -5,7 +5,7 @@
   "type": "module",
   "scripts": {
     "dev": "vite",
-    "build": "tsc -b && vite build",
+    "build": "vite build",
     "lint": "eslint .",
     "preview": "vite preview"
   },
@@ -33,4 +33,4 @@
     "typescript-eslint": "^8.22.0",
     "vite": "^6.1.0"
   }
-}
+}

+ 2 - 1
src/api/index.ts

@@ -51,7 +51,8 @@ export const getSensors = (data: any) => {
   return request({
     url: "/voiceSer/getSensorList",
     method: "post",
-    data: data
+    data: data,
+    headers: { "Ip": localStorage.getItem("role") === 'center' ? '' : window.location.origin }
   });
 };
 

+ 9 - 5
src/api/request.ts

@@ -5,9 +5,11 @@ const instance = axios.create({
   timeout: 300000,
   baseURL: import.meta.env.VITE_API_URL,
 });
-console.log(12312);
+
 instance.interceptors.request.use((config) => {
-  config.headers['IP'] = window.location.origin;
+  if (!Object.keys(config.headers).includes('Ip')) {
+    config.headers['Ip'] = window.location.origin;
+  }
   // 1.发送网络请求时,在页面中添加一个loading组件作为动画;
   // 2.某些网络请求要求用户必须登录,可以在请求中判断是否携带了token,没有携带token直接跳转到login页面;
   // 3.对某些请求参数进行序列化;
@@ -19,10 +21,12 @@ instance.interceptors.request.use((config) => {
 );
 
 instance.interceptors.response.use((response) => {
-  if (response.status === 200 && response.data.code === 10001) {
-    message.warning(response.data.msg);
+  const { data } = response;
+
+  if (response.status === 200 && data.code === 10001) {
+    message.warning(data.msg);
   }
-  return response.data;
+  return Promise.resolve(data);
 },
   (err) => {
     if (err && err.response) {

+ 9 - 3
src/page/audio/drawer.tsx

@@ -125,14 +125,20 @@ const DrawerExample: React.FC = forwardRef<DrawerExampleRef, {}>((props, ref) =>
     formData.append('file', file);
     const fn = (percent: number) => onProgress({ percent: percent });
     uploadVoiceFile(formData, fn).then((res: AxiosResponse<UploadResponse>) => {
-      const { fileUUID } = res as any;
+      const { code, msg, fileUUID } = res as any;
+      if (code !== 200) {
+        onError({ message: msg });
+        return;
+      }
       form.setFieldValue("soundFileUUid", fileUUID)
       onSuccess({ message: "文件上传成功" })
       message.success({
         content: '文件上传成功',
         duration: 2,
       })
-    })
+    }).catch(error => {
+      onError({ message: "文件上传失败" });
+    });
   };
 
   const onClose = () => {
@@ -255,7 +261,7 @@ const DrawerExample: React.FC = forwardRef<DrawerExampleRef, {}>((props, ref) =>
             <Input />
           </Form.Item>
 
-          <Form.Item name="soundSensorName" label="音频传感器名称"
+          <Form.Item name="soundSensorName" label=" 音频传感器名称"
             rules={[
               { required: true },
               { pattern: /^[0-9\u4e00-\u9fa5a-zA-Z-\/#\.]+$/, message: "仅支持数字、中文、大小写英文字母、特殊字符-/#." },

+ 5 - 0
src/page/audio/index.tsx

@@ -45,6 +45,7 @@ const Audio: React.FC = () => {
   const containerRef = useRef<HTMLDivElement>(null);
   const drawerExampleRef = useRef<DrawerExampleRef>(null);
   const drawRef = useRef<HTMLDivElement>(null);
+  const [total, setTotal] = useState<number>(0);
 
   const { dragHandleProps } = useResizable(drawRef as RefObject<HTMLElement>, {
     min: 200,
@@ -193,6 +194,7 @@ const Audio: React.FC = () => {
     setLoading(true);
     getSensors(params).then((res) => {
       setDataSource(res.data.records ?? [])
+      setTotal(res.data.total ?? 0)
       setLoading(false);
     });
   }
@@ -337,6 +339,9 @@ const Audio: React.FC = () => {
               scroll={{ y: `max(${tableHeight}px, ${tableHeight}px)` }}
               onChange={handleTableChange} // 监听分页器变化
               pagination={{
+                total: total,
+                current: params.pageNum,
+                pageSize: params.pageSize,
                 size: "default",
                 showTotal: (total) => `共 ${total} 条数据`, // 显示总条数
                 showSizeChanger: true, // 显示每页条数选择器