const echarts = require('../../../components/ec-canvas/echarts.js') Page({ data: { fold1: true, fold2: true, // 用户信息 promoterName: '', promoterAvatar: '', label: '邀约专员', // 统计数据 invitePharmacyCount: 0, invitePharmacistCount: 0, invitePatientCount: 0, jumpPatientCount: 0, enrollPatientCount: 0, indicationStats: [] as Array<{ indicationId: number indicationName: string invitePatientCount: number jumpPatientCount: number enrollPatientCount: number }>, // 图表数据 chartData: [] as Array<{ date: string, count: number }>, pharmacistChartData: [] as Array<{ date: string, count: number }>, pharmacyChartData: [] as Array<{ date: string, count: number }>, // 日期范围 startDate: '', endDate: '', today: '', // 统计类型: day-日统计, month-月统计 statType: 'day', // 项目列表 projectList: [] as Array<{ projectId: number; projectName: string; projectDescription: string }>, currentProjectId: 0, currentProjectName: '特诺雅', projectIndex: 0, }, ecDataTrendComponent1_1: null as any, ecDataTrendComponent2_1: null as any, ecDataTrendComponent3_1: null as any, async onLoad() { const app = getApp() // 地推端页面,仅允许地推人员(3)访问 app.waitLogin({ types: [3] }).then(() => { this.getUserInfo() this.getProjectList() }) // 初始化日期范围为最近30天 const today = this.formatDate(new Date()) const endDate = today const startDate = this.formatDate(new Date(Date.now() - 30 * 24 * 60 * 60 * 1000)) this.setData({ today, 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/promoter/promoter/profile', }).then((res: any) => { this.setData({ promoterName: res.name, promoterAvatar: res.avatar, }) }) }, // 获取项目列表 getProjectList() { wx.ajax({ method: 'GET', url: '/app/promoter/promoter/project-list', }).then((res: any) => { const projectList = res.list || [] const currentProjectId = res.currentProjectId || (projectList[0]?.projectId || 0) const currentProject = projectList.find((item: any) => item.projectId === currentProjectId) || projectList[0] const projectIndex = projectList.findIndex((item: any) => item.projectId === currentProjectId) this.setData({ projectList, currentProjectId, currentProjectName: currentProject?.projectName || '特诺雅', projectIndex: projectIndex >= 0 ? projectIndex : 0, }) // 获取统计数据 this.getStatistics() this.getPatientChart() this.getPharmacistChart() this.getPharmacyChart() }) }, // 切换项目 onProjectChange(e: WechatMiniprogram.CustomEvent) { const index = e.detail.value const project = this.data.projectList[index] if (project && project.projectId !== this.data.currentProjectId) { this.setData({ currentProjectId: project.projectId, currentProjectName: project.projectName, projectIndex: index, }) // 重新加载数据 this.getStatistics() this.getPatientChart() this.getPharmacistChart() this.getPharmacyChart() } }, // 获取统计数据看板 getStatistics() { const data: any = {} if (this.data.currentProjectId) { data.projectId = this.data.currentProjectId } wx.ajax({ method: 'GET', url: '/app/promoter/promoter/statistics', data, }).then((res: any) => { this.setData({ invitePharmacyCount: res.invitePharmacyCount || 0, invitePharmacistCount: res.invitePharmacistCount || 0, invitePatientCount: res.invitePatientCount || 0, jumpPatientCount: res.jumpPatientCount || 0, enrollPatientCount: res.enrollPatientCount || 0, indicationStats: res.indicationStats || [], }) }) }, // 获取邀约患者统计图表 getPatientChart() { const data: any = { type: this.data.statType, startDate: this.data.startDate, endDate: this.data.endDate, } if (this.data.currentProjectId) { data.projectId = this.data.currentProjectId } wx.ajax({ method: 'GET', url: '/app/promoter/promoter/patient-chart', data, }).then((list: any) => { this.setData({ chartData: list || [], }) this.initChartBar(list || []) }) }, // 获取邀约药师统计图表 getPharmacistChart() { const data: any = { type: this.data.statType, startDate: this.data.startDate, endDate: this.data.endDate, } if (this.data.currentProjectId) { data.projectId = this.data.currentProjectId } wx.ajax({ method: 'GET', url: '/app/promoter/promoter/pharmacist-chart', data, }).then((list: any) => { this.setData({ pharmacistChartData: list || [], }) this.initChartLine(list || [], '#chart2_1', 'ecDataTrendComponent2_1') }) }, // 获取邀约药店统计图表 getPharmacyChart() { const data: any = { type: this.data.statType, startDate: this.data.startDate, endDate: this.data.endDate, } if (this.data.currentProjectId) { data.projectId = this.data.currentProjectId } wx.ajax({ method: 'GET', url: '/app/promoter/promoter/pharmacy-chart', data, }).then((list: any) => { this.setData({ pharmacyChartData: list || [], }) this.initChartLine(list || [], '#chart3_1', 'ecDataTrendComponent3_1') }) }, // 切换统计类型 switchStatType(e: WechatMiniprogram.CustomEvent) { const type = e.currentTarget.dataset.type this.setData({ statType: type, }) this.getPatientChart() this.getPharmacistChart() this.getPharmacyChart() }, // 日期选择变化 onDateChange(e: WechatMiniprogram.CustomEvent) { const { field } = e.currentTarget.dataset const value = e.detail.value // 验证日期范围 if (field === 'startDate' && this.data.endDate && value > this.data.endDate) { wx.showToast({ title: '开始时间不能大于结束时间', icon: 'none', }) return } if (field === 'endDate' && this.data.startDate && value < this.data.startDate) { wx.showToast({ title: '结束时间不能小于开始时间', icon: 'none', }) return } this.setData({ [field]: value, }) // 重新加载图表数据 this.getPatientChart() this.getPharmacistChart() this.getPharmacyChart() }, // 切换到上一天 prevDate() { const currentDate = new Date(this.data.startDate) const newDate = new Date(currentDate.getTime() - 24 * 60 * 60 * 1000) const startDate = this.formatDate(newDate) this.setData({ startDate }) // 重新加载图表数据 this.getPatientChart() this.getPharmacistChart() this.getPharmacyChart() }, // 切换到下一天 nextDate() { const currentDate = new Date(this.data.startDate) const newDate = new Date(currentDate.getTime() + 24 * 60 * 60 * 1000) const today = new Date() // 最大日期不能大于当前日期 if (newDate > today) { wx.showToast({ title: '不能选择未来日期', icon: 'none', }) return } const startDate = this.formatDate(newDate) this.setData({ startDate }) // 重新加载图表数据 this.getPatientChart() this.getPharmacistChart() this.getPharmacyChart() }, 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', width: 4, color: '#FED877', barWidth: 12, data: y1, }, { name: '跳转患者数', type: 'bar', stack: 'a', color: '#4A8DFF', barWidth: 12, data: y1, }, { name: '入组患者数', type: 'bar', stack: 'a', color: '#3ADDC8', barWidth: 12, data: y1, }, ], dataZoom: { type: 'inside', startValue: x.length - 6, endValue: x.length - 1, filterMode: 'none', }, } chart.setOption(option) reslove(chart) return chart }) }) }, initChartLine(list: any[], name: string, componentName: string) { return new Promise((reslove) => { this[componentName] = this.selectComponent(name) this[componentName].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', }, data: [], }, 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: 'line', stack: 'a', width: 4, color: '#FED877', barWidth: 12, data: y1, smooth: 0.5, label: { show: true, position: 'top', fontSize: 12, color: '#B5B8BB', }, }, ], dataZoom: { type: 'inside', startValue: x.length - 6, endValue: x.length - 1, filterMode: 'none', }, } chart.setOption(option) reslove(chart) return chart }) }) }, handleInvite() { wx.navigateTo({ url: '/ground/pages/invite/index', }) }, handleInfo() { wx.navigateTo({ url: '/ground/pages/stat/index', }) }, handleFold(e) { const { key } = e.currentTarget.dataset this.setData({ [key]: !this.data[key], }) }, })