my-activities.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. <template>
  2. <view class="my-activities-page">
  3. <!-- 标签页切换 -->
  4. <view class="tabs">
  5. <view class="tab-item" :class="{ active: currentTab === 'registered' }" @click="switchTab('registered')">
  6. <text class="tab-text">我的活动</text>
  7. <view class="tab-badge" v-if="registeredCount > 0">{{ registeredCount }}</view>
  8. </view>
  9. <view class="tab-item" :class="{ active: currentTab === 'joined' }" @click="switchTab('joined')">
  10. <text class="tab-text">我的课程</text>
  11. <view class="tab-badge" v-if="purchasedCourses.length > 0">{{ purchasedCourses.length }}</view>
  12. </view>
  13. </view>
  14. <!-- 活动/课程列表 -->
  15. <view class="activities-list">
  16. <!-- 活动卡片 -->
  17. <view class="activity-card" v-for="item in registeredActivities" :key="`activity-${item.id}`" @click="goToActivityDetail(item.activityId)" v-if="currentTab === 'registered'">
  18. <image :src="item.coverImage" class="activity-cover" mode="aspectFill"></image>
  19. <view class="activity-info">
  20. <view class="activity-header">
  21. <text class="activity-name">{{ item.name }}</text>
  22. <!-- 修复:改用数组+字符串拼接的合法 :class 格式 -->
  23. <view class="status-tag" :class="['status-' + getStatusType(item.status)]">{{ getStatusText(item.status) }}</view>
  24. </view>
  25. <view class="activity-meta activity-meta--activity">
  26. <view class="meta-item">
  27. <text class="meta-icon">📅</text>
  28. <text class="meta-text">{{ formatDate(item.startTime) }}</text>
  29. </view>
  30. <view class="meta-item" v-if="item.location">
  31. <text class="meta-icon">📍</text>
  32. <text class="meta-text">{{ item.location }}</text>
  33. </view>
  34. <view class="meta-item">
  35. <text class="meta-icon">👥</text>
  36. <text class="meta-text">{{ item.actualParticipants }}/{{ item.maxParticipants }}人</text>
  37. </view>
  38. </view>
  39. <view class="activity-footer">
  40. <text class="activity-price">¥{{ item.price || 0 }}</text>
  41. <view class="activity-actions">
  42. <button class="action-btn primary" size="mini" @click.stop="goToActivityDetail(item.activityId,item)">查看详情</button>
  43. <!-- <button class="action-btn cancel" size="mini" @click.stop="cancelActivity(item)" v-if="item.status === 1">取消报名</button> -->
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. <!-- 课程卡片 -->
  49. <view class="course-card" v-for="item in purchasedCourses" :key="`course-${item.id}`" @click="goToDetail(item.id,item)" v-if="currentTab === 'joined'">
  50. <image :src="item.cover_image" class="course-cover" mode="aspectFill"></image>
  51. <view class="course-info">
  52. <view class="course-header">
  53. <text class="course-name">{{ item.name }}</text>
  54. <view class="course-rating" v-if="item.rating">
  55. <text class="meta-icon">⭐</text>
  56. <text class="rating-text">{{ item.rating }}</text>
  57. </view>
  58. </view>
  59. <view class="course-meta">
  60. <view class="meta-item">
  61. <text class="meta-icon">👤</text>
  62. <text class="meta-text">讲师: {{ item.teacher_name || '未知讲师' }}</text>
  63. </view>
  64. <view class="meta-item">
  65. <text class="meta-icon">⭐</text>
  66. <text class="meta-text">{{item.rating}}</text>
  67. </view>
  68. <view class="meta-item">
  69. <text class="meta-icon">👥</text>
  70. <text class="meta-text">{{ item.student_count || 0 }}人已学习</text>
  71. </view>
  72. </view>
  73. <view class="course-footer">
  74. <text class="course-price">¥{{ item.price || 0 }}</text>
  75. <view class="course-actions">
  76. <button class="action-btn primary course-btn" @click.stop="goToDetail(item.id,item)">查看详情</button>
  77. </view>
  78. </view>
  79. </view>
  80. </view>
  81. <!-- 活动空状态 -->
  82. <view class="empty-state" v-if="currentTab === 'registered' && registeredActivities.length === 0">
  83. <text class="empty-icon">📅</text>
  84. <text class="empty-text">暂无已报名的活动</text>
  85. <button class="browse-btn" @click="goToActivityList">浏览活动</button>
  86. </view>
  87. <!-- 课程空状态 -->
  88. <view class="empty-state" v-if="currentTab === 'joined' && purchasedCourses.length === 0">
  89. <text class="empty-icon">📚</text>
  90. <text class="empty-text">暂无已购买的课程</text>
  91. <button class="browse-btn" @click="goToCourseList">浏览课程</button>
  92. </view>
  93. </view>
  94. </view>
  95. </template>
  96. <script>
  97. import api from '@/utils/api'
  98. export default {
  99. data() {
  100. return {
  101. currentTab: 'registered',
  102. // 活动相关
  103. registeredActivities: [],
  104. registeredCount: 0,
  105. // 课程相关
  106. purchasedCourses: [], // 已购买的课程
  107. }
  108. },
  109. onLoad() {
  110. this.loadMyData()
  111. },
  112. methods: {
  113. // 加载我的活动和课程数据
  114. async loadMyData() {
  115. try {
  116. const userInfo = uni.getStorageSync('userInfo')
  117. if (!userInfo || !userInfo.userId) {
  118. uni.showToast({
  119. title: '请先登录',
  120. icon: 'none'
  121. })
  122. return
  123. }
  124. // 并行加载活动和课程数据
  125. await Promise.all([
  126. this.loadRegisteredActivities(userInfo.userId),
  127. this.loadPurchasedCourses(userInfo.userId)
  128. ])
  129. } catch (error) {
  130. console.error('加载数据失败:', error)
  131. uni.showToast({
  132. title: '加载数据失败',
  133. icon: 'none'
  134. })
  135. }
  136. },
  137. // 加载已报名的活动
  138. async loadRegisteredActivities(userId) {
  139. const baseUrl = 'https://api.zhongruanke.cn'
  140. try {
  141. const [error, res] = await uni.request({
  142. url: `${baseUrl}/api/activity-registration/my-activities?userId=${userId}&status=1`,
  143. method: 'GET'
  144. })
  145. if (res && res.data && res.data.code === 200) {
  146. this.registeredActivities = (res.data.data || []).map(item => ({
  147. id: item.id,
  148. activityId: item.activityId,
  149. name: item.activityName,
  150. coverImage: item.coverImage || '/static/default-activity.png',
  151. startTime: item.startTime,
  152. endTime: item.endTime,
  153. location: item.location,
  154. price: item.price,
  155. actualParticipants: item.actualParticipants || 0,
  156. maxParticipants: item.maxParticipants || 0,
  157. status: this.getActivityStatusByTime(item.startTime, item.endTime)
  158. }))
  159. this.registeredCount = this.registeredActivities.length
  160. }
  161. } catch (error) {
  162. console.error('加载活动数据失败:', error)
  163. }
  164. },
  165. // 加载已购买的课程
  166. async loadPurchasedCourses(userId) {
  167. try {
  168. const coursesRes = await api.courseOrder.getPurchasedCourses(userId)
  169. console.log(coursesRes)
  170. this.purchasedCourses = coursesRes
  171. if (coursesRes.code === 200) {
  172. this.purchasedCourses = (coursesRes.data || []).map(course => ({
  173. id: course.id,
  174. courseId: course.id,
  175. name: course.courseName,
  176. coverImage: course.coverImage || '/static/default-course.png',
  177. teacher: course.teacher,
  178. price: course.price,
  179. studentsCount: course.studentsCount || 0,
  180. rating: course.rating || 4.5,
  181. chaptersCount: course.chaptersCount || 0, // 课程章节数
  182. lessonsCount: course.lessonsCount || 0, // 课程课时数
  183. learnProgress: course.learnProgress || 0, // 学习进度
  184. lastLearnTime: course.lastLearnTime // 最后学习时间
  185. }))
  186. console.log(this.purchasedCourses)
  187. }
  188. } catch (error) {
  189. console.error('加载课程数据失败:', error)
  190. }
  191. },
  192. // 根据时间判断活动状态
  193. getActivityStatusByTime(startTime, endTime) {
  194. if (!startTime || !endTime) return 1
  195. const now = new Date()
  196. const start = new Date(startTime)
  197. const end = new Date(endTime)
  198. if (now < start) return 1 // 未开始
  199. if (now >= start && now <= end) return 2 // 进行中
  200. return 3 // 已结束
  201. },
  202. // 获取活动状态类型(用于样式)
  203. getStatusType(status) {
  204. const types = { 1: 'pending', 2: 'ongoing', 3: 'ended' }
  205. return types[status] || 'pending'
  206. },
  207. // 获取活动状态文本
  208. getStatusText(status) {
  209. const texts = { 1: '未开始', 2: '进行中', 3: '已结束' }
  210. return texts[status] || '未知'
  211. },
  212. // 切换标签页
  213. switchTab(tab) {
  214. this.currentTab = tab
  215. // 切换时滚动到顶部
  216. uni.pageScrollTo({
  217. scrollTop: 0,
  218. duration: 300
  219. })
  220. },
  221. // 格式化日期
  222. formatDate(dateStr) {
  223. if (!dateStr) return '时间待定'
  224. try {
  225. const date = new Date(dateStr)
  226. const month = date.getMonth() + 1
  227. const day = date.getDate()
  228. const hours = date.getHours()
  229. const minutes = date.getMinutes()
  230. return `${month}月${day}日 ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`
  231. } catch (error) {
  232. return '时间待定'
  233. }
  234. },
  235. // 取消活动报名
  236. async cancelActivity(activity) {
  237. uni.showModal({
  238. title: '取消报名',
  239. content: '确定要取消本次活动报名吗?',
  240. success: async (res) => {
  241. if (res.confirm) {
  242. try {
  243. const userInfo = uni.getStorageSync('userInfo')
  244. const baseUrl = 'https://api.zhongruanke.cn'
  245. const [error, result] = await uni.request({
  246. url: `${baseUrl}/api/activity-registration/cancel?id=${activity.id}&userId=${userInfo.userId}`,
  247. method: 'PUT'
  248. })
  249. if (result && result.data && result.data.code === 200) {
  250. uni.showToast({
  251. title: '取消报名成功',
  252. icon: 'success'
  253. })
  254. // 重新加载活动数据
  255. this.loadRegisteredActivities(userInfo.userId)
  256. } else {
  257. uni.showToast({
  258. title: (result && result.data && result.data.msg) || '取消失败',
  259. icon: 'none'
  260. })
  261. }
  262. } catch (error) {
  263. uni.showToast({
  264. title: '取消失败,请重试',
  265. icon: 'none'
  266. })
  267. }
  268. }
  269. }
  270. })
  271. },
  272. // 跳转到活动详情
  273. goToActivityDetail(itemId,item) {
  274. uni.navigateTo({
  275. url: `/pages/activities/detail?id=${item.activityId}`
  276. })
  277. },
  278. // 跳转到详情
  279. goToDetail(id,item) {
  280. uni.navigateTo({
  281. url: `/pages/courses/detail?id=${item.id}`,
  282. success: () => {
  283. },
  284. fail: (err) => {
  285. uni.showToast({
  286. title: '跳转失败',
  287. icon: 'none'
  288. })
  289. }
  290. })
  291. },
  292. // 跳转到活动列表
  293. goToActivityList() {
  294. uni.navigateTo({
  295. url: '/pages/activities/list'
  296. })
  297. },
  298. // 跳转到课程列表
  299. goToCourseList() {
  300. uni.navigateTo({
  301. url: '/pages/courses/list'
  302. })
  303. }
  304. }
  305. }
  306. </script>
  307. <style scoped>
  308. .my-activities-page {
  309. min-height: 100vh;
  310. background: #FFF9F9;
  311. }
  312. /* 标签页样式 */
  313. .tabs {
  314. display: flex;
  315. background: white;
  316. padding: 0 30rpx;
  317. border-bottom: 1rpx solid #f0f0f0;
  318. }
  319. .tab-item {
  320. flex: 1;
  321. text-align: center;
  322. padding: 30rpx 0;
  323. position: relative;
  324. }
  325. .tab-text {
  326. font-size: 30rpx;
  327. color: #666;
  328. }
  329. .tab-item.active .tab-text {
  330. color: #E91E63;
  331. font-weight: 600;
  332. }
  333. .tab-item.active::after {
  334. content: '';
  335. position: absolute;
  336. bottom: 0;
  337. left: 50%;
  338. transform: translateX(-50%);
  339. width: 80rpx;
  340. height: 6rpx;
  341. background: #E91E63;
  342. border-radius: 3rpx;
  343. }
  344. .tab-badge {
  345. position: absolute;
  346. top: 20rpx;
  347. right: 20%;
  348. background: #FF4D4F;
  349. color: white;
  350. font-size: 20rpx;
  351. padding: 2rpx 8rpx;
  352. border-radius: 10rpx;
  353. min-width: 32rpx;
  354. text-align: center;
  355. }
  356. /* 列表容器 */
  357. .activities-list {
  358. display: grid;
  359. grid-template-columns: 1fr 1fr;
  360. gap: 20rpx;
  361. padding: 20rpx;
  362. }
  363. /* 活动卡片样式 */
  364. .activity-card {
  365. position: relative;
  366. background: white;
  367. border-radius: 20rpx;
  368. overflow: hidden;
  369. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  370. }
  371. .activity-cover {
  372. width: 100%;
  373. height: 240rpx;
  374. background: #F5F5F5;
  375. }
  376. .activity-info {
  377. padding: 20rpx;
  378. }
  379. .activity-header {
  380. display: flex;
  381. justify-content: space-between;
  382. align-items: flex-start;
  383. margin-bottom: 15rpx;
  384. }
  385. .activity-name {
  386. font-size: 28rpx;
  387. font-weight: 600;
  388. color: #333;
  389. flex: 1;
  390. margin-right: 10rpx;
  391. display: -webkit-box;
  392. -webkit-box-orient: vertical;
  393. -webkit-line-clamp: 2;
  394. overflow: hidden;
  395. line-height: 1.4;
  396. }
  397. /* 活动状态标签 */
  398. .status-tag {
  399. font-size: 20rpx;
  400. padding: 4rpx 12rpx;
  401. border-radius: 12rpx;
  402. color: white;
  403. }
  404. .status-pending {
  405. background-color: #409EFF; /* 未开始-蓝色 */
  406. }
  407. .status-ongoing {
  408. background-color: #67C23A; /* 进行中-绿色 */
  409. }
  410. .status-ended {
  411. background-color: #909399; /* 已结束-灰色 */
  412. }
  413. /* 活动元信息 */
  414. .activity-meta--activity {
  415. display: flex;
  416. flex-direction: column;
  417. gap: 10rpx;
  418. margin-bottom: 20rpx;
  419. }
  420. .meta-item {
  421. display: flex;
  422. align-items: center;
  423. gap: 8rpx;
  424. }
  425. .meta-icon {
  426. font-size: 24rpx;
  427. }
  428. .meta-text {
  429. font-size: 24rpx;
  430. color: #666;
  431. line-height: 1.4;
  432. }
  433. .activity-footer {
  434. display: flex;
  435. justify-content: space-between;
  436. align-items: center;
  437. }
  438. .activity-price {
  439. color: #E91E63;
  440. font-weight: 600;
  441. font-size: 32rpx;
  442. }
  443. /* 课程卡片样式 */
  444. .course-card {
  445. position: relative;
  446. background: white;
  447. border-radius: 20rpx;
  448. overflow: hidden;
  449. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  450. }
  451. .course-cover {
  452. width: 100%;
  453. height: 240rpx;
  454. background: #F5F5F5;
  455. }
  456. .course-info {
  457. padding: 20rpx;
  458. }
  459. .course-header {
  460. display: flex;
  461. justify-content: space-between;
  462. align-items: flex-start;
  463. margin-bottom: 15rpx;
  464. }
  465. .course-name {
  466. font-size: 28rpx;
  467. font-weight: 600;
  468. color: #333;
  469. flex: 1;
  470. margin-right: 10rpx;
  471. display: -webkit-box;
  472. -webkit-box-orient: vertical;
  473. -webkit-line-clamp: 2;
  474. overflow: hidden;
  475. line-height: 1.4;
  476. }
  477. .course-rating {
  478. display: flex;
  479. align-items: center;
  480. gap: 4rpx;
  481. background-color: #FFF8E1;
  482. padding: 4rpx 10rpx;
  483. border-radius: 10rpx;
  484. }
  485. .rating-text {
  486. font-size: 20rpx;
  487. color: #FF9800;
  488. }
  489. /* 课程元信息 */
  490. .course-meta {
  491. display: flex;
  492. flex-direction: column;
  493. gap: 10rpx;
  494. margin-bottom: 20rpx;
  495. }
  496. .course-footer {
  497. display: flex;
  498. justify-content: space-between;
  499. align-items: center;
  500. }
  501. .course-price {
  502. color: #9C27B0;
  503. font-weight: 600;
  504. font-size: 32rpx;
  505. }
  506. /* 按钮样式 */
  507. .activity-actions {
  508. display: flex;
  509. gap: 10rpx;
  510. }
  511. .action-btn {
  512. padding: 8rpx 16rpx;
  513. border-radius: 20rpx;
  514. font-size: 24rpx;
  515. border: none;
  516. }
  517. .action-btn.primary {
  518. background-color: #E91E63;
  519. color: white;
  520. }
  521. .action-btn.cancel {
  522. background-color: #F5F5F5;
  523. color: #666;
  524. }
  525. .course-btn {
  526. background-color: #9C27B0 !important;
  527. width: 140rpx;
  528. text-align: center;
  529. }
  530. /* 空状态样式 */
  531. .empty-state {
  532. grid-column: 1 / -1;
  533. display: flex;
  534. flex-direction: column;
  535. align-items: center;
  536. justify-content: center;
  537. padding: 120rpx 0;
  538. }
  539. .empty-icon {
  540. font-size: 120rpx;
  541. margin-bottom: 30rpx;
  542. }
  543. .empty-text {
  544. font-size: 28rpx;
  545. color: #999;
  546. margin-bottom: 40rpx;
  547. }
  548. .browse-btn {
  549. padding: 16rpx 60rpx;
  550. background: #E91E63;
  551. color: white;
  552. border-radius: 40rpx;
  553. font-size: 28rpx;
  554. border: none;
  555. }
  556. </style>