crowdFunding.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <template>
  2. <view class="crowd-funding-page">
  3. <!-- 头部导航 -->
  4. <view class="navbar" ref="header">
  5. <view style="display: flex; align-items: center">
  6. <view class="nav-left" @click="goBack">
  7. <text class="fa fa-angle-left back-icon"></text>
  8. </view>
  9. <view class="nav-title">众筹</view>
  10. </view>
  11. <!-- 搜索框 -->
  12. <view class="search-bar-container" @click="goPages('/pages/crowdFunding/Search')">
  13. <view class="search-input-wrapper">
  14. <image src="/static/crowdFunding/search.png" class="search-icon" mode="heightFix"></image>
  15. <input type="text" placeholder="搜索你感兴趣的内容" class="search-input" />
  16. </view>
  17. </view>
  18. <view class="nav-right" @click="goPages('/pages/crowdFunding/favorites')">
  19. <image src="/static/crowdFunding/collect-active1.png" class="action-icon"></image>
  20. </view>
  21. </view>
  22. <!-- Tab导航 -->
  23. <view class="tab-box" ref="tabbar">
  24. <scroll-view scroll-x class="tabs-scroll-view" :show-scrollbar="false"
  25. :scroll-into-view="'tab-' + (currentTab - 1)" scroll-with-animation>
  26. <view class="tabs-wrapper">
  27. <view v-for="(tab, index) in tabs" :key="index" :id="'tab-' + index"
  28. :class="['tab-item', currentTab === index ? 'active' : '']" @click="switchTab(index)">
  29. <view>
  30. <image v-if="tab.icon" :src="tab.icon" class="tab-icon" />{{ tab.name }}
  31. </view>
  32. </view>
  33. <!-- 右侧占位空白 -->
  34. <view class="tab-placeholder"></view>
  35. </view>
  36. </scroll-view>
  37. <view class="mask"></view>
  38. </view>
  39. <!-- 内容区域:swiper实现左右滑动切换tab,每个tab一个scroll-view,支持下拉刷新 -->
  40. <swiper class="tab-swiper" :current="currentTab" @change="onSwiperChange"
  41. :style="{ height: swiperHeight + 'px', width: '100vw' }">
  42. <swiper-item v-for="(tab, tabIndex) in tabs" :key="tabIndex">
  43. <scroll-view scroll-y class="content-scroll" :style="{ height: swiperHeight + 'px' }"
  44. :refresher-enabled="true" :refresher-triggered="isRefreshing[tabIndex]"
  45. refresher-background="#f2f6f2" @refresherrefresh="onRefresh(tabIndex)"
  46. @scroll="(e) => onScroll(e, tabIndex)"
  47. :scroll-top="shouldRestoreScroll[tabIndex] ? (scrollTop[tabIndex] || 0) : undefined"
  48. @scrolltolower="onScrollToLower(tabIndex)">
  49. <view class="items-grid">
  50. <CrowdFundingItem v-for="item in tabData[tabIndex]" :key="item.id" :item="item"
  51. @click="goToDetail(item.id)" />
  52. </view>
  53. <view v-if="loadingMore[tabIndex]" class="loading-more">加载中...</view>
  54. <view v-else-if="!hasMore[tabIndex]" class="no-more">没有更多了</view>
  55. </scroll-view>
  56. </swiper-item>
  57. </swiper>
  58. </view>
  59. </template>
  60. <script>
  61. import CrowdFundingItem from "./components/CrowdFundingItem/CrowdFundingItem.vue";
  62. export default {
  63. components: {
  64. CrowdFundingItem
  65. },
  66. data() {
  67. return {
  68. tabs: [],
  69. currentTab: 0,
  70. tabData: [],
  71. scrollTop: {},
  72. swiperHeight: 600,
  73. isRefreshing: [],
  74. shouldRestoreScroll: [],
  75. page: [],
  76. hasMore: [],
  77. loadingMore: [],
  78. pageSize: 20,
  79. keyword: '',
  80. };
  81. },
  82. mounted() {
  83. // 动态获取头部和tab栏高度
  84. const sys = uni.getSystemInfoSync();
  85. const windowHeight = sys.windowHeight;
  86. this.$nextTick(() => {
  87. uni.createSelectorQuery()
  88. .in(this)
  89. .select('.navbar')
  90. .boundingClientRect(rect1 => {
  91. uni.createSelectorQuery()
  92. .in(this)
  93. .select('.tab-box')
  94. .boundingClientRect(rect2 => {
  95. const headerHeight = rect1 ? rect1.height : 0;
  96. const tabbarHeight = rect2 ? rect2.height : 0;
  97. this.swiperHeight = windowHeight - headerHeight - tabbarHeight;
  98. })
  99. .exec();
  100. })
  101. .exec();
  102. });
  103. // 动态获取tabs
  104. this.getTabs();
  105. },
  106. methods: {
  107. getTabs() {
  108. uni.request({
  109. url: this.$apiHost + '/crowdfund/categories',
  110. method: 'GET',
  111. success: (res) => {
  112. console.log(res.data.data, "获取分类");
  113. if (res.data && res.data.success === 'yes' && Array.isArray(res.data.data.list)) {
  114. this.tabs = [{
  115. name: '全部',
  116. id: ''
  117. }, ...res.data.data.list.map(item => ({
  118. name: item.name,
  119. id: item.id,
  120. icon: item.icon
  121. }))];
  122. } else {
  123. this.tabs = [{
  124. name: '全部',
  125. id: ''
  126. }];
  127. }
  128. },
  129. fail: () => {
  130. this.tabs = [{
  131. name: '全部',
  132. id: ''
  133. }];
  134. },
  135. complete: () => {
  136. // 初始化tab相关数组长度
  137. const len = this.tabs.length;
  138. this.tabData = Array(len).fill([]);
  139. this.isRefreshing = Array(len).fill(false);
  140. this.shouldRestoreScroll = Array(len).fill(true);
  141. this.page = Array(len).fill(1);
  142. this.hasMore = Array(len).fill(true);
  143. this.loadingMore = Array(len).fill(false);
  144. // 加载第一个tab数据
  145. this.fetchData(0);
  146. }
  147. });
  148. },
  149. goBack() {
  150. uni.navigateBack();
  151. },
  152. goPages(url) {
  153. if (url === '/pages/crowdFunding/Search') {
  154. uni.$emit("check_login", () => {
  155. uni.navigateTo({
  156. url: '/pages/crowdFunding/Search',
  157. });
  158. });
  159. } else {
  160. uni.navigateTo({
  161. url,
  162. });
  163. }
  164. },
  165. switchTab(index) {
  166. this.currentTab = index;
  167. this.$set(this.shouldRestoreScroll, index, true);
  168. if (!this.tabData[index] || this.tabData[index].length === 0) {
  169. this.fetchData(index);
  170. }
  171. },
  172. onSwiperChange(e) {
  173. this.switchTab(e.detail.current);
  174. },
  175. async onRefresh(tabIndex) {
  176. this.$set(this.isRefreshing, tabIndex, true);
  177. await this.fetchData(tabIndex, true);
  178. this.$set(this.isRefreshing, tabIndex, false);
  179. },
  180. onScroll(e, tabIndex) {
  181. this.$set(this.scrollTop, tabIndex, e.detail.scrollTop);
  182. if (this.shouldRestoreScroll[tabIndex]) {
  183. this.$set(this.shouldRestoreScroll, tabIndex, false);
  184. }
  185. },
  186. async onScrollToLower(tabIndex) {
  187. if (!this.hasMore[tabIndex] || this.loadingMore[tabIndex]) return;
  188. this.$set(this.loadingMore, tabIndex, true);
  189. await this.fetchData(tabIndex, false, true);
  190. this.$set(this.loadingMore, tabIndex, false);
  191. },
  192. async fetchData(tabIndex, isRefresh = false, isLoadMore = false) {
  193. if (isRefresh) {
  194. this.page[tabIndex] = 1;
  195. this.hasMore[tabIndex] = true;
  196. }
  197. if (isLoadMore) {
  198. this.page[tabIndex]++;
  199. } else {
  200. this.page[tabIndex] = 1;
  201. }
  202. const params = {
  203. page: this.page[tabIndex],
  204. pageSize: this.pageSize,
  205. category_id: this.tabs[tabIndex]?.id || '',
  206. keyword: this.keyword
  207. };
  208. try {
  209. const [err, res] = await uni.request({
  210. url: this.$apiHost + '/crowdfund/list',
  211. method: 'GET',
  212. data: params
  213. });
  214. if (!err && res && res.data && res.data.data) {
  215. let list = res.data.data.list;
  216. console.log(list, "获取列表");
  217. if (isLoadMore) {
  218. this.$set(this.tabData, tabIndex, [...this.tabData[tabIndex], ...list]);
  219. } else {
  220. this.$set(this.tabData, tabIndex, list);
  221. }
  222. this.$set(this.hasMore, tabIndex, list.length === this.pageSize);
  223. } else {
  224. this.$set(this.hasMore, tabIndex, false);
  225. }
  226. } catch (e) {
  227. this.$set(this.hasMore, tabIndex, false);
  228. }
  229. },
  230. goToDetail(id) {
  231. // 跳转详情页
  232. uni.navigateTo({
  233. url: '/pages/crowdFunding/crowdfundingDetails?id=' + id
  234. });
  235. },
  236. // 搜索输入事件
  237. onSearchInput(e) {
  238. this.keyword = e.detail.value;
  239. this.fetchData(this.currentTab, true);
  240. },
  241. },
  242. };
  243. </script>
  244. <style lang="scss" scoped>
  245. .crowd-funding-page {
  246. display: flex;
  247. flex-direction: column;
  248. background: #f2f6f2;
  249. height: 100vh; // 移除,避免撑死内容
  250. }
  251. .navbar {
  252. display: flex;
  253. align-items: center;
  254. justify-content: space-between;
  255. // height: 96rpx;
  256. background: #fff;
  257. padding: 46rpx 20rpx 26rpx 30rpx;
  258. padding-top: calc(var(--status-bar-height) + 46rpx);
  259. box-sizing: content-box;
  260. .nav-left {
  261. width: 36rpx;
  262. display: flex;
  263. align-items: center;
  264. .back-icon {
  265. font-size: 50rpx;
  266. }
  267. }
  268. .nav-title {
  269. font-size: 36rpx;
  270. font-weight: bold;
  271. color: #222;
  272. letter-spacing: 2rpx;
  273. }
  274. .search-bar-container {
  275. padding: 0 24rpx;
  276. display: flex;
  277. align-items: center;
  278. width: 450rpx;
  279. height: 64rpx;
  280. background: #f2f6f2;
  281. border-radius: 36rpx;
  282. ::v-deep.input-placeholder {
  283. color: #999;
  284. }
  285. .search-input-wrapper {
  286. display: flex;
  287. align-items: center;
  288. width: 100%;
  289. .search-icon {
  290. width: 32rpx;
  291. height: 32rpx;
  292. margin-right: 10rpx;
  293. }
  294. .search-input {
  295. font-size: 28rpx;
  296. color: #999;
  297. line-height: 64rpx;
  298. background: transparent;
  299. border: none;
  300. outline: none;
  301. }
  302. }
  303. }
  304. .nav-right {
  305. .action-icon {
  306. width: 64rpx;
  307. height: 64rpx;
  308. border-radius: 50%;
  309. }
  310. }
  311. }
  312. .tab-box {
  313. width: 100vw;
  314. height: auto;
  315. position: relative;
  316. left: 0;
  317. top: 0;
  318. border-bottom: solid 5rpx #F2F6F2;
  319. .mask {
  320. position: absolute;
  321. right: 0;
  322. top: 0;
  323. width: 200rpx;
  324. height: 100%;
  325. background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, #ffffff 100%);
  326. pointer-events: none;
  327. }
  328. }
  329. .tabs-scroll-view {
  330. width: 100%;
  331. background: #fff;
  332. padding-bottom: 22rpx;
  333. .tabs-wrapper {
  334. display: flex;
  335. align-items: center;
  336. padding-left: 12rpx;
  337. padding-right: 200rpx;
  338. box-sizing: content-box;
  339. }
  340. }
  341. .tab-item {
  342. display: flex;
  343. align-items: center;
  344. font-size: 28rpx;
  345. color: #666;
  346. padding: 8rpx 28rpx;
  347. margin-right: 8rpx;
  348. border-radius: 32rpx;
  349. font-weight: 500;
  350. background: transparent;
  351. transition: background 0.2s, color 0.2s;
  352. view {
  353. white-space: nowrap;
  354. display: flex;
  355. align-items: center;
  356. .tab-icon {
  357. width: 40rpx;
  358. height: 40rpx;
  359. margin: 0;
  360. padding: 0;
  361. display: inline-block;
  362. }
  363. }
  364. text-wrap: nowrap;
  365. .tab-icon {
  366. width: 32rpx;
  367. height: 32rpx;
  368. margin-right: 8rpx;
  369. }
  370. &.active {
  371. font-weight: bold;
  372. color: #fff;
  373. background: #222;
  374. box-shadow: 0 2rpx 8rpx rgba(34, 34, 34, 0.08);
  375. .tab-icon {
  376. filter: brightness(1.5) saturate(2);
  377. }
  378. }
  379. }
  380. .tab-swiper {
  381. width: 100vw;
  382. background: #f2f6f2;
  383. }
  384. .swiper-item {
  385. background: #f2f6f2;
  386. }
  387. .content-scroll {
  388. overflow-y: auto;
  389. padding-bottom: 24rpx;
  390. background: #f2f6f2;
  391. }
  392. .items-grid {
  393. display: grid;
  394. grid-template-columns: repeat(2, 1fr);
  395. gap: 24rpx 12rpx;
  396. padding: 16rpx 12rpx 0 12rpx;
  397. background: #f2f6f2;
  398. }
  399. .tab-placeholder {
  400. width: 200rpx;
  401. flex-shrink: 0;
  402. height: 1px; // 不影响高度
  403. }
  404. .loading-more {
  405. text-align: center;
  406. color: #999;
  407. font-size: 26rpx;
  408. padding: 24rpx 0;
  409. }
  410. .no-more {
  411. text-align: center;
  412. color: #ccc;
  413. font-size: 24rpx;
  414. padding: 20rpx 0;
  415. }
  416. </style>