From 44ad43f9b33b72583b57c3b614151ca8c751d5d0 Mon Sep 17 00:00:00 2001 From: LiuJiaNan <15703339975@163.com> Date: Fri, 7 Nov 2025 10:50:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EuseGetUrlQuery?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/HiddenInfo/gwj/index.js | 17 ++++++++--------- hooks/useGetUrlQuery/index.d.ts | 4 ++++ hooks/useGetUrlQuery/index.js | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 hooks/useGetUrlQuery/index.d.ts create mode 100644 hooks/useGetUrlQuery/index.js diff --git a/components/HiddenInfo/gwj/index.js b/components/HiddenInfo/gwj/index.js index 3ffbd96..abeef79 100644 --- a/components/HiddenInfo/gwj/index.js +++ b/components/HiddenInfo/gwj/index.js @@ -10,6 +10,7 @@ import { UPLOAD_FILE_TYPE_ENUM } from "../../../enum/uploadFile/gwj"; import useGetFile from "../../../hooks/useGetFile"; import { getLabelName } from "../../../utils"; import { HIDDEN_SOURCE_ENUM, HIDDEN_STATE_ENUM } from "../../../enum/hidden/gwj"; +import useGetUrlQuery from "../../../hooks/useGetUrlQuery"; /** * 隐患查看组件(港务局版本) @@ -39,29 +40,27 @@ function HiddenInfo(props) { const [rectificationPlanImageFiles, setRectificationPlanImageFiles] = useState([]); const [acceptImageFiles, setAcceptImageFiles] = useState([]); const { getFile } = useGetFile(); + const query = useGetUrlQuery(); const getData = async () => { - const urlParams = new URLSearchParams(window.location.search); - const queryId = urlParams.get(idKey); - const queryHiddenId = urlParams.get(hiddenIdKey); - request(`/hidden/hidden/${id || queryId}`, "get").then((res) => { + request(`/hidden/hidden/${id || query[idKey]}`, "get").then((res) => { setInfo(res.data); }); - const hiddenImageFiles = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM["3"], eqForeignKey: hiddenId || queryHiddenId }); + const hiddenImageFiles = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM["3"], eqForeignKey: hiddenId || query[hiddenIdKey] }); setHiddenImageFiles(hiddenImageFiles); - const hiddenVideoFiles = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM["102"], eqForeignKey: hiddenId || queryHiddenId }); + const hiddenVideoFiles = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM["102"], eqForeignKey: hiddenId || query[hiddenIdKey] }); setHiddenVideoFiles(hiddenVideoFiles); const afterRectificationImageFiles = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM["4"], - eqForeignKey: hiddenId || queryHiddenId, + eqForeignKey: hiddenId || query[hiddenIdKey], }); setAfterRectificationImageFiles(afterRectificationImageFiles); const rectificationPlanImageFiles = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM["8"], - eqForeignKey: hiddenId || queryHiddenId, + eqForeignKey: hiddenId || query[hiddenIdKey], }); setRectificationPlanImageFiles(rectificationPlanImageFiles); - const acceptImageFiles = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM["5"], eqForeignKey: hiddenId || queryHiddenId }); + const acceptImageFiles = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM["5"], eqForeignKey: hiddenId || query[hiddenIdKey] }); setAcceptImageFiles(acceptImageFiles); }; useEffect(() => { diff --git a/hooks/useGetUrlQuery/index.d.ts b/hooks/useGetUrlQuery/index.d.ts new file mode 100644 index 0000000..1ca50df --- /dev/null +++ b/hooks/useGetUrlQuery/index.d.ts @@ -0,0 +1,4 @@ +/** + * 获取路由参数 + */ +export default function useGetUrlQuery(): Record; diff --git a/hooks/useGetUrlQuery/index.js b/hooks/useGetUrlQuery/index.js new file mode 100644 index 0000000..5b0f03f --- /dev/null +++ b/hooks/useGetUrlQuery/index.js @@ -0,0 +1,16 @@ +/** + * 获取路由参数 + */ +function useGetUrlQuery() { + const urlQuery = new URLSearchParams(window.location.search); + + // 直接返回包含所有参数的对象 + const queryParams = {}; + for (const [key, value] of urlQuery.entries()) { + queryParams[key] = value; + } + + return queryParams; +} + +export default useGetUrlQuery;