list.vue 11 KB

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