detail.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. export default {
  74. data() {
  75. return {
  76. courseId: null,
  77. course: {},
  78. DEFAULT_IMAGES
  79. }
  80. },
  81. onLoad(options) {
  82. console.log('精品课程详情页面加载, 参数:', options)
  83. if (options.id) {
  84. this.courseId = options.id
  85. console.log('课程ID:', this.courseId)
  86. // 根据ID设置对应的课程数据
  87. this.setCourseDataById(this.courseId)
  88. this.loadCourseDetail()
  89. } else {
  90. console.log('未传入课程ID,使用默认数据')
  91. }
  92. // 数据健康检查(确保页面显示正常)
  93. setTimeout(() => {
  94. this.checkDataHealth()
  95. }, 100)
  96. },
  97. methods: {
  98. // 根据ID设置课程数据
  99. setCourseDataById(courseId) {
  100. const courseData = {}
  101. const targetCourse = courseData[courseId]
  102. if (targetCourse) {
  103. this.course = {
  104. ...this.course,
  105. ...targetCourse,
  106. cover_image: DEFAULT_IMAGES.course,
  107. teacher_avatar: DEFAULT_IMAGES.avatar
  108. }
  109. console.log('设置课程数据:', this.course.name)
  110. }
  111. },
  112. // 加载课程详情
  113. async loadCourseDetail() {
  114. try {
  115. console.log('尝试从API加载课程详情:', this.courseId)
  116. const data = await api.course.getDetail(this.courseId)
  117. if (data) {
  118. this.course = data
  119. console.log('API课程详情加载成功')
  120. } else {
  121. console.log('API返回空数据,使用默认课程详情')
  122. }
  123. } catch (error) {
  124. console.error('加载课程详情失败:', error)
  125. console.log('API调用失败,保留默认课程详情数据')
  126. uni.showToast({
  127. title: '使用示例数据',
  128. icon: 'none',
  129. duration: 1500
  130. })
  131. }
  132. },
  133. // 处理购买
  134. handlePurchase() {
  135. // 检查是否已购买
  136. if (this.course.purchase_status === 1) {
  137. uni.showToast({
  138. title: '您已购买过此课程',
  139. icon: 'none'
  140. })
  141. return
  142. }
  143. uni.showModal({
  144. title: '确认购买',
  145. content: `确认购买"${this.course.name}"课程吗?\n价格:¥${this.course.price}`,
  146. success: async (res) => {
  147. if (res.confirm) {
  148. try {
  149. console.log('尝试购买课程:', this.courseId)
  150. // await api.course.purchase(this.courseId, {})
  151. // 模拟购买成功
  152. uni.showToast({
  153. title: '购买成功!',
  154. icon: 'success'
  155. })
  156. // 更新购买状态
  157. this.course.purchase_status = 1
  158. this.course.student_count = (this.course.student_count || 0) + 1
  159. setTimeout(() => {
  160. uni.showModal({
  161. title: '购买成功',
  162. content: '您已成功购买此课程,现在可以开始学习了!',
  163. showCancel: false,
  164. confirmText: '开始学习',
  165. success: (modalRes) => {
  166. if (modalRes.confirm) {
  167. // TODO: 跳转到课程学习页面
  168. uni.showToast({
  169. title: '学习功能开发中',
  170. icon: 'none'
  171. })
  172. }
  173. }
  174. })
  175. }, 1500)
  176. } catch (error) {
  177. console.error('购买失败:', error)
  178. uni.showToast({
  179. title: '购买失败,请重试',
  180. icon: 'none'
  181. })
  182. }
  183. }
  184. }
  185. })
  186. },
  187. // 返回
  188. goBack() {
  189. console.log('课程详情返回上一页')
  190. uni.navigateBack({
  191. fail: () => {
  192. // 返回失败时跳转到课程列表
  193. uni.navigateTo({
  194. url: '/pages/courses/list'
  195. })
  196. }
  197. })
  198. },
  199. // 数据健康检查
  200. checkDataHealth() {
  201. console.log('=== 精品课程详情页面数据健康检查 ===')
  202. console.log('courseId:', this.courseId)
  203. console.log('course:', this.course)
  204. console.log('DEFAULT_IMAGES:', this.DEFAULT_IMAGES)
  205. // 确保关键数据不为空
  206. if (!this.course.name || this.course.name === '加载中...') {
  207. this.course.name = '恋爱沟通技巧大师课'
  208. }
  209. if (!this.course.teacher_name) {
  210. this.course.teacher_name = '张情感老师'
  211. }
  212. if (!this.course.cover_image) {
  213. this.course.cover_image = this.DEFAULT_IMAGES.course
  214. }
  215. }
  216. }
  217. }
  218. </script>
  219. <style lang="scss" scoped>
  220. .course-detail-page {
  221. min-height: 100vh;
  222. background-color: #FFF9F9;
  223. padding-top: 90rpx;
  224. padding-bottom: 120rpx;
  225. }
  226. /* 自定义导航栏 */
  227. .custom-navbar {
  228. position: fixed;
  229. top: 0;
  230. left: 0;
  231. right: 0;
  232. height: 90rpx;
  233. display: flex;
  234. align-items: center;
  235. justify-content: space-between;
  236. padding: 0 20rpx;
  237. background-color: #E91E63;
  238. z-index: 999;
  239. .navbar-left,
  240. .navbar-right {
  241. width: 80rpx;
  242. }
  243. .back-icon {
  244. font-size: 40rpx;
  245. color: #FFFFFF;
  246. font-weight: bold;
  247. }
  248. .navbar-title {
  249. flex: 1;
  250. text-align: center;
  251. font-size: 32rpx;
  252. font-weight: bold;
  253. color: #FFFFFF;
  254. }
  255. }
  256. /* 课程封面 */
  257. .course-cover {
  258. position: relative;
  259. width: 100%;
  260. height: 500rpx;
  261. .cover-image {
  262. width: 100%;
  263. height: 100%;
  264. }
  265. .cover-mask {
  266. position: absolute;
  267. top: 0;
  268. left: 0;
  269. right: 0;
  270. bottom: 0;
  271. background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3), transparent);
  272. padding: 30rpx;
  273. .discount-tag {
  274. display: inline-block;
  275. padding: 10rpx 20rpx;
  276. background: linear-gradient(135deg, #FF6B6B 0%, #FF9800 100%);
  277. color: #FFFFFF;
  278. font-size: 24rpx;
  279. font-weight: bold;
  280. border-radius: 30rpx;
  281. }
  282. }
  283. }
  284. /* 课程内容 */
  285. .course-content {
  286. padding: 30rpx;
  287. background-color: #FFFFFF;
  288. margin: 20rpx;
  289. border-radius: 20rpx;
  290. .course-title {
  291. font-size: 36rpx;
  292. font-weight: bold;
  293. color: #333333;
  294. margin-bottom: 30rpx;
  295. line-height: 1.5;
  296. }
  297. .teacher-info {
  298. display: flex;
  299. align-items: center;
  300. margin-bottom: 30rpx;
  301. .teacher-avatar {
  302. width: 80rpx;
  303. height: 80rpx;
  304. border-radius: 50%;
  305. margin-right: 20rpx;
  306. }
  307. .teacher-detail {
  308. flex: 1;
  309. display: flex;
  310. flex-direction: column;
  311. gap: 5rpx;
  312. .teacher-name {
  313. font-size: 28rpx;
  314. font-weight: bold;
  315. color: #333333;
  316. }
  317. .teacher-desc {
  318. font-size: 24rpx;
  319. color: #999999;
  320. }
  321. }
  322. }
  323. .course-stats {
  324. display: flex;
  325. justify-content: space-around;
  326. padding: 20rpx 0;
  327. background-color: #FFF9F9;
  328. border-radius: 15rpx;
  329. .stat-item {
  330. display: flex;
  331. flex-direction: column;
  332. align-items: center;
  333. gap: 10rpx;
  334. .stat-icon {
  335. font-size: 32rpx;
  336. }
  337. .stat-text {
  338. font-size: 24rpx;
  339. color: #666666;
  340. }
  341. }
  342. }
  343. .divider {
  344. height: 1rpx;
  345. background-color: #F0F0F0;
  346. margin: 30rpx 0;
  347. }
  348. .section-title {
  349. font-size: 30rpx;
  350. font-weight: bold;
  351. color: #333333;
  352. margin-bottom: 20rpx;
  353. }
  354. .description-text,
  355. .outline-text {
  356. font-size: 28rpx;
  357. color: #666666;
  358. line-height: 1.8;
  359. white-space: pre-wrap;
  360. }
  361. }
  362. /* 底部购买按钮 */
  363. .bottom-bar {
  364. position: fixed;
  365. bottom: 0;
  366. left: 0;
  367. right: 0;
  368. display: flex;
  369. align-items: center;
  370. justify-content: space-between;
  371. padding: 20rpx 30rpx;
  372. background-color: #FFFFFF;
  373. border-top: 1rpx solid #F0F0F0;
  374. z-index: 999;
  375. .price-info {
  376. flex: 1;
  377. .price-label {
  378. display: block;
  379. font-size: 24rpx;
  380. color: #999999;
  381. margin-bottom: 5rpx;
  382. }
  383. .price-value {
  384. display: flex;
  385. align-items: baseline;
  386. color: #E91E63;
  387. font-weight: bold;
  388. .price-symbol {
  389. font-size: 28rpx;
  390. }
  391. .price-num {
  392. font-size: 40rpx;
  393. }
  394. .original-price {
  395. margin-left: 10rpx;
  396. font-size: 24rpx;
  397. color: #999999;
  398. text-decoration: line-through;
  399. }
  400. .discount-text {
  401. margin-left: 15rpx;
  402. font-size: 22rpx;
  403. color: #FF9800;
  404. background: rgba(255, 152, 0, 0.1);
  405. padding: 5rpx 12rpx;
  406. border-radius: 15rpx;
  407. }
  408. }
  409. }
  410. .purchase-btn {
  411. padding: 20rpx 60rpx;
  412. background: linear-gradient(135deg, #E91E63 0%, #FF6B6B 100%);
  413. border-radius: 50rpx;
  414. box-shadow: 0 8rpx 16rpx rgba(233, 30, 99, 0.3);
  415. transition: all 0.3s;
  416. .btn-text {
  417. font-size: 32rpx;
  418. font-weight: bold;
  419. color: #FFFFFF;
  420. }
  421. &.purchased {
  422. background: #4CAF50;
  423. box-shadow: 0 8rpx 16rpx rgba(76, 175, 80, 0.3);
  424. .btn-text {
  425. color: #FFFFFF;
  426. }
  427. }
  428. &:active {
  429. transform: scale(0.95);
  430. }
  431. }
  432. }
  433. </style>