93 lines
2.4 KiB
JavaScript
93 lines
2.4 KiB
JavaScript
// components/List/index.js
|
|
import { getDataByPath } from "../../utils/util"
|
|
import { get } from "../../utils/api"
|
|
Component({
|
|
/**
|
|
* 组件的属性列表
|
|
*/
|
|
properties: {
|
|
url: {
|
|
type: String
|
|
},
|
|
dataPath: {
|
|
type: String
|
|
},
|
|
list: {
|
|
type: Array
|
|
},
|
|
params: {
|
|
type: Object,
|
|
},
|
|
limit: {
|
|
type: Number,
|
|
value: 10
|
|
},
|
|
scrolling:{
|
|
type:Boolean,
|
|
value:true,
|
|
},
|
|
emptyText:{
|
|
type:String,
|
|
value:"暂无数据"
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 组件的初始数据
|
|
*/
|
|
data: {
|
|
page: 1,
|
|
limit: 10,
|
|
more: true,
|
|
loading: false
|
|
},
|
|
// 数据监听器
|
|
observers: {
|
|
"url,params"(url) {
|
|
if (!url) return
|
|
this.resetPage();
|
|
},
|
|
},
|
|
/**
|
|
* 组件的方法列表
|
|
*/
|
|
methods: {
|
|
loadMore() {
|
|
let page = this.data.page + 1;
|
|
this.setData({ page })
|
|
this.getData();
|
|
},
|
|
getData() {
|
|
if (!this.data.more) return
|
|
if (!this.data.loading && (this.data.params || Object.keys(this.data.params).length > 0)) {
|
|
this.setData({ loading: true })
|
|
get(this.data.url, Object.assign({}, { page: this.data.page, limit: this.data.limit }, this.data.params || {})).then(res => {
|
|
let records;
|
|
if (res.code === 0) {
|
|
records = getDataByPath(res, this.data.dataPath || "data.records")
|
|
} else {
|
|
records = [];
|
|
}
|
|
if (records.length < this.data.limit) {
|
|
this.setData({ more: false })
|
|
}
|
|
this.triggerEvent("update-list", records)
|
|
}).catch(e => {
|
|
wx.showToast({
|
|
title: '系统错误',
|
|
icon: 'error',
|
|
duration: 1500,
|
|
});
|
|
}).finally(() => {
|
|
this.setData({ loading: false })
|
|
})
|
|
}
|
|
},
|
|
resetPage() {
|
|
this.setData({ more: true, page: 1 })
|
|
this.triggerEvent('reset-list')
|
|
this.getData();
|
|
}
|
|
}
|
|
})
|