long-list-page.uvue 5.5 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
    <list-item class="list-item" v-for="(item, _) in dataList" :key="item.plugin_id">
5
      <view class="list-item-icon">
H
hdx 已提交
6 7
        <image class="list-item-icon-image" :src="item.plugin_img_link"></image>
      </view>
8
      <view class="list-item-fill">
H
hdx 已提交
9 10
        <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
        <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>
25 26 27
    </list-item>
    <list-item>
      <view class="loading">
H
hdx 已提交
28 29 30
        <text class="loading-text">{{loadingText}}</text>
      </view>
    </list-item>
H
hdx 已提交
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
  </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 已提交
59 60 61 62
      },
      preload: {
        type: Boolean,
        default: false
H
hdx 已提交
63 64 65 66 67 68 69
      }
    },
    data() {
      return {
        loading: false,
        dataList: [] as ListItem[],
        isEnded: false,
H
hdx 已提交
70
        loadingError: '',
71
        $currentPage: 1
H
hdx 已提交
72 73
      }
    },
H
hdx 已提交
74 75 76 77 78 79 80 81 82 83 84
    computed: {
      loadingText() : string {
        if (this.loading) {
          return "加载中..."
        } else if (this.isEnded) {
          return "没有更多了"
        } else if (this.loadingError.length > 0) {
          return this.loadingError
        } else {
          return ""
        }
85
      }
H
hdx 已提交
86
    },
H
hdx 已提交
87 88
    created() {
      if (this.preload) {
89
        this.loadData(null)
H
hdx 已提交
90
      }
H
hdx 已提交
91 92
    },
    methods: {
H
hdx 已提交
93
      refreshData(loadComplete : (() => void) | null) {
94 95
        this.dataList.length = 0
        this.$currentPage = 1
H
hdx 已提交
96
        this.loadData(loadComplete)
97
      },
H
hdx 已提交
98
      loadData(loadComplete : (() => void) | null) {
H
hdx 已提交
99 100 101 102 103
        if (this.loading || this.isEnded) {
          return
        }
        this.loading = true

H
hdx 已提交
104
        uni.request<ResponseDataType>({
H
hdx 已提交
105
          url: SERVER_URL,
H
hdx 已提交
106 107 108 109 110 111
          data: {
            type: this.type,
            page: this.$currentPage,
            page_size: PAGE_SIZE
          },
          success: (res) => {
H
hdx 已提交
112
            const responseData = res.data
H
hdx 已提交
113 114 115 116
            if (responseData == null) {
              return
            }

117
            this.dataList.push(...responseData.data)
H
hdx 已提交
118

119
            if (responseData.data.length == 0) {
H
hdx 已提交
120 121 122 123 124 125
              this.isEnded = true
            } else {
              this.$currentPage++
            }
          },
          fail: (err) => {
H
hdx 已提交
126
            this.loadingError = err.errMsg
H
hdx 已提交
127 128 129
          },
          complete: () => {
            this.loading = false
H
hdx 已提交
130 131
            if (loadComplete != null) {
              loadComplete()
132
            }
H
hdx 已提交
133 134 135 136
          }
        })
      },
      // score 0 ~ 50
137
      convertToStarUnicode(score : number) : string {
H
hdx 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151
        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 已提交
152
      }
H
hdx 已提交
153 154 155 156 157
    }
  }
</script>

<style>
H
hdx 已提交
158
  @font-face {
H
hdx 已提交
159 160
    font-family: "UtsStarIcons";
    src: url('@/static/fonts/icon-star.ttf');
H
hdx 已提交
161
  }
162

H
hdx 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  .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 已提交
195 196 197
    font-family: "UtsStarIcons";
    font-size: 16px;
    font-style: normal;
H
hdx 已提交
198 199 200 201 202 203 204 205 206 207
    color: #ffca3e;
    letter-spacing: 3px;
  }

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

  .tag-item {
H
hdx 已提交
208
    font-size: 12px;
H
hdx 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    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 已提交
236 237

  .loading {
238
    padding: 20px;
H
hdx 已提交
239
    background-color: #f8f8f8;
H
hdx 已提交
240 241 242 243 244
    justify-content: center;
  }

  .loading-text {
    text-align: center;
H
hdx 已提交
245
  }
fxy060608's avatar
fxy060608 已提交
246
</style>