detail.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. gatewayURL: 'http://localhost:8083',
  79. activityId: null,
  80. activity: {
  81. name: '加载中...',
  82. coverImage: DEFAULT_IMAGES.activity,
  83. price: 0
  84. }
  85. }
  86. },
  87. onLoad(options) {
  88. if (options.id) {
  89. this.activityId = options.id
  90. this.loadActivityDetail()
  91. }
  92. },
  93. methods: {
  94. // 加载活动详情
  95. async loadActivityDetail() {
  96. try {
  97. const data = await api.activity.getDetail(this.activityId)
  98. if (data) {
  99. // 处理字段映射,确保前端使用的字段名统一
  100. this.activity = {
  101. ...data,
  102. coverImage: this.validateImageUrl(data.coverImage || data.cover_image || data.imageUrl || data.image_url),
  103. startTime: data.startTime || data.start_time || data.startDate || data.start_date || '',
  104. endTime: data.endTime || data.end_time || data.endDate || data.end_date || '',
  105. location: data.location || data.address || data.venue || '待定',
  106. maxParticipants: data.maxParticipants || data.max_participants || null,
  107. actualParticipants: data.actualParticipants || data.actual_participants || 0,
  108. price: data.price || data.fee || 0,
  109. description: data.description || data.desc || '暂无介绍',
  110. notes: data.notes || data.notice || '请准时参加活动'
  111. }
  112. console.log('活动详情加载成功:', this.activity)
  113. }
  114. } catch (error) {
  115. console.error('加载活动详情失败:', error)
  116. uni.showToast({
  117. title: '加载失败',
  118. icon: 'none'
  119. })
  120. }
  121. },
  122. // 验证并修正图片URL
  123. validateImageUrl(url) {
  124. if (!url || url === 'null' || url === 'undefined') {
  125. return DEFAULT_IMAGES.activity || 'http://115.190.125.125:9001/static-images/login-bg.png'
  126. }
  127. let cleanedUrl = String(url).trim()
  128. const httpIndex = cleanedUrl.indexOf('http')
  129. if (httpIndex > 0) {
  130. cleanedUrl = cleanedUrl.slice(httpIndex)
  131. }
  132. if (!cleanedUrl) {
  133. return DEFAULT_IMAGES.activity || 'http://115.190.125.125:9001/static-images/login-bg.png'
  134. }
  135. // 如果是相对路径,添加协议和域名
  136. if (cleanedUrl.startsWith('/')) {
  137. return `http://115.190.125.125:9000${cleanedUrl}`
  138. }
  139. // 如果已经是完整URL,直接返回
  140. if (cleanedUrl.startsWith('http://') || cleanedUrl.startsWith('https://')) {
  141. return cleanedUrl
  142. }
  143. // 其他情况返回默认图片
  144. return DEFAULT_IMAGES.activity || 'http://115.190.125.125:9001/static-images/login-bg.png'
  145. },
  146. // 格式化时间
  147. formatTime,
  148. // 格式化活动时间(显示开始和结束时间)
  149. formatActivityTime(startTime, endTime) {
  150. if (!startTime) return '时间待定'
  151. try {
  152. const start = new Date(startTime)
  153. const startMonth = start.getMonth() + 1
  154. const startDay = start.getDate()
  155. const startHour = start.getHours().toString().padStart(2, '0')
  156. const startMinute = start.getMinutes().toString().padStart(2, '0')
  157. let timeStr = `${startMonth}月${startDay}日 ${startHour}:${startMinute}`
  158. // 如果有结束时间,添加结束时间
  159. if (endTime) {
  160. const end = new Date(endTime)
  161. const endMonth = end.getMonth() + 1
  162. const endDay = end.getDate()
  163. const endHour = end.getHours().toString().padStart(2, '0')
  164. const endMinute = end.getMinutes().toString().padStart(2, '0')
  165. timeStr += ` - ${endMonth}月${endDay}日 ${endHour}:${endMinute}`
  166. }
  167. return timeStr
  168. } catch (error) {
  169. console.error('时间格式化失败:', error)
  170. return '时间待定'
  171. }
  172. },
  173. // 处理报名
  174. handleRegister() {
  175. // 获取当前用户ID
  176. const userId = userAuth.getUserId()
  177. if (!userId) {
  178. uni.showModal({
  179. title: '需要登录',
  180. content: '请先登录后再报名活动',
  181. showCancel: false,
  182. success: () => {
  183. uni.navigateTo({
  184. url: '/pages/page3/page3'
  185. })
  186. }
  187. })
  188. return
  189. }
  190. // 检查人数限制(前端预检查,提供更好的用户体验)
  191. if (this.activity.maxParticipants &&
  192. this.activity.actualParticipants >= this.activity.maxParticipants) {
  193. uni.showModal({
  194. title: '报名已满',
  195. content: '当前活动报名人数已达上限,感谢您的关注,敬请期待后续活动~',
  196. showCancel: false
  197. })
  198. return
  199. }
  200. uni.showModal({
  201. title: '确认报名',
  202. content: `确认报名参加"${this.activity.name}"吗?${this.activity.price > 0 ? `\n费用:¥${this.activity.price}` : ''}`,
  203. success: (res) => {
  204. if (res.confirm) {
  205. // 如果活动价格大于0,需要进行支付
  206. if (this.activity.price > 0) {
  207. this.requestPay(userId, this.activity)
  208. } else {
  209. // 免费活动直接报名
  210. this.freeRegister(userId)
  211. }
  212. }
  213. }
  214. })
  215. },
  216. // 免费活动报名
  217. async freeRegister(userId) {
  218. try {
  219. await api.activity.register(this.activityId, userId)
  220. uni.showModal({
  221. title: '报名成功',
  222. content: '恭喜您报名成功!',
  223. showCancel: false,
  224. success: () => {
  225. this.loadActivityDetail()
  226. }
  227. })
  228. } catch (error) {
  229. console.error('报名失败:', error)
  230. const errorMsg = error.message || '报名失败,请稍后重试'
  231. uni.showModal({
  232. title: '提示',
  233. content: errorMsg,
  234. showCancel: false
  235. })
  236. }
  237. },
  238. /**
  239. * 请求支付参数并调起微信支付
  240. */
  241. requestPay(userId, activity) {
  242. uni.showLoading({
  243. title: '处理中...'
  244. })
  245. uni.request({
  246. url: this.gatewayURL + '/api/activity-order/create',
  247. method: 'POST',
  248. data: {
  249. userId: userId,
  250. activityId: this.activityId,
  251. activityName: activity.name,
  252. price: activity.price
  253. },
  254. header: {
  255. 'content-type': 'application/json',
  256. 'token': uni.getStorageSync('token') // 携带登录态
  257. },
  258. success: (res) => {
  259. uni.hideLoading()
  260. if (res.data && res.data.code === 200) {
  261. const payParams = res.data.ext || res.data.data // 后端返回的支付参数
  262. this.callWxPay(payParams, activity)
  263. } else {
  264. uni.showToast({
  265. title: res.data?.message || '获取支付参数失败',
  266. icon: 'none'
  267. })
  268. }
  269. },
  270. fail: (err) => {
  271. uni.hideLoading()
  272. console.error('请求支付参数失败:', err)
  273. uni.showToast({
  274. title: '网络请求失败',
  275. icon: 'none'
  276. })
  277. }
  278. })
  279. },
  280. /**
  281. * 调起微信支付组件
  282. */
  283. callWxPay(payParams, activity) {
  284. uni.requestPayment({
  285. provider: 'wxpay',
  286. timeStamp: payParams.timeStamp,
  287. nonceStr: payParams.nonceStr,
  288. package: payParams.package,
  289. signType: payParams.signType,
  290. paySign: payParams.paySign,
  291. success: (payResult) => {
  292. if (payResult.errMsg === 'requestPayment:ok') {
  293. // 支付成功后处理
  294. uni.showModal({
  295. title: '支付成功',
  296. content: '恭喜您报名成功!',
  297. showCancel: false,
  298. success: () => {
  299. this.loadActivityDetail()
  300. // 可以跳转到我的活动列表或其他页面
  301. // uni.navigateTo({ url: '/pages/mine/my-activities' })
  302. }
  303. })
  304. }
  305. },
  306. fail: (err) => {
  307. console.error('支付失败:', err)
  308. if (err.errMsg !== 'requestPayment:fail cancel') {
  309. uni.showModal({
  310. title: '支付失败',
  311. content: '请稍后重试',
  312. showCancel: false
  313. })
  314. }
  315. }
  316. })
  317. },
  318. // 返回
  319. goBack() {
  320. uni.navigateBack()
  321. },
  322. // 图片加载错误处理
  323. handleImageError(e) {
  324. console.warn('活动封面图片加载失败:', e)
  325. this.activity.coverImage = DEFAULT_IMAGES.activity || 'http://115.190.125.125:9001/static-images/default-activity.jpg'
  326. },
  327. // 图片加载成功
  328. handleImageLoad(e) {
  329. console.log('活动封面图片加载成功')
  330. }
  331. }
  332. }
  333. </script>
  334. <style lang="scss" scoped>
  335. .activity-detail-page {
  336. min-height: 100vh;
  337. background-color: #FFF9F9;
  338. padding-top: 90rpx;
  339. padding-bottom: 120rpx;
  340. }
  341. /* 自定义导航栏 */
  342. .custom-navbar {
  343. position: fixed;
  344. top: 0;
  345. left: 0;
  346. right: 0;
  347. height: 90rpx;
  348. display: flex;
  349. align-items: center;
  350. justify-content: space-between;
  351. padding: 0 20rpx;
  352. background-color: #E91E63;
  353. z-index: 999;
  354. .navbar-left,
  355. .navbar-right {
  356. width: 80rpx;
  357. }
  358. .back-icon {
  359. font-size: 40rpx;
  360. color: #FFFFFF;
  361. font-weight: bold;
  362. }
  363. .navbar-title {
  364. flex: 1;
  365. text-align: center;
  366. font-size: 32rpx;
  367. font-weight: bold;
  368. color: #FFFFFF;
  369. }
  370. }
  371. /* 活动封面 */
  372. .activity-cover {
  373. position: relative;
  374. width: 100%;
  375. height: 500rpx;
  376. .cover-image {
  377. width: 100%;
  378. height: 100%;
  379. }
  380. .cover-mask {
  381. position: absolute;
  382. top: 0;
  383. left: 0;
  384. right: 0;
  385. bottom: 0;
  386. background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3), transparent);
  387. padding: 30rpx;
  388. .activity-tag {
  389. display: inline-block;
  390. padding: 10rpx 20rpx;
  391. background: linear-gradient(135deg, #FF6B6B 0%, #FF9800 100%);
  392. color: #FFFFFF;
  393. font-size: 24rpx;
  394. font-weight: bold;
  395. border-radius: 30rpx;
  396. }
  397. }
  398. }
  399. /* 活动内容 */
  400. .activity-content {
  401. padding: 30rpx;
  402. background-color: #FFFFFF;
  403. margin: 20rpx;
  404. border-radius: 20rpx;
  405. .activity-title {
  406. font-size: 36rpx;
  407. font-weight: bold;
  408. color: #333333;
  409. margin-bottom: 30rpx;
  410. line-height: 1.5;
  411. }
  412. .info-section {
  413. .info-item {
  414. display: flex;
  415. align-items: center;
  416. margin-bottom: 20rpx;
  417. font-size: 28rpx;
  418. .info-icon {
  419. font-size: 32rpx;
  420. margin-right: 10rpx;
  421. }
  422. .info-label {
  423. color: #666666;
  424. }
  425. .info-value {
  426. color: #333333;
  427. flex: 1;
  428. &.price {
  429. color: #E91E63;
  430. font-weight: bold;
  431. font-size: 32rpx;
  432. }
  433. }
  434. }
  435. }
  436. .divider {
  437. height: 1rpx;
  438. background-color: #F0F0F0;
  439. margin: 30rpx 0;
  440. }
  441. .section-title {
  442. font-size: 30rpx;
  443. font-weight: bold;
  444. color: #333333;
  445. margin-bottom: 20rpx;
  446. }
  447. .description-text,
  448. .notes-text {
  449. font-size: 28rpx;
  450. color: #666666;
  451. line-height: 1.8;
  452. white-space: pre-wrap;
  453. }
  454. }
  455. /* 底部报名按钮 */
  456. .bottom-bar {
  457. position: fixed;
  458. bottom: 0;
  459. left: 0;
  460. right: 0;
  461. display: flex;
  462. align-items: center;
  463. justify-content: space-between;
  464. padding: 20rpx 30rpx;
  465. background-color: #FFFFFF;
  466. border-top: 1rpx solid #F0F0F0;
  467. z-index: 999;
  468. .price-info {
  469. flex: 1;
  470. .price-label {
  471. display: block;
  472. font-size: 24rpx;
  473. color: #999999;
  474. margin-bottom: 5rpx;
  475. }
  476. .price-value {
  477. color: #E91E63;
  478. font-weight: bold;
  479. .price-symbol {
  480. font-size: 28rpx;
  481. }
  482. .price-num {
  483. font-size: 40rpx;
  484. }
  485. }
  486. }
  487. .register-btn {
  488. padding: 20rpx 60rpx;
  489. background: linear-gradient(135deg, #E91E63 0%, #FF6B6B 100%);
  490. border-radius: 50rpx;
  491. box-shadow: 0 8rpx 16rpx rgba(233, 30, 99, 0.3);
  492. .btn-text {
  493. font-size: 32rpx;
  494. font-weight: bold;
  495. color: #FFFFFF;
  496. }
  497. }
  498. }
  499. </style>