detail.vue 13 KB

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