张延森 4 år sedan
förälder
incheckning
e6f822ebb3
3 ändrade filer med 63 tillägg och 7 borttagningar
  1. 1
    0
      package.json
  2. 39
    0
      src/components/ECharts/index.jsx
  3. 23
    7
      src/pages/Welcome/components/HeaderContent.jsx

+ 1
- 0
package.json Visa fil

@@ -54,6 +54,7 @@
54 54
     "antd": "^4.12.0",
55 55
     "array-move": "^3.0.1",
56 56
     "classnames": "^2.2.6",
57
+    "echarts": "^5.1.0",
57 58
     "lodash": "^4.17.11",
58 59
     "md5": "^2.3.0",
59 60
     "moment": "^2.25.3",

+ 39
- 0
src/components/ECharts/index.jsx Visa fil

@@ -0,0 +1,39 @@
1
+// echarts 包太大, 因此需要按需引入
2
+import React, { useEffect, useRef, useImperativeHandle } from 'react';
3
+// 引入 echarts 核心模块,核心模块提供了 echarts 使用必须要的接口。
4
+import * as echarts from 'echarts/core';
5
+// 引入柱状图图表,图表后缀都为 Chart
6
+import {
7
+    BarChart
8
+} from 'echarts/charts';
9
+// 引入提示框,标题,直角坐标系组件,组件后缀都为 Component
10
+import {
11
+    TitleComponent,
12
+    TooltipComponent,
13
+    GridComponent
14
+} from 'echarts/components';
15
+
16
+// 注册必须的组件
17
+echarts.use(
18
+  [TitleComponent, TooltipComponent, GridComponent, BarChart]
19
+);
20
+
21
+export default React.forwardRef((props, ref) => {
22
+  const el = useRef()
23
+  const chartRef = useRef()
24
+
25
+  // 向父组件公开 echarts 实例
26
+  useImperativeHandle(ref, () => ({
27
+    chart: chartRef.current,
28
+  }))
29
+
30
+  useEffect(() => {
31
+    const chart = echarts.init(el.current)
32
+    chart.setOption(props.option)
33
+    chartRef.current = chart
34
+
35
+    return () => chart.dispose()
36
+  }, [props.option])
37
+
38
+  return <div ref={el}></div>
39
+})

+ 23
- 7
src/pages/Welcome/components/HeaderContent.jsx Visa fil

@@ -1,10 +1,24 @@
1
-import React from 'react'
2
-import { Image, Statistic, Row, Col, Badge, Divider } from 'antd'
1
+import React, { useState, useEffect } from 'react'
2
+import { Image, Statistic, Spin, Badge } from 'antd'
3 3
 import { connect } from 'umi'
4
+import request from '@/utils/request'
4 5
 import { defaultAvatar } from '@/utils/utils';
5 6
 import Styles from '../style.less'
6 7
 
7 8
 const HeaderContent = (props) => {
9
+  const [loading, setLoading] = useState(false)
10
+  const [statis, setStatis] = useState({})
11
+
12
+  useEffect(() => {
13
+    setLoading(true)
14
+    request('/index/post-data').then((res) => {
15
+      setStatis(res)
16
+      setLoading(false)
17
+    }).catch((e) => {
18
+      setLoading(false)
19
+    })
20
+  }, [])
21
+
8 22
   return (
9 23
     <div className={Styles['header-content']}>
10 24
       <div className={Styles['ctt-header']}>
@@ -18,11 +32,13 @@ const HeaderContent = (props) => {
18 32
         </p>
19 33
       </div>
20 34
       <div className={Styles['ctt-footer']}>
21
-        <div className={Styles['st-wrapper']}>
22
-          <div className={Styles['st-item']}><Statistic title="科普数量" value={112893} /></div>
23
-          <div className={Styles['st-item']}><Statistic title="浏览总数" value={112893} /></div>
24
-          <div className={Styles['st-item']}><Statistic title="学生总数" value={112893} /></div>
25
-        </div>
35
+        <Spin spinning={loading}>
36
+          <div className={Styles['st-wrapper']}>
37
+            <div className={Styles['st-item']}><Statistic title="科普数量" value={statis.total} /></div>
38
+            <div className={Styles['st-item']}><Statistic title="浏览总数" value={statis.pv} /></div>
39
+            <div className={Styles['st-item']}><Statistic title="学生总数" value={statis.student} /></div>
40
+          </div>
41
+        </Spin>
26 42
       </div>
27 43
     </div>
28 44
   )