2025-07-03 18:16:42 +08:00

183 lines
3.5 KiB
JavaScript

import request from '~/api/request';
Page({
/**
* 页面的初始数据
*/
data: {
newTime: '',
timeType: 2,
list: [],
page: 1,
pageSize: 21,
loading: false,
noMore: false,
hasMore: true
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
const now = new Date()
const year = now.getFullYear()
const month = now.getMonth() + 1
const day = now.getDate()
const hours = now.getHours()
const minutes = now.getMinutes()
const seconds = now.getSeconds()
const formattedTime = `${year}${month.toString().padStart(2, '0')}${day.toString().padStart(2, '0')}`
this.setData({
newTime: formattedTime
})
},
async getList(isLoadMore = false){
if (this.data.loading || (!isLoadMore && !this.data.hasMore)) {
return
}
this.setData({
loading: true
})
try {
const res = await request('patient/medicine_records', 'GET', {
time_type: this.data.timeType,
status: 0,
page: this.data.page,
page_size: this.data.pageSize
})
const arr = res.list.map(item => {
item.detail = JSON.parse(item.detail)
return item
})
// 按照 medicine_date 字段将数组分组为二维数组
const groupedArr = []
const dateGroups = {}
for (const item of arr) {
const date = item.medicine_date || 'unknown'
if (!dateGroups[date]) {
dateGroups[date] = {
medicine_date: date,
dayList: []
}
}
dateGroups[date].dayList.push(item)
}
// 将分组后的数据转换为数组
for (const date in dateGroups) {
groupedArr.push(dateGroups[date])
}
// 判断是否还有更多数据
const hasMore = arr.length === this.data.pageSize
if (isLoadMore) {
// 加载更多时,合并数据
this.setData({
list: [...this.data.list, ...groupedArr],
page: this.data.page + 1,
hasMore: hasMore,
noMore: !hasMore
})
} else {
// 首次加载或切换时间类型时,重置数据
this.setData({
list: groupedArr,
page: 2,
hasMore: hasMore,
noMore: !hasMore
})
}
} catch (error) {
console.error('获取数据失败:', error)
wx.showToast({
title: '获取数据失败',
icon: 'none'
})
} finally {
this.setData({
loading: false
})
}
},
// 滚动到底部加载更多
onLoadMore() {
if (this.data.hasMore && !this.data.loading) {
this.getList(true)
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
this.getList()
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
},
changeBtn(e) {
if(this.data.timeType != e.target.dataset.index){
this.setData({
timeType: e.target.dataset.index,
page: 1,
hasMore: true,
noMore: false
})
this.getList()
}
}
})