123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <template>
- <view class="crowd-funding-page">
- <!-- 头部导航 -->
- <view class="navbar" ref="header">
- <view style="display: flex; align-items: center">
- <view
- class="nav-left"
- @click="goBack"
- >
- <text class="fa fa-angle-left back-icon"></text>
- </view>
- <view class="nav-title">众筹</view>
- </view>
- <!-- 搜索框 -->
- <view
- class="search-bar-container"
- @click="goPages('/pages/crowdFunding/Search')"
- >
- <view class="search-input-wrapper" >
- <image
- src="/static/crowdFunding/search.png"
- class="search-icon"
- ></image >
- <input
- type="text"
- placeholder="搜索你感兴趣的内容"
- class="search-input"
-
- />
- </view>
- </view>
- <view class="nav-right" @click="goPages('/pages/crowdFunding/favorites')">
- <image
- src="/static/crowdFunding/collect-active1.png"
- class="action-icon"
- ></image>
- </view>
- </view>
- <!-- Tab导航 -->
- <view class="tab-box" ref="tabbar">
- <scroll-view
- scroll-x
- class="tabs-scroll-view"
- :show-scrollbar="false"
- :scroll-into-view="'tab-' + (currentTab - 1)"
- scroll-with-animation
- >
- <view class="tabs-wrapper">
- <view
- v-for="(tab, index) in tabs"
- :key="index"
- :id="'tab-' + index"
- :class="['tab-item', currentTab === index ? 'active' : '']"
- @click="switchTab(index)"
- >
- <view><image v-if="tab.icon" :src="tab.icon" class="tab-icon"/>{{ tab.name }}</view>
-
- </view>
- <!-- 右侧占位空白 -->
- <view class="tab-placeholder"></view>
- </view>
- </scroll-view>
- <view class="mask"></view>
- </view>
- <!-- 内容区域:swiper实现左右滑动切换tab,每个tab一个scroll-view,支持下拉刷新 -->
- <swiper
- class="tab-swiper"
- :current="currentTab"
- @change="onSwiperChange"
- :style="{ height: swiperHeight + 'px', width: '100vw' }"
- >
- <swiper-item
- v-for="(tab, tabIndex) in tabs"
- :key="tabIndex"
- >
- <scroll-view
- scroll-y
- class="content-scroll"
- :style="{ height: swiperHeight + 'px' }"
- :refresher-enabled="true"
- :refresher-triggered="isRefreshing[tabIndex]"
- refresher-background="#f2f6f2"
- @refresherrefresh="onRefresh(tabIndex)"
- @scroll="(e) => onScroll(e, tabIndex)"
- :scroll-top="shouldRestoreScroll[tabIndex] ? (scrollTop[tabIndex] || 0) : undefined"
- @scrolltolower="onScrollToLower(tabIndex)"
- >
- <view class="items-grid">
- <CrowdFundingItem
- v-for="item in tabData[tabIndex]"
- :key="item.id"
- :item="item"
- @click="goToDetail(item.id)"
- />
- </view>
- <view v-if="loadingMore[tabIndex]" class="loading-more">加载中...</view>
- <view v-else-if="!hasMore[tabIndex]" class="no-more">没有更多了</view>
- </scroll-view>
- </swiper-item>
- </swiper>
- </view>
- </template>
- <script>
- import CrowdFundingItem from "./components/CrowdFundingItem/CrowdFundingItem.vue";
- export default {
- components: { CrowdFundingItem },
- data() {
- return {
- tabs: [],
- currentTab: 0,
- tabData: [],
- scrollTop: {},
- swiperHeight: 600,
- isRefreshing: [],
- shouldRestoreScroll: [],
- page: [],
- hasMore: [],
- loadingMore: [],
- pageSize: 20,
- keyword: '',
- };
- },
- mounted() {
- // 动态获取头部和tab栏高度
- const sys = uni.getSystemInfoSync();
- const windowHeight = sys.windowHeight;
- this.$nextTick(() => {
- uni.createSelectorQuery()
- .in(this)
- .select('.navbar')
- .boundingClientRect(rect1 => {
- uni.createSelectorQuery()
- .in(this)
- .select('.tab-box')
- .boundingClientRect(rect2 => {
- const headerHeight = rect1 ? rect1.height : 0;
- const tabbarHeight = rect2 ? rect2.height : 0;
- this.swiperHeight = windowHeight - headerHeight - tabbarHeight;
- })
- .exec();
- })
- .exec();
- });
- // 动态获取tabs
- this.getTabs();
- },
- methods: {
- getTabs() {
- uni.request({
- url: this.$apiHost + '/crowdfund/categories',
- method: 'GET',
- success: (res) => {
- console.log(res.data.data,"获取分类");
-
- if (res.data && res.data.success === 'yes' && Array.isArray(res.data.data.list)) {
- this.tabs = [{ name: '全部', id: '' }, ...res.data.data.list.map(item => ({ name: item.name, id: item.id, icon: item.icon }))];
- } else {
- this.tabs = [{ name: '全部', id: '' }];
- }
- },
- fail: () => {
- this.tabs = [{ name: '全部', id: '' }];
- },
- complete: () => {
- // 初始化tab相关数组长度
- const len = this.tabs.length;
- this.tabData = Array(len).fill([]);
- this.isRefreshing = Array(len).fill(false);
- this.shouldRestoreScroll = Array(len).fill(true);
- this.page = Array(len).fill(1);
- this.hasMore = Array(len).fill(true);
- this.loadingMore = Array(len).fill(false);
- // 加载第一个tab数据
- this.fetchData(0);
- }
- });
- },
- goBack() {
- uni.navigateBack();
- },
- goPages(url) {
- if (url === '/pages/crowdFunding/Search') {
- uni.$emit("check_login", () => {
- uni.navigateTo({
- url: '/pages/crowdFunding/Search',
- });
- });
- } else {
- uni.navigateTo({
- url,
- });
- }
- },
- switchTab(index) {
- this.currentTab = index;
- this.$set(this.shouldRestoreScroll, index, true);
- if (!this.tabData[index] || this.tabData[index].length === 0) {
- this.fetchData(index);
- }
- },
- onSwiperChange(e) {
- this.switchTab(e.detail.current);
- },
- async onRefresh(tabIndex) {
- this.$set(this.isRefreshing, tabIndex, true);
- await this.fetchData(tabIndex, true);
- this.$set(this.isRefreshing, tabIndex, false);
- },
- onScroll(e, tabIndex) {
- this.$set(this.scrollTop, tabIndex, e.detail.scrollTop);
- if (this.shouldRestoreScroll[tabIndex]) {
- this.$set(this.shouldRestoreScroll, tabIndex, false);
- }
- },
- async onScrollToLower(tabIndex) {
- if (!this.hasMore[tabIndex] || this.loadingMore[tabIndex]) return;
- this.$set(this.loadingMore, tabIndex, true);
- await this.fetchData(tabIndex, false, true);
- this.$set(this.loadingMore, tabIndex, false);
- },
- async fetchData(tabIndex, isRefresh = false, isLoadMore = false) {
- if (isRefresh) {
- this.page[tabIndex] = 1;
- this.hasMore[tabIndex] = true;
- }
- if (isLoadMore) {
- this.page[tabIndex]++;
- } else {
- this.page[tabIndex] = 1;
- }
- const params = {
- page: this.page[tabIndex],
- pageSize: this.pageSize,
- category_id: this.tabs[tabIndex]?.id || '',
- keyword: this.keyword
- };
- try {
- const [err, res] = await uni.request({
- url: this.$apiHost + '/crowdfund/list',
- method: 'GET',
- data: params
- });
- if (!err && res && res.data && res.data.data) {
- let list = res.data.data.list;
- console.log(list,"获取列表");
- if (isLoadMore) {
- this.$set(this.tabData, tabIndex, [...this.tabData[tabIndex], ...list]);
- } else {
- this.$set(this.tabData, tabIndex, list);
- }
- this.$set(this.hasMore, tabIndex, list.length === this.pageSize);
- } else {
- this.$set(this.hasMore, tabIndex, false);
- }
- } catch (e) {
- this.$set(this.hasMore, tabIndex, false);
- }
- },
- goToDetail(id) {
- // 跳转详情页
- uni.navigateTo({ url: '/pages/crowdFunding/crowdfundingDetails?id=' + id });
- },
- // 搜索输入事件
- onSearchInput(e) {
- this.keyword = e.detail.value;
- this.fetchData(this.currentTab, true);
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- .crowd-funding-page {
- display: flex;
- flex-direction: column;
- background: #f2f6f2;
- height: 100vh; // 移除,避免撑死内容
- }
- .navbar {
- display: flex;
- align-items: center;
- justify-content: space-between;
- // height: 96rpx;
- background: #fff;
- padding: 46rpx 20rpx 26rpx 30rpx;
- padding-top: calc(var(--status-bar-height) + 46rpx);
- box-sizing: content-box;
- .nav-left {
- width: 36rpx;
- display: flex;
- align-items: center;
- .back-icon {
- font-size: 50rpx;
- }
- }
- .nav-title {
- font-size: 36rpx;
- font-weight: bold;
- color: #222;
- letter-spacing: 2rpx;
- }
- .search-bar-container {
- padding: 0 24rpx;
- display: flex;
- align-items: center;
- width: 450rpx;
- height: 64rpx;
- background: #f2f6f2;
- border-radius: 36rpx;
- ::v-deep.input-placeholder {
- color: #999;
- }
- .search-input-wrapper {
- display: flex;
- align-items: center;
- width: 100%;
- .search-icon {
- width: 32rpx;
- height: 32rpx;
- margin-right: 10rpx;
- }
- .search-input {
- font-size: 28rpx;
- color: #999;
- line-height: 64rpx;
- background: transparent;
- border: none;
- outline: none;
- width: 100%;
- }
- }
- }
- .nav-right {
- .action-icon {
- width: 64rpx;
- height: 64rpx;
- border-radius: 50%;
- }
- }
- }
- .tab-box {
- width: 100vw;
- height: auto;
- position: relative;
- left: 0;
- top: 0;
- border-bottom: solid 5rpx #F2F6F2;
- .mask {
- position: absolute;
- right: 0;
- top: 0;
- width: 200rpx;
- height: 100%;
- background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, #ffffff 100%);
- pointer-events: none;
- }
- }
- .tabs-scroll-view {
- width: 100%;
- background: #fff;
- padding-bottom: 22rpx;
- .tabs-wrapper {
- display: flex;
- align-items: center;
- padding-left: 12rpx;
- padding-right: 200rpx;
- box-sizing: content-box;
- }
- }
- .tab-item {
- display: flex;
- align-items: center;
- font-size: 28rpx;
- color: #666;
- padding: 8rpx 28rpx;
- margin-right: 8rpx;
- border-radius: 32rpx;
- font-weight: 500;
- background: transparent;
- transition: background 0.2s, color 0.2s;
- view {
- white-space: nowrap;
- display: flex;
- align-items: center;
- .tab-icon{
- width: 40rpx;
- height: 40rpx;
- margin: 0;
- padding: 0;
- display: inline-block;
- }
- }
- text-wrap: nowrap;
- .tab-icon {
- width: 32rpx;
- height: 32rpx;
- margin-right: 8rpx;
- }
- &.active {
- font-weight: bold;
- color: #fff;
- background: #222;
- box-shadow: 0 2rpx 8rpx rgba(34, 34, 34, 0.08);
- .tab-icon {
- filter: brightness(1.5) saturate(2);
- }
- }
- }
- .tab-swiper {
- width: 100vw;
- background: #f2f6f2;
- }
- .swiper-item {
- background: #f2f6f2;
- }
- .content-scroll {
- overflow-y: auto;
- padding-bottom: 24rpx;
- background: #f2f6f2;
- }
- .items-grid {
- display: grid;
- grid-template-columns: repeat(2, 1fr);
- gap: 24rpx 12rpx;
- padding: 16rpx 12rpx 0 12rpx;
- background: #f2f6f2;
- }
- .tab-placeholder {
- width: 200rpx;
- flex-shrink: 0;
- height: 1px; // 不影响高度
- }
- .loading-more { text-align: center; color: #999; font-size: 26rpx; padding: 24rpx 0; }
- .no-more { text-align: center; color: #ccc; font-size: 24rpx; padding: 20rpx 0; }
- </style>
|