Skip to content

Commit

Permalink
✨ feat:新增问问同学接口
Browse files Browse the repository at this point in the history
  • Loading branch information
eleliauk committed Sep 20, 2024
1 parent a888da2 commit d326bb2
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 34 deletions.
15 changes: 11 additions & 4 deletions src/common/components/AnswerToStudent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import './index.scss';

// eslint-disable-next-line import/first
import Ask from '@/common/assets/img/publishQuestion/img.png';
// eslint-disable-next-line import/first
import { Answerv1Answer } from '@/common/types/userTypes';

function AnswerToStudent() {
interface Question {
content: string;
preview_answers: Array<Answerv1Answer>;
}
const AnswerToStudent: React.FC<Question> = (props) => {
const { content, preview_answers } = props;
return (
<View className="question-container">
{/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment */}
<Image src={Ask} className="img"></Image>
<View>问题问题问题问题问题:</View>
<View>1个回答</View>
<View>{content}</View>
<View>{preview_answers.length}个回答</View>
</View>
);
}
};
export default AnswerToStudent;
47 changes: 47 additions & 0 deletions src/common/types/userTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,50 @@ export interface Grade {
percent?: number;
total_grades: number[];
}
/**
* ginx.Result
*/
export interface ResponseQuestion {
/**
* 错误码,非 0 表示失败
*/
code?: number;
data: WebQuestionVo[];
/**
* 错误或成功 描述
*/
msg?: string;
}

/**
* web.QuestionVo
*/
export interface WebQuestionVo {
answer_cnt?: number;
/**
* 具体针对那种业务的提问,如 Course
*/
biz?: string;
biz_id?: number;
content: string;
ctime?: number;
id?: number;
preview_answers: Answerv1Answer[];
/**
* 提问者用户id
*/
questioner_id?: number;
utime?: number;
}

/**
* answerv1.Answer
*/
export interface Answerv1Answer {
content?: string;
ctime?: number;
id?: number;
publisher_id?: number;
question_id?: number;
utime?: number;
}
82 changes: 52 additions & 30 deletions src/pages/classInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* eslint-disable import/first */
import { Text, View } from '@tarojs/components';
import Taro from '@tarojs/taro';
import { useEffect, useMemo, useState } from 'react';
import { useEffect, useState } from 'react';

import './index.scss';

Expand All @@ -15,7 +15,7 @@ import AnswerToStudent from '@/common/components/AnswerToStudent';
import LineChart from '@/common/components/chart';
import Label3 from '@/common/components/label3/label3';
import ShowStar from '@/common/components/showStar/showStar';
import { GradeChart } from '@/common/types/userTypes';
import { GradeChart, WebQuestionVo } from '@/common/types/userTypes';
import { get } from '@/common/utils/fetch';

const coursePropertyMap = {
Expand Down Expand Up @@ -43,7 +43,7 @@ export default function Index() {
const [comments, setComments] = useState<CommentInfoType[]>([]);
const [grade, setGrade] = useState<GradeChart>();
const [questionNum, setQuestionNum] = useState<number>(0);
//const [questionlist,setQuestionlist] = useState<>([]);
const [questionlist, setQuestionlist] = useState<WebQuestionVo[]>([]);
useEffect(() => {
const getParams = () => {
const instance = Taro.getCurrentInstance();
Expand Down Expand Up @@ -113,26 +113,41 @@ export default function Index() {
console.error(e);
}
};
const fetchAnswer = async () => {
try {
await get(
`/questions/list?biz=Course&biz_id=${courseId}&cur_question_id=&limit=`,
true
).then((res) => {
console.log(res);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setQuestionlist(res.data);
});
} catch (e) {
console.error('Failed to fetch course data:', e);
}
};
if (courseId) {
void fetchGrades();
void getNumData();
void fetchAnswer();
}
}, [courseId]);
const xLabels = useMemo(
() => ['0-40', '40-50', '50-60', '60-70', '70-80', '80-90', '90-100'],
[]
);
const { heightLightPercent, yData } = useMemo(() => {
const percent = (grade?.avg ?? 0) / 10;
return {
heightLightPercent: percent > 4 ? percent - 3 : 0,
yData: grade?.grades.map((item) => item.total_grades?.length ?? 0),
};
}, [grade]);
if (!course || !grade) {
return <Text>请先确定已签约成绩共享计划</Text>; // 数据加载中
}

const xLabels = ['0-40', '40-50', '50-60', '60-70', '70-80', '80-90', '90-100'];

const avgScore = grade.avg;
const heightLightIndex = Math.floor(avgScore / 10) - 4; // 假设 0-40 开始对应 index 0,每个区间跨度 10
// 处理 y 轴的数据,确保它们在 0 到 100 之间
const yData = grade?.grades?.flatMap((g) =>
g?.total_grades?.map((score) => score ?? 0)
);
// 计算高亮百分比
const heightLightPercent = heightLightIndex / xLabels.length;

const featuresList =
course.features && Array.isArray(course.features) ? course.features : [];

Expand All @@ -155,13 +170,13 @@ export default function Index() {
<Label3 key={keyindex} content={feature} />
))}
</View>
<View className="h-1/3 pt-1.5">
<View className="h-1/3 w-5/6 pt-1.5">
<LineChart
className="mx-auto text-center"
className="text-center"
data={yData}
xLabels={xLabels}
heightLightPercent={heightLightPercent ?? 0}
title={`平均分: ${grade?.avg.toFixed(1)}`}
heightLightPercent={heightLightPercent}
title={`平均分: ${avgScore}`}
/>
</View>
<View>
Expand All @@ -171,21 +186,28 @@ export default function Index() {
</View>
</View>
<>
<AnswerToStudent></AnswerToStudent>
<AnswerToStudent></AnswerToStudent>
<AnswerToStudent></AnswerToStudent>
{questionlist &&
questionlist.map((question) => (
<AnswerToStudent
content={question.content}
key={question.id}
preview_answers={question.preview_answers}
/>
))}
</>
<View
onClick={() => {
void Taro.navigateTo({ url: '/pages/questionInfo/index' });
}}
className="text-right"
>
全部&gt;
</View>
{questionlist.length > 0 && (
<View
onClick={() => {
void Taro.navigateTo({ url: '/pages/questionInfo/index' });
}}
className="text-right"
>
全部&gt;
</View>
)}
</View>
<View>
<View className="line-container pt-2.5 text-center text-xl">最新评论</View>
<View className="line-container pt-5 text-center text-xl">最新评论</View>
</View>
{comments &&
comments.map((comment) => (
Expand Down

0 comments on commit d326bb2

Please sign in to comment.