From 733eebc7350cc6149dac1b18474d4b8b3c86a665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B6=85=E7=BA=A7=E6=97=A0=E6=95=8C=E6=95=B0=E7=A0=81?= =?UTF-8?q?=E6=9A=B4=E9=BE=99=E6=88=98=E5=A3=AB?= <67992313+konodioda727@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:22:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=B8=BB=E9=A1=B5?= =?UTF-8?q?=E4=B8=8D=E6=9B=B4=E6=96=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/questionList/index.tsx | 108 +++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/pages/questionList/index.tsx diff --git a/src/pages/questionList/index.tsx b/src/pages/questionList/index.tsx new file mode 100644 index 0000000..d550eac --- /dev/null +++ b/src/pages/questionList/index.tsx @@ -0,0 +1,108 @@ +// import React from 'react'; +import { Button, View } from '@tarojs/components'; +import Taro from '@tarojs/taro'; +import { useEffect, useState } from 'react'; + +import './index.scss'; + +import { get } from '../../common/utils/fetch'; + +import { Course } from '../../common/assets/types'; +import CourseInfo from '../../common/components/CourseInfo/CourseInfo'; +import QuestionListComponent from '../../common/components/QuestionListComponent/QuestionListComponent'; + +interface IQuestion { + id: number; + questioner_id: number; + biz: string; + biz_id: number; + content: string; + answer_cnt: number; + preview_answers: Array<{ + id: number; + publisher_id: number; + question_id: number; + content: string; + utime: number; + ctime: number; + }>; + utime: number; + ctime: number; +} + +const App = () => { + const [course, setCourse] = useState(null); + const [questions, setQuestions] = useState(null); + // const courseId = 2347; //先用概率统计A来调试吧 + const [courseId, setCourseId] = useState(null); + useEffect(() => { + const getParams = () => { + const instance = Taro.getCurrentInstance(); + const params = instance?.router?.params || {}; + + if (params.course_id) setCourseId(params.course_id); + }; + + getParams(); + }, []); + + useEffect(() => { + // eslint-disable-next-line @typescript-eslint/require-await + const getCourseData = async () => { + try { + void get(`/courses/${courseId}/detail`, true).then((res) => { + console.log(res); + // 检查 res 是否有 data 属性,并且断言其类型 + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + setCourse(res?.data as Course); + }); + } catch (error) { + // 错误处理,例如弹出提示 + console.error('Failed to fetch course data:', error); + } + }; + + if (courseId) void getCourseData().then((r) => console.log(r)); + + // eslint-disable-next-line @typescript-eslint/require-await + const getQuestionList = async () => { + try { + void get( + `/questions/list?biz=Course&biz_id=${courseId}&cur_question_id=0&limit=100`, + true + ).then((res) => { + console.log(res); + // 检查 res 是否有 data 属性,并且断言其类型 + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + setQuestions(res?.data as IQuestion[]); + }); + } catch (error) { + // 错误处理,例如弹出提示 + console.error('Failed to fetch course data:', error); + } + }; + + if (courseId) void getQuestionList().then((r) => console.log(r)); + }, [courseId]); + + const handleAsk = () => { + void Taro.navigateTo({ + url: `/pages/publishQuestion/index?course_id=${courseId}`, + }); + }; + + return ( + + + {questions !== null && + questions.map((question, index) => ( + + ))} + + + ); +}; + +export default App;