张延森 3 年之前
父節點
當前提交
4405f3f566

+ 10
- 1
src/api/stat.js 查看文件

2
 
2
 
3
 export const getStatTotal = () => request({
3
 export const getStatTotal = () => request({
4
   url: '/admin/stat-total'
4
   url: '/admin/stat-total'
5
-})
5
+})
6
+
7
+
8
+export const getStatPV = (params) => request({
9
+  url: '/admin/pv', params
10
+})
11
+
12
+export const getStatUV = (params) => request({
13
+  url: '/admin/uv', params
14
+})

+ 11
- 3
src/components/ECharts/Line.vue 查看文件

1
 <template>
1
 <template>
2
-  <echarts :option="option" :loading="loading"></echarts>
2
+  <echarts :option="opt" :loading="loading"></echarts>
3
 </template>
3
 </template>
4
 
4
 
5
 <script>
5
 <script>
6
 export default {
6
 export default {
7
   name: 'LineChart',
7
   name: 'LineChart',
8
+  components: {
9
+    echarts: () => import('./index.vue')
10
+  },
8
   props: {
11
   props: {
9
     option: {
12
     option: {
10
       type: Object,
13
       type: Object,
25
         yAxis: {
28
         yAxis: {
26
           type: 'value'
29
           type: 'value'
27
         },
30
         },
31
+        tooltip: {
32
+          trigger: 'axis'
33
+        },
34
+        dataset: {
35
+          dimensions: ['key', 'value'],
36
+          source: this.value,
37
+        },
28
         series: [
38
         series: [
29
           {
39
           {
30
             type: 'line',
40
             type: 'line',
31
             smooth: true
41
             smooth: true
32
           }
42
           }
33
         ],
43
         ],
34
-        dimensions: ['key', 'value'],
35
-        source: this.value,
36
         ...this.option,
44
         ...this.option,
37
       }
45
       }
38
     }
46
     }

+ 3
- 1
src/components/ECharts/index.vue 查看文件

54
       handler(v) {
54
       handler(v) {
55
         if (this.echartsRef) {
55
         if (this.echartsRef) {
56
           this.echartsRef.setOption(v)
56
           this.echartsRef.setOption(v)
57
+          console.log('-------------', v)
57
         }
58
         }
58
       },
59
       },
59
       immediate: true
60
       immediate: true
70
   },
71
   },
71
   mounted() {
72
   mounted() {
72
     this.echartsRef = echarts.init(this.$el)
73
     this.echartsRef = echarts.init(this.$el)
73
-    this.echartsRef.setOption(v)
74
+    this.echartsRef.setOption(this.option)
74
   }
75
   }
75
 }
76
 }
76
 </script>
77
 </script>
79
 .echart {
80
 .echart {
80
   width: 100%;
81
   width: 100%;
81
   height: 100%;
82
   height: 100%;
83
+  min-height: 400px;
82
 }
84
 }
83
 </style>
85
 </style>

+ 1
- 1
src/views/dashboard/components/Statistic.vue 查看文件

9
 export default {
9
 export default {
10
   name: 'Statistic',
10
   name: 'Statistic',
11
   props: {
11
   props: {
12
-    title: String,
12
+    title: [String, Number],
13
     value: String,
13
     value: String,
14
   }
14
   }
15
 }
15
 }

+ 73
- 5
src/views/dashboard/index.vue 查看文件

2
   <div class="dashboard-container">
2
   <div class="dashboard-container">
3
     <div class="dashboard-text">欢迎使用{{ name }}</div>
3
     <div class="dashboard-text">欢迎使用{{ name }}</div>
4
 
4
 
5
-    <el-row type="flex" class="row-bg" justify="space-between">
5
+    <el-row class="sec" type="flex" justify="space-between">
6
       <el-col :span="6">
6
       <el-col :span="6">
7
         <statistic title="报名总数" :value="total.reg" />
7
         <statistic title="报名总数" :value="total.reg" />
8
       </el-col>
8
       </el-col>
13
         <statistic title="UV 总数" :value="total.uv" />
13
         <statistic title="UV 总数" :value="total.uv" />
14
       </el-col>
14
       </el-col>
15
     </el-row>
15
     </el-row>
16
+
17
+    <el-row class="sec" type="flex" justify="space-between" gutter="24">
18
+      <el-col :span="12">
19
+        <el-card>
20
+          <div slot="header">
21
+            <span>PV访问</span>
22
+          </div>
23
+          <line-chart :value="pv" />
24
+        </el-card>
25
+      </el-col>
26
+      <el-col :span="12">
27
+        <el-card>
28
+          <div slot="header">
29
+            <span>UV访问</span>
30
+          </div>
31
+          <line-chart :value="uv" />
32
+        </el-card>
33
+      </el-col>
34
+    </el-row>
35
+
16
   </div>
36
   </div>
17
 </template>
37
 </template>
18
 
38
 
19
 <script>
39
 <script>
20
 import { mapGetters } from 'vuex'
40
 import { mapGetters } from 'vuex'
21
-import { getStatTotal } from '@/api/stat'
41
+import { getStatTotal, getStatPV, getStatUV } from '@/api/stat'
42
+
43
+const formateNum = n => n > 9 ? `${n}` : `0${n}`
22
 
44
 
23
 export default {
45
 export default {
24
   name: 'Dashboard',
46
   name: 'Dashboard',
25
   components: {
47
   components: {
26
-    Statistic: () => import('./components/Statistic.vue')
48
+    Statistic: () => import('./components/Statistic.vue'),
49
+    LineChart: () => import('@/components/ECharts/Line.vue'),
27
   },
50
   },
28
   computed: {
51
   computed: {
29
-    ...mapGetters(['name'])
52
+    ...mapGetters(['name']),
53
+    lineParam() {
54
+      // 为了包含今天, 需要少计算1天
55
+      const mills = (this.days - 1) * 24 * 60 * 60 * 1000
56
+      const start = new Date(this.today - mills)
57
+      const year = start.getFullYear()
58
+      const mon = start.getMonth() + 1
59
+      const dt = start.getDate()
60
+
61
+      return {
62
+        startDate: `${year}-${formateNum(mon)}-${formateNum(dt)}`,
63
+        days: this.days,
64
+      }
65
+    }
30
   },
66
   },
31
   data() {
67
   data() {
32
     return {
68
     return {
33
-      total: {}
69
+      total: {},
70
+      today: new Date(),
71
+      days: 15, // 默认统计 15 天的
72
+      pv: [],
73
+      uv: [],
74
+      loading: {
75
+        pv: false,
76
+        uv: false,
77
+      }
34
     }
78
     }
35
   },
79
   },
36
   mounted() {
80
   mounted() {
37
     this.statTotal()
81
     this.statTotal()
82
+    this.statPV()
83
+    this.statUV()
38
   },
84
   },
39
   methods: {
85
   methods: {
40
     statTotal() {
86
     statTotal() {
41
       getStatTotal().then(res => {
87
       getStatTotal().then(res => {
42
         this.total = res
88
         this.total = res
43
       })
89
       })
90
+    },
91
+    statPV() {
92
+      this.loading.pv = true
93
+      getStatPV(this.lineParam).then(res => {
94
+        this.pv = res
95
+        this.loading.pv = false
96
+      }).catch(() => {
97
+        this.loading.pv = false
98
+      })
99
+    },
100
+    statUV() {
101
+      this.loading.uv = true
102
+      getStatUV(this.lineParam).then(res => {
103
+        this.uv = res
104
+        this.loading.uv = false
105
+      }).catch(() => {
106
+        this.loading.uv = false
107
+      })
44
     }
108
     }
45
   }
109
   }
46
 }
110
 }
56
     line-height: 46px;
120
     line-height: 46px;
57
   }
121
   }
58
 }
122
 }
123
+
124
+.sec {
125
+  margin: 24px 0;
126
+}
59
 </style>
127
 </style>