|
@@ -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;
|