index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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="item.courseImages[0]" :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. const apis = useApis()
  50. const store = useStore()
  51. const role:'teachers' | 'student' = store.user.role === 'STUDENT' ? 'student':'teachers';
  52. const props = defineProps<{
  53. title: string,
  54. type: 'all' | 'my'
  55. }>()
  56. // 课程数据
  57. const courses = reactive({
  58. course: [],
  59. })
  60. //只获取前五个课程
  61. const getCoursesSlice = () => {
  62. return courses.course.slice(0, 5)
  63. }
  64. const handleMore = () => {
  65. router.push({
  66. name: 'moreCourse',
  67. query: {
  68. title: props.title,
  69. type: props.type
  70. }
  71. })
  72. }
  73. // 处理课程卡片的点击事件
  74. const handleClick = (value:number) => {
  75. router.push(`/home/courseDetail/${value}`)
  76. }
  77. const query = reactive<CourseQuery>({
  78. courseId:null,
  79. teacherId:null,
  80. studentId:null,
  81. courseName:null,
  82. semester:null,
  83. })
  84. const fetchData = () => {
  85. if(props.type === 'all'){
  86. fetchAllCourse()
  87. } else if(props.type === 'my'){
  88. fetchCourseByUserId()
  89. }else{
  90. alert("获取课程失败")
  91. }
  92. }
  93. const fetchAllCourse = () => {
  94. apis.getAllCourse(query, 0, 10)
  95. .then((res: any) => {
  96. courses.course = res.data.data.records
  97. processCourses()
  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. processCourses()
  110. })
  111. .catch((err: any) => {
  112. console.error(err);
  113. })
  114. .finally(() => {});
  115. } else if(role === "teachers"){
  116. apis.getCourseByTeacherId()
  117. .then((res: any) => {
  118. courses.course = res.data.data.records
  119. processCourses()
  120. })
  121. .catch((err: any) => {
  122. console.error(err);
  123. })
  124. .finally(() => {});
  125. }
  126. }
  127. /*
  128. * 处理课程图片
  129. */
  130. const processCourses = () => {
  131. for(let i:number = 0; i < courses.course.length; i++){
  132. courses.course[i].courseImages[0] = courses.course[i].courseImages[0].split("?")[0]
  133. }
  134. }
  135. onMounted(()=>{
  136. fetchData()
  137. })
  138. </script>
  139. <script lang="ts">
  140. export default {
  141. name: "Course"
  142. }
  143. </script>
  144. <style scoped>
  145. .collapsed {
  146. overflow: hidden;
  147. display: -webkit-box;
  148. -webkit-box-orient: vertical;
  149. -webkit-line-clamp: 2; /* Limit to 2 lines */
  150. max-height: 3em; /* Height for 2 lines of text */
  151. min-height: 3em;
  152. }
  153. </style>