courses.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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">
  10. <view class="points-info" @click="goToPointsDetail">
  11. <text class="points-icon">💎</text>
  12. <text class="points-value">{{ currentPoints }}</text>
  13. </view>
  14. </view>
  15. </view>
  16. <!-- 课程分类标签 -->
  17. <view class="course-tabs">
  18. <view class="tab-item" :class="{ active: activeTab === 'all' }" @click="switchTab('all')">全部课程</view>
  19. <view class="tab-item" :class="{ active: activeTab === 'basic' }" @click="switchTab('basic')">基础课程</view>
  20. <view class="tab-item" :class="{ active: activeTab === 'advanced' }" @click="switchTab('advanced')">进阶课程</view>
  21. <view class="tab-item" :class="{ active: activeTab === 'premium' }" @click="switchTab('premium')">精品课程</view>
  22. </view>
  23. <!-- 排序选项 -->
  24. <view class="sort-options">
  25. <view class="sort-item" :class="{ active: activeSort === 'recommend' }" @click="switchSort('recommend')">推荐排序</view>
  26. <view class="sort-item" :class="{ active: activeSort === 'latest' }" @click="switchSort('latest')">最新课程</view>
  27. <view class="sort-item" :class="{ active: activeSort === 'popular' }" @click="switchSort('popular')">热门课程</view>
  28. </view>
  29. <!-- 课程列表 -->
  30. <view class="course-grid">
  31. <view class="course-card" v-for="(item, index) in courseList" :key="index"
  32. @click="goToDetail(item.id)">
  33. <image :src="item.cover_image" class="course-image" mode="aspectFill"></image>
  34. <!-- 已兑换标签 -->
  35. <view class="purchased-tag" v-if="item.isPurchased">已兑换</view>
  36. <view class="course-info">
  37. <view class="course-name">{{ item.name }}</view>
  38. <view class="course-meta">
  39. <text class="course-teacher">{{ item.teacher_name }}</text>
  40. <text class="course-rating">⭐ {{ item.rating || 4.5 }}</text>
  41. </view>
  42. <view class="course-footer">
  43. <view class="course-price">
  44. <text class="points-icon">💎</text>
  45. <text class="price-value">{{ item.points_price || item.price * 10 }}</text>
  46. <text class="price-unit">积分</text>
  47. </view>
  48. <text class="course-students">{{ item.student_count || 0 }}人学习</text>
  49. </view>
  50. <view class="course-btn" :class="{ purchased: item.isPurchased }" @click.stop="handleExchange(item)">
  51. {{ item.isPurchased ? '立即学习' : '积分兑换' }}
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. <!-- 加载更多 -->
  57. <view class="load-more" v-if="hasMore">
  58. <text class="load-text">{{ loading ? '加载中...' : '上拉加载更多' }}</text>
  59. </view>
  60. <view class="no-more" v-else-if="courseList.length > 0">
  61. <text class="no-more-text">没有更多了</text>
  62. </view>
  63. <view class="empty-state" v-else-if="!loading">
  64. <text class="empty-text">暂无课程</text>
  65. </view>
  66. </view>
  67. </template>
  68. <script>
  69. import api from '@/utils/api.js'
  70. import { DEFAULT_IMAGES } from '@/config/index.js'
  71. export default {
  72. data() {
  73. return {
  74. courseList: [],
  75. pageNum: 1,
  76. pageSize: 10,
  77. total: 0,
  78. hasMore: true,
  79. loading: false,
  80. DEFAULT_IMAGES,
  81. activeTab: 'all',
  82. activeSort: 'recommend',
  83. currentPoints: 0,
  84. makerId: null,
  85. purchasedCourseIds: []
  86. }
  87. },
  88. onLoad() {
  89. console.log('红娘培训课程页面加载')
  90. this.loadMakerInfo()
  91. },
  92. onShow() {
  93. // 每次显示页面时刷新积分
  94. this.loadCurrentPoints()
  95. },
  96. onReachBottom() {
  97. if (this.hasMore && !this.loading) {
  98. this.pageNum++
  99. this.loadCourseList()
  100. }
  101. },
  102. methods: {
  103. // 加载红娘信息
  104. async loadMakerInfo() {
  105. try {
  106. const userInfo = uni.getStorageSync('userInfo')
  107. if (userInfo && userInfo.userId) {
  108. const matchmakerInfo = await api.matchmaker.getByUserId(userInfo.userId)
  109. if (matchmakerInfo) {
  110. this.makerId = matchmakerInfo.matchmakerId || matchmakerInfo.matchmaker_id
  111. console.log('红娘ID:', this.makerId)
  112. this.loadCurrentPoints()
  113. this.loadPurchasedCourses()
  114. this.loadCourseList()
  115. }
  116. }
  117. } catch (error) {
  118. console.error('获取红娘信息失败:', error)
  119. this.loadCourseList()
  120. }
  121. },
  122. // 加载当前积分
  123. async loadCurrentPoints() {
  124. if (!this.makerId) return
  125. try {
  126. const res = await api.pointsMall.getBalance(this.makerId)
  127. this.currentPoints = res || 0
  128. console.log('当前积分:', this.currentPoints)
  129. } catch (error) {
  130. console.error('获取积分失败:', error)
  131. }
  132. },
  133. // 加载已兑换的课程
  134. async loadPurchasedCourses() {
  135. if (!this.makerId) return
  136. try {
  137. // 调用红娘课程API获取已兑换的课程ID列表
  138. const res = await api.matchmakerCourse.getPurchasedList(this.makerId)
  139. if (res && Array.isArray(res)) {
  140. this.purchasedCourseIds = res.map(item => item.course_id || item.courseId || item.id)
  141. }
  142. console.log('已兑换课程ID:', this.purchasedCourseIds)
  143. } catch (error) {
  144. console.error('获取已兑换课程失败:', error)
  145. }
  146. },
  147. // 加载课程列表
  148. async loadCourseList() {
  149. if (this.loading) return
  150. this.loading = true
  151. try {
  152. console.log('加载红娘培训课程列表...', this.activeTab)
  153. // 使用红娘课程独立API,根据当前tab传入分类类型
  154. const response = await api.matchmakerCourse.getList({
  155. categoryType: this.activeTab
  156. })
  157. let courseData = []
  158. let totalCount = 0
  159. if (response && response.list) {
  160. courseData = response.list
  161. totalCount = response.total || 0
  162. } else if (Array.isArray(response)) {
  163. courseData = response
  164. totalCount = response.length
  165. }
  166. if (courseData && courseData.length > 0) {
  167. const processedData = courseData.map(item => ({
  168. ...item,
  169. cover_image: item.cover_image || item.coverImage || this.DEFAULT_IMAGES.course,
  170. teacher_name: item.teacher_name || item.instructor || '专业导师',
  171. student_count: item.student_count || item.participants || 0,
  172. points_price: item.points || (item.price ? item.price * 10 : 100),
  173. isPurchased: this.purchasedCourseIds.includes(item.id)
  174. }))
  175. if (this.pageNum === 1) {
  176. this.courseList = processedData
  177. this.total = totalCount
  178. } else {
  179. this.courseList = [...this.courseList, ...processedData]
  180. }
  181. this.hasMore = this.courseList.length < totalCount
  182. } else {
  183. this.hasMore = false
  184. if (this.pageNum === 1) {
  185. this.courseList = this.getMockCourses()
  186. this.total = this.courseList.length
  187. }
  188. }
  189. } catch (error) {
  190. console.error('加载课程列表失败:', error)
  191. if (this.pageNum === 1) {
  192. this.courseList = this.getMockCourses()
  193. this.total = this.courseList.length
  194. }
  195. this.hasMore = false
  196. } finally {
  197. this.loading = false
  198. }
  199. },
  200. // 模拟数据
  201. getMockCourses() {
  202. return [
  203. {
  204. id: 1,
  205. name: '非暴力沟通:让问题更...',
  206. teacher_name: '王老师',
  207. rating: 4.9,
  208. points_price: 2990,
  209. student_count: 54,
  210. cover_image: 'https://images.unsplash.com/photo-1506744038136-46273834b3fb?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60',
  211. isPurchased: false
  212. },
  213. {
  214. id: 2,
  215. name: '红娘新手入门:相亲场...',
  216. teacher_name: '张老师',
  217. rating: 4.5,
  218. points_price: 890,
  219. student_count: 33,
  220. cover_image: 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60',
  221. isPurchased: false
  222. },
  223. {
  224. id: 3,
  225. name: '从零做红娘:牵线必备...',
  226. teacher_name: '高老师',
  227. rating: 4.9,
  228. points_price: 1990,
  229. student_count: 45,
  230. cover_image: 'https://images.unsplash.com/photo-1516321318423-f06f85e504b3?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60',
  231. isPurchased: false
  232. },
  233. {
  234. id: 4,
  235. name: '新手红娘必学:相亲沟...',
  236. teacher_name: '魏老师',
  237. rating: 4.4,
  238. points_price: 990,
  239. student_count: 54,
  240. cover_image: 'https://images.unsplash.com/photo-1502672260266-1c1ef2d93688?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60',
  241. isPurchased: false
  242. }
  243. ]
  244. },
  245. // 切换标签页
  246. switchTab(tab) {
  247. this.activeTab = tab
  248. this.pageNum = 1
  249. this.courseList = []
  250. this.hasMore = true
  251. this.loadCourseList()
  252. },
  253. // 切换排序
  254. switchSort(sort) {
  255. this.activeSort = sort
  256. if (this.activeSort === 'popular') {
  257. this.courseList.sort((a, b) => (b.student_count || 0) - (a.student_count || 0))
  258. } else if (this.activeSort === 'latest') {
  259. this.courseList.sort((a, b) => b.id - a.id)
  260. }
  261. },
  262. // 处理兑换
  263. handleExchange(item) {
  264. if (item.isPurchased) {
  265. // 已兑换,跳转学习
  266. this.goToDetail(item.id)
  267. return
  268. }
  269. const pointsNeeded = item.points_price || item.price * 10
  270. if (this.currentPoints < pointsNeeded) {
  271. uni.showModal({
  272. title: '积分不足',
  273. content: `兑换该课程需要${pointsNeeded}积分,您当前只有${this.currentPoints}积分`,
  274. showCancel: true,
  275. cancelText: '取消',
  276. confirmText: '去赚积分',
  277. success: (res) => {
  278. if (res.confirm) {
  279. uni.navigateTo({
  280. url: '/pages/matchmaker-workbench/earn-points'
  281. })
  282. }
  283. }
  284. })
  285. return
  286. }
  287. uni.showModal({
  288. title: '确认兑换',
  289. content: `确定使用${pointsNeeded}积分兑换课程"${item.name}"吗?`,
  290. success: async (res) => {
  291. if (res.confirm) {
  292. await this.doExchange(item, pointsNeeded)
  293. }
  294. }
  295. })
  296. },
  297. // 执行兑换
  298. async doExchange(item, pointsNeeded) {
  299. uni.showLoading({ title: '兑换中...' })
  300. try {
  301. // 调用红娘课程兑换API
  302. const res = await api.matchmakerCourse.exchange({
  303. makerId: this.makerId,
  304. courseId: item.id,
  305. points: pointsNeeded
  306. })
  307. if (res) {
  308. uni.hideLoading()
  309. uni.showToast({
  310. title: '兑换成功',
  311. icon: 'success'
  312. })
  313. // 更新状态
  314. item.isPurchased = true
  315. this.purchasedCourseIds.push(item.id)
  316. this.currentPoints -= pointsNeeded
  317. // 跳转到课程详情
  318. setTimeout(() => {
  319. this.goToDetail(item.id)
  320. }, 1500)
  321. }
  322. } catch (error) {
  323. uni.hideLoading()
  324. console.error('兑换失败:', error)
  325. uni.showToast({
  326. title: error.msg || '兑换失败',
  327. icon: 'none'
  328. })
  329. }
  330. },
  331. // 跳转到详情
  332. goToDetail(id) {
  333. uni.navigateTo({
  334. url: `/pages/matchmaker-workbench/course-detail?id=${id}`
  335. })
  336. },
  337. // 跳转积分明细
  338. goToPointsDetail() {
  339. uni.navigateTo({
  340. url: '/pages/matchmaker-workbench/points-detail'
  341. })
  342. },
  343. // 返回
  344. goBack() {
  345. uni.navigateBack({
  346. fail: () => {
  347. uni.navigateTo({
  348. url: '/pages/matchmaker-workbench/mine'
  349. })
  350. }
  351. })
  352. }
  353. }
  354. }
  355. </script>
  356. <style lang="scss" scoped>
  357. .courses-page {
  358. min-height: 100vh;
  359. background-color: #FFF9F9;
  360. padding-top: 90rpx;
  361. }
  362. /* 自定义导航栏 */
  363. .custom-navbar {
  364. position: fixed;
  365. top: 0;
  366. left: 0;
  367. right: 0;
  368. height: 90rpx;
  369. display: flex;
  370. align-items: center;
  371. justify-content: space-between;
  372. padding: 0 20rpx;
  373. background: linear-gradient(135deg, #FCE4EC 0%, #F8BBD0 100%);
  374. z-index: 999;
  375. .navbar-left,
  376. .navbar-right {
  377. width: 120rpx;
  378. }
  379. .back-icon {
  380. font-size: 40rpx;
  381. color: #333333;
  382. font-weight: bold;
  383. }
  384. .navbar-title {
  385. flex: 1;
  386. text-align: center;
  387. font-size: 32rpx;
  388. font-weight: bold;
  389. color: #333333;
  390. }
  391. .points-info {
  392. display: flex;
  393. align-items: center;
  394. background: rgba(255, 255, 255, 0.8);
  395. padding: 8rpx 16rpx;
  396. border-radius: 30rpx;
  397. .points-icon {
  398. font-size: 24rpx;
  399. margin-right: 6rpx;
  400. }
  401. .points-value {
  402. font-size: 26rpx;
  403. color: #E91E63;
  404. font-weight: bold;
  405. }
  406. }
  407. }
  408. /* 课程分类标签 */
  409. .course-tabs {
  410. display: flex;
  411. align-items: center;
  412. justify-content: space-around;
  413. padding: 20rpx 0;
  414. background-color: #FFFFFF;
  415. border-bottom: 1rpx solid #F0F0F0;
  416. .tab-item {
  417. padding: 12rpx 24rpx;
  418. font-size: 28rpx;
  419. color: #666666;
  420. border-radius: 30rpx;
  421. transition: all 0.3s;
  422. &.active {
  423. background-color: #9C27B0;
  424. color: #FFFFFF;
  425. }
  426. }
  427. }
  428. /* 排序选项 */
  429. .sort-options {
  430. display: flex;
  431. align-items: center;
  432. justify-content: space-around;
  433. padding: 20rpx 0;
  434. background-color: #FFFFFF;
  435. border-bottom: 1rpx solid #F0F0F0;
  436. .sort-item {
  437. font-size: 26rpx;
  438. color: #666666;
  439. transition: all 0.3s;
  440. &.active {
  441. color: #9C27B0;
  442. font-weight: bold;
  443. }
  444. }
  445. }
  446. /* 课程列表 - 两列布局 */
  447. .course-grid {
  448. display: grid;
  449. grid-template-columns: 1fr 1fr;
  450. gap: 20rpx;
  451. padding: 20rpx;
  452. .course-card {
  453. position: relative;
  454. background-color: #FFFFFF;
  455. border-radius: 20rpx;
  456. overflow: hidden;
  457. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  458. .course-image {
  459. width: 100%;
  460. height: 240rpx;
  461. background-color: #F5F5F5;
  462. }
  463. .purchased-tag {
  464. position: absolute;
  465. top: 16rpx;
  466. right: 16rpx;
  467. background: linear-gradient(135deg, #4CAF50 0%, #8BC34A 100%);
  468. color: #FFFFFF;
  469. font-size: 22rpx;
  470. padding: 6rpx 16rpx;
  471. border-radius: 20rpx;
  472. }
  473. .course-info {
  474. padding: 20rpx;
  475. .course-name {
  476. font-size: 28rpx;
  477. font-weight: bold;
  478. color: #333333;
  479. margin-bottom: 10rpx;
  480. display: -webkit-box;
  481. -webkit-box-orient: vertical;
  482. -webkit-line-clamp: 2;
  483. overflow: hidden;
  484. }
  485. .course-meta {
  486. display: flex;
  487. justify-content: space-between;
  488. font-size: 24rpx;
  489. color: #666666;
  490. margin-bottom: 10rpx;
  491. }
  492. .course-footer {
  493. display: flex;
  494. justify-content: space-between;
  495. align-items: center;
  496. margin-bottom: 15rpx;
  497. .course-price {
  498. display: flex;
  499. align-items: center;
  500. color: #E91E63;
  501. font-weight: bold;
  502. .points-icon {
  503. font-size: 22rpx;
  504. margin-right: 4rpx;
  505. }
  506. .price-value {
  507. font-size: 32rpx;
  508. }
  509. .price-unit {
  510. font-size: 22rpx;
  511. margin-left: 4rpx;
  512. }
  513. }
  514. .course-students {
  515. font-size: 22rpx;
  516. color: #999999;
  517. }
  518. }
  519. .course-btn {
  520. width: 100%;
  521. height: 60rpx;
  522. display: flex;
  523. align-items: center;
  524. justify-content: center;
  525. background-color: #9C27B0;
  526. color: #FFFFFF;
  527. border-radius: 30rpx;
  528. font-size: 26rpx;
  529. &.purchased {
  530. background: linear-gradient(135deg, #4CAF50 0%, #8BC34A 100%);
  531. }
  532. }
  533. }
  534. }
  535. }
  536. /* 加载更多 */
  537. .load-more,
  538. .no-more {
  539. padding: 30rpx 0;
  540. text-align: center;
  541. .load-text,
  542. .no-more-text {
  543. font-size: 24rpx;
  544. color: #999999;
  545. }
  546. }
  547. .empty-state {
  548. padding: 100rpx 0;
  549. text-align: center;
  550. .empty-text {
  551. font-size: 28rpx;
  552. color: #999999;
  553. }
  554. }
  555. </style>