李志伟 3 anni fa
parent
commit
55ec2cf80b

+ 1
- 1
src/app.jsx Vedi File

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

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

@@ -1,55 +1,45 @@
1 1
 import React, { useState, useCallback, useEffect } from 'react';
2 2
 import { QrcodeOutlined } from '@ant-design/icons';
3
-import { Menu, Spin } from 'antd';
3
+import { Button, Menu, Spin } from 'antd';
4 4
 import { useModel } from 'umi';
5 5
 import HeaderDropdown from '../HeaderDropdown';
6 6
 import QRCode from 'qrcodejs2'
7 7
 import styles from './index.less';
8
+import { useRef } from 'react';
8 9
 
9 10
 const AvatarDropdown = () => {
10
-  const [showPasswordModal, setShowPasswordModal] = useState(false);
11
-  const { initialState, setInitialState } = useModel('@@initialState');
11
+  const { initialState } = useModel('@@initialState');
12 12
   const [imgURL, setImgURL] = useState();
13
+  const [isLoading, setLoading] = useState(false);
14
+  const qrcodeRef = useRef()
13 15
 
14
-  
15 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 18
       width: 200,
19 19
       height: 200, // 高度
20
-      text: localStorage.getItem('qrcode')
20
+      text: initialState.report_url
21 21
     })
22 22
   }
23 23
   // canvans转base64 很消耗内存所以把转换后的结果imgURL拿出来
24 24
   const toUrl = () => {
25
-    document.getElementById('qrcode').innerHTML = ''
25
+    qrcodeRef.current.value = ''
26 26
     qrcode() // 下载之前首先要有二维码
27 27
     // 先找到canvas节点下的二维码图片
28
-    const myCanvas = document.getElementById('qrcode').getElementsByTagName('canvas')
28
+    const myCanvas = qrcodeRef.current.getElementsByTagName('canvas')
29 29
     // 设置a的href属性将canvas变成png格式
30
+    setLoading(true);
30 31
     setImgURL(myCanvas[0].toDataURL('image/jpg'))
32
+    setLoading(false);
31 33
   }
32 34
   // 下载二维码 把base64转为blob并且下载
33 35
   const downloadQrcode = () => {
34
-    const img = document.getElementById('qrcode').getElementsByTagName('img')
36
+    const img = qrcodeRef.current.getElementsByTagName('img')
35 37
     // 创建一个a节点
36 38
     const a = document.createElement('a')
37 39
     // 获得浏览器用户代理
38 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 43
       a.download = '物业二维码.png'// 下载图片名称,如果填内容识别不到,下载为未知文件,所以我这里就不填为空
54 44
       // 讲blob数据转换成url
55 45
       a.href = URL.createObjectURL(blob)
@@ -70,7 +60,7 @@ const AvatarDropdown = () => {
70 60
     // 截取后pasts[0]等于data:image/png  parts[1]就是base64吗 (至于为什么我们上面写image/jpg 现在变成png我也不知道哈哈哈哈哈)
71 61
     const contentType = parts[0].split(':')[1]
72 62
     // 用:分隔contentType 等于image/png
73
-    const raw = window.atob(parts[1])
63
+    const raw = window.atob(code.split(',')[1])
74 64
     // 获取解码后的长度
75 65
     const rawLength = raw.length
76 66
     const uInt8Array = new Uint8Array(rawLength)
@@ -83,25 +73,8 @@ const AvatarDropdown = () => {
83 73
   }
84 74
 
85 75
   useEffect(() => {
86
-    console.log(initialState);
87 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 78
   const loading = (
106 79
     <span className={`${styles.action} ${styles.account}`}>
107 80
       <Spin
@@ -113,37 +86,22 @@ const AvatarDropdown = () => {
113 86
       />
114 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 90
     return loading;
125 91
   }
126
-
127
-
128 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 99
   return (
143 100
     <HeaderDropdown overlay={menuHeaderDropdown}>
144 101
       <span className={`${styles.action} ${styles.account}`} style={{ padding: 0 }}>
145 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 105
       </span>
148 106
     </HeaderDropdown>
149 107
   );

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

@@ -10,6 +10,13 @@
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
+}
13 20
 
14 21
 .right {
15 22
   display: flex;

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

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