index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. }).then(() => {
  73. window.location.reload();
  74. })
  75. }
  76. // 处理课程卡片的点击事件
  77. const handleClick = (value:number) => {
  78. router.push(`/home/courseDetail/${value}`)
  79. }
  80. const query = reactive<CourseQuery>({
  81. courseId:null,
  82. teacherId:null,
  83. studentId:null,
  84. courseName:null,
  85. semester:null,
  86. })
  87. const fetchData = () => {
  88. if(props.type === 'all'){
  89. fetchAllCourse()
  90. } else if(props.type === 'my'){
  91. fetchCourseByUserId()
  92. }else{
  93. alert("获取课程失败")
  94. }
  95. }
  96. const fetchAllCourse = () => {
  97. apis.getAllCourse(query, 0, 10)
  98. .then((res: any) => {
  99. courses.course = res.data.data.records
  100. })
  101. .catch((err: any) => {
  102. console.error("错误信息",err);
  103. })
  104. .finally(() => {});
  105. }
  106. const fetchCourseByUserId = () => {
  107. if(role === 'student'){
  108. apis.getCourseByStudentId()
  109. .then((res: any) => {
  110. courses.course = res.data.data.records
  111. })
  112. .catch((err: any) => {
  113. console.error(err);
  114. })
  115. .finally(() => {});
  116. } else if(role === "teachers"){
  117. apis.getCourseByTeacherId()
  118. .then((res: any) => {
  119. courses.course = res.data.data.records
  120. })
  121. .catch((err: any) => {
  122. console.error(err);
  123. })
  124. .finally(() => {});
  125. }
  126. }
  127. onMounted(()=>{
  128. fetchData()
  129. })
  130. </script>
  131. <script lang="ts">
  132. export default {
  133. name: "Course"
  134. }
  135. </script>
  136. <style scoped>
  137. .collapsed {
  138. overflow: hidden;
  139. display: -webkit-box;
  140. -webkit-box-orient: vertical;
  141. -webkit-line-clamp: 2; /* Limit to 2 lines */
  142. max-height: 3em; /* Height for 2 lines of text */
  143. min-height: 3em;
  144. }
  145. </style>