list.vue 11 KB

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