const echarts = require('../../../components/ec-canvas/echarts.js') Page({ data: { fold1: true, fold2: true, // 用户信息 pharmacistName: '', pharmacistAvatar: '', pharmacyName: '', // 待处理患者数 pendingCount: 0, // 统计数据 invitePatientCount: 0, jumpPatientCount: 0, enrollPatientCount: 0, // 图表数据 chartData: [] as Array<{ date: string, count: number }>, // 日期范围 startDate: '', endDate: '', // 统计类型: day-日统计, month-月统计 statType: 'day', }, ecDataTrendComponent1_1: null as any, async onLoad() { const app = getApp() // 药店端页面,仅允许药店人员(4)访问 app.waitLogin({ types: [4] }).then(() => { this.getUserInfo() this.getPendingCount() this.getStatistics() this.getPatientChart() }) // 初始化日期范围为最近30天 const endDate = this.formatDate(new Date()) const startDate = this.formatDate(new Date(Date.now() - 30 * 24 * 60 * 60 * 1000)) this.setData({ startDate, 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}` }, // 获取用户信息 getUserInfo() { wx.ajax({ method: 'GET', url: '/app/pharmacist/pharmacist/info', }).then((res: any) => { this.setData({ pharmacistName: res.name, pharmacistAvatar: res.avatar, pharmacyName: res.pharmacyName, }) }) }, // 获取待处理患者数 getPendingCount() { wx.ajax({ method: 'GET', url: '/app/pharmacist/pharmacist/pending-count', }).then((res: any) => { this.setData({ pendingCount: res.count || 0, }) }) }, // 获取统计数据看板 getStatistics() { wx.ajax({ method: 'GET', url: '/app/pharmacist/pharmacist/statistics', }).then((res: any) => { this.setData({ invitePatientCount: res.invitePatientCount, jumpPatientCount: res.jumpPatientCount, enrollPatientCount: res.enrollPatientCount, }) }) }, // 获取邀约患者统计图表 getPatientChart() { wx.ajax({ method: 'GET', url: '/app/pharmacist/pharmacist/patient-chart', data: { type: this.data.statType, startDate: this.data.startDate, endDate: this.data.endDate, }, }).then((res: any) => { this.setData({ chartData: res.list || [], }) this.initChartBar(res.list || []) }) }, // 切换统计类型 switchStatType(e: WechatMiniprogram.CustomEvent) { const type = e.currentTarget.dataset.type this.setData({ statType: type, }) this.getPatientChart() }, // 日期选择变化 onDateChange(e: WechatMiniprogram.CustomEvent) { const { field } = e.currentTarget.dataset this.setData({ [field]: e.detail.value, }) // 重新加载图表数据 this.getPatientChart() }, initChartBar(list: any[]) { return new Promise((reslove) => { this.ecDataTrendComponent1_1 = this.selectComponent('#chart1_1') this.ecDataTrendComponent1_1.init((canvas, width, height, dpr) => { const chart = echarts.init(canvas, null, { width, height, devicePixelRatio: dpr, }) canvas.setChart(chart) const x: string[] = [] const y1: string[] = [] list.forEach((item) => { x.push(item.date || item.StatMonth) y1.push(item.count || item.MonthInvitePCount) }) const option = { legend: { top: 0, right: 0, itemWidth: 8, itemHeight: 8, icon: 'rect', lineStyle: { width: '0', }, textStyle: { color: '#B5B8BB', fontSize: '12', }, }, grid: { top: '10%', left: '3%', right: '4%', bottom: '0', containLabel: true, }, xAxis: [ { type: 'category', axisTick: { show: false, }, axisLabel: { fontSize: 10, color: '#B5B8BB', }, axisLine: { show: false, }, data: x, }, ], yAxis: [ { type: 'value', minInterval: 1, splitLine: { lineStyle: { type: 'dashed', }, }, axisLabel: { fontSize: 10, color: '#B5B8BB', formatter(value) { return Math.abs(value) }, }, }, ], series: [ { name: '入组患者数', type: 'bar', stack: 'a', color: '#FF5722', barWidth: 12, data: y1, }, { name: '跳转患者数', type: 'bar', stack: 'a', color: '#FF8A4C', barWidth: 12, data: y1, }, { name: '邀约患者数', type: 'bar', stack: 'a', width: 4, color: '#FFA64D', barWidth: 12, data: y1, }, ], dataZoom: { type: 'inside', startValue: x.length - 6, endValue: x.length - 1, filterMode: 'none', }, } chart.setOption(option) reslove(chart) return chart }) }) }, handleInvite() { wx.navigateTo({ url: '/doctor/pages/invite/index', }) }, handleStat() { wx.navigateTo({ url: '/doctor/pages/stat/index', }) }, handleFold(e) { const { key } = e.currentTarget.dataset this.setData({ [key]: !this.data[key], }) }, })