detail.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <template>
  2. <view class="activity-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="activity-cover">
  13. <image :src="activity.coverImage" class="cover-image" mode="aspectFill"
  14. @error="handleImageError" @load="handleImageLoad"></image>
  15. <view class="cover-mask">
  16. <view class="activity-tag" v-if="activity.isHot">🔥 热门活动</view>
  17. </view>
  18. </view>
  19. <!-- 活动信息 -->
  20. <view class="activity-content">
  21. <view class="activity-title">{{ activity.name }}</view>
  22. <view class="info-section">
  23. <view class="info-item">
  24. <text class="info-icon">⏰</text>
  25. <text class="info-label">活动时间:</text>
  26. <text class="info-value">{{ formatActivityTime(activity.startTime, activity.endTime) }}</text>
  27. </view>
  28. <view class="info-item">
  29. <text class="info-icon">📍</text>
  30. <text class="info-label">活动地点:</text>
  31. <text class="info-value">{{ activity.location || '待定' }}</text>
  32. </view>
  33. <view class="info-item">
  34. <text class="info-icon">👥</text>
  35. <text class="info-label">报名人数:</text>
  36. <text class="info-value">{{ activity.actualParticipants || 0 }} / {{ activity.maxParticipants || '不限' }}</text>
  37. </view>
  38. <view class="info-item">
  39. <text class="info-icon">💰</text>
  40. <text class="info-label">活动费用:</text>
  41. <text class="info-value price">¥{{ activity.price || 0 }}</text>
  42. </view>
  43. </view>
  44. <view class="divider"></view>
  45. <view class="description-section">
  46. <view class="section-title">活动介绍</view>
  47. <view class="description-text">{{ activity.description || '暂无介绍' }}</view>
  48. </view>
  49. <view class="divider"></view>
  50. <view class="notes-section">
  51. <view class="section-title">注意事项</view>
  52. <view class="notes-text">{{ activity.notes || '请准时参加活动' }}</view>
  53. </view>
  54. </view>
  55. <!-- 底部报名按钮 -->
  56. <view class="bottom-bar">
  57. <view class="price-info">
  58. <text class="price-label">活动费用</text>
  59. <view class="price-value">
  60. <text class="price-symbol">¥</text>
  61. <text class="price-num">{{ activity.price || 0 }}</text>
  62. </view>
  63. </view>
  64. <view class="register-btn" @click="handleRegister">
  65. <text class="btn-text">立即报名</text>
  66. </view>
  67. </view>
  68. </view>
  69. </template>
  70. <script>
  71. import api from '@/utils/api.js'
  72. import { formatTime } from '@/utils/util.js'
  73. import { DEFAULT_IMAGES } from '@/config/index.js'
  74. import userAuth from '@/utils/userAuth.js'
  75. export default {
  76. data() {
  77. return {
  78. activityId: null,
  79. activity: {
  80. name: '加载中...',
  81. coverImage: DEFAULT_IMAGES.activity,
  82. price: 0
  83. }
  84. }
  85. },
  86. onLoad(options) {
  87. if (options.id) {
  88. this.activityId = options.id
  89. this.loadActivityDetail()
  90. }
  91. },
  92. methods: {
  93. // 加载活动详情
  94. async loadActivityDetail() {
  95. try {
  96. const data = await api.activity.getDetail(this.activityId)
  97. if (data) {
  98. // 处理字段映射,确保前端使用的字段名统一
  99. this.activity = {
  100. ...data,
  101. coverImage: this.validateImageUrl(data.coverImage || data.cover_image || data.imageUrl || data.image_url),
  102. startTime: data.startTime || data.start_time || data.startDate || data.start_date || '',
  103. endTime: data.endTime || data.end_time || data.endDate || data.end_date || '',
  104. location: data.location || data.address || data.venue || '待定',
  105. maxParticipants: data.maxParticipants || data.max_participants || null,
  106. actualParticipants: data.actualParticipants || data.actual_participants || 0,
  107. price: data.price || data.fee || 0,
  108. description: data.description || data.desc || '暂无介绍',
  109. notes: data.notes || data.notice || '请准时参加活动'
  110. }
  111. console.log('活动详情加载成功:', this.activity)
  112. }
  113. } catch (error) {
  114. console.error('加载活动详情失败:', error)
  115. uni.showToast({
  116. title: '加载失败',
  117. icon: 'none'
  118. })
  119. }
  120. },
  121. // 验证并修正图片URL
  122. validateImageUrl(url) {
  123. if (!url || url === 'null' || url === 'undefined') {
  124. return DEFAULT_IMAGES.activity || 'http://115.190.125.125:9001/static-images/login-bg.png'
  125. }
  126. let cleanedUrl = String(url).trim()
  127. const httpIndex = cleanedUrl.indexOf('http')
  128. if (httpIndex > 0) {
  129. cleanedUrl = cleanedUrl.slice(httpIndex)
  130. }
  131. if (!cleanedUrl) {
  132. return DEFAULT_IMAGES.activity || 'http://115.190.125.125:9001/static-images/login-bg.png'
  133. }
  134. // 如果是相对路径,添加协议和域名
  135. if (cleanedUrl.startsWith('/')) {
  136. return `http://115.190.125.125:9000${cleanedUrl}`
  137. }
  138. // 如果已经是完整URL,直接返回
  139. if (cleanedUrl.startsWith('http://') || cleanedUrl.startsWith('https://')) {
  140. return cleanedUrl
  141. }
  142. // 其他情况返回默认图片
  143. return DEFAULT_IMAGES.activity || 'http://115.190.125.125:9001/static-images/login-bg.png'
  144. },
  145. // 格式化时间
  146. formatTime,
  147. // 格式化活动时间(显示开始和结束时间)
  148. formatActivityTime(startTime, endTime) {
  149. if (!startTime) return '时间待定'
  150. try {
  151. const start = new Date(startTime)
  152. const startMonth = start.getMonth() + 1
  153. const startDay = start.getDate()
  154. const startHour = start.getHours().toString().padStart(2, '0')
  155. const startMinute = start.getMinutes().toString().padStart(2, '0')
  156. let timeStr = `${startMonth}月${startDay}日 ${startHour}:${startMinute}`
  157. // 如果有结束时间,添加结束时间
  158. if (endTime) {
  159. const end = new Date(endTime)
  160. const endMonth = end.getMonth() + 1
  161. const endDay = end.getDate()
  162. const endHour = end.getHours().toString().padStart(2, '0')
  163. const endMinute = end.getMinutes().toString().padStart(2, '0')
  164. timeStr += ` - ${endMonth}月${endDay}日 ${endHour}:${endMinute}`
  165. }
  166. return timeStr
  167. } catch (error) {
  168. console.error('时间格式化失败:', error)
  169. return '时间待定'
  170. }
  171. },
  172. // 处理报名
  173. handleRegister() {
  174. // 获取当前用户ID
  175. const userId = userAuth.getUserId()
  176. if (!userId) {
  177. uni.showModal({
  178. title: '需要登录',
  179. content: '请先登录后再报名活动',
  180. showCancel: false,
  181. success: () => {
  182. uni.navigateTo({
  183. url: '/pages/page3/page3'
  184. })
  185. }
  186. })
  187. return
  188. }
  189. // 检查人数限制(前端预检查,提供更好的用户体验)
  190. if (this.activity.maxParticipants &&
  191. this.activity.actualParticipants >= this.activity.maxParticipants) {
  192. uni.showModal({
  193. title: '报名已满',
  194. content: '当前活动报名人数已达上限,感谢您的关注,敬请期待后续活动~',
  195. showCancel: false
  196. })
  197. return
  198. }
  199. uni.showModal({
  200. title: '确认报名',
  201. content: `确认报名参加"${this.activity.name}"吗?`,
  202. success: async (res) => {
  203. if (res.confirm) {
  204. try {
  205. await api.activity.register(this.activityId, userId)
  206. uni.showModal({
  207. title: '报名成功',
  208. content: '恭喜您报名成功!',
  209. showCancel: false,
  210. success: () => {
  211. this.loadActivityDetail()
  212. }
  213. })
  214. } catch (error) {
  215. console.error('报名失败:', error)
  216. // 后端会返回友好的错误信息(通过Result类的message字段)
  217. const errorMsg = error.message || '报名失败,请稍后重试'
  218. uni.showModal({
  219. title: '提示',
  220. content: errorMsg,
  221. showCancel: false
  222. })
  223. }
  224. }
  225. }
  226. })
  227. },
  228. // 返回
  229. goBack() {
  230. uni.navigateBack()
  231. },
  232. // 图片加载错误处理
  233. handleImageError(e) {
  234. console.warn('活动封面图片加载失败:', e)
  235. this.activity.coverImage = DEFAULT_IMAGES.activity || 'http://115.190.125.125:9001/static-images/default-activity.jpg'
  236. },
  237. // 图片加载成功
  238. handleImageLoad(e) {
  239. console.log('活动封面图片加载成功')
  240. }
  241. }
  242. }
  243. </script>
  244. <style lang="scss" scoped>
  245. .activity-detail-page {
  246. min-height: 100vh;
  247. background-color: #FFF9F9;
  248. padding-top: 90rpx;
  249. padding-bottom: 120rpx;
  250. }
  251. /* 自定义导航栏 */
  252. .custom-navbar {
  253. position: fixed;
  254. top: 0;
  255. left: 0;
  256. right: 0;
  257. height: 90rpx;
  258. display: flex;
  259. align-items: center;
  260. justify-content: space-between;
  261. padding: 0 20rpx;
  262. background-color: #E91E63;
  263. z-index: 999;
  264. .navbar-left,
  265. .navbar-right {
  266. width: 80rpx;
  267. }
  268. .back-icon {
  269. font-size: 40rpx;
  270. color: #FFFFFF;
  271. font-weight: bold;
  272. }
  273. .navbar-title {
  274. flex: 1;
  275. text-align: center;
  276. font-size: 32rpx;
  277. font-weight: bold;
  278. color: #FFFFFF;
  279. }
  280. }
  281. /* 活动封面 */
  282. .activity-cover {
  283. position: relative;
  284. width: 100%;
  285. height: 500rpx;
  286. .cover-image {
  287. width: 100%;
  288. height: 100%;
  289. }
  290. .cover-mask {
  291. position: absolute;
  292. top: 0;
  293. left: 0;
  294. right: 0;
  295. bottom: 0;
  296. background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3), transparent);
  297. padding: 30rpx;
  298. .activity-tag {
  299. display: inline-block;
  300. padding: 10rpx 20rpx;
  301. background: linear-gradient(135deg, #FF6B6B 0%, #FF9800 100%);
  302. color: #FFFFFF;
  303. font-size: 24rpx;
  304. font-weight: bold;
  305. border-radius: 30rpx;
  306. }
  307. }
  308. }
  309. /* 活动内容 */
  310. .activity-content {
  311. padding: 30rpx;
  312. background-color: #FFFFFF;
  313. margin: 20rpx;
  314. border-radius: 20rpx;
  315. .activity-title {
  316. font-size: 36rpx;
  317. font-weight: bold;
  318. color: #333333;
  319. margin-bottom: 30rpx;
  320. line-height: 1.5;
  321. }
  322. .info-section {
  323. .info-item {
  324. display: flex;
  325. align-items: center;
  326. margin-bottom: 20rpx;
  327. font-size: 28rpx;
  328. .info-icon {
  329. font-size: 32rpx;
  330. margin-right: 10rpx;
  331. }
  332. .info-label {
  333. color: #666666;
  334. }
  335. .info-value {
  336. color: #333333;
  337. flex: 1;
  338. &.price {
  339. color: #E91E63;
  340. font-weight: bold;
  341. font-size: 32rpx;
  342. }
  343. }
  344. }
  345. }
  346. .divider {
  347. height: 1rpx;
  348. background-color: #F0F0F0;
  349. margin: 30rpx 0;
  350. }
  351. .section-title {
  352. font-size: 30rpx;
  353. font-weight: bold;
  354. color: #333333;
  355. margin-bottom: 20rpx;
  356. }
  357. .description-text,
  358. .notes-text {
  359. font-size: 28rpx;
  360. color: #666666;
  361. line-height: 1.8;
  362. white-space: pre-wrap;
  363. }
  364. }
  365. /* 底部报名按钮 */
  366. .bottom-bar {
  367. position: fixed;
  368. bottom: 0;
  369. left: 0;
  370. right: 0;
  371. display: flex;
  372. align-items: center;
  373. justify-content: space-between;
  374. padding: 20rpx 30rpx;
  375. background-color: #FFFFFF;
  376. border-top: 1rpx solid #F0F0F0;
  377. z-index: 999;
  378. .price-info {
  379. flex: 1;
  380. .price-label {
  381. display: block;
  382. font-size: 24rpx;
  383. color: #999999;
  384. margin-bottom: 5rpx;
  385. }
  386. .price-value {
  387. color: #E91E63;
  388. font-weight: bold;
  389. .price-symbol {
  390. font-size: 28rpx;
  391. }
  392. .price-num {
  393. font-size: 40rpx;
  394. }
  395. }
  396. }
  397. .register-btn {
  398. padding: 20rpx 60rpx;
  399. background: linear-gradient(135deg, #E91E63 0%, #FF6B6B 100%);
  400. border-radius: 50rpx;
  401. box-shadow: 0 8rpx 16rpx rgba(233, 30, 99, 0.3);
  402. .btn-text {
  403. font-size: 32rpx;
  404. font-weight: bold;
  405. color: #FFFFFF;
  406. }
  407. }
  408. }
  409. </style>