From b4cbc4bd36a1623403005250d4fae15a3bdce86f Mon Sep 17 00:00:00 2001 From: zhangjiming Date: Thu, 13 Mar 2025 10:47:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=A0=E9=99=A4=E8=BD=A8?= =?UTF-8?q?=E5=9E=8B=E5=92=8C=E6=A0=B8=E6=A0=A1=E7=9A=84=E5=BC=82=E6=AD=A5?= =?UTF-8?q?Action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/measure/components/MeasureAction.tsx | 2 +- src/pages/measure/components/MeasureConfig.tsx | 23 +++++-- src/store/features/baseDataSlice.ts | 88 ++++++++++++++++++++++---- 3 files changed, 95 insertions(+), 18 deletions(-) diff --git a/src/pages/measure/components/MeasureAction.tsx b/src/pages/measure/components/MeasureAction.tsx index 2df1b19..aedb020 100644 --- a/src/pages/measure/components/MeasureAction.tsx +++ b/src/pages/measure/components/MeasureAction.tsx @@ -184,7 +184,7 @@ export default function MeasureAction() { dispatch(fetchRailTypes()); dispatch(fetchCalibrationTypes()); } - }, [baseData.railTypes.length, dispatch]); + }, [baseData.railTypes, dispatch]); /** ----------------------- WebSocket 消息处理 ----------------------- **/ useEffect(() => { diff --git a/src/pages/measure/components/MeasureConfig.tsx b/src/pages/measure/components/MeasureConfig.tsx index 59eed0b..8bbea57 100644 --- a/src/pages/measure/components/MeasureConfig.tsx +++ b/src/pages/measure/components/MeasureConfig.tsx @@ -32,8 +32,8 @@ export default function MeasureConfig() { if (res.status !== 0) { messageApi.error(res.data.info); } else { - dispatch(updateRailTypeId(values["railType"])) - dispatch(updateCalibrationTypeId(values["calibrationType"])) + dispatch(updateRailTypeId(values["railType"])); + dispatch(updateCalibrationTypeId(values["calibrationType"])); navigate("../action"); } }); @@ -44,7 +44,7 @@ export default function MeasureConfig() { dispatch(fetchRailTypes()); dispatch(fetchCalibrationTypes()); } - }, [baseData.railTypes.length, dispatch]); + }, [baseData.railTypes, dispatch]); const [form] = Form.useForm(); @@ -54,16 +54,22 @@ export default function MeasureConfig() { const fieldValue: Record = { username: userInfo.nickname || "", direction: "左", - } + }; if (baseData.railTypes.length > 0) { - fieldValue.railType = baseData.railTypes[0].id + fieldValue.railType = baseData.railTypes[0].id; } if (baseData.calibrationTypes.length > 0) { - fieldValue.calibrationType = baseData.calibrationTypes[0].id + fieldValue.calibrationType = baseData.calibrationTypes[0].id; } form.setFieldsValue(fieldValue); }, [baseData.calibrationTypes, baseData.railTypes, form]); + // useEffect(() => { + // if (!baseData.loading && baseData.error) { + // messageApi.error(baseData.error); + // } + // }, [baseData.error, baseData.loading, messageApi]); + return ( <> {contextHolder} @@ -127,6 +133,11 @@ export default function MeasureConfig() { 开始测量 + {/* + + */} diff --git a/src/store/features/baseDataSlice.ts b/src/store/features/baseDataSlice.ts index 52c602c..62ca0a3 100644 --- a/src/store/features/baseDataSlice.ts +++ b/src/store/features/baseDataSlice.ts @@ -2,27 +2,73 @@ import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit"; import { Calibration, Rail } from "../../services/apiTypes"; -import { getRailTypes } from "../../services/standardRail/standardRail"; -import { getCalibrationTypes } from "../../services/calibration/calibration"; +import { deleteRailTypes, getRailTypes } from "../../services/standardRail/standardRail"; +import { deleteCalibrationTypes, getCalibrationTypes } from "../../services/calibration/calibration"; export interface BaseDataState { + loading: boolean; + error: string; railTypes: Rail[]; calibrationTypes: Calibration[]; } const initialState: BaseDataState = { + loading: false, + error: "", // 轨型列表 railTypes: [], // 核校类型列表 calibrationTypes: [], }; -export const fetchRailTypes = createAsyncThunk("fetchRailTypes", async () => { - const res = await getRailTypes(); - return res.success ? res.data : []; +// 可以添加泛型参数,第一个是 成功返回类型,第二个是 入参类型,第三个是 reject返回类型 +// createAsyncThunk +export const fetchRailTypes = createAsyncThunk("fetchRailTypes", async (_, thunkAPI) => { + try { + const res = await getRailTypes(); + if (res.success) { + return res.data; + } else { + return thunkAPI.rejectWithValue({ msg: res.data.info }); + } + } catch (error: any) { + return thunkAPI.rejectWithValue({ msg: (error.message as string) || "请求失败" }); + } }); -export const fetchCalibrationTypes = createAsyncThunk("fetchCalibrationTypes", async () => { - const res = await getCalibrationTypes(); - return res.success ? res.data : []; +export const deleteRailType = createAsyncThunk("deleteRailType", async (ids: number[], thunkAPI) => { + try { + const res = await deleteRailTypes(ids); + if (res.success) { + thunkAPI.dispatch(fetchRailTypes()); + } else { + return thunkAPI.rejectWithValue({ msg: res.data.info }); + } + } catch (error: any) { + return thunkAPI.rejectWithValue({ msg: (error.message as string) || "请求失败" }); + } +}); +export const fetchCalibrationTypes = createAsyncThunk("fetchCalibrationTypes", async (_, thunkAPI) => { + try { + const res = await getCalibrationTypes(); + if (res.success) { + return res.data; + } else { + return thunkAPI.rejectWithValue({ msg: res.data.info }); + } + } catch (error: any) { + return thunkAPI.rejectWithValue({ msg: (error.message as string) || "请求失败" }); + } +}); +export const deleteCalibrationType = createAsyncThunk("deleteCalibrationType", async (ids: number[], thunkAPI) => { + try { + const res = await deleteCalibrationTypes(ids); + if (res.success) { + thunkAPI.dispatch(fetchCalibrationTypes()); + } else { + return thunkAPI.rejectWithValue({ msg: res.data.info }); + } + } catch (error: any) { + return thunkAPI.rejectWithValue({ msg: (error.message as string) || "请求失败" }); + } }); export const baseDataSlice = createSlice({ @@ -45,11 +91,31 @@ export const baseDataSlice = createSlice({ builder.addCase(fetchCalibrationTypes.fulfilled, (state, action) => { state.calibrationTypes = action.payload; }); + + builder.addMatcher( + action => action.type.endsWith("/pending"), + state => { + state.loading = true; + state.error = ""; + } + ); + builder.addMatcher( + action => action.type.endsWith("/fulfilled"), + state => { + state.loading = false; + state.error = ""; + } + ); + builder.addMatcher( + action => action.type.endsWith("/rejected"), + (state, action: PayloadAction<{ msg: string }>) => { + state.loading = false; + state.error = action.payload?.msg || "请求失败"; + } + ); }, }); -// 导出加减的方法 -export const { updateRailTypes, updateCalibrationTypes } = baseDataSlice.actions; +// export const { updateRailTypes, updateCalibrationTypes } = baseDataSlice.actions; -// 默认导出 export default baseDataSlice.reducer;