list.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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"></view>
  10. </view>
  11. <!-- 课程列表 -->
  12. <view class="course-list">
  13. <view class="course-card" v-for="(item, index) in courseList" :key="index"
  14. @click="goToDetail(item.id)">
  15. <image :src="item.cover_image" class="course-image" mode="aspectFill"></image>
  16. <view class="course-info">
  17. <view class="course-name">{{ item.name }}</view>
  18. <view class="course-desc">{{ item.description }}</view>
  19. <view class="course-meta">
  20. <text class="course-teacher">👩‍🏫 {{ item.teacher_name }}</text>
  21. <text class="course-students">👥 {{ item.student_count || 0 }}人学习</text>
  22. </view>
  23. <view class="course-footer">
  24. <view class="course-price">
  25. <text class="price-symbol">¥</text>
  26. <text class="price-value">{{ item.price }}</text>
  27. <text class="original-price" v-if="item.original_price">¥{{ item.original_price }}</text>
  28. </view>
  29. <view class="course-btn">立即购买</view>
  30. </view>
  31. </view>
  32. <view class="discount-tag" v-if="item.discount_rate">{{ item.discount_rate }}折</view>
  33. </view>
  34. </view>
  35. <!-- 加载更多 -->
  36. <view class="load-more" v-if="hasMore">
  37. <text class="load-text">{{ loading ? '加载中...' : '上拉加载更多' }}</text>
  38. </view>
  39. <view class="no-more" v-else>
  40. <text class="no-more-text">没有更多了</text>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import api from '@/utils/api.js'
  46. import { DEFAULT_IMAGES } from '@/config/index.js'
  47. export default {
  48. data() {
  49. return {
  50. courseList: [
  51. // 默认课程数据,确保页面有内容
  52. {
  53. id: 1,
  54. name: '恋爱沟通技巧大师课',
  55. description: '学会有效沟通,让感情更甜蜜。涵盖倾听技巧、表达方式、冲突化解等核心内容。',
  56. cover_image: DEFAULT_IMAGES.course,
  57. teacher_name: '张情感老师',
  58. teacher_desc: '资深情感咨询师,10年经验',
  59. teacher_avatar: DEFAULT_IMAGES.avatar,
  60. price: 299,
  61. original_price: 399,
  62. discount_rate: 7.5,
  63. student_count: 1256,
  64. rating: 4.9,
  65. chapter_count: 12,
  66. outline: '第1章:沟通的艺术\n第2章:倾听的力量\n第3章:表达的技巧\n第4章:冲突的化解\n...'
  67. },
  68. {
  69. id: 2,
  70. name: '相亲必胜宝典',
  71. description: '从形象打造到话题聊天,全方位提升相亲成功率。适合单身男女快速脱单。',
  72. cover_image: DEFAULT_IMAGES.course,
  73. teacher_name: '李红娘老师',
  74. teacher_desc: '专业红娘,成功牵线300+对',
  75. teacher_avatar: DEFAULT_IMAGES.avatar,
  76. price: 199,
  77. original_price: 299,
  78. discount_rate: 6.6,
  79. student_count: 2580,
  80. rating: 4.8,
  81. chapter_count: 8,
  82. outline: '第1章:形象管理\n第2章:心态调整\n第3章:聊天技巧\n第4章:约会礼仪\n...'
  83. },
  84. {
  85. id: 3,
  86. name: '婚姻经营智慧课',
  87. description: '婚后生活经营秘籍,让婚姻更加幸福美满。处理婆媳关系、夫妻沟通等实用内容。',
  88. cover_image: DEFAULT_IMAGES.course,
  89. teacher_name: '王婚姻老师',
  90. teacher_desc: '婚姻家庭咨询专家',
  91. teacher_avatar: DEFAULT_IMAGES.avatar,
  92. price: 399,
  93. original_price: 599,
  94. discount_rate: 6.7,
  95. student_count: 856,
  96. rating: 5.0,
  97. chapter_count: 15,
  98. outline: '第1章:夫妻沟通\n第2章:婆媳关系\n第3章:财务管理\n第4章:亲子教育\n...'
  99. }
  100. ],
  101. pageNum: 1,
  102. pageSize: 10,
  103. hasMore: true,
  104. loading: false,
  105. DEFAULT_IMAGES
  106. }
  107. },
  108. onLoad() {
  109. console.log('精品课程列表页面加载')
  110. console.log('默认课程数据:', this.courseList.length)
  111. // 数据健康检查
  112. this.checkDataHealth()
  113. this.loadCourseList()
  114. },
  115. onReachBottom() {
  116. if (this.hasMore && !this.loading) {
  117. this.pageNum++
  118. this.loadCourseList()
  119. }
  120. },
  121. methods: {
  122. // 加载课程列表
  123. async loadCourseList() {
  124. if (this.loading) return
  125. this.loading = true
  126. try {
  127. console.log('尝试加载精品课程API数据...')
  128. const data = await api.course.getList({
  129. pageNum: this.pageNum,
  130. pageSize: this.pageSize,
  131. status: 1
  132. })
  133. if (data && data.length > 0) {
  134. if (this.pageNum === 1) {
  135. // 第一页,替换数据
  136. this.courseList = data
  137. } else {
  138. // 后续页,追加数据
  139. this.courseList = [...this.courseList, ...data]
  140. }
  141. this.hasMore = data.length >= this.pageSize
  142. console.log('API课程数据加载成功:', data.length, '条')
  143. } else {
  144. this.hasMore = false
  145. console.log('API返回课程数据为空,使用默认数据')
  146. }
  147. } catch (error) {
  148. console.error('加载课程列表失败:', error)
  149. // API失败时,如果是第一页,保留默认数据
  150. if (this.pageNum === 1) {
  151. console.log('API失败,保留默认课程数据')
  152. uni.showToast({
  153. title: '使用示例数据',
  154. icon: 'none'
  155. })
  156. } else {
  157. uni.showToast({
  158. title: '加载失败',
  159. icon: 'none'
  160. })
  161. }
  162. this.hasMore = false
  163. } finally {
  164. this.loading = false
  165. }
  166. },
  167. // 跳转到详情
  168. goToDetail(id) {
  169. console.log('跳转到课程详情, id:', id)
  170. uni.navigateTo({
  171. url: `/pages/courses/detail?id=${id}`,
  172. success: () => {
  173. console.log('跳转课程详情成功')
  174. },
  175. fail: (err) => {
  176. console.error('跳转课程详情失败:', err)
  177. uni.showToast({
  178. title: '跳转失败',
  179. icon: 'none'
  180. })
  181. }
  182. })
  183. },
  184. // 返回
  185. goBack() {
  186. console.log('课程列表返回上一页')
  187. uni.navigateBack({
  188. fail: () => {
  189. // 返回失败时跳转首页
  190. uni.navigateTo({
  191. url: '/pages/index/index'
  192. })
  193. }
  194. })
  195. },
  196. // 数据健康检查
  197. checkDataHealth() {
  198. console.log('=== 精品课程列表页面数据健康检查 ===')
  199. console.log('courseList长度:', this.courseList.length)
  200. console.log('DEFAULT_IMAGES:', this.DEFAULT_IMAGES)
  201. console.log('第一个课程:', this.courseList[0])
  202. // 确保每个课程都有必要的字段
  203. this.courseList.forEach((item, index) => {
  204. if (!item.id) {
  205. item.id = index + 1
  206. }
  207. if (!item.cover_image) {
  208. item.cover_image = this.DEFAULT_IMAGES.course
  209. }
  210. if (!item.teacher_avatar) {
  211. item.teacher_avatar = this.DEFAULT_IMAGES.avatar
  212. }
  213. })
  214. }
  215. }
  216. }
  217. </script>
  218. <style lang="scss" scoped>
  219. .courses-page {
  220. min-height: 100vh;
  221. background-color: #FFF9F9;
  222. padding-top: 90rpx;
  223. }
  224. /* 自定义导航栏 */
  225. .custom-navbar {
  226. position: fixed;
  227. top: 0;
  228. left: 0;
  229. right: 0;
  230. height: 90rpx;
  231. display: flex;
  232. align-items: center;
  233. justify-content: space-between;
  234. padding: 0 20rpx;
  235. background-color: #E91E63;
  236. z-index: 999;
  237. .navbar-left,
  238. .navbar-right {
  239. width: 80rpx;
  240. }
  241. .back-icon {
  242. font-size: 40rpx;
  243. color: #FFFFFF;
  244. font-weight: bold;
  245. }
  246. .navbar-title {
  247. flex: 1;
  248. text-align: center;
  249. font-size: 32rpx;
  250. font-weight: bold;
  251. color: #FFFFFF;
  252. }
  253. }
  254. /* 课程列表 */
  255. .course-list {
  256. padding: 20rpx 30rpx;
  257. .course-card {
  258. position: relative;
  259. margin-bottom: 20rpx;
  260. background-color: #FFFFFF;
  261. border-radius: 20rpx;
  262. overflow: hidden;
  263. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  264. .course-image {
  265. width: 100%;
  266. height: 360rpx;
  267. background-color: #F5F5F5;
  268. }
  269. .course-info {
  270. padding: 30rpx;
  271. .course-name {
  272. font-size: 32rpx;
  273. font-weight: bold;
  274. color: #333333;
  275. margin-bottom: 15rpx;
  276. display: -webkit-box;
  277. -webkit-box-orient: vertical;
  278. -webkit-line-clamp: 2;
  279. overflow: hidden;
  280. }
  281. .course-desc {
  282. font-size: 26rpx;
  283. color: #666666;
  284. line-height: 1.6;
  285. margin-bottom: 20rpx;
  286. display: -webkit-box;
  287. -webkit-box-orient: vertical;
  288. -webkit-line-clamp: 2;
  289. overflow: hidden;
  290. }
  291. .course-meta {
  292. display: flex;
  293. justify-content: space-between;
  294. font-size: 24rpx;
  295. color: #999999;
  296. margin-bottom: 20rpx;
  297. }
  298. .course-footer {
  299. display: flex;
  300. justify-content: space-between;
  301. align-items: center;
  302. .course-price {
  303. color: #E91E63;
  304. font-weight: bold;
  305. .price-symbol {
  306. font-size: 24rpx;
  307. }
  308. .price-value {
  309. font-size: 36rpx;
  310. }
  311. .original-price {
  312. margin-left: 10rpx;
  313. font-size: 24rpx;
  314. color: #999999;
  315. text-decoration: line-through;
  316. }
  317. }
  318. .course-btn {
  319. padding: 10rpx 30rpx;
  320. background-color: #E91E63;
  321. color: #FFFFFF;
  322. text-align: center;
  323. border-radius: 30rpx;
  324. font-size: 24rpx;
  325. }
  326. }
  327. }
  328. .discount-tag {
  329. position: absolute;
  330. top: 20rpx;
  331. left: 20rpx;
  332. padding: 8rpx 20rpx;
  333. background: linear-gradient(135deg, #FF6B6B 0%, #FF9800 100%);
  334. color: #FFFFFF;
  335. font-size: 24rpx;
  336. font-weight: bold;
  337. border-radius: 10rpx;
  338. }
  339. }
  340. }
  341. /* 加载更多 */
  342. .load-more,
  343. .no-more {
  344. padding: 30rpx 0;
  345. text-align: center;
  346. .load-text,
  347. .no-more-text {
  348. font-size: 24rpx;
  349. color: #999999;
  350. }
  351. }
  352. </style>