crowdFunding.vue 11 KB

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