list-news.uvue 5.8 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1 2
<template>
  <list-view style="flex: 1;" refresher-enabled=true @refresherrefresh="onRefresherrefresh"
W
wanganxp 已提交
3
    :refresher-triggered="refresherTriggered" enable-back-to-top="true">
4
    <list-item class="banner" @click="bannerClick(banner)" type=1>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
5 6
      <image class="banner-img" :src="banner.cover"></image>
      <text class="banner-title">{{ banner.title }}</text>
7
    </list-item>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
8 9 10
    <sticky-header>
      <view
        style="width: 100%;height:44px;background-color: #f5f5f5;flex-direction: row;justify-content:center;align-items:center;">
雪洛's avatar
雪洛 已提交
11
        <text v-for="(name,index) in th_item" :key="index" @click="clickTH(index)" style="margin-right: 20px;">
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
12 13 14
          {{name}}
        </text>
      </view>
15
    </sticky-header>
16
    <list-item v-for="(value, index) in listData" :key="index" type=2>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
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
      <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>
</template>

<script>
  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
  }

  export default {
    data() {
      return {
        th_item: ["排序", "筛选"],
        refresherTriggered: false,
        banner: {} as Banner,
        listData: [] as Item[],
        last_id: '',
      };
    },
    onLoad() {
      this.getBanner();
      this.getList();
    },
    onUnload() {
    },
    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 => {
            this.refresherTriggered = false
            if (data.statusCode == 200) {
              const result = data.data
              if (result != null) {
                this.banner = result;
              }
            }
          },
          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;
        } */

        uni.request<Item[]>({
          url: url,
          method: "GET",
          success: (res) => {
            if (res.statusCode == 200) {
              console.log(res);
              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) => {
            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() {
        this.refresherTriggered = true
        this.getBanner();
        this.getList();
      }
    }
  };
</script>

<style>
  .banner {
H
hdx 已提交
151
    height: 180px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
152 153 154 155 156 157 158 159 160 161
    overflow: hidden;
    position: relative;
    background-color: #ccc;
  }

  .banner-img {
    width: 100%;
  }

  .banner-title {
H
hdx 已提交
162
    max-height: 42px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
163 164
    overflow: hidden;
    position: absolute;
H
hdx 已提交
165 166
    left: 15px;
    bottom: 15px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
167
    width: 90%;
H
hdx 已提交
168
    font-size: 16px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
169
    font-weight: 400;
H
hdx 已提交
170
    line-height: 21px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
171 172 173 174
    color: white;
  }

  .uni-media-list {
H
hdx 已提交
175
    padding: 11px 15px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
176 177 178 179 180 181 182
    box-sizing: border-box;
    display: flex;
    width: 100%;
    flex-direction: row;
  }

  .uni-media-list-logo {
H
hdx 已提交
183 184
    width: 90px;
    height: 70px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
185 186 187 188
  }

  .uni-media-list-body {
    flex: 1;
H
hdx 已提交
189
    padding-left: 7px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
190 191 192 193
    justify-content: space-around;
  }

  .uni-media-list-text-top {
H
hdx 已提交
194 195
    /* height: 37px; */
    font-size: 14px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
196 197 198 199 200 201 202 203 204 205 206 207
    lines: 2;
    overflow: hidden;
  }

  .uni-media-list-text-bottom {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }

  .uni-media-list-text {
    color: #9D9D9F;
H
hdx 已提交
208
    font-size: 13px;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
209
  }
210
</style>