detail.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <template>
  2. <view class="course-detail-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"></view>
  10. </view>
  11. <!-- 课程封面 -->
  12. <view class="course-cover">
  13. <image :src="course.cover_image" class="cover-image" mode="aspectFill"></image>
  14. <view class="cover-mask">
  15. <view class="discount-tag" v-if="course.discount_rate">限时{{ course.discount_rate }}折</view>
  16. </view>
  17. </view>
  18. <!-- 课程信息 -->
  19. <view class="course-content">
  20. <view class="course-title">{{ course.name }}</view>
  21. <view class="teacher-info">
  22. <image :src="course.teacher_avatar || DEFAULT_IMAGES.avatar" class="teacher-avatar" mode="aspectFill"></image>
  23. <view class="teacher-detail">
  24. <text class="teacher-name">{{ course.teacher_name }}</text>
  25. <text class="teacher-desc">{{ course.teacher_desc || '资深情感导师' }}</text>
  26. </view>
  27. </view>
  28. <view class="course-stats">
  29. <view class="stat-item">
  30. <text class="stat-icon">👥</text>
  31. <text class="stat-text">{{ course.student_count || 0 }}人学习</text>
  32. </view>
  33. <view class="stat-item">
  34. <text class="stat-icon">⭐</text>
  35. <text class="stat-text">{{ course.rating || '5.0' }}分</text>
  36. </view>
  37. <view class="stat-item">
  38. <text class="stat-icon">📚</text>
  39. <text class="stat-text">{{ course.chapter_count || 0 }}个章节</text>
  40. </view>
  41. </view>
  42. <view class="divider"></view>
  43. <view class="description-section">
  44. <view class="section-title">课程介绍</view>
  45. <view class="description-text">{{ course.description || '暂无介绍' }}</view>
  46. </view>
  47. <view class="divider"></view>
  48. <view class="outline-section">
  49. <view class="section-title">课程大纲</view>
  50. <view class="outline-text">{{ course.outline || '敬请期待' }}</view>
  51. </view>
  52. </view>
  53. <!-- 底部购买按钮 -->
  54. <view class="bottom-bar">
  55. <view class="price-info">
  56. <text class="price-label">课程价格</text>
  57. <view class="price-value">
  58. <text class="price-symbol">¥</text>
  59. <text class="price-num">{{ course.price || 0 }}</text>
  60. <text class="original-price" v-if="course.original_price">¥{{ course.original_price }}</text>
  61. <text class="discount-text" v-if="course.discount_rate">{{ course.discount_rate }}折优惠</text>
  62. </view>
  63. </view>
  64. <view class="purchase-btn" :class="{ purchased: course.purchase_status === 1 }" @click="handlePurchase">
  65. <text class="btn-text">{{ course.purchase_status === 1 ? '已购买' : '立即购买' }}</text>
  66. </view>
  67. </view>
  68. </view>
  69. </template>
  70. <script>
  71. import api from '@/utils/api.js'
  72. import { DEFAULT_IMAGES } from '@/config/index.js'
  73. import userAuth from '@/utils/userAuth.js'
  74. export default {
  75. data() {
  76. return {
  77. courseId: null,
  78. course: {},
  79. DEFAULT_IMAGES,
  80. gatewayURL: 'http://localhost:8083',
  81. currentUserId: null,
  82. isPurchased: false
  83. }
  84. },
  85. onLoad(options) {
  86. console.log('精品课程详情页面加载, 参数:', options)
  87. // 获取当前用户ID
  88. this.currentUserId = userAuth.getUserId()
  89. console.log('当前用户ID:', this.currentUserId)
  90. if (options.id) {
  91. this.courseId = options.id
  92. console.log('课程ID:', this.courseId)
  93. // 根据ID设置对应的课程数据
  94. this.setCourseDataById(this.courseId)
  95. this.loadCourseDetail()
  96. // 检查是否已购买
  97. this.checkPurchaseStatus()
  98. } else {
  99. console.log('未传入课程ID,使用默认数据')
  100. }
  101. // 数据健康检查(确保页面显示正常)
  102. setTimeout(() => {
  103. this.checkDataHealth()
  104. }, 100)
  105. },
  106. methods: {
  107. // 根据ID设置课程数据
  108. setCourseDataById(courseId) {
  109. const courseData = {}
  110. const targetCourse = courseData[courseId]
  111. if (targetCourse) {
  112. this.course = {
  113. ...this.course,
  114. ...targetCourse,
  115. cover_image: DEFAULT_IMAGES.course,
  116. teacher_avatar: DEFAULT_IMAGES.avatar
  117. }
  118. console.log('设置课程数据:', this.course.name)
  119. }
  120. },
  121. // 加载课程详情
  122. async loadCourseDetail() {
  123. try {
  124. console.log('尝试从API加载课程详情:', this.courseId)
  125. const data = await api.course.getDetail(this.courseId)
  126. if (data) {
  127. this.course = {
  128. ...data,
  129. purchase_status: 0 // 初始化为未购买,由 checkPurchaseStatus 判断
  130. }
  131. console.log('API课程详情加载成功, purchase_status:', this.course.purchase_status)
  132. } else {
  133. console.log('API返回空数据,使用默认课程详情')
  134. }
  135. } catch (error) {
  136. console.error('加载课程详情失败:', error)
  137. console.log('API调用失败,保留默认课程详情数据')
  138. uni.showToast({
  139. title: '使用示例数据',
  140. icon: 'none',
  141. duration: 1500
  142. })
  143. }
  144. },
  145. // 检查购买状态
  146. async checkPurchaseStatus() {
  147. if (!this.currentUserId || !this.courseId) return
  148. try {
  149. const purchased = await api.courseOrder.checkPurchased(this.currentUserId, this.courseId)
  150. console.log('检查购买状态结果:', purchased, typeof purchased)
  151. // 只有明确返回 true 时才设置为已购买
  152. if (purchased === true) {
  153. this.isPurchased = true
  154. this.course.purchase_status = 1
  155. }
  156. } catch (error) {
  157. console.log('检查购买状态失败:', error)
  158. // 失败时不改变购买状态,默认为未购买
  159. }
  160. },
  161. // 处理购买
  162. handlePurchase() {
  163. // 检查是否已购买
  164. if (this.course.purchase_status === 1 || this.isPurchased) {
  165. uni.showToast({
  166. title: '您已购买过此课程',
  167. icon: 'none'
  168. })
  169. return
  170. }
  171. // 检查是否登录
  172. if (!this.currentUserId) {
  173. uni.showModal({
  174. title: '提示',
  175. content: '请先登录后再购买课程',
  176. confirmText: '去登录',
  177. success: (res) => {
  178. if (res.confirm) {
  179. uni.navigateTo({
  180. url: '/pages/page3/page3'
  181. })
  182. }
  183. }
  184. })
  185. return
  186. }
  187. uni.showModal({
  188. title: '确认购买',
  189. content: `确认购买"${this.course.name}"课程吗?\n价格:¥${this.course.price}`,
  190. success: (res) => {
  191. if (res.confirm) {
  192. this.requestPay()
  193. }
  194. }
  195. })
  196. },
  197. // 请求支付参数并调起微信支付
  198. requestPay() {
  199. uni.showLoading({
  200. title: '处理中...'
  201. })
  202. uni.request({
  203. url: this.gatewayURL + '/api/course-order/purchase',
  204. method: 'POST',
  205. data: {
  206. userId: this.currentUserId,
  207. courseId: parseInt(this.courseId),
  208. courseName: this.course.name,
  209. price: this.course.price
  210. },
  211. header: {
  212. 'content-type': 'application/json',
  213. 'token': uni.getStorageSync('token')
  214. },
  215. success: (res) => {
  216. uni.hideLoading()
  217. if (res.data && res.data.code === 200) {
  218. const payParams = res.data.data
  219. this.callWxPay(payParams)
  220. } else {
  221. uni.showToast({
  222. title: res.data?.message || res.data?.msg || '获取支付参数失败',
  223. icon: 'none'
  224. })
  225. }
  226. },
  227. fail: (err) => {
  228. uni.hideLoading()
  229. console.error('请求支付参数失败:', err)
  230. uni.showToast({
  231. title: '网络请求失败',
  232. icon: 'none'
  233. })
  234. }
  235. })
  236. },
  237. // 调起微信支付
  238. callWxPay(payParams) {
  239. uni.requestPayment({
  240. provider: 'wxpay',
  241. timeStamp: payParams.timeStamp,
  242. nonceStr: payParams.nonceStr,
  243. package: payParams.package,
  244. signType: payParams.signType,
  245. paySign: payParams.paySign,
  246. success: (payResult) => {
  247. if (payResult.errMsg === 'requestPayment:ok') {
  248. uni.showToast({
  249. title: '购买成功!',
  250. icon: 'success',
  251. duration: 2000
  252. })
  253. // 更新购买状态
  254. this.course.purchase_status = 1
  255. this.isPurchased = true
  256. this.course.student_count = (this.course.student_count || 0) + 1
  257. setTimeout(() => {
  258. uni.showModal({
  259. title: '购买成功',
  260. content: '您已成功购买此课程,现在可以开始学习了!',
  261. showCancel: false,
  262. confirmText: '开始学习',
  263. success: (modalRes) => {
  264. if (modalRes.confirm) {
  265. uni.showToast({
  266. title: '学习功能开发中',
  267. icon: 'none'
  268. })
  269. }
  270. }
  271. })
  272. }, 2000)
  273. }
  274. },
  275. fail: (payError) => {
  276. console.error('支付失败:', payError)
  277. if (payError.errMsg.includes('cancel')) {
  278. uni.showToast({
  279. title: '已取消支付',
  280. icon: 'none'
  281. })
  282. } else {
  283. uni.showToast({
  284. title: `支付失败:${payError.errMsg}`,
  285. icon: 'none'
  286. })
  287. }
  288. }
  289. })
  290. },
  291. // 返回
  292. goBack() {
  293. console.log('课程详情返回上一页')
  294. uni.navigateBack({
  295. fail: () => {
  296. // 返回失败时跳转到课程列表
  297. uni.navigateTo({
  298. url: '/pages/courses/list'
  299. })
  300. }
  301. })
  302. },
  303. // 数据健康检查
  304. checkDataHealth() {
  305. console.log('=== 精品课程详情页面数据健康检查 ===')
  306. console.log('courseId:', this.courseId)
  307. console.log('course:', this.course)
  308. console.log('DEFAULT_IMAGES:', this.DEFAULT_IMAGES)
  309. // 确保关键数据不为空
  310. if (!this.course.name || this.course.name === '加载中...') {
  311. this.course.name = '恋爱沟通技巧大师课'
  312. }
  313. if (!this.course.teacher_name) {
  314. this.course.teacher_name = '张情感老师'
  315. }
  316. if (!this.course.cover_image) {
  317. this.course.cover_image = this.DEFAULT_IMAGES.course
  318. }
  319. }
  320. }
  321. }
  322. </script>
  323. <style lang="scss" scoped>
  324. .course-detail-page {
  325. min-height: 100vh;
  326. background-color: #FFF9F9;
  327. padding-top: 90rpx;
  328. padding-bottom: 120rpx;
  329. }
  330. /* 自定义导航栏 */
  331. .custom-navbar {
  332. position: fixed;
  333. top: 0;
  334. left: 0;
  335. right: 0;
  336. height: 90rpx;
  337. display: flex;
  338. align-items: center;
  339. justify-content: space-between;
  340. padding: 0 20rpx;
  341. background-color: #E91E63;
  342. z-index: 999;
  343. .navbar-left,
  344. .navbar-right {
  345. width: 80rpx;
  346. }
  347. .back-icon {
  348. font-size: 40rpx;
  349. color: #FFFFFF;
  350. font-weight: bold;
  351. }
  352. .navbar-title {
  353. flex: 1;
  354. text-align: center;
  355. font-size: 32rpx;
  356. font-weight: bold;
  357. color: #FFFFFF;
  358. }
  359. }
  360. /* 课程封面 */
  361. .course-cover {
  362. position: relative;
  363. width: 100%;
  364. height: 500rpx;
  365. .cover-image {
  366. width: 100%;
  367. height: 100%;
  368. }
  369. .cover-mask {
  370. position: absolute;
  371. top: 0;
  372. left: 0;
  373. right: 0;
  374. bottom: 0;
  375. background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3), transparent);
  376. padding: 30rpx;
  377. .discount-tag {
  378. display: inline-block;
  379. padding: 10rpx 20rpx;
  380. background: linear-gradient(135deg, #FF6B6B 0%, #FF9800 100%);
  381. color: #FFFFFF;
  382. font-size: 24rpx;
  383. font-weight: bold;
  384. border-radius: 30rpx;
  385. }
  386. }
  387. }
  388. /* 课程内容 */
  389. .course-content {
  390. padding: 30rpx;
  391. background-color: #FFFFFF;
  392. margin: 20rpx;
  393. border-radius: 20rpx;
  394. .course-title {
  395. font-size: 36rpx;
  396. font-weight: bold;
  397. color: #333333;
  398. margin-bottom: 30rpx;
  399. line-height: 1.5;
  400. }
  401. .teacher-info {
  402. display: flex;
  403. align-items: center;
  404. margin-bottom: 30rpx;
  405. .teacher-avatar {
  406. width: 80rpx;
  407. height: 80rpx;
  408. border-radius: 50%;
  409. margin-right: 20rpx;
  410. }
  411. .teacher-detail {
  412. flex: 1;
  413. display: flex;
  414. flex-direction: column;
  415. gap: 5rpx;
  416. .teacher-name {
  417. font-size: 28rpx;
  418. font-weight: bold;
  419. color: #333333;
  420. }
  421. .teacher-desc {
  422. font-size: 24rpx;
  423. color: #999999;
  424. }
  425. }
  426. }
  427. .course-stats {
  428. display: flex;
  429. justify-content: space-around;
  430. padding: 20rpx 0;
  431. background-color: #FFF9F9;
  432. border-radius: 15rpx;
  433. .stat-item {
  434. display: flex;
  435. flex-direction: column;
  436. align-items: center;
  437. gap: 10rpx;
  438. .stat-icon {
  439. font-size: 32rpx;
  440. }
  441. .stat-text {
  442. font-size: 24rpx;
  443. color: #666666;
  444. }
  445. }
  446. }
  447. .divider {
  448. height: 1rpx;
  449. background-color: #F0F0F0;
  450. margin: 30rpx 0;
  451. }
  452. .section-title {
  453. font-size: 30rpx;
  454. font-weight: bold;
  455. color: #333333;
  456. margin-bottom: 20rpx;
  457. }
  458. .description-text,
  459. .outline-text {
  460. font-size: 28rpx;
  461. color: #666666;
  462. line-height: 1.8;
  463. white-space: pre-wrap;
  464. }
  465. }
  466. /* 底部购买按钮 */
  467. .bottom-bar {
  468. position: fixed;
  469. bottom: 0;
  470. left: 0;
  471. right: 0;
  472. display: flex;
  473. align-items: center;
  474. justify-content: space-between;
  475. padding: 20rpx 30rpx;
  476. background-color: #FFFFFF;
  477. border-top: 1rpx solid #F0F0F0;
  478. z-index: 999;
  479. .price-info {
  480. flex: 1;
  481. .price-label {
  482. display: block;
  483. font-size: 24rpx;
  484. color: #999999;
  485. margin-bottom: 5rpx;
  486. }
  487. .price-value {
  488. display: flex;
  489. align-items: baseline;
  490. color: #E91E63;
  491. font-weight: bold;
  492. .price-symbol {
  493. font-size: 28rpx;
  494. }
  495. .price-num {
  496. font-size: 40rpx;
  497. }
  498. .original-price {
  499. margin-left: 10rpx;
  500. font-size: 24rpx;
  501. color: #999999;
  502. text-decoration: line-through;
  503. }
  504. .discount-text {
  505. margin-left: 15rpx;
  506. font-size: 22rpx;
  507. color: #FF9800;
  508. background: rgba(255, 152, 0, 0.1);
  509. padding: 5rpx 12rpx;
  510. border-radius: 15rpx;
  511. }
  512. }
  513. }
  514. .purchase-btn {
  515. padding: 20rpx 60rpx;
  516. background: linear-gradient(135deg, #E91E63 0%, #FF6B6B 100%);
  517. border-radius: 50rpx;
  518. box-shadow: 0 8rpx 16rpx rgba(233, 30, 99, 0.3);
  519. transition: all 0.3s;
  520. .btn-text {
  521. font-size: 32rpx;
  522. font-weight: bold;
  523. color: #FFFFFF;
  524. }
  525. &.purchased {
  526. background: #4CAF50;
  527. box-shadow: 0 8rpx 16rpx rgba(76, 175, 80, 0.3);
  528. .btn-text {
  529. color: #FFFFFF;
  530. }
  531. }
  532. &:active {
  533. transform: scale(0.95);
  534. }
  535. }
  536. }
  537. </style>