From cdd181496646f5c9ca14b45286a2ff8d408b438a Mon Sep 17 00:00:00 2001 From: Terry Cai Date: Thu, 19 Sep 2024 17:22:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=89=AB=E7=A0=81bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project.alipay.json | 3 ++- src/components/link/index.tsx | 7 ------- src/components/topics/index.tsx | 6 +----- src/hooks/useRouterCodeUrl.ts | 9 ++++++--- src/pages/topic/index.tsx | 12 ++++++++++++ 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/project.alipay.json b/project.alipay.json index c88ffc0..e011d22 100644 --- a/project.alipay.json +++ b/project.alipay.json @@ -7,5 +7,6 @@ } }, "component2": true - } + }, + "miniprogramRoot": "dist/" } diff --git a/src/components/link/index.tsx b/src/components/link/index.tsx index 7eac556..bfb97c9 100644 --- a/src/components/link/index.tsx +++ b/src/components/link/index.tsx @@ -1,5 +1,4 @@ import { View } from '@tarojs/components'; -import Taro from '@tarojs/taro'; import * as utils from '@/libs/utils'; import React from 'react'; @@ -53,11 +52,5 @@ const Link: React.FC = ({ to, className, children }) => { ); }; -Link.defaultProps = { - to: { - url: '', - params: {}, - }, -}; export default Link; diff --git a/src/components/topics/index.tsx b/src/components/topics/index.tsx index 2e2f6ee..111808f 100644 --- a/src/components/topics/index.tsx +++ b/src/components/topics/index.tsx @@ -8,13 +8,9 @@ interface IProps { topics: ITopic[]; } -const TopicsList: React.FC = ({ topics }) => { +const TopicsList: React.FC = ({ topics = [] }) => { const renderTopics = topics.map((topic) => ); return {renderTopics}; }; -TopicsList.defaultProps = { - topics: [], -}; - export { TopicsList }; diff --git a/src/hooks/useRouterCodeUrl.ts b/src/hooks/useRouterCodeUrl.ts index 591f8dc..9070989 100644 --- a/src/hooks/useRouterCodeUrl.ts +++ b/src/hooks/useRouterCodeUrl.ts @@ -4,12 +4,15 @@ import { useRouter } from '@tarojs/taro'; const useRouterCodeUrl = (basePath = '') => { const router = useRouter(true); const [url, setUrl] = useState(''); - const page = router.path.replace(basePath, '') + let page = router.path.replace(basePath, '') + if (page.includes('?')) { + page = page.split('?')[0]; + } const params = router.params; const query = Object.keys(params).map(key => `${key}=${params[key]}`).join('&'); - const fullUrl = query ? `${page}?${encodeURIComponent(query)}` : page; + const fullUrl = query ? `${page}${encodeURIComponent('?' + query)}` : page; useEffect(() => { - const newUrl = `alipays://platformapi/startapp?appId=2021002183695117&page=${encodeURIComponent(fullUrl)}`; + const newUrl = `alipays://platformapi/startapp?appId=2021002183695117&page=${fullUrl}`; setUrl(newUrl); }, [router.path, router.params.id, basePath]); diff --git a/src/pages/topic/index.tsx b/src/pages/topic/index.tsx index 98864ee..e69274b 100644 --- a/src/pages/topic/index.tsx +++ b/src/pages/topic/index.tsx @@ -46,6 +46,12 @@ const Topic: React.FC = () => { fetchTopic(); }, [dispatch]); + + // watch route change + useEffect(() => { + fetchTopic(); + }, [router.params.id]); + const addReply = (id: string) => { setCurReplyId(id); if (!userInfo.userId) { @@ -90,6 +96,12 @@ const Topic: React.FC = () => { const fetchTopic = () => { const topicId = router?.params.id || ''; + if (!topicId) { + // navigate to home page + return utils.redirectTo({ + url: "/pages/index/index" + }); + } setTopicId(topicId); getTopic({ topicId: topicId