const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
cusor: 0, //当前栏目
status: [],
type: ['未使用', '已使用', '已過期'],
list: [], //全部列表数据
showList: [], //当前展示数据
currentTime: Date.parse(new Date()) / 1000, //当前时间戳
currentPage: 1,
noMore: false,
pageBackgroundColor: "#fff", //页面背景颜色
},
// 切换栏目
handtab(e) {
this.setData({
cusor: e.currentTarget.dataset.index,
})
// 获取当前栏目数据
this.getList(false, this.data.cusor)
},
// 获取数据
getList(isLoadMore = false, index) {
// 初始化展示列表
this.data.showList = []
wx.showLoading({
title: '加載中...',
})
const currentPage = isLoadMore ? this.data.currentPage + 1 : 1;
api.baseRequest('/', 'POST', {
page: currentPage
}, true).then(res => {
wx.stopPullDownRefresh();
wx.hideLoading()
if (res.code == 200) {
var results = res.data
if (currentPage > 1) {
results = this.data.list.concat(results);
}
this.setData({
list: results,
currentPage: currentPage,
noMore: res.data.length < 10,
pageBackgroundColor: "#f4f6f7"
})
} else {
this.setData({
pageBackgroundColor: "#fff"
})
}
var _arr = this.data.showList
// 遍历寻找符合条件数据
this.data.list.forEach((item) => {
// 未使用
if (index == 0) {
if (item.w_status == 0) {
_arr.push(item)
}
// 已使用
} else if (index == 1) {
if (item.w_status == 1) {
_arr.push(item)
}
// 已过期
} else if (index == 2) {
if (item.w_status == 2) {
_arr.push(item)
}
}
})
this.setData({
showList: _arr
})
console.log(this.data.showList);
})
},
// 获取时间戳差
getDiffTime(endTime) {
//获取当前时间戳
var newDateTime = Date.parse(new Date())
console.log(newDateTime)
//指定的时间戳
var datetime = endTime;
console.log(formatTime(newDateTime))
console.log(formatTime(datetime))
//时间戳相差多少秒
console.log((newDateTime - datetime) / 1000)
//时间戳转换为时间
function formatTime(newDateTime) {
var date = new Date(newDateTime)
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
return year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒"
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.getList(false,this.data.cusor); //获取数据
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
this.getList(false, this.data.cusor);
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
if (this.data.noMore == false) {
this.getList(true, this.data.cusor);
}
},
})
【小程序】分页获取数据
于 2023-10-03 23:43:37 首次发布