李志伟 3 years ago
parent
commit
55ec2cf80b

+ 1
- 1
src/app.jsx View File

28
 
28
 
29
   if (history.location.pathname !== loginPath) {
29
   if (history.location.pathname !== loginPath) {
30
     const res = await fetchUserInfo();
30
     const res = await fetchUserInfo();
31
-    localStorage.setItem('qrcode',res.report_url);
32
     return {
31
     return {
33
       fetchUserInfo,
32
       fetchUserInfo,
34
       currentUser: res.user,
33
       currentUser: res.user,
35
       menuAccess: res.menu,
34
       menuAccess: res.menu,
35
+      report_url:res.report_url,
36
       settings: {},
36
       settings: {},
37
     };
37
     };
38
   }
38
   }

+ 22
- 64
src/components/RightContent/QRCode.jsx View File

1
 import React, { useState, useCallback, useEffect } from 'react';
1
 import React, { useState, useCallback, useEffect } from 'react';
2
 import { QrcodeOutlined } from '@ant-design/icons';
2
 import { QrcodeOutlined } from '@ant-design/icons';
3
-import { Menu, Spin } from 'antd';
3
+import { Button, Menu, Spin } from 'antd';
4
 import { useModel } from 'umi';
4
 import { useModel } from 'umi';
5
 import HeaderDropdown from '../HeaderDropdown';
5
 import HeaderDropdown from '../HeaderDropdown';
6
 import QRCode from 'qrcodejs2'
6
 import QRCode from 'qrcodejs2'
7
 import styles from './index.less';
7
 import styles from './index.less';
8
+import { useRef } from 'react';
8
 
9
 
9
 const AvatarDropdown = () => {
10
 const AvatarDropdown = () => {
10
-  const [showPasswordModal, setShowPasswordModal] = useState(false);
11
-  const { initialState, setInitialState } = useModel('@@initialState');
11
+  const { initialState } = useModel('@@initialState');
12
   const [imgURL, setImgURL] = useState();
12
   const [imgURL, setImgURL] = useState();
13
+  const [isLoading, setLoading] = useState(false);
14
+  const qrcodeRef = useRef()
13
 
15
 
14
-  
15
   const qrcode = () => {
16
   const qrcode = () => {
16
-    // eslint-disable-next-line no-unused-vars
17
-    const qrcode = new QRCode('qrcode', {
17
+    const qrcode = new QRCode(qrcodeRef.current, {
18
       width: 200,
18
       width: 200,
19
       height: 200, // 高度
19
       height: 200, // 高度
20
-      text: localStorage.getItem('qrcode')
20
+      text: initialState.report_url
21
     })
21
     })
22
   }
22
   }
23
   // canvans转base64 很消耗内存所以把转换后的结果imgURL拿出来
23
   // canvans转base64 很消耗内存所以把转换后的结果imgURL拿出来
24
   const toUrl = () => {
24
   const toUrl = () => {
25
-    document.getElementById('qrcode').innerHTML = ''
25
+    qrcodeRef.current.value = ''
26
     qrcode() // 下载之前首先要有二维码
26
     qrcode() // 下载之前首先要有二维码
27
     // 先找到canvas节点下的二维码图片
27
     // 先找到canvas节点下的二维码图片
28
-    const myCanvas = document.getElementById('qrcode').getElementsByTagName('canvas')
28
+    const myCanvas = qrcodeRef.current.getElementsByTagName('canvas')
29
     // 设置a的href属性将canvas变成png格式
29
     // 设置a的href属性将canvas变成png格式
30
+    setLoading(true);
30
     setImgURL(myCanvas[0].toDataURL('image/jpg'))
31
     setImgURL(myCanvas[0].toDataURL('image/jpg'))
32
+    setLoading(false);
31
   }
33
   }
32
   // 下载二维码 把base64转为blob并且下载
34
   // 下载二维码 把base64转为blob并且下载
33
   const downloadQrcode = () => {
35
   const downloadQrcode = () => {
34
-    const img = document.getElementById('qrcode').getElementsByTagName('img')
36
+    const img = qrcodeRef.current.getElementsByTagName('img')
35
     // 创建一个a节点
37
     // 创建一个a节点
36
     const a = document.createElement('a')
38
     const a = document.createElement('a')
37
     // 获得浏览器用户代理
39
     // 获得浏览器用户代理
38
     const ua = navigator.userAgent
40
     const ua = navigator.userAgent
39
-    if (ua.indexOf('Trident') !== -1 && ua.indexOf('Windows') !== -1) { // IE内核 并且  windows系统 情况下 才执行
40
-      // canvas转base64 后生成的数据是由 图片格式+逗号+真正的base64码组成的
41
-      var bstr = atob(imgURL.split(',')[1])
42
-      var n = bstr.length
43
-      var u8arr = new Uint8Array(n)
44
-      while (n--) {
45
-        u8arr[n] = bstr.charCodeAt(n)
46
-      }
47
-      var blob = new Blob([u8arr])
48
-      window.navigator.msSaveOrOpenBlob(blob, '物业二维码.png')
49
-    } else if (ua.indexOf('Firefox') > -1) { // 火狐兼容下载
50
-      const blob = base64ToBlob(imgURL) // new Blob([content]);
51
-      const evt = document.createEvent('HTMLEvents')
52
-      evt.initEvent('click', true, true)// initEvent 不加后两个参数在FF下会报错  事件类型,是否冒泡,是否阻止浏览器的默认行为
41
+    var blob = base64ToBlob(imgURL)
42
+    if (ua.indexOf('Firefox') > -1) { // 火狐兼容下载
53
       a.download = '物业二维码.png'// 下载图片名称,如果填内容识别不到,下载为未知文件,所以我这里就不填为空
43
       a.download = '物业二维码.png'// 下载图片名称,如果填内容识别不到,下载为未知文件,所以我这里就不填为空
54
       // 讲blob数据转换成url
44
       // 讲blob数据转换成url
55
       a.href = URL.createObjectURL(blob)
45
       a.href = URL.createObjectURL(blob)
70
     // 截取后pasts[0]等于data:image/png  parts[1]就是base64吗 (至于为什么我们上面写image/jpg 现在变成png我也不知道哈哈哈哈哈)
60
     // 截取后pasts[0]等于data:image/png  parts[1]就是base64吗 (至于为什么我们上面写image/jpg 现在变成png我也不知道哈哈哈哈哈)
71
     const contentType = parts[0].split(':')[1]
61
     const contentType = parts[0].split(':')[1]
72
     // 用:分隔contentType 等于image/png
62
     // 用:分隔contentType 等于image/png
73
-    const raw = window.atob(parts[1])
63
+    const raw = window.atob(code.split(',')[1])
74
     // 获取解码后的长度
64
     // 获取解码后的长度
75
     const rawLength = raw.length
65
     const rawLength = raw.length
76
     const uInt8Array = new Uint8Array(rawLength)
66
     const uInt8Array = new Uint8Array(rawLength)
83
   }
73
   }
84
 
74
 
85
   useEffect(() => {
75
   useEffect(() => {
86
-    console.log(initialState);
87
     toUrl();
76
     toUrl();
88
   }, [])
77
   }, [])
89
-
90
-  const onMenuClick = useCallback(
91
-    (event) => {
92
-      const { key } = event;
93
-      if (key === 'download') {
94
-        downloadQrcode()
95
-        return;
96
-      }
97
-
98
-      if (key === 'changepassword') {
99
-        setShowPasswordModal(true);
100
-        return;
101
-      }
102
-    },
103
-    [setInitialState,imgURL],
104
-  );
105
   const loading = (
78
   const loading = (
106
     <span className={`${styles.action} ${styles.account}`}>
79
     <span className={`${styles.action} ${styles.account}`}>
107
       <Spin
80
       <Spin
113
       />
86
       />
114
     </span>
87
     </span>
115
   );
88
   );
116
-
117
-  if (!initialState) {
118
-    return loading;
119
-  }
120
-
121
-  const { currentUser } = initialState;
122
-
123
-  if (!currentUser || !currentUser.userName) {
89
+  if (isLoading) {
124
     return loading;
90
     return loading;
125
   }
91
   }
126
-
127
-
128
   const menuHeaderDropdown = (
92
   const menuHeaderDropdown = (
129
-    <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick}>
130
-      <Menu.Item key="changepassword">
131
-        {
132
-          imgURL ? <img src={imgURL} style={{ width: '160px', height: '160px' }} />
133
-            : '二维码'
134
-        }
135
-      </Menu.Item>
136
-      <Menu.Item key="download" style={{textAlign:'center'}}>
137
-        下载
138
-      </Menu.Item>
139
-    </Menu>
93
+    <div className={styles.qrcodebox}>
94
+      <img src={imgURL} style={{ width: '160px', height: '160px' }} />
95
+      <p><Button onClick={() => downloadQrcode()}>下载</Button></p>
96
+    </div>
140
   );
97
   );
141
 
98
 
142
   return (
99
   return (
143
     <HeaderDropdown overlay={menuHeaderDropdown}>
100
     <HeaderDropdown overlay={menuHeaderDropdown}>
144
       <span className={`${styles.action} ${styles.account}`} style={{ padding: 0 }}>
101
       <span className={`${styles.action} ${styles.account}`} style={{ padding: 0 }}>
145
         <span className={`${styles.name} anticon`}><QrcodeOutlined style={{ fontSize: '24px' }} /></span>
102
         <span className={`${styles.name} anticon`}><QrcodeOutlined style={{ fontSize: '24px' }} /></span>
146
-        <div style={{display:'none'}} id="qrcode" />
103
+        {/* 只是用来画canvans不用显示 */}
104
+        <div style={{ display: 'none' }} ref={qrcodeRef} />
147
       </span>
105
       </span>
148
     </HeaderDropdown>
106
     </HeaderDropdown>
149
   );
107
   );

+ 7
- 0
src/components/RightContent/index.less View File

10
     min-width: 160px;
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
+}
13
 
20
 
14
 .right {
21
 .right {
15
   display: flex;
22
   display: flex;

+ 4
- 10
src/pages/user/Login/index.jsx View File

1
 import { LockOutlined, UserOutlined } from '@ant-design/icons';
1
 import { LockOutlined, UserOutlined } from '@ant-design/icons';
2
-import { Alert, message, Tabs } from 'antd';
2
+import { message } from 'antd';
3
 import React, { useState } from 'react';
3
 import React, { useState } from 'react';
4
 import md5 from 'md5';
4
 import md5 from 'md5';
5
 import ProForm, { ProFormText } from '@ant-design/pro-form';
5
 import ProForm, { ProFormText } from '@ant-design/pro-form';
6
-import { useIntl, Link, history, FormattedMessage, SelectLang, useModel } from 'umi';
6
+import { useIntl, Link, history, FormattedMessage, useModel } from 'umi';
7
 import Footer from '@/components/Footer';
7
 import Footer from '@/components/Footer';
8
 import { login } from '@/services/user';
8
 import { login } from '@/services/user';
9
 import styles from './index.less';
9
 import styles from './index.less';
10
 
10
 
11
 const Login = () => {
11
 const Login = () => {
12
   const [submitting, setSubmitting] = useState(false);
12
   const [submitting, setSubmitting] = useState(false);
13
-  const { initialState, setInitialState } = useModel('@@initialState');
13
+  const { setInitialState } = useModel('@@initialState');
14
   const intl = useIntl();
14
   const intl = useIntl();
15
 
15
 
16
   const handleSubmit = async (values) => {
16
   const handleSubmit = async (values) => {
17
     setSubmitting(true);
17
     setSubmitting(true);
18
-
19
     try {
18
     try {
20
       // 登录
19
       // 登录
21
       const { user, menu,report_url } = await login({ ...values, password: md5(values.password) });
20
       const { user, menu,report_url } = await login({ ...values, password: md5(values.password) });
22
 
21
 
23
-      await setInitialState((s) => ({ ...s, currentUser: user, menuAccess: menu }));
24
-
22
+      await setInitialState((s) => ({ ...s, currentUser: user, menuAccess: menu,report_url:report_url }));
25
       message.success('登录成功!');
23
       message.success('登录成功!');
26
-
27
-      localStorage.setItem('qrcode',report_url);
28
-      
29
       /** 此方法会跳转到 redirect 参数所在的位置 */
24
       /** 此方法会跳转到 redirect 参数所在的位置 */
30
       if (!history) return;
25
       if (!history) return;
31
       const { query } = history.location;
26
       const { query } = history.location;
35
     } catch (error) {
30
     } catch (error) {
36
       console.error(error);
31
       console.error(error);
37
     }
32
     }
38
-
39
     setSubmitting(false);
33
     setSubmitting(false);
40
   };
34
   };
41
 
35