long-list-page.uvue 6.3 KB
Newer Older
H
hdx 已提交
1
<template>
H
hdx 已提交
2
  <list-view ref="listView" class="list" :rebound="false" :scroll-y="true" :custom-nested-scroll="true"
H
hdx 已提交
3
    @scrolltolower="loadData(null)">
H
hdx 已提交
4 5 6 7 8 9 10
    <list-item class="list-item" v-for="(item, _) in dataList" :key="item.plugin_id">
      <view class="list-item-icon">
        <image class="list-item-icon-image" :src="item.plugin_img_link"></image>
      </view>
      <view class="list-item-fill">
        <view class="flex-row">
          <text class="title">{{item.plugin_name}}</text>
H
hdx 已提交
11
        </view>
H
hdx 已提交
12 13
        <view class="description">
          <text class="description-text">{{item.plugin_intro}}</text>
H
hdx 已提交
14
        </view>
H
hdx 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
        <text class="icon-star">{{convertToStarUnicode(item.score)}}</text>
        <view class="tag-list">
          <text class="tag-item" v-for="(item2, index2) in item.tags" :key="index2">{{item2}}</text>
        </view>
        <view class="flex-row update-date">
          <text class="update-date-text">更新日期</text>
          <text class="update-date-value">{{item.update_date}}</text>
          <text class="author">{{item.author_name}}</text>
        </view>
      </view>
      <!-- TODO 暂时这样写才能保证 loading 在 list 底部 -->
      <view v-if="item.plugin_id==-1" class="loading">
        <text class="loading-text">{{loadingText}}</text>
      </view>
    </list-item>
H
hdx 已提交
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
  </list-view>
</template>

<script>
  const SERVER_URL = "https://ext.dcloud.net.cn/plugin/uniappx-plugin-list"
  const PAGE_SIZE = 10; // 最大值 10

  type ListItem = {
    plugin_id : number,
    plugin_img_link : string,
    plugin_name : string,
    plugin_intro : string,
    score : number,
    tags : Array<string>,
    update_date : string,
    author_name : string,
  }

  type ResponseDataType = {
    code : number,
    data : ListItem[]
  }

  export default {
    props: {
      type: {
        type: String,
        default: ''
H
hdx 已提交
58 59 60 61
      },
      preload: {
        type: Boolean,
        default: false
H
hdx 已提交
62 63 64 65 66 67 68
      }
    },
    data() {
      return {
        loading: false,
        dataList: [] as ListItem[],
        isEnded: false,
H
hdx 已提交
69
        loadingError: '',
70
        $currentPage: 1
H
hdx 已提交
71 72
      }
    },
H
hdx 已提交
73 74 75 76 77 78 79 80 81 82 83
    computed: {
      loadingText() : string {
        if (this.loading) {
          return "加载中..."
        } else if (this.isEnded) {
          return "没有更多了"
        } else if (this.loadingError.length > 0) {
          return this.loadingError
        } else {
          return ""
        }
84
      },
H
hdx 已提交
85
    },
H
hdx 已提交
86
    created() {
H
hdx 已提交
87
      this.insertLoadingData()
H
hdx 已提交
88 89

      if (this.preload) {
90
        this.loadData(null)
H
hdx 已提交
91
      }
H
hdx 已提交
92 93
    },
    methods: {
H
hdx 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
      // TODO 目前 list-item 不支持注释节点
      // 暂时在数据末尾插入一条空数据用于显示 loading
      insertLoadingData() {
        const loadingData = {
          plugin_id: -1,
          plugin_img_link: '',
          plugin_name: '',
          plugin_intro: '',
          score: 0,
          tags: [],
          update_date: '',
          author_name: '',
        } as ListItem
        this.dataList.push(loadingData)
      },
H
hdx 已提交
109
      refreshData(loadComplete : (() => void) | null) {
110 111
        this.dataList.length = 0
        this.$currentPage = 1
H
hdx 已提交
112
        this.insertLoadingData()
H
hdx 已提交
113
        this.loadData(loadComplete)
114
      },
H
hdx 已提交
115
      loadData(loadComplete : (() => void) | null) {
H
hdx 已提交
116 117 118 119 120
        if (this.loading || this.isEnded) {
          return
        }
        this.loading = true

H
hdx 已提交
121
        uni.request<ResponseDataType>({
H
hdx 已提交
122
          url: SERVER_URL,
H
hdx 已提交
123 124 125 126 127 128
          data: {
            type: this.type,
            page: this.$currentPage,
            page_size: PAGE_SIZE
          },
          success: (res) => {
H
hdx 已提交
129
            const responseData = res.data
H
hdx 已提交
130 131 132 133
            if (responseData == null) {
              return
            }

H
hdx 已提交
134 135 136
            // TODO
            this.dataList.splice(this.dataList.length - 1, 0, ...responseData.data)
            // this.dataList.push(...responseData.data)
H
hdx 已提交
137

138
            if (responseData.data.length == 0) {
H
hdx 已提交
139 140 141 142 143 144
              this.isEnded = true
            } else {
              this.$currentPage++
            }
          },
          fail: (err) => {
H
hdx 已提交
145
            this.loadingError = err.errMsg
H
hdx 已提交
146 147 148
          },
          complete: () => {
            this.loading = false
H
hdx 已提交
149 150
            if (loadComplete != null) {
              loadComplete()
151
            }
H
hdx 已提交
152 153 154 155
          }
        })
      },
      // score 0 ~ 50
156
      convertToStarUnicode(score : number) : string {
H
hdx 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170
        const fill_code = '\ue879'
        const half_code = '\ue87a'
        const null_code = '\ue87b'

        const fillStarCount = parseInt(score / 10 % 10 + '')
        const halfStarCount = score % 10 >= 5 ? 1 : 0
        const nullStarCount = 5 - fillStarCount - halfStarCount

        let result = ''
        if (fillStarCount > 0) { result += fill_code.repeat(fillStarCount) }
        if (halfStarCount > 0) { result += half_code.repeat(halfStarCount) }
        if (nullStarCount > 0) { result += null_code.repeat(nullStarCount) }

        return result
H
hdx 已提交
171
      }
H
hdx 已提交
172 173 174 175 176
    }
  }
</script>

<style>
H
hdx 已提交
177 178 179 180 181
  /* TODO 暂时写到了页面 long-list */
  /* @font-face {
    font-family: "UtsStarIcons";
    src: url('@/static/fonts/icon-star.ttf');
  } */
182

H
hdx 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  .list {
    flex: 1;
    background-color: #ffffff;
  }

  .list-item {
    flex-direction: row;
    margin-top: 10px;
    padding: 10px;
  }

  .list-item-icon {
    position: relative;
  }

  .list-item-icon-image {
    width: 80px;
    height: 80px;
  }

  .list-item-fill {
    flex: 1;
    margin-left: 15px;
  }

  .description-text {
    font-size: 13px;
    color: #666;
    line-height: 19px;
  }

  .icon-star {
H
hdx 已提交
215 216 217
    font-family: "UtsStarIcons";
    font-size: 16px;
    font-style: normal;
H
hdx 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    color: #ffca3e;
    letter-spacing: 3px;
  }

  .tag-list {
    flex-direction: row;
    margin-top: 5px;
  }

  .tag-item {
    font-size: 14px;
    background-color: #EFF9F0;
    color: #639069;
    border-radius: 20px;
    margin-right: 5px;
    padding: 2px 5px;
  }

  .update-date {
    margin-top: 10px;
  }

  .update-date-text {
    font-size: 12px;
    color: #888888;
  }

  .update-date-value {
    font-size: 12px;
    color: #777777;
    margin-left: 5px;
  }

  .author {
    font-size: 12px;
    color: #008000;
    margin-left: auto;
  }
H
hdx 已提交
256 257

  .loading {
H
hdx 已提交
258 259 260 261 262
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
H
hdx 已提交
263
    background-color: #f8f8f8;
H
hdx 已提交
264
    margin-bottom: 1px;
H
hdx 已提交
265 266 267 268 269 270
    justify-content: center;
  }

  .loading-text {
    padding: 20px;
    text-align: center;
H
hdx 已提交
271
  }
H
hdx 已提交
272
</style>