courses.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. this.loadMakerInfo()
  90. },
  91. onShow() {
  92. // 每次显示页面时刷新积分
  93. this.loadCurrentPoints()
  94. },
  95. onReachBottom() {
  96. if (this.hasMore && !this.loading) {
  97. this.pageNum++
  98. this.loadCourseList()
  99. }
  100. },
  101. methods: {
  102. // 加载红娘信息
  103. async loadMakerInfo() {
  104. try {
  105. const userInfo = uni.getStorageSync('userInfo')
  106. if (userInfo && userInfo.userId) {
  107. const matchmakerInfo = await api.matchmaker.getByUserId(userInfo.userId)
  108. if (matchmakerInfo) {
  109. this.makerId = matchmakerInfo.matchmakerId || matchmakerInfo.matchmaker_id
  110. this.loadCurrentPoints()
  111. this.loadPurchasedCourses()
  112. this.loadCourseList()
  113. }
  114. }
  115. } catch (error) {
  116. this.loadCourseList()
  117. }
  118. },
  119. // 加载当前积分
  120. async loadCurrentPoints() {
  121. if (!this.makerId) return
  122. try {
  123. const res = await api.pointsMall.getBalance(this.makerId)
  124. this.currentPoints = res || 0
  125. } catch (error) {
  126. }
  127. },
  128. // 加载已兑换的课程
  129. async loadPurchasedCourses() {
  130. if (!this.makerId) return
  131. try {
  132. // 调用红娘课程API获取已兑换的课程ID列表
  133. const res = await api.matchmakerCourse.getPurchasedList(this.makerId)
  134. if (res && Array.isArray(res)) {
  135. this.purchasedCourseIds = res.map(item => item.course_id || item.courseId || item.id)
  136. }
  137. } catch (error) {
  138. }
  139. },
  140. // 加载课程列表
  141. async loadCourseList() {
  142. if (this.loading) return
  143. this.loading = true
  144. try {
  145. // 使用红娘课程独立API,根据当前tab传入分类类型
  146. const response = await api.matchmakerCourse.getList({
  147. categoryType: this.activeTab
  148. })
  149. let courseData = []
  150. let totalCount = 0
  151. if (response && response.list) {
  152. courseData = response.list
  153. totalCount = response.total || 0
  154. } else if (Array.isArray(response)) {
  155. courseData = response
  156. totalCount = response.length
  157. }
  158. if (courseData && courseData.length > 0) {
  159. const processedData = courseData.map(item => ({
  160. ...item,
  161. cover_image: item.cover_image || item.coverImage || this.DEFAULT_IMAGES.course,
  162. teacher_name: item.teacher_name || item.instructor || '专业导师',
  163. student_count: item.student_count || item.participants || 0,
  164. points_price: item.points || (item.price ? item.price * 10 : 100),
  165. isPurchased: this.purchasedCourseIds.includes(item.id)
  166. }))
  167. if (this.pageNum === 1) {
  168. this.courseList = processedData
  169. this.total = totalCount
  170. } else {
  171. this.courseList = [...this.courseList, ...processedData]
  172. }
  173. this.hasMore = this.courseList.length < totalCount
  174. } else {
  175. this.hasMore = false
  176. if (this.pageNum === 1) {
  177. this.courseList = this.getMockCourses()
  178. this.total = this.courseList.length
  179. }
  180. }
  181. } catch (error) {
  182. if (this.pageNum === 1) {
  183. this.courseList = this.getMockCourses()
  184. this.total = this.courseList.length
  185. }
  186. this.hasMore = false
  187. } finally {
  188. this.loading = false
  189. }
  190. },
  191. // 模拟数据
  192. getMockCourses() {
  193. return [
  194. ]
  195. },
  196. // 切换标签页
  197. switchTab(tab) {
  198. this.activeTab = tab
  199. this.pageNum = 1
  200. this.courseList = []
  201. this.hasMore = true
  202. this.loadCourseList()
  203. },
  204. // 切换排序
  205. switchSort(sort) {
  206. this.activeSort = sort
  207. if (this.activeSort === 'popular') {
  208. this.courseList.sort((a, b) => (b.student_count || 0) - (a.student_count || 0))
  209. } else if (this.activeSort === 'latest') {
  210. this.courseList.sort((a, b) => b.id - a.id)
  211. }
  212. },
  213. // 处理兑换
  214. handleExchange(item) {
  215. if (item.isPurchased) {
  216. // 已兑换,跳转学习
  217. this.goToDetail(item.id)
  218. return
  219. }
  220. const pointsNeeded = item.points_price || item.price * 10
  221. if (this.currentPoints < pointsNeeded) {
  222. uni.showModal({
  223. title: '积分不足',
  224. content: `兑换该课程需要${pointsNeeded}积分,您当前只有${this.currentPoints}积分`,
  225. showCancel: true,
  226. cancelText: '取消',
  227. confirmText: '去赚积分',
  228. success: (res) => {
  229. if (res.confirm) {
  230. uni.navigateTo({
  231. url: '/pages/matchmaker-workbench/earn-points'
  232. })
  233. }
  234. }
  235. })
  236. return
  237. }
  238. uni.showModal({
  239. title: '确认兑换',
  240. content: `确定使用${pointsNeeded}积分兑换课程"${item.name}"吗?`,
  241. success: async (res) => {
  242. if (res.confirm) {
  243. await this.doExchange(item, pointsNeeded)
  244. }
  245. }
  246. })
  247. },
  248. // 执行兑换
  249. async doExchange(item, pointsNeeded) {
  250. uni.showLoading({ title: '兑换中...' })
  251. try {
  252. // 调用红娘课程兑换API
  253. const res = await api.matchmakerCourse.exchange({
  254. makerId: this.makerId,
  255. courseId: item.id,
  256. points: pointsNeeded
  257. })
  258. if (res) {
  259. uni.hideLoading()
  260. uni.showToast({
  261. title: '兑换成功',
  262. icon: 'success'
  263. })
  264. // 更新状态
  265. item.isPurchased = true
  266. this.purchasedCourseIds.push(item.id)
  267. this.currentPoints -= pointsNeeded
  268. // 跳转到课程详情
  269. setTimeout(() => {
  270. this.goToDetail(item.id)
  271. }, 1500)
  272. }
  273. } catch (error) {
  274. uni.hideLoading()
  275. uni.showToast({
  276. title: error.msg || '兑换失败',
  277. icon: 'none'
  278. })
  279. }
  280. },
  281. // 跳转到详情
  282. goToDetail(id) {
  283. uni.navigateTo({
  284. url: `/pages/matchmaker-workbench/course-detail?id=${id}`
  285. })
  286. },
  287. // 跳转积分明细
  288. goToPointsDetail() {
  289. uni.navigateTo({
  290. url: '/pages/matchmaker-workbench/points-detail'
  291. })
  292. },
  293. // 返回
  294. goBack() {
  295. uni.navigateBack({
  296. fail: () => {
  297. uni.navigateTo({
  298. url: '/pages/matchmaker-workbench/mine'
  299. })
  300. }
  301. })
  302. }
  303. }
  304. }
  305. </script>
  306. <style lang="scss" scoped>
  307. .courses-page {
  308. min-height: 100vh;
  309. background-color: #FFF9F9;
  310. padding-top: 90rpx;
  311. }
  312. /* 自定义导航栏 */
  313. .custom-navbar {
  314. position: fixed;
  315. top: 0;
  316. left: 0;
  317. right: 0;
  318. height: 90rpx;
  319. display: flex;
  320. align-items: center;
  321. justify-content: space-between;
  322. padding: 0 20rpx;
  323. background: linear-gradient(135deg, #FCE4EC 0%, #F8BBD0 100%);
  324. z-index: 999;
  325. .navbar-left,
  326. .navbar-right {
  327. width: 120rpx;
  328. }
  329. .back-icon {
  330. font-size: 40rpx;
  331. color: #333333;
  332. font-weight: bold;
  333. }
  334. .navbar-title {
  335. flex: 1;
  336. text-align: center;
  337. font-size: 32rpx;
  338. font-weight: bold;
  339. color: #333333;
  340. }
  341. .points-info {
  342. display: flex;
  343. align-items: center;
  344. background: rgba(255, 255, 255, 0.8);
  345. padding: 8rpx 16rpx;
  346. border-radius: 30rpx;
  347. .points-icon {
  348. font-size: 24rpx;
  349. margin-right: 6rpx;
  350. }
  351. .points-value {
  352. font-size: 26rpx;
  353. color: #E91E63;
  354. font-weight: bold;
  355. }
  356. }
  357. }
  358. /* 课程分类标签 */
  359. .course-tabs {
  360. display: flex;
  361. align-items: center;
  362. justify-content: space-around;
  363. padding: 20rpx 0;
  364. background-color: #FFFFFF;
  365. border-bottom: 1rpx solid #F0F0F0;
  366. .tab-item {
  367. padding: 12rpx 24rpx;
  368. font-size: 28rpx;
  369. color: #666666;
  370. border-radius: 30rpx;
  371. transition: all 0.3s;
  372. &.active {
  373. background-color: #9C27B0;
  374. color: #FFFFFF;
  375. }
  376. }
  377. }
  378. /* 排序选项 */
  379. .sort-options {
  380. display: flex;
  381. align-items: center;
  382. justify-content: space-around;
  383. padding: 20rpx 0;
  384. background-color: #FFFFFF;
  385. border-bottom: 1rpx solid #F0F0F0;
  386. .sort-item {
  387. font-size: 26rpx;
  388. color: #666666;
  389. transition: all 0.3s;
  390. &.active {
  391. color: #9C27B0;
  392. font-weight: bold;
  393. }
  394. }
  395. }
  396. /* 课程列表 - 两列布局 */
  397. .course-grid {
  398. display: grid;
  399. grid-template-columns: 1fr 1fr;
  400. gap: 20rpx;
  401. padding: 20rpx;
  402. .course-card {
  403. position: relative;
  404. background-color: #FFFFFF;
  405. border-radius: 20rpx;
  406. overflow: hidden;
  407. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  408. .course-image {
  409. width: 100%;
  410. height: 240rpx;
  411. background-color: #F5F5F5;
  412. }
  413. .purchased-tag {
  414. position: absolute;
  415. top: 16rpx;
  416. right: 16rpx;
  417. background: linear-gradient(135deg, #4CAF50 0%, #8BC34A 100%);
  418. color: #FFFFFF;
  419. font-size: 22rpx;
  420. padding: 6rpx 16rpx;
  421. border-radius: 20rpx;
  422. }
  423. .course-info {
  424. padding: 20rpx;
  425. .course-name {
  426. font-size: 28rpx;
  427. font-weight: bold;
  428. color: #333333;
  429. margin-bottom: 10rpx;
  430. display: -webkit-box;
  431. -webkit-box-orient: vertical;
  432. -webkit-line-clamp: 2;
  433. overflow: hidden;
  434. }
  435. .course-meta {
  436. display: flex;
  437. justify-content: space-between;
  438. font-size: 24rpx;
  439. color: #666666;
  440. margin-bottom: 10rpx;
  441. }
  442. .course-footer {
  443. display: flex;
  444. justify-content: space-between;
  445. align-items: center;
  446. margin-bottom: 15rpx;
  447. .course-price {
  448. display: flex;
  449. align-items: center;
  450. color: #E91E63;
  451. font-weight: bold;
  452. .points-icon {
  453. font-size: 22rpx;
  454. margin-right: 4rpx;
  455. }
  456. .price-value {
  457. font-size: 32rpx;
  458. }
  459. .price-unit {
  460. font-size: 22rpx;
  461. margin-left: 4rpx;
  462. }
  463. }
  464. .course-students {
  465. font-size: 22rpx;
  466. color: #999999;
  467. }
  468. }
  469. .course-btn {
  470. width: 100%;
  471. height: 60rpx;
  472. display: flex;
  473. align-items: center;
  474. justify-content: center;
  475. background-color: #9C27B0;
  476. color: #FFFFFF;
  477. border-radius: 30rpx;
  478. font-size: 26rpx;
  479. &.purchased {
  480. background: linear-gradient(135deg, #4CAF50 0%, #8BC34A 100%);
  481. }
  482. }
  483. }
  484. }
  485. }
  486. /* 加载更多 */
  487. .load-more,
  488. .no-more {
  489. padding: 30rpx 0;
  490. text-align: center;
  491. .load-text,
  492. .no-more-text {
  493. font-size: 24rpx;
  494. color: #999999;
  495. }
  496. }
  497. .empty-state {
  498. padding: 100rpx 0;
  499. text-align: center;
  500. .empty-text {
  501. font-size: 28rpx;
  502. color: #999999;
  503. }
  504. }
  505. </style>