Skip to content

Commit

Permalink
Merge pull request #120 from eleliauk/main
Browse files Browse the repository at this point in the history
✨ feat:新增chart接口
  • Loading branch information
eleliauk authored Sep 12, 2024
2 parents 3df24b9 + 2e43fd1 commit 9c8b500
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/common/components/AnswerToStudent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function AnswerToStudent() {
return (
<View className="question-container">
<View>问题问题问题问题问题:</View>
<View>10个回答</View>
<View>1个回答</View>
</View>
);
}
Expand Down
27 changes: 27 additions & 0 deletions src/common/types/userTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,30 @@ export type WebUserProfileVo = {
using_title: string;
utime?: number;
};
/**
* ginx.Result
*/
export interface GradeResponse {
/**
* 错误码,非 0 表示失败
*/
code?: number;
data?: GradeChart[];
/**
* 错误或成功 描述
*/
msg?: string;
}

/**
* web.GradeChartVo
*/
export interface GradeChart {
avg: number;
grades: Grade[];
}

export interface Grade {
percent?: number;
total_grades: number[];
}
4 changes: 2 additions & 2 deletions src/custom-tab-bar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/first */
import { View } from '@tarojs/components';
import Taro from '@tarojs/taro';
import { memo } from 'react';
import React, { memo } from 'react';
import { AtIcon } from 'taro-ui';

import './index.scss';
Expand All @@ -14,7 +14,7 @@ interface TabBarProps {}
const TAB_LIST: Array<{ pagePath: string; name: string; icon?: string }> = [
{ pagePath: '/pages/main/index', name: 'Home', icon: 'streaming' },
{ pagePath: '/pages/main/index', name: 'Download', icon: 'download-cloud' },
{ pagePath: '/pages/main/index', name: '+' },
{ pagePath: '/pages/evaluate/evaluate', name: '+' },
{ pagePath: '/pages/notification/index', name: 'Massage', icon: 'message' },
{ pagePath: '/pages/profile/index', name: 'Profile', icon: 'user' },
];
Expand Down
59 changes: 40 additions & 19 deletions src/pages/classInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +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 { get } from '@/common/utils/fetch';

const coursePropertyMap = {
Expand All @@ -38,22 +39,20 @@ function translateCourseProperty(englishDescription) {

export default function Index() {
const [course, setCourse] = useState<Course | null>(null);

const [courseId, setCourseId] = useState<string | null>(null);

const [comments, setComments] = useState<CommentInfoType[]>([]);
const [grade, setGrade] = useState<GradeChart>(); // 将 grade 的类型设置为 GradeChart | null

useEffect(() => {
const getParams = () => {
const instance = Taro.getCurrentInstance();
// 使用可选链操作符安全访问 router 和 params
const params = instance?.router?.params || {};

if (params.course_id) setCourseId(params.course_id);
};

getParams();
}, []); // 这个 effect 仅在组件挂载时运行一次
}, []);

useEffect(() => {
// eslint-disable-next-line @typescript-eslint/require-await
Expand All @@ -65,7 +64,6 @@ export default function Index() {
setCourse(res.data);
});
} catch (error) {
// 错误处理,例如弹出提示
console.error('Failed to fetch course data:', error);
}
};
Expand All @@ -83,26 +81,44 @@ export default function Index() {
setComments(res.data as CommentInfoType[]);
});
} catch (error) {
// 错误处理,例如弹出提示
console.error('Failed to fetch course data:', error);
}
};

if (courseId) void getCommentData();
}, [courseId]); // 在courseId变化时运行
}, [courseId]);

if (!course) {
useEffect(() => {
const fetchGrades = async () => {
try {
await get(`/grades/courses/${courseId}`, true).then((res) => {
console.log(res.data);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setGrade(res.data); // 设置 grade 数据
});
} catch (err) {
console.error('Failed to fetch grades data', err);
}
};
if (courseId) void fetchGrades();
}, [courseId]);

if (!course || !grade) {
return <Text>Loading...</Text>; // 数据加载中
}

// 检查 course.features 是否存在并且是一个数组
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 : [];

// @ts-ignore
// @ts-ignore
// @ts-ignore
// @ts-ignore
return (
<View className="classInfo">
<View className="theClassnme">{course?.name}</View>
Expand All @@ -118,14 +134,19 @@ export default function Index() {
</View>
<View className="p">
课程特点: {}
{/* @ts-ignore*/}
{featuresList.map((feature, keyindex) => (
<Label3 key={keyindex} content={feature} />
))}
</View>
{/*<>*/}
{/* 将 grade 数据传递给 LineChart */}
<View className="h-1/3 w-5/6 pt-1.5">
<LineChart className="text-center"></LineChart>
<LineChart
className="text-center"
data={yData}
xLabels={xLabels}
heightLightPercent={heightLightPercent}
title={`平均分: ${avgScore}`}
/>
</View>
<View>
<View>
Expand Down Expand Up @@ -154,9 +175,9 @@ export default function Index() {
url: `/pages/evaluateInfo/index?comment=${serializedComment}`,
});
}}
key={comment.id} // 使用唯一key值来帮助React识别哪些元素是不同的
{...comment} // 展开comment对象,将属性传递给Comment组件
type="inner" // 固定属性,不需要从数组中获取
key={comment.id}
{...comment}
type="inner"
/>
))}
</View>
Expand Down

0 comments on commit 9c8b500

Please sign in to comment.