张延森 преди 3 години
родител
ревизия
d6834b110d

+ 0
- 111
src/components/RightContent/QRCode.jsx Целия файл

@@ -1,111 +0,0 @@
1
-import React, { useState, useCallback, useEffect } from 'react';
2
-import { QrcodeOutlined } from '@ant-design/icons';
3
-import { Button, Menu, Spin } from 'antd';
4
-import { useModel } from 'umi';
5
-import HeaderDropdown from '../HeaderDropdown';
6
-import QRCode from 'qrcodejs2'
7
-import styles from './index.less';
8
-import { useRef } from 'react';
9
-
10
-const AvatarDropdown = () => {
11
-  const { initialState } = useModel('@@initialState');
12
-  const [imgURL, setImgURL] = useState();
13
-  const [isLoading, setLoading] = useState(false);
14
-  const qrcodeRef = useRef()
15
-
16
-  const qrcode = () => {
17
-    const qrcode = new QRCode(qrcodeRef.current, {
18
-      width: 200,
19
-      height: 200, // 高度
20
-      text: initialState.report_url
21
-    })
22
-  }
23
-  // canvans转base64 很消耗内存所以把转换后的结果imgURL拿出来
24
-  const toUrl = () => {
25
-    qrcodeRef.current.value = ''
26
-    qrcode() // 下载之前首先要有二维码
27
-    // 先找到canvas节点下的二维码图片
28
-    const myCanvas = qrcodeRef.current.getElementsByTagName('canvas')
29
-    // 设置a的href属性将canvas变成png格式
30
-    setLoading(true);
31
-    setImgURL(myCanvas[0].toDataURL('image/jpg'))
32
-    setLoading(false);
33
-  }
34
-  // 下载二维码 把base64转为blob并且下载
35
-  const downloadQrcode = () => {
36
-    const img = qrcodeRef.current.getElementsByTagName('img')
37
-    // 创建一个a节点
38
-    const a = document.createElement('a')
39
-    // 获得浏览器用户代理
40
-    const ua = navigator.userAgent
41
-    var blob = base64ToBlob(imgURL)
42
-    if (ua.indexOf('Firefox') > -1) { // 火狐兼容下载
43
-      a.download = '物业二维码.png'// 下载图片名称,如果填内容识别不到,下载为未知文件,所以我这里就不填为空
44
-      // 讲blob数据转换成url
45
-      a.href = URL.createObjectURL(blob)
46
-      a.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }))// 兼容火狐
47
-    } else { // 谷歌兼容下载
48
-      img.src = imgURL
49
-      a.href = img.src
50
-      // 设置下载文件的名字
51
-      a.download = '物业二维码.png'
52
-      // 点击
53
-      a.click()
54
-    }
55
-  }
56
-  // base64转blob
57
-  const base64ToBlob = (code) => {
58
-    // this.imgURl打印出来可以看到等于data:image/png;base64,+base64码
59
-    const parts = code.split(';base64,')
60
-    // 截取后pasts[0]等于data:image/png  parts[1]就是base64吗 (至于为什么我们上面写image/jpg 现在变成png我也不知道哈哈哈哈哈)
61
-    const contentType = parts[0].split(':')[1]
62
-    // 用:分隔contentType 等于image/png
63
-    const raw = window.atob(code.split(',')[1])
64
-    // 获取解码后的长度
65
-    const rawLength = raw.length
66
-    const uInt8Array = new Uint8Array(rawLength)
67
-    for (let i = 0; i < rawLength; ++i) {
68
-      // 循环 把每位都转为相对应的unicode码
69
-      uInt8Array[i] = raw.charCodeAt(i)
70
-    }
71
-    // Blob储存二进制的
72
-    return new Blob([uInt8Array], { type: contentType })
73
-  }
74
-
75
-  useEffect(() => {
76
-    toUrl();
77
-  }, [])
78
-  const loading = (
79
-    <span className={`${styles.action} ${styles.account}`}>
80
-      <Spin
81
-        size="small"
82
-        style={{
83
-          marginLeft: 8,
84
-          marginRight: 8,
85
-        }}
86
-      />
87
-    </span>
88
-  );
89
-  if (isLoading) {
90
-    return loading;
91
-  }
92
-  const menuHeaderDropdown = (
93
-    <div className={styles.qrcodebox}>
94
-      <img src={imgURL} style={{ width: '160px', height: '160px' }} />
95
-      <p><Button onClick={() => downloadQrcode()}>下载</Button></p>
96
-    </div>
97
-  );
98
-
99
-  return (
100
-    <HeaderDropdown overlay={menuHeaderDropdown}>
101
-      <span className={`${styles.action} ${styles.account}`} style={{ padding: 0 }}>
102
-        <span className={`${styles.name} anticon`}><QrcodeOutlined style={{ fontSize: '24px' }} /></span>
103
-        {/* 只是用来画canvans不用显示 */}
104
-        <div style={{ display: 'none' }} ref={qrcodeRef} />
105
-      </span>
106
-    </HeaderDropdown>
107
-  );
108
-};
109
-
110
-
111
-export default AvatarDropdown;

+ 55
- 0
src/components/RightContent/QrCode/QrCode.jsx Целия файл

@@ -0,0 +1,55 @@
1
+import React, {useState, useRef, useEffect, useCallback} from 'react'
2
+import { useModel } from 'umi'
3
+import { Button, Spin } from 'antd';
4
+import QRCode from 'qrcodejs2'
5
+import styles from './style.less';
6
+
7
+export default (props) => {
8
+  const { initialState } = useModel('@@initialState');
9
+  const [isLoading, setLoading] = useState(false);
10
+  const qrcodeRef = useRef()
11
+  const linkRef = useRef()
12
+
13
+  const downloadQrcode = useCallback(() => {
14
+    if (linkRef.current) {
15
+      linkRef.current.click();
16
+    }
17
+  }, [])
18
+
19
+
20
+  useEffect(() => {
21
+    // 初始化二维码
22
+    const qrcode = new QRCode(qrcodeRef.current, {
23
+      width: 200,
24
+      height: 200, // 高度
25
+      text: initialState.report_url
26
+    })
27
+
28
+    const canvas = qrcodeRef.current.querySelector('canvas')
29
+
30
+    // 创建下载链接
31
+    const aElement = document.createElement('a');
32
+    linkRef.current = aElement;
33
+    linkRef.current.download = 'qrcode.png'
34
+
35
+    // 构造链接地址
36
+    setLoading(true);
37
+    linkRef.current.href = canvas.toDataURL('image/jpg')
38
+    setLoading(false);
39
+  }, [])
40
+
41
+  return isLoading ?
42
+    <Spin
43
+      size="small"
44
+      style={{
45
+        marginLeft: 8,
46
+        marginRight: 8,
47
+      }}
48
+    />
49
+  : (
50
+      <div className={styles.qrcodebox}>
51
+        <div ref={qrcodeRef} />
52
+        <p><Button onClick={downloadQrcode}>下载</Button></p>
53
+      </div>
54
+    )
55
+}

+ 16
- 0
src/components/RightContent/QrCode/index.jsx Целия файл

@@ -0,0 +1,16 @@
1
+import React from 'react';
2
+import { QrcodeOutlined } from '@ant-design/icons';
3
+import HeaderDropdown from '@/components/HeaderDropdown';
4
+import QrCode from './QrCode';
5
+import styles from '../index.less';
6
+
7
+export default (props) => {
8
+
9
+  return (
10
+    <HeaderDropdown overlay={<QrCode />}>
11
+      <span className={`${styles.action} ${styles.account}`} style={{ padding: 0 }}>
12
+        <span className={`${styles.name} anticon`}><QrcodeOutlined style={{ fontSize: '24px' }} /></span>
13
+      </span>
14
+    </HeaderDropdown>
15
+  );
16
+};

+ 7
- 0
src/components/RightContent/QrCode/style.less Целия файл

@@ -0,0 +1,7 @@
1
+.qrcodebox{
2
+  padding: 16px;
3
+  text-align: center;
4
+  img{
5
+    margin-bottom: 16px;
6
+  }
7
+}

+ 2
- 2
src/components/RightContent/index.jsx Целия файл

@@ -1,6 +1,6 @@
1 1
 import { Space } from 'antd';
2 2
 import Avatar from './AvatarDropdown';
3
-import QRCodeCompontent from './QRCode';
3
+import QRCode from './QrCode';
4 4
 import styles from './index.less';
5 5
 
6 6
 const GlobalHeaderRight = () => {
@@ -10,7 +10,7 @@ const GlobalHeaderRight = () => {
10 10
   
11 11
   return (
12 12
     <Space className={className}>
13
-      <QRCodeCompontent />
13
+      <QRCode />
14 14
       <Avatar />      
15 15
     </Space>
16 16
   );

+ 0
- 7
src/components/RightContent/index.less Целия файл

@@ -10,13 +10,6 @@
10 10
     min-width: 160px;
11 11
   }
12 12
 }
13
-.qrcodebox{
14
-  padding: 16px;
15
-  text-align: center;
16
-  img{
17
-    margin-bottom: 16px;
18
-  }
19
-}
20 13
 
21 14
 .right {
22 15
   display: flex;