list.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. this.loadCourseList()
  113. },
  114. onReachBottom() {
  115. if (this.hasMore && !this.loading) {
  116. this.pageNum++
  117. this.loadCourseList()
  118. }
  119. },
  120. methods: {
  121. // 加载课程列表
  122. async loadCourseList() {
  123. if (this.loading) return
  124. this.loading = true
  125. try {
  126. const response = await api.course.getList({
  127. page: this.pageNum,
  128. pageSize: this.pageSize,
  129. status: 1
  130. })
  131. // 处理返回的数据结构
  132. let courseData = []
  133. let totalCount = 0
  134. if (response && response.list) {
  135. // 如果返回的是 {list: [], total: xx} 格式
  136. courseData = response.list
  137. totalCount = response.total || 0
  138. } else if (Array.isArray(response)) {
  139. // 如果直接返回数组
  140. courseData = response
  141. totalCount = response.length
  142. }
  143. if (courseData && courseData.length > 0) {
  144. // 处理每个课程数据,确保字段完整
  145. const processedData = courseData.map(item => ({
  146. ...item,
  147. // 确保必要字段存在
  148. cover_image: item.cover_image || item.coverImage || this.DEFAULT_IMAGES.course,
  149. teacher_name: item.teacher_name || item.instructor || '专业导师',
  150. student_count: item.student_count || item.participants || 0,
  151. // 保留其他字段
  152. id: item.id,
  153. name: item.name,
  154. description: item.description,
  155. price: item.price,
  156. original_price: item.original_price || item.originalPrice,
  157. discount_rate: item.discount_rate || item.discountRate,
  158. rating: item.rating,
  159. duration: item.duration
  160. }))
  161. if (this.pageNum === 1) {
  162. // 第一页,替换数据
  163. this.courseList = processedData
  164. this.total = totalCount
  165. } else {
  166. // 后续页,追加数据
  167. this.courseList = [...this.courseList, ...processedData]
  168. }
  169. // 判断是否还有更多数据:当前已加载数量 < 总数量
  170. this.hasMore = this.courseList.length < totalCount
  171. } else {
  172. // 如果API返回为空,使用模拟数据
  173. this.hasMore = false
  174. if (this.pageNum === 1) {
  175. this.courseList = this.mockCourses
  176. this.total = this.mockCourses.length
  177. }
  178. }
  179. } catch (error) {
  180. // 当API调用失败时,使用模拟数据
  181. if (this.pageNum === 1) {
  182. this.courseList = this.mockCourses
  183. this.total = this.mockCourses.length
  184. }
  185. this.hasMore = false
  186. } finally {
  187. this.loading = false
  188. }
  189. },
  190. // 切换标签页
  191. switchTab(tab) {
  192. this.activeTab = tab
  193. this.pageNum = 1
  194. this.courseList = []
  195. this.hasMore = true
  196. this.loadCourseList()
  197. },
  198. // 切换排序
  199. switchSort(sort) {
  200. this.activeSort = sort
  201. // 这里可以根据排序方式重新排序课程列表
  202. // 模拟排序效果
  203. if (this.activeSort === 'popular') {
  204. this.courseList.sort((a, b) => (b.student_count || 0) - (a.student_count || 0))
  205. } else if (this.activeSort === 'latest') {
  206. // 假设id越大越新
  207. this.courseList.sort((a, b) => b.id - a.id)
  208. } else {
  209. // 推荐排序
  210. if (this.pageNum === 1) {
  211. this.loadCourseList()
  212. }
  213. }
  214. },
  215. // 跳转到详情
  216. goToDetail(id) {
  217. uni.navigateTo({
  218. url: `/pages/courses/detail?id=${id}`,
  219. success: () => {
  220. },
  221. fail: (err) => {
  222. uni.showToast({
  223. title: '跳转失败',
  224. icon: 'none'
  225. })
  226. }
  227. })
  228. },
  229. // 返回
  230. goBack() {
  231. uni.navigateBack({
  232. fail: () => {
  233. // 返回失败时跳转首页
  234. uni.navigateTo({
  235. url: '/pages/index/index'
  236. })
  237. }
  238. })
  239. }
  240. }
  241. }
  242. </script>
  243. <style lang="scss" scoped>
  244. .courses-page {
  245. min-height: 100vh;
  246. background-color: #FFF9F9;
  247. padding-top: 90rpx;
  248. }
  249. /* 自定义导航栏 */
  250. .custom-navbar {
  251. position: fixed;
  252. top: 0;
  253. left: 0;
  254. right: 0;
  255. height: 90rpx;
  256. display: flex;
  257. align-items: center;
  258. justify-content: space-between;
  259. padding: 0 20rpx;
  260. background: linear-gradient(135deg, #FCE4EC 0%, #F8BBD0 100%);
  261. z-index: 999;
  262. .navbar-left,
  263. .navbar-right {
  264. width: 80rpx;
  265. }
  266. .back-icon {
  267. font-size: 40rpx;
  268. color: #333333;
  269. font-weight: bold;
  270. }
  271. .navbar-title {
  272. flex: 1;
  273. text-align: center;
  274. font-size: 32rpx;
  275. font-weight: bold;
  276. color: #333333;
  277. }
  278. }
  279. /* 课程分类标签 */
  280. .course-tabs {
  281. display: flex;
  282. align-items: center;
  283. justify-content: space-around;
  284. padding: 20rpx 0;
  285. background-color: #FFFFFF;
  286. border-bottom: 1rpx solid #F0F0F0;
  287. .tab-item {
  288. padding: 12rpx 24rpx;
  289. font-size: 28rpx;
  290. color: #666666;
  291. border-radius: 30rpx;
  292. transition: all 0.3s;
  293. &.active {
  294. background-color: #9C27B0;
  295. color: #FFFFFF;
  296. }
  297. }
  298. }
  299. /* 排序选项 */
  300. .sort-options {
  301. display: flex;
  302. align-items: center;
  303. justify-content: space-around;
  304. padding: 20rpx 0;
  305. background-color: #FFFFFF;
  306. border-bottom: 1rpx solid #F0F0F0;
  307. .sort-item {
  308. font-size: 26rpx;
  309. color: #666666;
  310. transition: all 0.3s;
  311. &.active {
  312. color: #9C27B0;
  313. font-weight: bold;
  314. }
  315. }
  316. }
  317. /* 课程列表 - 两列布局 */
  318. .course-grid {
  319. display: grid;
  320. grid-template-columns: 1fr 1fr;
  321. gap: 20rpx;
  322. padding: 20rpx;
  323. .course-card {
  324. position: relative;
  325. background-color: #FFFFFF;
  326. border-radius: 20rpx;
  327. overflow: hidden;
  328. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  329. .course-image {
  330. width: 100%;
  331. height: 240rpx;
  332. background-color: #F5F5F5;
  333. }
  334. .course-info {
  335. padding: 20rpx;
  336. .course-name {
  337. font-size: 28rpx;
  338. font-weight: bold;
  339. color: #333333;
  340. margin-bottom: 10rpx;
  341. display: -webkit-box;
  342. -webkit-box-orient: vertical;
  343. -webkit-line-clamp: 2;
  344. overflow: hidden;
  345. }
  346. .course-meta {
  347. display: flex;
  348. justify-content: space-between;
  349. font-size: 24rpx;
  350. color: #666666;
  351. margin-bottom: 10rpx;
  352. }
  353. .course-footer {
  354. display: flex;
  355. justify-content: space-between;
  356. align-items: center;
  357. margin-bottom: 15rpx;
  358. .course-price {
  359. color: #E91E63;
  360. font-weight: bold;
  361. .price-symbol {
  362. font-size: 22rpx;
  363. }
  364. .price-value {
  365. font-size: 32rpx;
  366. }
  367. }
  368. .course-students {
  369. font-size: 22rpx;
  370. color: #999999;
  371. }
  372. }
  373. .course-btn {
  374. width: 100%;
  375. height: 60rpx;
  376. display: flex;
  377. align-items: center;
  378. justify-content: center;
  379. background-color: #9C27B0;
  380. color: #FFFFFF;
  381. border-radius: 30rpx;
  382. font-size: 26rpx;
  383. }
  384. }
  385. }
  386. }
  387. /* 加载更多 */
  388. .load-more,
  389. .no-more {
  390. padding: 30rpx 0;
  391. text-align: center;
  392. .load-text,
  393. .no-more-text {
  394. font-size: 24rpx;
  395. color: #999999;
  396. }
  397. }
  398. </style>