index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.teacherIds.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. courseId: 5,
  61. courseName: '英文论文写作',
  62. description: '',
  63. teacherIds: [],
  64. courseImages: [],
  65. targetGrade: null,
  66. startTime: new Date(),
  67. endTime: new Date(),
  68. },
  69. {
  70. courseId: 7,
  71. courseName: '数学分析',
  72. description: '',
  73. teacherIds: [],
  74. courseImages: [],
  75. targetGrade: null,
  76. startTime: new Date(),
  77. endTime: new Date(),
  78. },
  79. {
  80. courseId: 8,
  81. courseName: '线性代数',
  82. description: '',
  83. teacherIds: [],
  84. courseImages: [],
  85. targetGrade: null,
  86. startTime: new Date(),
  87. endTime: new Date(),
  88. },
  89. // {
  90. // courseId: 4,
  91. // courseName: '计算机科学',
  92. // description: '',
  93. // teacherIds: [],
  94. // courseImages: [],
  95. // targetGrade: null,
  96. // startTime: new Date(),
  97. // endTime: new Date(),
  98. // },
  99. // {
  100. // courseId: 5,
  101. // courseName: '物理学基础',
  102. // description: '',
  103. // teacherIds: [],
  104. // courseImages: [],
  105. // targetGrade: null,
  106. // startTime: new Date(),
  107. // endTime: new Date(),
  108. // },
  109. // {
  110. // courseId: 6,
  111. // courseName: '化学基础',
  112. // description: '',
  113. // teacherIds: [],
  114. // courseImages: [],
  115. // targetGrade: null,
  116. // startTime: new Date(),
  117. // endTime: new Date(),
  118. // },
  119. // 可以根据需要添加更多课程对象
  120. ],
  121. })
  122. //只获取前五个课程
  123. const getCoursesSlice = () => {
  124. return courses.course.slice(0, 5)
  125. }
  126. const handleMore = () => {
  127. router.push({
  128. name: 'moreCourse',
  129. query: {
  130. title: props.title,
  131. type: props.type
  132. }
  133. })
  134. }
  135. // 处理课程卡片的点击事件
  136. const handleClick = (value:number) => {
  137. router.push(`/home/courseDetail/${value}`)
  138. }
  139. const query = reactive<CourseQuery>({
  140. courseId:null,
  141. teacherId:null,
  142. studentId:null,
  143. courseName:null,
  144. semester:null,
  145. })
  146. const fetchData = () => {
  147. if(props.type === 'all'){
  148. fetchAllCourse()
  149. } else if(props.type === 'my'){
  150. fetchCourseByUserId()
  151. }else{
  152. alert("获取课程失败")
  153. }
  154. }
  155. const fetchAllCourse = () => {
  156. apis.getAllCourse(query, 0, 10)
  157. .then((res: any) => {
  158. courses.course = res.data.data.records
  159. processCourses()
  160. })
  161. .catch((err: any) => {
  162. console.error("错误信息",err);
  163. })
  164. .finally(() => {});
  165. }
  166. const fetchCourseByUserId = () => {
  167. if(role === 'student'){
  168. apis.getCourseByStudentId(store.user.id)
  169. .then((res: any) => {
  170. courses.course = res.data.data.records
  171. processCourses()
  172. })
  173. .catch((err: any) => {
  174. console.error(err);
  175. })
  176. .finally(() => {});
  177. } else if(role === "teachers"){
  178. apis.getCourseByTeacherId(store.user.id)
  179. .then((res: any) => {
  180. courses.course = res.data.data.records
  181. processCourses()
  182. })
  183. .catch((err: any) => {
  184. console.error(err);
  185. })
  186. .finally(() => {});
  187. }
  188. }
  189. /*
  190. 用于显示教师姓名
  191. */
  192. const processCourses = () => {
  193. for(let i:number = 0; i < courses.course.length; i++){
  194. for(let j = 0; j < courses.course[i].teacherIds.length; j++){
  195. apis.findUserById(courses.course[i].teacherIds[j])
  196. .then((res:any) => {
  197. courses.course[i].teacherIds[j] = res.data.data.records[0].name
  198. })
  199. .catch((err:any) => {
  200. console.log("错误信息",err)
  201. })
  202. }
  203. }
  204. }
  205. onMounted(()=>{
  206. fetchData()
  207. })
  208. </script>
  209. <script lang="ts">
  210. export default {
  211. name: "Course"
  212. }
  213. </script>
  214. <style scoped>
  215. .collapsed {
  216. overflow: hidden;
  217. display: -webkit-box;
  218. -webkit-box-orient: vertical;
  219. -webkit-line-clamp: 2; /* Limit to 2 lines */
  220. max-height: 3em; /* Height for 2 lines of text */
  221. min-height: 3em;
  222. }
  223. </style>