| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- <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 class="points-info" @click="goToPointsDetail">
- <text class="points-icon">💎</text>
- <text class="points-value">{{ currentPoints }}</text>
- </view>
- </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="purchased-tag" v-if="item.isPurchased">已兑换</view>
- <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="points-icon">💎</text>
- <text class="price-value">{{ item.points_price || item.price * 10 }}</text>
- <text class="price-unit">积分</text>
- </view>
- <text class="course-students">{{ item.student_count || 0 }}人学习</text>
- </view>
- <view class="course-btn" :class="{ purchased: item.isPurchased }" @click.stop="handleExchange(item)">
- {{ item.isPurchased ? '立即学习' : '积分兑换' }}
- </view>
- </view>
- </view>
- </view>
- <!-- 加载更多 -->
- <view class="load-more" v-if="hasMore">
- <text class="load-text">{{ loading ? '加载中...' : '上拉加载更多' }}</text>
- </view>
- <view class="no-more" v-else-if="courseList.length > 0">
- <text class="no-more-text">没有更多了</text>
- </view>
- <view class="empty-state" v-else-if="!loading">
- <text class="empty-text">暂无课程</text>
- </view>
- </view>
- </template>
- <script>
- import api from '@/utils/api.js'
- import { DEFAULT_IMAGES } from '@/config/index.js'
- export default {
- data() {
- return {
- courseList: [],
- pageNum: 1,
- pageSize: 10,
- total: 0,
- hasMore: true,
- loading: false,
- DEFAULT_IMAGES,
- activeTab: 'all',
- activeSort: 'recommend',
- currentPoints: 0,
- makerId: null,
- purchasedCourseIds: []
- }
- },
- onLoad() {
-
- this.loadMakerInfo()
- },
- onShow() {
- // 每次显示页面时刷新积分
- this.loadCurrentPoints()
- },
- onReachBottom() {
- if (this.hasMore && !this.loading) {
- this.pageNum++
- this.loadCourseList()
- }
- },
- methods: {
- // 加载红娘信息
- async loadMakerInfo() {
- try {
- const userInfo = uni.getStorageSync('userInfo')
- if (userInfo && userInfo.userId) {
- const matchmakerInfo = await api.matchmaker.getByUserId(userInfo.userId)
- if (matchmakerInfo) {
- this.makerId = matchmakerInfo.matchmakerId || matchmakerInfo.matchmaker_id
-
- this.loadCurrentPoints()
- this.loadPurchasedCourses()
- this.loadCourseList()
- }
- }
- } catch (error) {
-
- this.loadCourseList()
- }
- },
- // 加载当前积分
- async loadCurrentPoints() {
- if (!this.makerId) return
- try {
- const res = await api.pointsMall.getBalance(this.makerId)
- this.currentPoints = res || 0
-
- } catch (error) {
-
- }
- },
- // 加载已兑换的课程
- async loadPurchasedCourses() {
- if (!this.makerId) return
- try {
- // 调用红娘课程API获取已兑换的课程ID列表
- const res = await api.matchmakerCourse.getPurchasedList(this.makerId)
- if (res && Array.isArray(res)) {
- this.purchasedCourseIds = res.map(item => item.course_id || item.courseId || item.id)
- }
-
- } catch (error) {
-
- }
- },
- // 加载课程列表
- async loadCourseList() {
- if (this.loading) return
- this.loading = true
- try {
-
- // 使用红娘课程独立API,根据当前tab传入分类类型
- const response = await api.matchmakerCourse.getList({
- categoryType: this.activeTab
- })
- let courseData = []
- let totalCount = 0
- if (response && response.list) {
- 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,
- points_price: item.points || (item.price ? item.price * 10 : 100),
- isPurchased: this.purchasedCourseIds.includes(item.id)
- }))
- if (this.pageNum === 1) {
- this.courseList = processedData
- this.total = totalCount
- } else {
- this.courseList = [...this.courseList, ...processedData]
- }
- this.hasMore = this.courseList.length < totalCount
- } else {
- this.hasMore = false
- if (this.pageNum === 1) {
- this.courseList = this.getMockCourses()
- this.total = this.courseList.length
- }
- }
- } catch (error) {
-
- if (this.pageNum === 1) {
- this.courseList = this.getMockCourses()
- this.total = this.courseList.length
- }
- this.hasMore = false
- } finally {
- this.loading = false
- }
- },
- // 模拟数据
- getMockCourses() {
- return [
-
- ]
- },
- // 切换标签页
- 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') {
- this.courseList.sort((a, b) => b.id - a.id)
- }
- },
- // 处理兑换
- handleExchange(item) {
- if (item.isPurchased) {
- // 已兑换,跳转学习
- this.goToDetail(item.id)
- return
- }
- const pointsNeeded = item.points_price || item.price * 10
-
- if (this.currentPoints < pointsNeeded) {
- uni.showModal({
- title: '积分不足',
- content: `兑换该课程需要${pointsNeeded}积分,您当前只有${this.currentPoints}积分`,
- showCancel: true,
- cancelText: '取消',
- confirmText: '去赚积分',
- success: (res) => {
- if (res.confirm) {
- uni.navigateTo({
- url: '/pages/matchmaker-workbench/earn-points'
- })
- }
- }
- })
- return
- }
- uni.showModal({
- title: '确认兑换',
- content: `确定使用${pointsNeeded}积分兑换课程"${item.name}"吗?`,
- success: async (res) => {
- if (res.confirm) {
- await this.doExchange(item, pointsNeeded)
- }
- }
- })
- },
- // 执行兑换
- async doExchange(item, pointsNeeded) {
- uni.showLoading({ title: '兑换中...' })
- try {
- // 调用红娘课程兑换API
- const res = await api.matchmakerCourse.exchange({
- makerId: this.makerId,
- courseId: item.id,
- points: pointsNeeded
- })
- if (res) {
- uni.hideLoading()
- uni.showToast({
- title: '兑换成功',
- icon: 'success'
- })
-
- // 更新状态
- item.isPurchased = true
- this.purchasedCourseIds.push(item.id)
- this.currentPoints -= pointsNeeded
-
- // 跳转到课程详情
- setTimeout(() => {
- this.goToDetail(item.id)
- }, 1500)
- }
- } catch (error) {
- uni.hideLoading()
-
- uni.showToast({
- title: error.msg || '兑换失败',
- icon: 'none'
- })
- }
- },
- // 跳转到详情
- goToDetail(id) {
- uni.navigateTo({
- url: `/pages/matchmaker-workbench/course-detail?id=${id}`
- })
- },
- // 跳转积分明细
- goToPointsDetail() {
- uni.navigateTo({
- url: '/pages/matchmaker-workbench/points-detail'
- })
- },
- // 返回
- goBack() {
- uni.navigateBack({
- fail: () => {
- uni.navigateTo({
- url: '/pages/matchmaker-workbench/mine'
- })
- }
- })
- }
- }
- }
- </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: 120rpx;
- }
- .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;
- }
- .points-info {
- display: flex;
- align-items: center;
- background: rgba(255, 255, 255, 0.8);
- padding: 8rpx 16rpx;
- border-radius: 30rpx;
- .points-icon {
- font-size: 24rpx;
- margin-right: 6rpx;
- }
- .points-value {
- font-size: 26rpx;
- color: #E91E63;
- font-weight: bold;
- }
- }
- }
- /* 课程分类标签 */
- .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;
- }
- .purchased-tag {
- position: absolute;
- top: 16rpx;
- right: 16rpx;
- background: linear-gradient(135deg, #4CAF50 0%, #8BC34A 100%);
- color: #FFFFFF;
- font-size: 22rpx;
- padding: 6rpx 16rpx;
- border-radius: 20rpx;
- }
- .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 {
- display: flex;
- align-items: center;
- color: #E91E63;
- font-weight: bold;
- .points-icon {
- font-size: 22rpx;
- margin-right: 4rpx;
- }
- .price-value {
- font-size: 32rpx;
- }
- .price-unit {
- font-size: 22rpx;
- margin-left: 4rpx;
- }
- }
- .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;
- &.purchased {
- background: linear-gradient(135deg, #4CAF50 0%, #8BC34A 100%);
- }
- }
- }
- }
- }
- /* 加载更多 */
- .load-more,
- .no-more {
- padding: 30rpx 0;
- text-align: center;
- .load-text,
- .no-more-text {
- font-size: 24rpx;
- color: #999999;
- }
- }
- .empty-state {
- padding: 100rpx 0;
- text-align: center;
- .empty-text {
- font-size: 28rpx;
- color: #999999;
- }
- }
- </style>
|