list.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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-tabs">
  13. <view class="tab-item" :class="{ active: activeTab === 'all' }" @click="switchTab('all')">全部课程</view>
  14. <view class="tab-item" :class="{ active: activeTab === 'basic' }" @click="switchTab('basic')">基础课程</view>
  15. <view class="tab-item" :class="{ active: activeTab === 'advanced' }" @click="switchTab('advanced')">进阶课程</view>
  16. <view class="tab-item" :class="{ active: activeTab === 'premium' }" @click="switchTab('premium')">精品课程</view>
  17. </view>
  18. <!-- 排序选项 -->
  19. <view class="sort-options">
  20. <view class="sort-item" :class="{ active: activeSort === 'recommend' }" @click="switchSort('recommend')">推荐排序</view>
  21. <view class="sort-item" :class="{ active: activeSort === 'latest' }" @click="switchSort('latest')">最新课程</view>
  22. <view class="sort-item" :class="{ active: activeSort === 'popular' }" @click="switchSort('popular')">热门课程</view>
  23. </view>
  24. <!-- 课程列表 -->
  25. <view class="course-grid">
  26. <view class="course-card" v-for="(item, index) in courseList" :key="index"
  27. @click="goToDetail(item.id)">
  28. <image :src="item.cover_image" class="course-image" mode="aspectFill"></image>
  29. <view class="course-info">
  30. <view class="course-name">{{ item.name }}</view>
  31. <view class="course-meta">
  32. <text class="course-teacher">{{ item.teacher_name }}</text>
  33. <text class="course-rating">⭐ {{ item.rating || 4.5 }}</text>
  34. </view>
  35. <view class="course-footer">
  36. <view class="course-price">
  37. <text class="price-symbol">¥</text>
  38. <text class="price-value">{{ item.price }}</text>
  39. </view>
  40. <text class="course-students">{{ item.student_count || 0 }}人观看</text>
  41. </view>
  42. <view class="course-btn" @click.stop="goToDetail(item.id)">查看详情</view>
  43. </view>
  44. </view>
  45. </view>
  46. <!-- 加载更多 -->
  47. <view class="load-more" v-if="hasMore">
  48. <text class="load-text">{{ loading ? '加载中...' : '上拉加载更多' }}</text>
  49. </view>
  50. <view class="no-more" v-else>
  51. <text class="no-more-text">没有更多了</text>
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. import api from '@/utils/api.js'
  57. import { DEFAULT_IMAGES } from '@/config/index.js'
  58. export default {
  59. data() {
  60. return {
  61. courseList: [], // 从API加载数据,不使用硬编码
  62. pageNum: 1,
  63. pageSize: 10,
  64. total: 0, // 总记录数
  65. hasMore: true,
  66. loading: false,
  67. DEFAULT_IMAGES,
  68. activeTab: 'all', // 当前选中的标签页
  69. activeSort: 'recommend', // 当前排序方式
  70. // 模拟数据,当API返回为空时使用
  71. mockCourses: [
  72. {
  73. id: 1,
  74. name: '非暴力沟通:让问题更...',
  75. teacher_name: '王老师',
  76. rating: 4.9,
  77. price: 299,
  78. student_count: 54,
  79. cover_image: 'https://images.unsplash.com/photo-1506744038136-46273834b3fb?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
  80. },
  81. {
  82. id: 2,
  83. name: '红娘新手入门:相亲场...',
  84. teacher_name: '张老师',
  85. rating: 4.5,
  86. price: 89,
  87. student_count: 33,
  88. cover_image: 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
  89. },
  90. {
  91. id: 3,
  92. name: '从零做红娘:牵线必备...',
  93. teacher_name: '高老师',
  94. rating: 4.9,
  95. price: 199,
  96. student_count: 45,
  97. cover_image: 'https://images.unsplash.com/photo-1516321318423-f06f85e504b3?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
  98. },
  99. {
  100. id: 4,
  101. name: '新手红娘必学:相亲沟...',
  102. teacher_name: '魏老师',
  103. rating: 4.4,
  104. price: 99,
  105. student_count: 54,
  106. cover_image: 'https://images.unsplash.com/photo-1502672260266-1c1ef2d93688?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
  107. }
  108. ]
  109. }
  110. },
  111. onLoad() {
  112. console.log('精品课程列表页面加载')
  113. this.loadCourseList()
  114. },
  115. onReachBottom() {
  116. if (this.hasMore && !this.loading) {
  117. this.pageNum++
  118. this.loadCourseList()
  119. }
  120. },
  121. methods: {
  122. // 加载课程列表
  123. async loadCourseList() {
  124. if (this.loading) return
  125. this.loading = true
  126. try {
  127. console.log('尝试加载精品课程API数据...')
  128. const response = await api.course.getList({
  129. page: this.pageNum,
  130. pageSize: this.pageSize,
  131. status: 1
  132. })
  133. console.log('API返回数据:', response)
  134. // 处理返回的数据结构
  135. let courseData = []
  136. let totalCount = 0
  137. if (response && response.list) {
  138. // 如果返回的是 {list: [], total: xx} 格式
  139. courseData = response.list
  140. totalCount = response.total || 0
  141. } else if (Array.isArray(response)) {
  142. // 如果直接返回数组
  143. courseData = response
  144. totalCount = response.length
  145. }
  146. console.log('当前页码:', this.pageNum, '返回数据量:', courseData.length, '总数据量:', totalCount)
  147. if (courseData && courseData.length > 0) {
  148. // 处理每个课程数据,确保字段完整
  149. const processedData = courseData.map(item => ({
  150. ...item,
  151. // 确保必要字段存在
  152. cover_image: item.cover_image || item.coverImage || this.DEFAULT_IMAGES.course,
  153. teacher_name: item.teacher_name || item.instructor || '专业导师',
  154. student_count: item.student_count || item.participants || 0,
  155. // 保留其他字段
  156. id: item.id,
  157. name: item.name,
  158. description: item.description,
  159. price: item.price,
  160. original_price: item.original_price || item.originalPrice,
  161. discount_rate: item.discount_rate || item.discountRate,
  162. rating: item.rating,
  163. duration: item.duration
  164. }))
  165. if (this.pageNum === 1) {
  166. // 第一页,替换数据
  167. this.courseList = processedData
  168. this.total = totalCount
  169. } else {
  170. // 后续页,追加数据
  171. this.courseList = [...this.courseList, ...processedData]
  172. }
  173. // 判断是否还有更多数据:当前已加载数量 < 总数量
  174. this.hasMore = this.courseList.length < totalCount
  175. console.log('API课程数据加载成功:', processedData.length, '条')
  176. console.log('已加载总数:', this.courseList.length, '/', totalCount)
  177. console.log('是否还有更多:', this.hasMore)
  178. console.log('第一条课程数据:', processedData[0])
  179. } else {
  180. // 如果API返回为空,使用模拟数据
  181. this.hasMore = false
  182. console.log('API返回课程数据为空,使用模拟数据')
  183. if (this.pageNum === 1) {
  184. this.courseList = this.mockCourses
  185. this.total = this.mockCourses.length
  186. }
  187. }
  188. } catch (error) {
  189. console.error('加载课程列表失败:', error)
  190. // 当API调用失败时,使用模拟数据
  191. if (this.pageNum === 1) {
  192. this.courseList = this.mockCourses
  193. this.total = this.mockCourses.length
  194. }
  195. this.hasMore = false
  196. } finally {
  197. this.loading = false
  198. }
  199. },
  200. // 切换标签页
  201. switchTab(tab) {
  202. this.activeTab = tab
  203. this.pageNum = 1
  204. this.courseList = []
  205. this.hasMore = true
  206. this.loadCourseList()
  207. },
  208. // 切换排序
  209. switchSort(sort) {
  210. this.activeSort = sort
  211. // 这里可以根据排序方式重新排序课程列表
  212. // 模拟排序效果
  213. if (this.activeSort === 'popular') {
  214. this.courseList.sort((a, b) => (b.student_count || 0) - (a.student_count || 0))
  215. } else if (this.activeSort === 'latest') {
  216. // 假设id越大越新
  217. this.courseList.sort((a, b) => b.id - a.id)
  218. } else {
  219. // 推荐排序
  220. if (this.pageNum === 1) {
  221. this.loadCourseList()
  222. }
  223. }
  224. },
  225. // 跳转到详情
  226. goToDetail(id) {
  227. console.log('跳转到课程详情, id:', id)
  228. uni.navigateTo({
  229. url: `/pages/courses/detail?id=${id}`,
  230. success: () => {
  231. console.log('跳转课程详情成功')
  232. },
  233. fail: (err) => {
  234. console.error('跳转课程详情失败:', err)
  235. uni.showToast({
  236. title: '跳转失败',
  237. icon: 'none'
  238. })
  239. }
  240. })
  241. },
  242. // 返回
  243. goBack() {
  244. console.log('课程列表返回上一页')
  245. uni.navigateBack({
  246. fail: () => {
  247. // 返回失败时跳转首页
  248. uni.navigateTo({
  249. url: '/pages/index/index'
  250. })
  251. }
  252. })
  253. }
  254. }
  255. }
  256. </script>
  257. <style lang="scss" scoped>
  258. .courses-page {
  259. min-height: 100vh;
  260. background-color: #FFF9F9;
  261. padding-top: 90rpx;
  262. }
  263. /* 自定义导航栏 */
  264. .custom-navbar {
  265. position: fixed;
  266. top: 0;
  267. left: 0;
  268. right: 0;
  269. height: 90rpx;
  270. display: flex;
  271. align-items: center;
  272. justify-content: space-between;
  273. padding: 0 20rpx;
  274. background: linear-gradient(135deg, #FCE4EC 0%, #F8BBD0 100%);
  275. z-index: 999;
  276. .navbar-left,
  277. .navbar-right {
  278. width: 80rpx;
  279. }
  280. .back-icon {
  281. font-size: 40rpx;
  282. color: #333333;
  283. font-weight: bold;
  284. }
  285. .navbar-title {
  286. flex: 1;
  287. text-align: center;
  288. font-size: 32rpx;
  289. font-weight: bold;
  290. color: #333333;
  291. }
  292. }
  293. /* 课程分类标签 */
  294. .course-tabs {
  295. display: flex;
  296. align-items: center;
  297. justify-content: space-around;
  298. padding: 20rpx 0;
  299. background-color: #FFFFFF;
  300. border-bottom: 1rpx solid #F0F0F0;
  301. .tab-item {
  302. padding: 12rpx 24rpx;
  303. font-size: 28rpx;
  304. color: #666666;
  305. border-radius: 30rpx;
  306. transition: all 0.3s;
  307. &.active {
  308. background-color: #9C27B0;
  309. color: #FFFFFF;
  310. }
  311. }
  312. }
  313. /* 排序选项 */
  314. .sort-options {
  315. display: flex;
  316. align-items: center;
  317. justify-content: space-around;
  318. padding: 20rpx 0;
  319. background-color: #FFFFFF;
  320. border-bottom: 1rpx solid #F0F0F0;
  321. .sort-item {
  322. font-size: 26rpx;
  323. color: #666666;
  324. transition: all 0.3s;
  325. &.active {
  326. color: #9C27B0;
  327. font-weight: bold;
  328. }
  329. }
  330. }
  331. /* 课程列表 - 两列布局 */
  332. .course-grid {
  333. display: grid;
  334. grid-template-columns: 1fr 1fr;
  335. gap: 20rpx;
  336. padding: 20rpx;
  337. .course-card {
  338. position: relative;
  339. background-color: #FFFFFF;
  340. border-radius: 20rpx;
  341. overflow: hidden;
  342. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  343. .course-image {
  344. width: 100%;
  345. height: 240rpx;
  346. background-color: #F5F5F5;
  347. }
  348. .course-info {
  349. padding: 20rpx;
  350. .course-name {
  351. font-size: 28rpx;
  352. font-weight: bold;
  353. color: #333333;
  354. margin-bottom: 10rpx;
  355. display: -webkit-box;
  356. -webkit-box-orient: vertical;
  357. -webkit-line-clamp: 2;
  358. overflow: hidden;
  359. }
  360. .course-meta {
  361. display: flex;
  362. justify-content: space-between;
  363. font-size: 24rpx;
  364. color: #666666;
  365. margin-bottom: 10rpx;
  366. }
  367. .course-footer {
  368. display: flex;
  369. justify-content: space-between;
  370. align-items: center;
  371. margin-bottom: 15rpx;
  372. .course-price {
  373. color: #E91E63;
  374. font-weight: bold;
  375. .price-symbol {
  376. font-size: 22rpx;
  377. }
  378. .price-value {
  379. font-size: 32rpx;
  380. }
  381. }
  382. .course-students {
  383. font-size: 22rpx;
  384. color: #999999;
  385. }
  386. }
  387. .course-btn {
  388. width: 100%;
  389. height: 60rpx;
  390. display: flex;
  391. align-items: center;
  392. justify-content: center;
  393. background-color: #9C27B0;
  394. color: #FFFFFF;
  395. border-radius: 30rpx;
  396. font-size: 26rpx;
  397. }
  398. }
  399. }
  400. }
  401. /* 加载更多 */
  402. .load-more,
  403. .no-more {
  404. padding: 30rpx 0;
  405. text-align: center;
  406. .load-text,
  407. .no-more-text {
  408. font-size: 24rpx;
  409. color: #999999;
  410. }
  411. }
  412. </style>