| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- <template>
- <view class="courses-page">
- <!-- 自定义导航栏 -->
- <view class="custom-navbar">
- <view class="navbar-left" @click="goBack">
- <text class="back-icon">←</text>
- </view>
- <view class="navbar-title">精品课程</view>
- <view class="navbar-right"></view>
- </view>
- <!-- 课程分类标签 -->
- <view class="course-tabs">
- <view class="tab-item" :class="{ active: activeTab === 'all' }" @click="switchTab('all')">全部课程</view>
- <view class="tab-item" :class="{ active: activeTab === 'basic' }" @click="switchTab('basic')">基础课程</view>
- <view class="tab-item" :class="{ active: activeTab === 'advanced' }" @click="switchTab('advanced')">进阶课程</view>
- <view class="tab-item" :class="{ active: activeTab === 'premium' }" @click="switchTab('premium')">精品课程</view>
- </view>
- <!-- 排序选项 -->
- <view class="sort-options">
- <view class="sort-item" :class="{ active: activeSort === 'recommend' }" @click="switchSort('recommend')">推荐排序</view>
- <view class="sort-item" :class="{ active: activeSort === 'latest' }" @click="switchSort('latest')">最新课程</view>
- <view class="sort-item" :class="{ active: activeSort === 'popular' }" @click="switchSort('popular')">热门课程</view>
- </view>
- <!-- 课程列表 -->
- <view class="course-grid">
- <view class="course-card" v-for="(item, index) in courseList" :key="index"
- @click="goToDetail(item.id)">
- <image :src="item.cover_image" class="course-image" mode="aspectFill"></image>
- <view class="course-info">
- <view class="course-name">{{ item.name }}</view>
- <view class="course-meta">
- <text class="course-teacher">{{ item.teacher_name }}</text>
- <text class="course-rating">⭐ {{ item.rating || 4.5 }}</text>
- </view>
- <view class="course-footer">
- <view class="course-price">
- <text class="price-symbol">¥</text>
- <text class="price-value">{{ item.price }}</text>
- </view>
- <text class="course-students">{{ item.student_count || 0 }}人观看</text>
- </view>
- <view class="course-btn" @click.stop="goToDetail(item.id)">查看详情</view>
- </view>
- </view>
- </view>
- <!-- 加载更多 -->
- <view class="load-more" v-if="hasMore">
- <text class="load-text">{{ loading ? '加载中...' : '上拉加载更多' }}</text>
- </view>
- <view class="no-more" v-else>
- <text class="no-more-text">没有更多了</text>
- </view>
- </view>
- </template>
- <script>
- import api from '@/utils/api.js'
- import { DEFAULT_IMAGES } from '@/config/index.js'
- export default {
- data() {
- return {
- courseList: [], // 从API加载数据,不使用硬编码
- pageNum: 1,
- pageSize: 10,
- total: 0, // 总记录数
- hasMore: true,
- loading: false,
- DEFAULT_IMAGES,
- activeTab: 'all', // 当前选中的标签页
- activeSort: 'recommend', // 当前排序方式
- // 模拟数据,当API返回为空时使用
- mockCourses: [
- {
- id: 1,
- name: '非暴力沟通:让问题更...',
- teacher_name: '王老师',
- rating: 4.9,
- price: 299,
- student_count: 54,
- cover_image: 'https://images.unsplash.com/photo-1506744038136-46273834b3fb?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
- },
- {
- id: 2,
- name: '红娘新手入门:相亲场...',
- teacher_name: '张老师',
- rating: 4.5,
- price: 89,
- student_count: 33,
- cover_image: 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
- },
- {
- id: 3,
- name: '从零做红娘:牵线必备...',
- teacher_name: '高老师',
- rating: 4.9,
- price: 199,
- student_count: 45,
- cover_image: 'https://images.unsplash.com/photo-1516321318423-f06f85e504b3?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
- },
- {
- id: 4,
- name: '新手红娘必学:相亲沟...',
- teacher_name: '魏老师',
- rating: 4.4,
- price: 99,
- student_count: 54,
- cover_image: 'https://images.unsplash.com/photo-1502672260266-1c1ef2d93688?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60'
- }
- ]
- }
- },
- onLoad() {
-
- this.loadCourseList()
- },
- onReachBottom() {
- if (this.hasMore && !this.loading) {
- this.pageNum++
- this.loadCourseList()
- }
- },
- methods: {
- // 加载课程列表
- async loadCourseList() {
- if (this.loading) return
- this.loading = true
- try {
-
- const response = await api.course.getList({
- page: this.pageNum,
- pageSize: this.pageSize,
- status: 1
- })
-
- // 处理返回的数据结构
- let courseData = []
- let totalCount = 0
- if (response && response.list) {
- // 如果返回的是 {list: [], total: xx} 格式
- courseData = response.list
- totalCount = response.total || 0
- } else if (Array.isArray(response)) {
- // 如果直接返回数组
- courseData = response
- totalCount = response.length
- }
-
- if (courseData && courseData.length > 0) {
- // 处理每个课程数据,确保字段完整
- const processedData = courseData.map(item => ({
- ...item,
- // 确保必要字段存在
- cover_image: item.cover_image || item.coverImage || this.DEFAULT_IMAGES.course,
- teacher_name: item.teacher_name || item.instructor || '专业导师',
- student_count: item.student_count || item.participants || 0,
- // 保留其他字段
- id: item.id,
- name: item.name,
- description: item.description,
- price: item.price,
- original_price: item.original_price || item.originalPrice,
- discount_rate: item.discount_rate || item.discountRate,
- rating: item.rating,
- duration: item.duration
- }))
- if (this.pageNum === 1) {
- // 第一页,替换数据
- this.courseList = processedData
- this.total = totalCount
- } else {
- // 后续页,追加数据
- this.courseList = [...this.courseList, ...processedData]
- }
- // 判断是否还有更多数据:当前已加载数量 < 总数量
- this.hasMore = this.courseList.length < totalCount
-
- } else {
- // 如果API返回为空,使用模拟数据
- this.hasMore = false
- if (this.pageNum === 1) {
- this.courseList = this.mockCourses
- this.total = this.mockCourses.length
- }
- }
- } catch (error) {
-
- // 当API调用失败时,使用模拟数据
- if (this.pageNum === 1) {
- this.courseList = this.mockCourses
- this.total = this.mockCourses.length
- }
- this.hasMore = false
- } finally {
- this.loading = false
- }
- },
- // 切换标签页
- switchTab(tab) {
- this.activeTab = tab
- this.pageNum = 1
- this.courseList = []
- this.hasMore = true
- this.loadCourseList()
- },
- // 切换排序
- switchSort(sort) {
- this.activeSort = sort
- // 这里可以根据排序方式重新排序课程列表
- // 模拟排序效果
- if (this.activeSort === 'popular') {
- this.courseList.sort((a, b) => (b.student_count || 0) - (a.student_count || 0))
- } else if (this.activeSort === 'latest') {
- // 假设id越大越新
- this.courseList.sort((a, b) => b.id - a.id)
- } else {
- // 推荐排序
- if (this.pageNum === 1) {
- this.loadCourseList()
- }
- }
- },
- // 跳转到详情
- goToDetail(id) {
-
- uni.navigateTo({
- url: `/pages/courses/detail?id=${id}`,
- success: () => {
-
- },
- fail: (err) => {
-
- uni.showToast({
- title: '跳转失败',
- icon: 'none'
- })
- }
- })
- },
- // 返回
- goBack() {
-
- uni.navigateBack({
- fail: () => {
- // 返回失败时跳转首页
- uni.navigateTo({
- url: '/pages/index/index'
- })
- }
- })
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .courses-page {
- min-height: 100vh;
- background-color: #FFF9F9;
- padding-top: 90rpx;
- }
- /* 自定义导航栏 */
- .custom-navbar {
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- height: 90rpx;
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 0 20rpx;
- background: linear-gradient(135deg, #FCE4EC 0%, #F8BBD0 100%);
- z-index: 999;
- .navbar-left,
- .navbar-right {
- width: 80rpx;
- }
- .back-icon {
- font-size: 40rpx;
- color: #333333;
- font-weight: bold;
- }
- .navbar-title {
- flex: 1;
- text-align: center;
- font-size: 32rpx;
- font-weight: bold;
- color: #333333;
- }
- }
- /* 课程分类标签 */
- .course-tabs {
- display: flex;
- align-items: center;
- justify-content: space-around;
- padding: 20rpx 0;
- background-color: #FFFFFF;
- border-bottom: 1rpx solid #F0F0F0;
- .tab-item {
- padding: 12rpx 24rpx;
- font-size: 28rpx;
- color: #666666;
- border-radius: 30rpx;
- transition: all 0.3s;
- &.active {
- background-color: #9C27B0;
- color: #FFFFFF;
- }
- }
- }
- /* 排序选项 */
- .sort-options {
- display: flex;
- align-items: center;
- justify-content: space-around;
- padding: 20rpx 0;
- background-color: #FFFFFF;
- border-bottom: 1rpx solid #F0F0F0;
- .sort-item {
- font-size: 26rpx;
- color: #666666;
- transition: all 0.3s;
- &.active {
- color: #9C27B0;
- font-weight: bold;
- }
- }
- }
- /* 课程列表 - 两列布局 */
- .course-grid {
- display: grid;
- grid-template-columns: 1fr 1fr;
- gap: 20rpx;
- padding: 20rpx;
- .course-card {
- position: relative;
- background-color: #FFFFFF;
- border-radius: 20rpx;
- overflow: hidden;
- box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
- .course-image {
- width: 100%;
- height: 240rpx;
- background-color: #F5F5F5;
- }
- .course-info {
- padding: 20rpx;
- .course-name {
- font-size: 28rpx;
- font-weight: bold;
- color: #333333;
- margin-bottom: 10rpx;
- display: -webkit-box;
- -webkit-box-orient: vertical;
- -webkit-line-clamp: 2;
- overflow: hidden;
- }
- .course-meta {
- display: flex;
- justify-content: space-between;
- font-size: 24rpx;
- color: #666666;
- margin-bottom: 10rpx;
- }
- .course-footer {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 15rpx;
- .course-price {
- color: #E91E63;
- font-weight: bold;
- .price-symbol {
- font-size: 22rpx;
- }
- .price-value {
- font-size: 32rpx;
- }
- }
- .course-students {
- font-size: 22rpx;
- color: #999999;
- }
- }
- .course-btn {
- width: 100%;
- height: 60rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- background-color: #9C27B0;
- color: #FFFFFF;
- border-radius: 30rpx;
- font-size: 26rpx;
- }
- }
- }
- }
- /* 加载更多 */
- .load-more,
- .no-more {
- padding: 30rpx 0;
- text-align: center;
- .load-text,
- .no-more-text {
- font-size: 24rpx;
- color: #999999;
- }
- }
- </style>
|