crowdFunding.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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="widthFix"></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) + 26rpx);
  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. font-family: 'CustomFont';
  274. }
  275. .search-bar-container {
  276. padding: 0 24rpx;
  277. display: flex;
  278. align-items: center;
  279. width: 450rpx;
  280. height: 64rpx;
  281. background: #f2f6f2;
  282. border-radius: 36rpx;
  283. ::v-deep.input-placeholder {
  284. color: #999;
  285. }
  286. .search-input-wrapper {
  287. display: flex;
  288. align-items: center;
  289. width: 100%;
  290. .search-icon {
  291. width: 32rpx;
  292. height: 32rpx;
  293. margin-right: 10rpx;
  294. flex-shrink: 0; }
  295. .search-input {
  296. font-size: 28rpx;
  297. color: #999;
  298. line-height: 64rpx;
  299. background: transparent;
  300. border: none;
  301. outline: none;
  302. }
  303. }
  304. }
  305. .nav-right {
  306. .action-icon {
  307. width: 64rpx;
  308. height: 64rpx;
  309. border-radius: 50%;
  310. margin-top: 10rpx;
  311. }
  312. }
  313. }
  314. .tab-box {
  315. width: 100vw;
  316. height: auto;
  317. position: relative;
  318. left: 0;
  319. top: 0;
  320. border-bottom: solid 5rpx #F2F6F2;
  321. .mask {
  322. position: absolute;
  323. right: 0;
  324. top: 0;
  325. width: 200rpx;
  326. height: 100%;
  327. background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, #ffffff 100%);
  328. pointer-events: none;
  329. }
  330. }
  331. .tabs-scroll-view {
  332. width: 100%;
  333. background: #fff;
  334. padding-bottom: 22rpx;
  335. .tabs-wrapper {
  336. display: flex;
  337. align-items: center;
  338. padding-left: 12rpx;
  339. padding-right: 200rpx;
  340. box-sizing: content-box;
  341. }
  342. }
  343. .tab-item {
  344. display: flex;
  345. align-items: center;
  346. font-size: 28rpx;
  347. color: #666;
  348. padding: 8rpx 28rpx;
  349. margin-right: 8rpx;
  350. border-radius: 32rpx;
  351. font-weight: 500;
  352. background: transparent;
  353. transition: background 0.2s, color 0.2s;
  354. view {
  355. white-space: nowrap;
  356. display: flex;
  357. align-items: center;
  358. .tab-icon {
  359. width: 40rpx;
  360. height: 40rpx;
  361. margin: 0;
  362. padding: 0;
  363. display: inline-block;
  364. }
  365. }
  366. text-wrap: nowrap;
  367. .tab-icon {
  368. width: 32rpx;
  369. height: 32rpx;
  370. margin-right: 8rpx;
  371. }
  372. &.active {
  373. font-weight: bold;
  374. color: #fff;
  375. background: #222;
  376. box-shadow: 0 2rpx 8rpx rgba(34, 34, 34, 0.08);
  377. .tab-icon {
  378. // filter: brightness(1.5) saturate(2);
  379. }
  380. }
  381. }
  382. .tab-swiper {
  383. width: 100vw;
  384. background: #f2f6f2;
  385. }
  386. .swiper-item {
  387. background: #f2f6f2;
  388. }
  389. .content-scroll {
  390. overflow-y: auto;
  391. padding-bottom: 24rpx;
  392. background: #f2f6f2;
  393. }
  394. .items-grid {
  395. display: grid;
  396. grid-template-columns: repeat(2, 1fr);
  397. gap: 24rpx 12rpx;
  398. padding: 16rpx 12rpx 0 12rpx;
  399. background: #f2f6f2;
  400. }
  401. .tab-placeholder {
  402. width: 200rpx;
  403. flex-shrink: 0;
  404. height: 1px; // 不影响高度
  405. }
  406. .loading-more {
  407. text-align: center;
  408. color: #999;
  409. font-size: 26rpx;
  410. padding: 24rpx 0;
  411. }
  412. .no-more {
  413. text-align: center;
  414. color: #ccc;
  415. font-size: 24rpx;
  416. padding: 20rpx 0;
  417. }
  418. </style>