list.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <view class="courses-page">
  3. <!-- 自定义导航栏 -->
  4. <view class="custom-navbar">
  5. <view class="navbar-left" @click="goBack">
  6. <text class="back-icon">←</text>
  7. </view>
  8. <view class="navbar-title">精品课程</view>
  9. <view class="navbar-right"></view>
  10. </view>
  11. <!-- 课程列表 -->
  12. <view class="course-list">
  13. <view class="course-card" v-for="(item, index) in courseList" :key="index"
  14. @click="goToDetail(item.id)">
  15. <image :src="item.cover_image" class="course-image" mode="aspectFill"></image>
  16. <view class="course-info">
  17. <view class="course-name">{{ item.name }}</view>
  18. <view class="course-desc">{{ item.description }}</view>
  19. <view class="course-meta">
  20. <text class="course-teacher">👩‍🏫 {{ item.teacher_name }}</text>
  21. <text class="course-students">👥 {{ item.student_count || 0 }}人学习</text>
  22. </view>
  23. <view class="course-footer">
  24. <view class="course-price">
  25. <text class="price-symbol">¥</text>
  26. <text class="price-value">{{ item.price }}</text>
  27. <text class="original-price" v-if="item.original_price">¥{{ item.original_price }}</text>
  28. </view>
  29. <view class="course-btn">立即购买</view>
  30. </view>
  31. </view>
  32. <view class="discount-tag" v-if="item.discount_rate">{{ item.discount_rate }}折</view>
  33. </view>
  34. </view>
  35. <!-- 加载更多 -->
  36. <view class="load-more" v-if="hasMore">
  37. <text class="load-text">{{ loading ? '加载中...' : '上拉加载更多' }}</text>
  38. </view>
  39. <view class="no-more" v-else>
  40. <text class="no-more-text">没有更多了</text>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import api from '@/utils/api.js'
  46. import { DEFAULT_IMAGES } from '@/config/index.js'
  47. export default {
  48. data() {
  49. return {
  50. courseList: [], // 从API加载数据,不使用硬编码
  51. pageNum: 1,
  52. pageSize: 10,
  53. total: 0, // 总记录数
  54. hasMore: true,
  55. loading: false,
  56. DEFAULT_IMAGES
  57. }
  58. },
  59. onLoad() {
  60. console.log('精品课程列表页面加载')
  61. this.loadCourseList()
  62. },
  63. onReachBottom() {
  64. if (this.hasMore && !this.loading) {
  65. this.pageNum++
  66. this.loadCourseList()
  67. }
  68. },
  69. methods: {
  70. // 加载课程列表
  71. async loadCourseList() {
  72. if (this.loading) return
  73. this.loading = true
  74. try {
  75. console.log('尝试加载精品课程API数据...')
  76. const response = await api.course.getList({
  77. page: this.pageNum,
  78. pageSize: this.pageSize,
  79. status: 1
  80. })
  81. console.log('API返回数据:', response)
  82. // 处理返回的数据结构
  83. let courseData = []
  84. let totalCount = 0
  85. if (response && response.list) {
  86. // 如果返回的是 {list: [], total: xx} 格式
  87. courseData = response.list
  88. totalCount = response.total || 0
  89. } else if (Array.isArray(response)) {
  90. // 如果直接返回数组
  91. courseData = response
  92. totalCount = response.length
  93. }
  94. console.log('当前页码:', this.pageNum, '返回数据量:', courseData.length, '总数据量:', totalCount)
  95. if (courseData && courseData.length > 0) {
  96. // 处理每个课程数据,确保字段完整
  97. const processedData = courseData.map(item => ({
  98. ...item,
  99. // 确保必要字段存在
  100. cover_image: item.cover_image || item.coverImage || DEFAULT_IMAGES.course,
  101. teacher_name: item.teacher_name || item.instructor || '专业导师',
  102. student_count: item.student_count || item.participants || 0,
  103. // 保留其他字段
  104. id: item.id,
  105. name: item.name,
  106. description: item.description,
  107. price: item.price,
  108. original_price: item.original_price || item.originalPrice,
  109. discount_rate: item.discount_rate || item.discountRate,
  110. rating: item.rating,
  111. duration: item.duration
  112. }))
  113. if (this.pageNum === 1) {
  114. // 第一页,替换数据
  115. this.courseList = processedData
  116. this.total = totalCount
  117. } else {
  118. // 后续页,追加数据
  119. this.courseList = [...this.courseList, ...processedData]
  120. }
  121. // 判断是否还有更多数据:当前已加载数量 < 总数量
  122. this.hasMore = this.courseList.length < totalCount
  123. console.log('API课程数据加载成功:', processedData.length, '条')
  124. console.log('已加载总数:', this.courseList.length, '/', totalCount)
  125. console.log('是否还有更多:', this.hasMore)
  126. console.log('第一条课程数据:', processedData[0])
  127. } else {
  128. this.hasMore = false
  129. console.log('API返回课程数据为空')
  130. if (this.pageNum === 1) {
  131. uni.showToast({
  132. title: '暂无课程数据',
  133. icon: 'none'
  134. })
  135. }
  136. }
  137. } catch (error) {
  138. console.error('加载课程列表失败:', error)
  139. uni.showToast({
  140. title: '加载失败',
  141. icon: 'none'
  142. })
  143. this.hasMore = false
  144. } finally {
  145. this.loading = false
  146. }
  147. },
  148. // 跳转到详情
  149. goToDetail(id) {
  150. console.log('跳转到课程详情, id:', id)
  151. uni.navigateTo({
  152. url: `/pages/courses/detail?id=${id}`,
  153. success: () => {
  154. console.log('跳转课程详情成功')
  155. },
  156. fail: (err) => {
  157. console.error('跳转课程详情失败:', err)
  158. uni.showToast({
  159. title: '跳转失败',
  160. icon: 'none'
  161. })
  162. }
  163. })
  164. },
  165. // 返回
  166. goBack() {
  167. console.log('课程列表返回上一页')
  168. uni.navigateBack({
  169. fail: () => {
  170. // 返回失败时跳转首页
  171. uni.navigateTo({
  172. url: '/pages/index/index'
  173. })
  174. }
  175. })
  176. }
  177. }
  178. }
  179. </script>
  180. <style lang="scss" scoped>
  181. .courses-page {
  182. min-height: 100vh;
  183. background-color: #FFF9F9;
  184. padding-top: 90rpx;
  185. }
  186. /* 自定义导航栏 */
  187. .custom-navbar {
  188. position: fixed;
  189. top: 0;
  190. left: 0;
  191. right: 0;
  192. height: 90rpx;
  193. display: flex;
  194. align-items: center;
  195. justify-content: space-between;
  196. padding: 0 20rpx;
  197. background-color: #E91E63;
  198. z-index: 999;
  199. .navbar-left,
  200. .navbar-right {
  201. width: 80rpx;
  202. }
  203. .back-icon {
  204. font-size: 40rpx;
  205. color: #FFFFFF;
  206. font-weight: bold;
  207. }
  208. .navbar-title {
  209. flex: 1;
  210. text-align: center;
  211. font-size: 32rpx;
  212. font-weight: bold;
  213. color: #FFFFFF;
  214. }
  215. }
  216. /* 课程列表 */
  217. .course-list {
  218. padding: 20rpx 30rpx;
  219. .course-card {
  220. position: relative;
  221. margin-bottom: 20rpx;
  222. background-color: #FFFFFF;
  223. border-radius: 20rpx;
  224. overflow: hidden;
  225. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  226. .course-image {
  227. width: 100%;
  228. height: 360rpx;
  229. background-color: #F5F5F5;
  230. }
  231. .course-info {
  232. padding: 30rpx;
  233. .course-name {
  234. font-size: 32rpx;
  235. font-weight: bold;
  236. color: #333333;
  237. margin-bottom: 15rpx;
  238. display: -webkit-box;
  239. -webkit-box-orient: vertical;
  240. -webkit-line-clamp: 2;
  241. overflow: hidden;
  242. }
  243. .course-desc {
  244. font-size: 26rpx;
  245. color: #666666;
  246. line-height: 1.6;
  247. margin-bottom: 20rpx;
  248. display: -webkit-box;
  249. -webkit-box-orient: vertical;
  250. -webkit-line-clamp: 2;
  251. overflow: hidden;
  252. }
  253. .course-meta {
  254. display: flex;
  255. justify-content: space-between;
  256. font-size: 24rpx;
  257. color: #999999;
  258. margin-bottom: 20rpx;
  259. }
  260. .course-footer {
  261. display: flex;
  262. justify-content: space-between;
  263. align-items: center;
  264. .course-price {
  265. color: #E91E63;
  266. font-weight: bold;
  267. .price-symbol {
  268. font-size: 24rpx;
  269. }
  270. .price-value {
  271. font-size: 36rpx;
  272. }
  273. .original-price {
  274. margin-left: 10rpx;
  275. font-size: 24rpx;
  276. color: #999999;
  277. text-decoration: line-through;
  278. }
  279. }
  280. .course-btn {
  281. padding: 10rpx 30rpx;
  282. background-color: #E91E63;
  283. color: #FFFFFF;
  284. text-align: center;
  285. border-radius: 30rpx;
  286. font-size: 24rpx;
  287. }
  288. }
  289. }
  290. .discount-tag {
  291. position: absolute;
  292. top: 20rpx;
  293. left: 20rpx;
  294. padding: 8rpx 20rpx;
  295. background: linear-gradient(135deg, #FF6B6B 0%, #FF9800 100%);
  296. color: #FFFFFF;
  297. font-size: 24rpx;
  298. font-weight: bold;
  299. border-radius: 10rpx;
  300. }
  301. }
  302. }
  303. /* 加载更多 */
  304. .load-more,
  305. .no-more {
  306. padding: 30rpx 0;
  307. text-align: center;
  308. .load-text,
  309. .no-more-text {
  310. font-size: 24rpx;
  311. color: #999999;
  312. }
  313. }
  314. </style>