list-news.uvue 6.2 KB
Newer Older
W
微调  
wanganxp 已提交
1
<template>
W
wanganxp 已提交
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
  <list-view style="flex: 1;" refresher-enabled=true @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="refresherTriggered">
    <list-item class="banner" @click="bannerClick(banner)">
      <image class="banner-img" :src="banner.cover"></image>
      <text class="banner-title">{{ banner.title }}</text>
    </list-item>
    <sticky-header>
      <view style="width: 100%;height:44px;background-color: #f5f5f5;flex-direction: row;justify-content:center;align-items:center;">
        <button style="height: 40px;font-size: 13px;" v-for="(name,index) in th_item" @click="clickTH(index)">
          {{name}}
        </button>
      </view>
    </sticky-header>
    <list-item v-for="(value, index) in listData" :key="index">
      <view class="uni-list-cell" hover-class="uni-list-cell-hover" @click="goDetail(value)">
        <view class="uni-media-list">
          <image class="uni-media-list-logo" :src="value.cover"></image>
          <view class="uni-media-list-body">
            <text class="uni-media-list-text-top">{{ value.title }}</text>
            <view class="uni-media-list-text-bottom">
              <text class="uni-media-list-text">{{ value.author_name }}</text>
              <text class="uni-media-list-text">{{ value.published_at }}</text>
            </view>
          </view>
        </view>
      </view>
    </list-item>
  </list-view>
W
微调  
wanganxp 已提交
30 31
</template>

32
<script>
W
wanganxp 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45
  type Banner = {
    cover : string | null,
    title : string | null,
    post_id : string | null
  }
  type Item = {
    author_name : string,
    cover : string,
    id : number,
    post_id : string,
    published_at : string,
    title : string
  }
46

W
wanganxp 已提交
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
  export default {
    data() {
      return {
        th_item: ["排序", "筛选"],
        refresherTriggered: false,
        banner: {} as Banner,
        listData: [] as Item[],
        last_id: '',
        pageVisible: false
      };
    },
    onLoad() {
      this.pageVisible = true; //设这个变量是为了避免联网结束后页面已经关闭,此时还继续执行回调逻辑的问题。不是必须的
      this.getBanner();
      this.getList();
    },
    onUnload() {
      this.pageVisible = false;
    },
    methods: {
      getBanner() {
        let data = {
          column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名
        };
        uni.request<Banner>({
          url: 'https://unidemo.dcloud.net.cn/api/banner/36kr',
          data: data,
          success: data => {
            if (this.pageVisible) {
              this.refresherTriggered = false
              if (data.statusCode == 200) {
78
                const result = data.data
W
wanganxp 已提交
79
                if (result != null) {
80 81
                  this.banner = result;
                }
W
wanganxp 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
              }
            }
          },
          fail: (e) => {
            console.log('fail', e);
          }
        });
      },
      getList() {
        let url = "https://unidemo.dcloud.net.cn/api/news?column=id,post_id,title,author_name,cover,published_at";
        /* if (this.last_id != "") {
          const minId = parseInt((this.last_id))
          const time = new Date().getTime() + '';
          const pageSize = 10;
          url = url + "&minId=" + minId + "&time=" + time + "&pageSize=" + pageSize;
        } */
W
微调  
wanganxp 已提交
98

W
wanganxp 已提交
99 100 101 102 103 104
        uni.request<Item[]>({
          url: url,
          method: "GET",
          success: (res) => {
            if (this.pageVisible) {
              if (res.statusCode == 200) {
W
微调  
wanganxp 已提交
105
                console.log(res);
W
wanganxp 已提交
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
                const result = res.data
                if (result != null) {
                  this.listData = result //因本接口没有更多分页数据,所以重新赋值。正常有分页的列表应该如下面push方式增加数组项
                  // this.listData.push(...result)
                  // this.last_id = this.listData[0].id + "";
                }
                this.refresherTriggered = false;
              }
            }
          },
          fail: (res) => {
            if (this.pageVisible) {
              console.log('fail', res);
              this.refresherTriggered = false
            }
          }
        });
      },
      goDetail(e : Item) {
        const detail = e;
        const post_id = detail.post_id;
        const cover = detail.cover;
        const title = detail.title;
        uni.navigateTo({
          url: '/pages/template/list-news/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title
        });
      },
      bannerClick(e : Banner) {
        const detail = e;
        const post_id = detail.post_id;
        const cover = detail.cover;
        const title = detail.title;
        uni.navigateTo({
          url: '/pages/template/list-news/detail/detail?post_id=' + post_id + "&cover=" + cover + "&title=" + title
        });
      },
      clickTH(index:number){
        uni.showModal({
          content: "点击表头项:" + index,
          showCancel: false
        });
      },
      onRefresherrefresh() {
        if (this.pageVisible) {
          this.refresherTriggered = true
          this.getBanner();
          this.getList();
        }
      }
    }
  };
W
微调  
wanganxp 已提交
157 158 159
</script>

<style>
W
wanganxp 已提交
160 161 162 163 164 165
  .banner {
    height: 360rpx;
    overflow: hidden;
    position: relative;
    background-color: #ccc;
  }
W
微调  
wanganxp 已提交
166

W
wanganxp 已提交
167 168 169
  .banner-img {
    width: 100%;
  }
W
微调  
wanganxp 已提交
170

W
wanganxp 已提交
171 172 173 174 175 176 177 178 179 180 181 182
  .banner-title {
    max-height: 84rpx;
    overflow: hidden;
    position: absolute;
    left: 30rpx;
    bottom: 30rpx;
    width: 90%;
    font-size: 32rpx;
    font-weight: 400;
    line-height: 42rpx;
    color: white;
  }
W
微调  
wanganxp 已提交
183

W
wanganxp 已提交
184 185 186 187 188 189 190
  .uni-media-list {
    padding: 22rpx 30rpx;
    box-sizing: border-box;
    display: flex;
    width: 100%;
    flex-direction: row;
  }
W
微调  
wanganxp 已提交
191

W
wanganxp 已提交
192 193 194 195
  .uni-media-list-logo {
    width: 180rpx;
    height: 140rpx;
  }
W
微调  
wanganxp 已提交
196

W
wanganxp 已提交
197 198 199 200 201
  .uni-media-list-body {
    flex: 1;
    padding-left: 15rpx;
    justify-content: space-around;
  }
W
微调  
wanganxp 已提交
202

W
wanganxp 已提交
203 204 205 206 207 208 209 210
  .uni-list-content {
    background-color: #FFFFFF;
    position: relative;
    display: flex;
    flex-direction: column;
    border-bottom: 1px solid #c8c7cc;
    flex: 1;
  }
W
微调  
wanganxp 已提交
211

W
wanganxp 已提交
212 213 214 215 216 217
  .uni-media-list-text-top {
    /* height: 74rpx; */
    font-size: 28rpx;
    lines: 2;
    overflow: hidden;
  }
W
微调  
wanganxp 已提交
218

W
wanganxp 已提交
219 220 221 222 223
  .uni-media-list-text-bottom {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
W
微调  
wanganxp 已提交
224

W
wanganxp 已提交
225 226 227 228
  .uni-media-list-text {
    color: #9D9D9F;
    font-size: 25rpx;
  }
W
微调  
wanganxp 已提交
229
</style>