crowdFunding.vue 11 KB

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