绿满眶商城微信小程序-uniapp
zxl
2025-06-24 e50f021ddf73b80f5a1273b8b01e7dd4344a4cdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<template>
    <view class="wrapper">
        <view style="height: 100rpx"></view>
        <!-- 内容区域 -->
        <scroll-view scroll-y class="content" style="height: 40vh;" @scrolltolower="loadMore" :lower-threshold="100">
            <view class="waterfall">
            
                    <view class="item" v-for="(item, idx) in mockData" :key="item.id" @click="handleItemClick(item)">
                        <text class="title">{{ item.title }}</text>
                        <text class="publishDate">发布时间:{{ item.publishDate }}</text>
                    </view>
    
            </view>
            <!--     <view style="height: 150rpx;"></view> -->
            <!-- 改进的加载更多提示 -->
            <view class="load-more">
                <u-loadmore v-if="mockData.length > 0" :status="loading ? 'loading' : noMore ? 'nomore' : 'loadmore'"
                    :load-text="{
                        loadmore: '上拉加载更多',
                        loading: '正在加载',
                        nomore: '没有更多了'
                      }" />
            </view>
            <view style="height:150rpx">
        
            </view>
        </scroll-view>
    </view>
</template>
 
<script>
    import '@/components/uview-components/uview-ui';
    import { getNews,addNews,editNews,publish,delById } from '@/api/news.js'
    export default {
        data() {
            return {
                query: {
                    pageNumber: 1,
                    pageSize: 10,
                    publish:1,
                },
                loading:false,
                total:0,
                mockData:[],
                noMore:false,
                
            }
        },
        mounted() {
            
        },
        onLoad() {
            this.getNewsPage();
        },
        methods: {
            handleItemClick(item){
                uni.navigateTo({
                    url: `/pages/news/detail?id=${item.id}` // 参数通过 URL 传递
                });
            },
            async getNewsPage(){
                try {
                    const res = await getNews(this.query);
                    this.loading = false;
                    if(res.statusCode === 200){
                        const newData = res.data.data.map(value =>({
                            id:value.id,
                            title:value.title,
                            publish:value.publish,
                            publishDate:value.publishDate
                        }));
                        // 更新总数据量
                        this.total = res.data.total || 0;
                        // 追加或替换数据
                        this.mockData = this.query.pageNumber === 1 ?
                            newData :
                            [...this.mockData, ...newData];
                        
                        // 判断是否还有更多数据
                        this.noMore = newData.length < this.query.pageSize ||
                            this.mockData.length >= this.total;
                        
                    }
                    
                } catch (error) {
                    console.error('加载失败:', error);
                    // 失败时回退页码
                    if (this.query.pageNumber > 1) {
                        this.query.pageNumber -= 1;
                    }
                } finally {
                    this.loading = false;
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                }
                
            }
        }
    }
</script>
 
<style lang="scss" scoped>
.wrapper {
  padding: 0 20rpx;
  box-sizing: border-box;
  background-color: #f5f5f5;
  height: 100%;
  
  .content {
    padding-bottom: 20rpx;
    box-sizing: border-box;
    
    .waterfall {
      display: flex;
      flex-direction: column;
      padding: 0 10rpx;
      
      .item {
        background: #fff;
        border-radius: 12rpx;
        padding: 24rpx;
        margin-bottom: 20rpx;
        box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
        transition: transform 0.2s ease, box-shadow 0.2s ease;
        
        &:active {
          transform: scale(0.98);
          box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
        }
        
        .title {
          display: block;
          font-size: 28rpx;
          color: #333;
          font-weight: 500;
          line-height: 1.5;
          margin-bottom: 12rpx;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 3;
          -webkit-box-orient: vertical;
        }
        
        .publishDate {
          display: block;
          font-size: 22rpx;
          color: #999;
          text-align: right;
        }
      }
    }
    
    .load-more {
      padding: 30rpx 0;
      background-color: transparent;
      
      .u-loadmore {
        font-size: 26rpx !important;
        color: #999 !important;
      }
    }
  }
}
 
/* 平板及以上设备适配 */
@media (min-width: 768px) {
  .waterfall {
    flex-direction: row !important;
    flex-wrap: wrap;
    
    .item {
      width: calc(50% - 10rpx);
      margin: 0 5rpx 20rpx;
    }
  }
}
</style>