detail.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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: 'https://api.zhongruanke.cn',
  81. currentUserId: null,
  82. isPurchased: false
  83. }
  84. },
  85. onLoad(options) {
  86. this.currentUserId = userAuth.getUserId()
  87. if (options.id) {
  88. this.courseId = options.id
  89. this.setCourseDataById(this.courseId)
  90. this.loadCourseDetail()
  91. this.checkPurchaseStatus()
  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. }
  110. },
  111. // 加载课程详情
  112. async loadCourseDetail() {
  113. try {
  114. const data = await api.course.getDetail(this.courseId)
  115. if (data) {
  116. this.course = {
  117. ...data,
  118. purchase_status: 0 // 初始化为未购买,由 checkPurchaseStatus 判断
  119. }
  120. }
  121. } catch (error) {
  122. uni.showToast({
  123. title: '使用示例数据',
  124. icon: 'none',
  125. duration: 1500
  126. })
  127. }
  128. },
  129. // 检查购买状态
  130. async checkPurchaseStatus() {
  131. if (!this.currentUserId || !this.courseId) return
  132. try {
  133. const purchased = await api.courseOrder.checkPurchased(this.currentUserId, this.courseId)
  134. // 只有明确返回 true 时才设置为已购买
  135. if (purchased === true) {
  136. this.isPurchased = true
  137. this.course.purchase_status = 1
  138. }
  139. } catch (error) {
  140. }
  141. },
  142. handlePurchase() {
  143. if (this.course.purchase_status === 1 || this.isPurchased) {
  144. uni.showToast({
  145. title: '您已购买过此课程',
  146. icon: 'none'
  147. })
  148. return
  149. }
  150. // 检查是否登录
  151. if (!this.currentUserId) {
  152. uni.showModal({
  153. title: '提示',
  154. content: '请先登录后再购买课程',
  155. confirmText: '去登录',
  156. success: (res) => {
  157. if (res.confirm) {
  158. uni.navigateTo({
  159. url: '/pages/page3/page3'
  160. })
  161. }
  162. }
  163. })
  164. return
  165. }
  166. uni.showModal({
  167. title: '确认购买',
  168. content: `确认购买"${this.course.name}"课程吗?\n价格:¥${this.course.price}`,
  169. success: (res) => {
  170. if (res.confirm) {
  171. this.requestPay()
  172. }
  173. }
  174. })
  175. },
  176. // 请求支付参数并调起微信支付
  177. requestPay() {
  178. uni.showLoading({
  179. title: '处理中...'
  180. })
  181. uni.request({
  182. url: this.gatewayURL + '/api/course-order/purchase',
  183. method: 'POST',
  184. data: {
  185. userId: this.currentUserId,
  186. courseId: parseInt(this.courseId),
  187. courseName: this.course.name,
  188. price: this.course.price
  189. },
  190. header: {
  191. 'content-type': 'application/json',
  192. 'token': uni.getStorageSync('token')
  193. },
  194. success: (res) => {
  195. uni.hideLoading()
  196. if (res.data && res.data.code === 200) {
  197. const payParams = res.data.data
  198. this.callWxPay(payParams)
  199. } else {
  200. uni.showToast({
  201. title: res.data?.message || res.data?.msg || '获取支付参数失败',
  202. icon: 'none'
  203. })
  204. }
  205. },
  206. fail: (err) => {
  207. uni.hideLoading()
  208. uni.showToast({
  209. title: '网络请求失败',
  210. icon: 'none'
  211. })
  212. }
  213. })
  214. },
  215. // 调起微信支付
  216. callWxPay(payParams) {
  217. uni.requestPayment({
  218. provider: 'wxpay',
  219. timeStamp: payParams.timeStamp,
  220. nonceStr: payParams.nonceStr,
  221. package: payParams.package,
  222. signType: payParams.signType,
  223. paySign: payParams.paySign,
  224. success: (payResult) => {
  225. if (payResult.errMsg === 'requestPayment:ok') {
  226. uni.showToast({
  227. title: '购买成功!',
  228. icon: 'success',
  229. duration: 2000
  230. })
  231. // 更新购买状态
  232. this.course.purchase_status = 1
  233. this.isPurchased = true
  234. this.course.student_count = (this.course.student_count || 0) + 1
  235. setTimeout(() => {
  236. uni.showModal({
  237. title: '购买成功',
  238. content: '您已成功购买此课程,现在可以开始学习了!',
  239. showCancel: false,
  240. confirmText: '开始学习',
  241. success: (modalRes) => {
  242. if (modalRes.confirm) {
  243. uni.showToast({
  244. title: '学习功能开发中',
  245. icon: 'none'
  246. })
  247. }
  248. }
  249. })
  250. }, 2000)
  251. }
  252. },
  253. fail: (payError) => {
  254. if (payError.errMsg.includes('cancel')) {
  255. uni.showToast({
  256. title: '已取消支付',
  257. icon: 'none'
  258. })
  259. } else {
  260. uni.showToast({
  261. title: `支付失败:${payError.errMsg}`,
  262. icon: 'none'
  263. })
  264. }
  265. }
  266. })
  267. },
  268. // 返回
  269. goBack() {
  270. uni.navigateBack({
  271. fail: () => {
  272. uni.navigateTo({
  273. url: '/pages/courses/list'
  274. })
  275. }
  276. })
  277. },
  278. // 数据健康检查
  279. checkDataHealth() {
  280. if (!this.course.name || this.course.name === '加载中...') {
  281. this.course.name = '恋爱沟通技巧大师课'
  282. }
  283. if (!this.course.teacher_name) {
  284. this.course.teacher_name = '张情感老师'
  285. }
  286. if (!this.course.cover_image) {
  287. this.course.cover_image = this.DEFAULT_IMAGES.course
  288. }
  289. }
  290. }
  291. }
  292. </script>
  293. <style lang="scss" scoped>
  294. .course-detail-page {
  295. min-height: 100vh;
  296. background-color: #FFF9F9;
  297. padding-top: 90rpx;
  298. padding-bottom: 120rpx;
  299. }
  300. /* 自定义导航栏 */
  301. .custom-navbar {
  302. position: fixed;
  303. top: 0;
  304. left: 0;
  305. right: 0;
  306. height: 90rpx;
  307. display: flex;
  308. align-items: center;
  309. justify-content: space-between;
  310. padding: 0 20rpx;
  311. background-color: #E91E63;
  312. z-index: 999;
  313. .navbar-left,
  314. .navbar-right {
  315. width: 80rpx;
  316. }
  317. .back-icon {
  318. font-size: 40rpx;
  319. color: #FFFFFF;
  320. font-weight: bold;
  321. }
  322. .navbar-title {
  323. flex: 1;
  324. text-align: center;
  325. font-size: 32rpx;
  326. font-weight: bold;
  327. color: #FFFFFF;
  328. }
  329. }
  330. /* 课程封面 */
  331. .course-cover {
  332. position: relative;
  333. width: 100%;
  334. height: 500rpx;
  335. .cover-image {
  336. width: 100%;
  337. height: 100%;
  338. }
  339. .cover-mask {
  340. position: absolute;
  341. top: 0;
  342. left: 0;
  343. right: 0;
  344. bottom: 0;
  345. background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3), transparent);
  346. padding: 30rpx;
  347. .discount-tag {
  348. display: inline-block;
  349. padding: 10rpx 20rpx;
  350. background: linear-gradient(135deg, #FF6B6B 0%, #FF9800 100%);
  351. color: #FFFFFF;
  352. font-size: 24rpx;
  353. font-weight: bold;
  354. border-radius: 30rpx;
  355. }
  356. }
  357. }
  358. /* 课程内容 */
  359. .course-content {
  360. padding: 30rpx;
  361. background-color: #FFFFFF;
  362. margin: 20rpx;
  363. border-radius: 20rpx;
  364. .course-title {
  365. font-size: 36rpx;
  366. font-weight: bold;
  367. color: #333333;
  368. margin-bottom: 30rpx;
  369. line-height: 1.5;
  370. }
  371. .teacher-info {
  372. display: flex;
  373. align-items: center;
  374. margin-bottom: 30rpx;
  375. .teacher-avatar {
  376. width: 80rpx;
  377. height: 80rpx;
  378. border-radius: 50%;
  379. margin-right: 20rpx;
  380. }
  381. .teacher-detail {
  382. flex: 1;
  383. display: flex;
  384. flex-direction: column;
  385. gap: 5rpx;
  386. .teacher-name {
  387. font-size: 28rpx;
  388. font-weight: bold;
  389. color: #333333;
  390. }
  391. .teacher-desc {
  392. font-size: 24rpx;
  393. color: #999999;
  394. }
  395. }
  396. }
  397. .course-stats {
  398. display: flex;
  399. justify-content: space-around;
  400. padding: 20rpx 0;
  401. background-color: #FFF9F9;
  402. border-radius: 15rpx;
  403. .stat-item {
  404. display: flex;
  405. flex-direction: column;
  406. align-items: center;
  407. gap: 10rpx;
  408. .stat-icon {
  409. font-size: 32rpx;
  410. }
  411. .stat-text {
  412. font-size: 24rpx;
  413. color: #666666;
  414. }
  415. }
  416. }
  417. .divider {
  418. height: 1rpx;
  419. background-color: #F0F0F0;
  420. margin: 30rpx 0;
  421. }
  422. .section-title {
  423. font-size: 30rpx;
  424. font-weight: bold;
  425. color: #333333;
  426. margin-bottom: 20rpx;
  427. }
  428. .description-text,
  429. .outline-text {
  430. font-size: 28rpx;
  431. color: #666666;
  432. line-height: 1.8;
  433. white-space: pre-wrap;
  434. }
  435. }
  436. /* 底部购买按钮 */
  437. .bottom-bar {
  438. position: fixed;
  439. bottom: 0;
  440. left: 0;
  441. right: 0;
  442. display: flex;
  443. align-items: center;
  444. justify-content: space-between;
  445. padding: 20rpx 30rpx;
  446. background-color: #FFFFFF;
  447. border-top: 1rpx solid #F0F0F0;
  448. z-index: 999;
  449. .price-info {
  450. flex: 1;
  451. .price-label {
  452. display: block;
  453. font-size: 24rpx;
  454. color: #999999;
  455. margin-bottom: 5rpx;
  456. }
  457. .price-value {
  458. display: flex;
  459. align-items: baseline;
  460. color: #E91E63;
  461. font-weight: bold;
  462. .price-symbol {
  463. font-size: 28rpx;
  464. }
  465. .price-num {
  466. font-size: 40rpx;
  467. }
  468. .original-price {
  469. margin-left: 10rpx;
  470. font-size: 24rpx;
  471. color: #999999;
  472. text-decoration: line-through;
  473. }
  474. .discount-text {
  475. margin-left: 15rpx;
  476. font-size: 22rpx;
  477. color: #FF9800;
  478. background: rgba(255, 152, 0, 0.1);
  479. padding: 5rpx 12rpx;
  480. border-radius: 15rpx;
  481. }
  482. }
  483. }
  484. .purchase-btn {
  485. padding: 20rpx 60rpx;
  486. background: linear-gradient(135deg, #E91E63 0%, #FF6B6B 100%);
  487. border-radius: 50rpx;
  488. box-shadow: 0 8rpx 16rpx rgba(233, 30, 99, 0.3);
  489. transition: all 0.3s;
  490. .btn-text {
  491. font-size: 32rpx;
  492. font-weight: bold;
  493. color: #FFFFFF;
  494. }
  495. &.purchased {
  496. background: #4CAF50;
  497. box-shadow: 0 8rpx 16rpx rgba(76, 175, 80, 0.3);
  498. .btn-text {
  499. color: #FFFFFF;
  500. }
  501. }
  502. &:active {
  503. transform: scale(0.95);
  504. }
  505. }
  506. }
  507. </style>