index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!--
  2. Description: 课程广场课程卡片展示组件,一行展示五个课程
  3. Author: BaiQi
  4. Created Date: 2024-6-30
  5. -->
  6. <template>
  7. <div class="course-content">
  8. <div class="course-header">
  9. <div class="header-title">
  10. {{ props.title }}
  11. <el-button class="header-more" link @click="handleMore">
  12. 更多>>
  13. </el-button>
  14. </div>
  15. </div>
  16. <div class="course-items-container">
  17. <el-card v-for="item in getCoursesSlice()" :key="item.courseId" class="course-item active" @click="handleClick(item.courseId)">
  18. <div style="width: 200px; height: 120px">
  19. <img :src="convertImageUrl(item.courseImages[0], item.courseName)" :alt="item.courseName" class="course-item-img">
  20. </div>
  21. <div class="course-item-content">
  22. <div style="font-weight: bold; font-size: 16px; margin: 5px 0">{{item.courseName}}</div>
  23. <div style="line-height: 1.5;" class="collapsed" ><span style="color: rgba(0, 0, 0, 0.6)">课程描述:</span>{{item.description}}</div>
  24. <div style="line-height: 1.5; margin-top: 5px" class="collapsed" ><span style="color: rgba(0, 0, 0, 0.6)">任课教师:</span>{{item.teacherNames.join(",")}}</div>
  25. </div>
  26. </el-card>
  27. <template v-if="getCoursesSlice().length < 5">
  28. <el-card v-for="item in 5 - getCoursesSlice().length" :key="item" class="course-item">
  29. <template #header>
  30. <template v-if="props.type === 'all'">
  31. 敬请期待
  32. </template>
  33. <template v-if="props.type === 'my'">
  34. 暂无课程
  35. </template>
  36. </template>
  37. </el-card>
  38. </template>
  39. </div>
  40. </div>
  41. </template>
  42. <script lang="ts" setup>
  43. import {defineProps, onMounted, reactive, ref} from 'vue';
  44. import './index.scss'
  45. import useApis from "@/apis";
  46. import router from "@/router";
  47. import type {CourseQuery} from "@/types/query";
  48. import {useStore} from "@/store";
  49. import { convertImageUrl } from '@/utils/convertUtil';
  50. const apis = useApis()
  51. const store = useStore()
  52. const role:'teachers' | 'student' = store.user.role === 'STUDENT' ? 'student':'teachers';
  53. const props = defineProps<{
  54. title: string,
  55. type: 'all' | 'my'
  56. }>()
  57. // 课程数据
  58. const courses = reactive({
  59. course: [],
  60. })
  61. //只获取前五个课程
  62. const getCoursesSlice = () => {
  63. return courses.course.slice(0, 5)
  64. }
  65. const handleMore = () => {
  66. router.push({
  67. name: 'moreCourse',
  68. query: {
  69. title: props.title,
  70. type: props.type
  71. }
  72. })
  73. }
  74. // 处理课程卡片的点击事件
  75. const handleClick = (value:number) => {
  76. router.push(`/home/courseDetail/${value}`)
  77. }
  78. const query = reactive<CourseQuery>({
  79. courseId:null,
  80. teacherId:null,
  81. studentId:null,
  82. courseName:null,
  83. semester:null,
  84. })
  85. const fetchData = () => {
  86. if(props.type === 'all'){
  87. fetchAllCourse()
  88. } else if(props.type === 'my'){
  89. fetchCourseByUserId()
  90. }else{
  91. alert("获取课程失败")
  92. }
  93. }
  94. const fetchAllCourse = () => {
  95. apis.getAllCourse(query, 0, 10)
  96. .then((res: any) => {
  97. courses.course = res.data.data.records
  98. })
  99. .catch((err: any) => {
  100. console.error("错误信息",err);
  101. })
  102. .finally(() => {});
  103. }
  104. const fetchCourseByUserId = () => {
  105. if(role === 'student'){
  106. apis.getCourseByStudentId()
  107. .then((res: any) => {
  108. courses.course = res.data.data.records
  109. })
  110. .catch((err: any) => {
  111. console.error(err);
  112. })
  113. .finally(() => {});
  114. } else if(role === "teachers"){
  115. apis.getCourseByTeacherId()
  116. .then((res: any) => {
  117. courses.course = res.data.data.records
  118. })
  119. .catch((err: any) => {
  120. console.error(err);
  121. })
  122. .finally(() => {});
  123. }
  124. }
  125. onMounted(()=>{
  126. fetchData()
  127. })
  128. </script>
  129. <script lang="ts">
  130. export default {
  131. name: "Course"
  132. }
  133. </script>
  134. <style scoped>
  135. .collapsed {
  136. overflow: hidden;
  137. display: -webkit-box;
  138. -webkit-box-orient: vertical;
  139. -webkit-line-clamp: 2; /* Limit to 2 lines */
  140. max-height: 3em; /* Height for 2 lines of text */
  141. min-height: 3em;
  142. }
  143. </style>