activities.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <template>
  2. <view class="activities-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">
  10. <view class="points-display">
  11. <text class="points-label">积分</text>
  12. <text class="points-value">{{ currentPoints }}</text>
  13. </view>
  14. </view>
  15. </view>
  16. <!-- 顶部公告 -->
  17. <view class="announcement-bar">
  18. <text class="announcement-icon">📢</text>
  19. <text class="announcement-content">红娘活动报名开始啦!用积分兑换活动名额,积分兑好礼!</text>
  20. </view>
  21. <!-- 活动分类标签 - 横向滚动 -->
  22. <scroll-view scroll-x class="category-scroll" show-scrollbar="false">
  23. <view class="category-list">
  24. <view
  25. v-for="(tab, index) in tabs"
  26. :key="index"
  27. class="category-item"
  28. :class="{ active: activeTab === tab.type }"
  29. @click="switchTab(tab.type)"
  30. >
  31. {{ tab.name }}
  32. </view>
  33. </view>
  34. </scroll-view>
  35. <!-- 活动列表 -->
  36. <view class="activity-grid">
  37. <view class="activity-card" v-for="(item, index) in activityList" :key="index" @click="goToDetail(item)">
  38. <image :src="item.coverImage || item.cover_image" class="activity-image" mode="aspectFill"></image>
  39. <view class="activity-info">
  40. <view class="activity-name">{{ item.name }}</view>
  41. <view class="activity-meta">
  42. <text class="activity-location">📍 {{ item.location }}</text>
  43. </view>
  44. <view class="activity-footer">
  45. <view class="activity-points">
  46. <text class="points-symbol">💎</text>
  47. <text class="points-value">{{ item.points }}</text>
  48. </view>
  49. <text class="activity-participants">{{ item.participants || 0 }}人参加</text>
  50. </view>
  51. <view class="activity-btn"
  52. :class="{ 'btn-disabled': hasExchanged(item.id) }"
  53. @click.stop="handleActivityAction(item)">
  54. {{ hasExchanged(item.id) ? '已报名' : '积分兑换' }}
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. <!-- 空状态 -->
  60. <view class="empty-state" v-if="activityList.length === 0 && !loading">
  61. <image src="https://img.icons8.com/color/96/000000/calendar--v1.png" class="empty-icon"></image>
  62. <text class="empty-text">暂无活动</text>
  63. <text class="empty-subtext">敬请期待更多精彩活动</text>
  64. </view>
  65. <!-- 加载更多 -->
  66. <view class="load-more" v-if="hasMore && activityList.length > 0">
  67. <text class="load-text">{{ loading ? '加载中...' : '上拉加载更多' }}</text>
  68. </view>
  69. <view class="no-more" v-else-if="activityList.length > 0">
  70. <text class="no-more-text">没有更多了</text>
  71. </view>
  72. </view>
  73. </template>
  74. <script>
  75. import api from '../../utils/api.js'
  76. export default {
  77. data() {
  78. return {
  79. activityList: [],
  80. pageNum: 1,
  81. pageSize: 10,
  82. hasMore: true,
  83. loading: false,
  84. currentPoints: 0,
  85. makerId: null,
  86. exchangedActivities: [],
  87. tabs: [
  88. { name: '全部活动', type: 'all' },
  89. { name: '推荐活动', type: 'premium' }
  90. ],
  91. activeTab: 'all'
  92. }
  93. },
  94. onLoad() {
  95. this.initData()
  96. },
  97. onReachBottom() {
  98. if (this.hasMore && !this.loading) {
  99. this.pageNum++
  100. this.loadActivityList()
  101. }
  102. },
  103. methods: {
  104. // 初始化数据
  105. async initData() {
  106. const userInfo = uni.getStorageSync('userInfo')
  107. // 兼容多种字段名
  108. this.makerId = userInfo && (userInfo.matchmakerId || userInfo.makerId || userInfo.matchmaker_id)
  109. // 如果没有makerId,尝试通过userId获取
  110. if (!this.makerId && userInfo && userInfo.userId) {
  111. try {
  112. const res = await api.matchmaker.getByUserId(userInfo.userId)
  113. let matchmaker = res
  114. if (res && res.data) {
  115. matchmaker = res.data
  116. }
  117. if (matchmaker) {
  118. this.makerId = matchmaker.matchmakerId || matchmaker.matchmaker_id
  119. }
  120. } catch (e) {
  121. console.error('获取红娘信息失败:', e)
  122. }
  123. }
  124. if (!this.makerId) {
  125. uni.showToast({
  126. title: '请先登录红娘账号',
  127. icon: 'none'
  128. })
  129. return
  130. }
  131. await this.loadMakerInfo()
  132. await this.loadExchangedActivities()
  133. await this.loadActivityList()
  134. },
  135. // 加载红娘信息(包括积分)
  136. async loadMakerInfo() {
  137. try {
  138. const res = await api.matchmaker.getDetail(this.makerId)
  139. // 兼容不同的返回格式
  140. const makerInfo = res && res.data ? res.data : res
  141. if (makerInfo) {
  142. this.currentPoints = makerInfo.points || makerInfo.point || 0
  143. console.log('红娘积分:', this.currentPoints)
  144. }
  145. } catch (error) {
  146. console.error('加载红娘信息失败:', error)
  147. this.currentPoints = 0
  148. }
  149. },
  150. // 加载已兑换的活动列表
  151. async loadExchangedActivities() {
  152. try {
  153. const list = await api.matchmakerActivity.getPurchasedList(this.makerId)
  154. if (list && list.length > 0) {
  155. this.exchangedActivities = list.map(item => item.activity_id || item.activityId)
  156. }
  157. } catch (error) {
  158. console.error('加载已兑换活动失败:', error)
  159. this.exchangedActivities = []
  160. }
  161. },
  162. // 加载活动列表
  163. async loadActivityList() {
  164. if (this.loading) return
  165. this.loading = true
  166. try {
  167. const params = {
  168. categoryType: this.activeTab
  169. }
  170. const data = await api.matchmakerActivity.getList(params)
  171. if (data && data.length > 0) {
  172. this.activityList = [...this.activityList, ...data]
  173. this.hasMore = data.length >= this.pageSize
  174. } else {
  175. this.hasMore = false
  176. }
  177. } catch (error) {
  178. console.error('加载活动列表失败:', error)
  179. this.hasMore = false
  180. } finally {
  181. this.loading = false
  182. }
  183. },
  184. // 切换分类标签
  185. switchTab(type) {
  186. if (this.activeTab === type) return
  187. this.activeTab = type
  188. this.pageNum = 1
  189. this.activityList = []
  190. this.loadActivityList()
  191. },
  192. // 检查是否已兑换
  193. hasExchanged(activityId) {
  194. return this.exchangedActivities.includes(activityId)
  195. },
  196. // 处理活动操作(兑换或查看详情)
  197. async handleActivityAction(item) {
  198. if (this.hasExchanged(item.id)) {
  199. // 已兑换,跳转到详情页
  200. uni.navigateTo({
  201. url: `/pages/matchmaker-workbench/activity-detail?id=${item.id}`
  202. })
  203. } else {
  204. // 未兑换,显示兑换确认
  205. uni.showModal({
  206. title: '确认兑换',
  207. content: `是否用 ${item.points} 积分兑换"${item.name}"活动?`,
  208. confirmText: '确认',
  209. cancelText: '取消',
  210. success: (res) => {
  211. if (res.confirm) {
  212. this.exchangeActivity(item)
  213. }
  214. }
  215. })
  216. }
  217. },
  218. // 兑换活动
  219. async exchangeActivity(item) {
  220. const pointsNeeded = item.points || 0
  221. if (this.currentPoints < pointsNeeded) {
  222. uni.showToast({
  223. title: '积分不足',
  224. icon: 'none'
  225. })
  226. return
  227. }
  228. try {
  229. console.log('兑换参数:', {
  230. makerId: this.makerId,
  231. activityId: item.id,
  232. points: pointsNeeded
  233. })
  234. const result = await api.matchmakerActivity.exchange({
  235. makerId: this.makerId,
  236. activityId: item.id,
  237. points: pointsNeeded
  238. })
  239. console.log('兑换结果:', result)
  240. // API请求成功(request函数已处理code=200的情况,能走到这里说明已成功)
  241. uni.showToast({
  242. title: '兑换成功',
  243. icon: 'success'
  244. })
  245. this.currentPoints -= pointsNeeded
  246. this.exchangedActivities.push(item.id)
  247. // 跳转到详情页
  248. setTimeout(() => {
  249. uni.navigateTo({
  250. url: `/pages/matchmaker-workbench/activity-detail?id=${item.id}`
  251. })
  252. }, 500)
  253. } catch (error) {
  254. console.error('兑换失败:', error)
  255. uni.showToast({
  256. title: error.message || '兑换失败',
  257. icon: 'none'
  258. })
  259. }
  260. },
  261. // 跳转到活动详情
  262. goToDetail(item) {
  263. uni.navigateTo({
  264. url: `/pages/matchmaker-workbench/activity-detail?id=${item.id}`
  265. })
  266. },
  267. // 返回
  268. goBack() {
  269. uni.navigateBack()
  270. }
  271. }
  272. }
  273. </script>
  274. <style lang="scss" scoped>
  275. .activities-page {
  276. min-height: 100vh;
  277. background-color: #FFF9F9;
  278. padding-top: 90rpx;
  279. }
  280. /* 自定义导航栏 */
  281. .custom-navbar {
  282. position: fixed;
  283. top: 0;
  284. left: 0;
  285. right: 0;
  286. height: 90rpx;
  287. display: flex;
  288. align-items: center;
  289. justify-content: space-between;
  290. padding: 0 20rpx;
  291. background: linear-gradient(135deg, #FCE4EC 0%, #F8BBD0 100%);
  292. z-index: 999;
  293. .navbar-left,
  294. .navbar-right {
  295. width: 80rpx;
  296. }
  297. .back-icon {
  298. font-size: 40rpx;
  299. color: #333333;
  300. font-weight: bold;
  301. }
  302. .navbar-title {
  303. flex: 1;
  304. text-align: center;
  305. font-size: 32rpx;
  306. font-weight: bold;
  307. color: #333333;
  308. }
  309. .points-display {
  310. display: flex;
  311. align-items: center;
  312. gap: 8rpx;
  313. background-color: rgba(255, 255, 255, 0.8);
  314. padding: 8rpx 16rpx;
  315. border-radius: 20rpx;
  316. .points-label {
  317. font-size: 24rpx;
  318. color: #666666;
  319. }
  320. .points-value {
  321. font-size: 28rpx;
  322. color: #FF6B8A;
  323. font-weight: bold;
  324. }
  325. }
  326. }
  327. /* 公告栏 */
  328. .announcement-bar {
  329. display: flex;
  330. align-items: center;
  331. gap: 12rpx;
  332. padding: 16rpx 20rpx;
  333. background-color: #FFF3E0;
  334. margin: 10rpx 0;
  335. .announcement-icon {
  336. font-size: 28rpx;
  337. flex-shrink: 0;
  338. }
  339. .announcement-content {
  340. font-size: 24rpx;
  341. color: #E65100;
  342. flex: 1;
  343. }
  344. }
  345. /* 分类标签 - 横向滚动 */
  346. .category-scroll {
  347. white-space: nowrap;
  348. padding: 0 20rpx;
  349. margin: 10rpx 0;
  350. }
  351. .category-list {
  352. display: flex;
  353. gap: 12rpx;
  354. }
  355. .category-item {
  356. display: inline-block;
  357. padding: 12rpx 24rpx;
  358. background-color: #FFFFFF;
  359. border-radius: 30rpx;
  360. font-size: 26rpx;
  361. color: #666666;
  362. border: 2rpx solid #EEEEEE;
  363. transition: all 0.3s ease;
  364. &.active {
  365. background-color: #9C27B0;
  366. color: #FFFFFF;
  367. border-color: #9C27B0;
  368. }
  369. }
  370. /* 活动列表 - 两列布局 */
  371. .activity-grid {
  372. display: grid;
  373. grid-template-columns: 1fr 1fr;
  374. gap: 20rpx;
  375. padding: 20rpx;
  376. .activity-card {
  377. position: relative;
  378. background-color: #FFFFFF;
  379. border-radius: 20rpx;
  380. overflow: hidden;
  381. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  382. .activity-image {
  383. width: 100%;
  384. height: 240rpx;
  385. background-color: #F5F5F5;
  386. }
  387. .activity-info {
  388. padding: 20rpx;
  389. .activity-name {
  390. font-size: 28rpx;
  391. font-weight: bold;
  392. color: #333333;
  393. margin-bottom: 10rpx;
  394. display: -webkit-box;
  395. -webkit-box-orient: vertical;
  396. -webkit-line-clamp: 2;
  397. overflow: hidden;
  398. }
  399. .activity-meta {
  400. margin-bottom: 15rpx;
  401. .activity-location {
  402. font-size: 24rpx;
  403. color: #666666;
  404. }
  405. }
  406. .activity-footer {
  407. display: flex;
  408. justify-content: space-between;
  409. align-items: center;
  410. margin-bottom: 15rpx;
  411. .activity-points {
  412. display: flex;
  413. align-items: center;
  414. .points-symbol {
  415. font-size: 24rpx;
  416. margin-right: 5rpx;
  417. }
  418. .points-value {
  419. font-size: 26rpx;
  420. color: #FF6B8A;
  421. font-weight: bold;
  422. }
  423. }
  424. .activity-participants {
  425. font-size: 22rpx;
  426. color: #999999;
  427. }
  428. }
  429. .activity-btn {
  430. width: 100%;
  431. height: 60rpx;
  432. display: flex;
  433. align-items: center;
  434. justify-content: center;
  435. background-color: #9C27B0;
  436. color: #FFFFFF;
  437. border-radius: 30rpx;
  438. font-size: 26rpx;
  439. transition: all 0.3s ease;
  440. &:active {
  441. background-color: #7B1FA2;
  442. }
  443. &.btn-disabled {
  444. background-color: #CCCCCC;
  445. color: #999999;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. /* 空状态 */
  452. .empty-state {
  453. display: flex;
  454. flex-direction: column;
  455. align-items: center;
  456. justify-content: center;
  457. padding: 100rpx 0;
  458. .empty-icon {
  459. width: 120rpx;
  460. height: 120rpx;
  461. opacity: 0.5;
  462. margin-bottom: 20rpx;
  463. }
  464. .empty-text {
  465. font-size: 32rpx;
  466. color: #666666;
  467. margin-bottom: 10rpx;
  468. }
  469. .empty-subtext {
  470. font-size: 26rpx;
  471. color: #999999;
  472. }
  473. }
  474. /* 加载更多 */
  475. .load-more,
  476. .no-more {
  477. padding: 30rpx 0;
  478. text-align: center;
  479. .load-text,
  480. .no-more-text {
  481. font-size: 24rpx;
  482. color: #999999;
  483. }
  484. }
  485. </style>