const app = getApp() Page({ data: { // 时间筛选 startDate: '', endDate: '', type: 'day', // day-按日,month-按月 // 统计数据(从 summary 获取) invitePatientCount: 0, jumpPatientCount: 0, enrollPatientCount: 0, // 列表数据 list: [] as any[], // 分页信息 page: 1, pageSize: 20, pagination: { count: 0, page: 0, pages: 0, }, }, onLoad() { // 地推端统计页面,仅允许地推人员访问 app.waitLogin({ types: [3] }).then(() => { // 设置默认时间范围(2026年3月至今) this.setDefaultDateRange() // 获取列表数据(包含顶部统计数据) this.getList(true) }) }, // 设置默认时间范围(2026年3月至今) setDefaultDateRange() { const endDate = new Date() const startDate = new Date('2026-03-01') this.setData({ startDate: this.formatDate(startDate), endDate: this.formatDate(endDate), }) }, // 格式化日期 formatDate(date: Date): string { const year = date.getFullYear() const month = String(date.getMonth() + 1).padStart(2, '0') const day = String(date.getDate()).padStart(2, '0') return `${year}-${month}-${day}` }, // 获取列表数据(包含顶部统计数据) getList(reset = false) { const { startDate, endDate, type, page, pageSize, pagination } = this.data // 如果是重置(如筛选条件变化),先重置状态 if (reset) { if (pagination.page > 0 && pagination.page < pagination.pages) return // 如果正在加载,不重复请求 this.setData({ page: 1, list: [], pagination: { count: 0, page: 0, pages: 0 }, }) } else { // 非重置情况下,如果正在加载或已加载完,不再请求 if (pagination.page > 0 && (pagination.page >= pagination.pages)) return } const currentPage = reset ? 1 : page wx.ajax({ method: 'GET', url: '/app/promoter/promoter/patient-statistics-list', data: { startDate, endDate, type, page: currentPage, pageSize, }, }).then((res: any) => { const newList = res.list || [] const total = res.total || 0 const summary = res.summary || {} // 转换数据格式 const formattedList = newList.map((item: any) => ({ date: item.statDate, inviteCount: item.invitePatientCount || 0, jumpCount: item.jumpPatientCount || 0, enrollCount: item.enrollPatientCount || 0, indicationStats: (item.indicationStats || []).map((ind: any) => ({ indicationName: ind.indicationName, inviteCount: ind.invitePatientCount || 0, jumpCount: ind.jumpPatientCount || 0, enrollCount: ind.enrollPatientCount || 0, })), })) const list = reset ? formattedList : [...this.data.list, ...formattedList] const pages = Math.ceil(total / pageSize) this.setData({ // 顶部统计数据从 summary 获取 invitePatientCount: summary.invitePatientCount || 0, jumpPatientCount: summary.jumpPatientCount || 0, enrollPatientCount: summary.enrollPatientCount || 0, // 列表数据 list, page: currentPage + 1, pagination: { count: total, page: currentPage, pages, }, }) }) }, // 加载更多 onReachBottom() { this.getList() }, // 开始时间选择 handleStartDateChange(e: WechatMiniprogram.CustomEvent) { const startDate = e.detail.value const { endDate } = this.data // 如果已选择结束时间,验证开始时间不能大于结束时间 if (endDate && startDate > endDate) { wx.showToast({ title: '开始时间不能大于结束时间', icon: 'none', }) return } this.setData({ startDate }) this.getList(true) }, // 结束时间选择 handleEndDateChange(e: WechatMiniprogram.CustomEvent) { const endDate = e.detail.value const { startDate } = this.data // 如果已选择开始时间,验证结束时间不能小于开始时间 if (startDate && endDate < startDate) { wx.showToast({ title: '结束时间不能小于开始时间', icon: 'none', }) return } this.setData({ endDate }) this.getList(true) }, // 切换统计类型 handleTypeChange(e: WechatMiniprogram.CustomEvent) { const type = e.currentTarget.dataset.type this.setData({ type }) this.getList(true) }, handleBack() { wx.navigateBack() }, }) export {}