rk
10 小时以前 609a1931953b2298016bd2b0d6b410666b5ad7b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<template>
  <div class="data-dashboard">
    <!-- 顶部统计卡片 -->
    <div class="stat-cards">
      <div class="stat-card" v-for="(card, index) in statCards" :key="index">
        <div class="stat-label">{{ card.label }}</div>
        <div class="stat-value" :class="{ 'has-prefix': card.prefix }">
          <span v-if="card.prefix" class="stat-prefix">{{ card.prefix }}</span>
          {{ card.value }}
        </div>
        <div class="stat-sub" v-if="card.label !== '车辆总数'">
          <span>昨日 {{ card.label === '本月收益' ? '¥' : ''}}{{ card.yesterday }}</span>
          <span>今日 {{ card.label === '本月收益' ? '¥' : ''}}{{ card.today }}</span>
        </div>
      </div>
    </div>
 
    <!-- 图表区域 -->
    <div class="charts-row">
      <!-- 近30天收益分析 -->
      <div class="chart-panel chart-bar">
        <div class="chart-title">近30天收益分析</div>
        <div ref="barChart" class="chart-container"></div>
      </div>
 
      <!-- 车辆实时使用情况 -->
      <div class="chart-panel chart-donut">
        <div class="chart-header">
          <div class="chart-title">车辆实时使用情况</div>
          <div class="chart-toggle">
            <el-button-group>
              <el-button
                :type="vehicleType === 'bicycle' ? 'primary' : 'default'"
                size="mini"
                @click="switchVehicleType('bicycle')"
              >自行车</el-button>
              <el-button
                :type="vehicleType === 'electric' ? 'primary' : 'default'"
                size="mini"
                @click="switchVehicleType('electric')"
              >电动车</el-button>
            </el-button-group>
          </div>
        </div>
        <div class="chart-subtitle">总计:{{ vehicleTotal }}辆</div>
        <div ref="donutChart" class="chart-container"></div>
      </div>
 
      <!-- 套餐销售来源分析 -->
      <div class="chart-panel chart-pie">
        <div class="chart-header">
          <div class="chart-title">套餐销售来源分析</div>
          <div class="chart-toggle">
            <el-button-group>
              <el-button
                :type="salesPeriod === 'month' ? 'primary' : 'default'"
                size="mini"
                @click="switchSalesPeriod('month')"
              >本月</el-button>
              <el-button
                :type="salesPeriod === 'year' ? 'primary' : 'default'"
                size="mini"
                @click="switchSalesPeriod('year')"
              >本年</el-button>
            </el-button-group>
          </div>
        </div>
        <div ref="pieChart" class="chart-container"></div>
      </div>
    </div>
  </div>
</template>
 
<script>
import * as echarts from 'echarts'
import { dashboard, packageSourceStat, bikeUsageStat, incomeStat30 } from '@/api/business/report.js'
export default {
  data () {
    return {
      statCards: [],
      vehicleType: 'bicycle',
      vehicleTotal: 0,
      bikeUsageData: null,
      salesPeriod: 'month',
      packageSourceData: null,
      barChart: null,
      donutChart: null,
      pieChart: null
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initBarChart()
      this.initDonutChart()
      this.initPieChart()
    })
    this.loadDashboard()
    window.addEventListener('resize', this.handleResize)
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.handleResize)
    if (this.barChart) this.barChart.dispose()
    if (this.donutChart) this.donutChart.dispose()
    if (this.pieChart) this.pieChart.dispose()
  },
  methods: {
    initBarChart () {
      const chart = echarts.init(this.$refs.barChart)
      incomeStat30({}).then(res => {
        const data = res.dailyList || []
        const dates = data.map(item => {
          const d = item.date.split('-')
          return `${d[1]}-${d[2]}`
        })
        const incomes = data.map(item => item.income)
        chart.setOption({
          tooltip: { trigger: 'axis' },
          legend: {
            data: ['收入'],
            bottom: 50
          },
          grid: {
            left: 10,
            right: 10,
            top: 20,
            bottom: 80,
            containLabel: true
          },
          dataZoom: [
            {
              type: 'inside',
              xAxisIndex: 0,
              start: 0,
              end: 100
            },
            {
              type: 'slider',
              xAxisIndex: 0,
              start: 0,
              end: 100,
              bottom: 20,
              height: 20
            }
          ],
          xAxis: {
            type: 'category',
            data: dates,
            axisLine: { lineStyle: { color: '#e0e0e0' } },
            axisTick: { show: false }
          },
          yAxis: {
            type: 'value',
            axisLine: { show: false },
            axisTick: { show: false },
            splitLine: { lineStyle: { color: '#f0f0f0' } }
          },
          series: [{
            name: '收入',
            type: 'bar',
            barWidth: '60%',
            itemStyle: { color: '#5B8FF9' },
            data: incomes
          }]
        })
      }).catch(e => {
        this.$tip.apiFailed(e)
      })
      this.barChart = chart
    },
    initDonutChart () {
      const chart = echarts.init(this.$refs.donutChart)
      this.donutChart = chart
      bikeUsageStat({}).then(res => {
        this.bikeUsageData = res
        this.updateDonutChart()
      }).catch(e => {
        this.$tip.apiFailed(e)
      })
    },
    updateDonutChart () {
      if (!this.bikeUsageData || !this.donutChart) return
      const isBicycle = this.vehicleType === 'bicycle'
      const idle = isBicycle ? this.bikeUsageData.bikeIdle || 0 : this.bikeUsageData.eleBikeIdle || 0
      const inUse = isBicycle ? this.bikeUsageData.bikeInUse || 0 : this.bikeUsageData.eleBikeInUse || 0
      this.vehicleTotal = idle + inUse
      this.donutChart.setOption({
        tooltip: { trigger: 'item' },
        legend: {
          orient: 'horizontal',
          bottom: 0,
          data: ['空闲', '使用中']
        },
        series: [{
          type: 'pie',
          radius: ['50%', '75%'],
          center: ['50%', '45%'],
          avoidLabelOverlap: true,
          itemStyle: {
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: true,
            formatter: '{b}\n{c}',
            fontSize: 12
          },
          data: [
            { value: idle, name: '空闲', itemStyle: { color: '#73C0DE' } },
            { value: inUse, name: '使用中', itemStyle: { color: '#5B8FF9' } }
          ]
        }]
      })
    },
    switchVehicleType (type) {
      this.vehicleType = type
      this.updateDonutChart()
    },
    initPieChart () {
      const chart = echarts.init(this.$refs.pieChart)
      this.pieChart = chart
      packageSourceStat({}).then(res => {
        this.packageSourceData = res
        this.updatePieChart()
      }).catch(e => {
        this.$tip.apiFailed(e)
      })
    },
    updatePieChart () {
      if (!this.packageSourceData || !this.pieChart) return
      const data = this.salesPeriod === 'month' ? this.packageSourceData.month : this.packageSourceData.year
      const douyinCount = data ? (data.douyinCount || 0) : 0
      const miniCount = data ? (data.miniCount || 0) : 0
      this.pieChart.setOption({
        tooltip: { trigger: 'item' },
        legend: {
          orient: 'horizontal',
          top: 0,
          data: ['抖音团购', '小程序销售']
        },
        series: [{
          type: 'pie',
          radius: '70%',
          center: ['50%', '55%'],
          avoidLabelOverlap: true,
          itemStyle: {
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: true,
            formatter: '{b}\n{c}',
            fontSize: 12
          },
          data: [
            { value: douyinCount, name: '抖音团购', itemStyle: { color: '#73C0DE' } },
            { value: miniCount, name: '小程序销售', itemStyle: { color: '#5B8FF9' } }
          ]
        }]
      })
    },
    switchSalesPeriod (period) {
      this.salesPeriod = period
      this.updatePieChart()
    },
    handleResize () {
      if (this.barChart) this.barChart.resize()
      if (this.donutChart) this.donutChart.resize()
      if (this.pieChart) this.pieChart.resize()
    },
    loadDashboard () {
      dashboard({}).then(res => {
        if (res) {
          this.statCards = [
            { label: '本月收益', prefix: '¥', value: res.monthIncome || 0, yesterday: res.yesterdayIncome || 0, today: res.todayIncome || 0 },
            { label: '本月订单', value: res.monthOrderCount || 0, yesterday: res.yesterdayOrderCount || 0, today: res.todayOrderCount || 0 },
            { label: '在客户数', value: res.totalMemberCount || 0, yesterday: res.yesterdayNewMember || 0, today: res.todayNewMember || 0 },
            { label: '车辆总数', value: res.totalBikeCount || 0, yesterday: '', today: '' }
          ]
        }
      }).catch(e => {
        this.$tip.apiFailed(e)
      })
    }
  }
}
</script>
 
<style lang="scss" scoped>
.data-dashboard {
  padding: 16px;
  background: #f0f2f5;
  min-height: 100%;
  box-sizing: border-box;
}
 
// 统计卡片
.stat-cards {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
}
 
.stat-card {
  flex: 1;
  background: #eff6ff;
  border-radius: 8px;
  padding: 20px 24px;
}
 
.stat-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 12px;
}
 
.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #1a1a1a;
  margin-bottom: 10px;
 
  &.has-prefix {
    display: flex;
    align-items: baseline;
  }
}
 
.stat-prefix {
  font-size: 20px;
  margin-right: 2px;
}
 
.stat-sub {
  font-size: 12px;
  color: #999;
  display: flex;
  gap: 16px;
}
 
// 图表行
.charts-row {
  display: flex;
  gap: 16px;
}
 
.chart-panel {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-sizing: border-box;
}
 
.chart-bar {
  flex: 1.2;
}
 
.chart-donut,
.chart-pie {
  flex: 1;
}
 
.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}
 
.chart-title {
  font-size: 16px;
  font-weight: bold;
  color: #1a1a1a;
}
 
.chart-toggle {
  ::v-deep .el-button--primary {
    background: #216EEE;
    border-color: #216EEE;
  }
}
 
.chart-subtitle {
  font-size: 12px;
  color: #999;
  margin-bottom: 10px;
}
 
.chart-container {
  width: 100%;
  height: 320px;
}
</style>