Selaa lähdekoodia

Merge branch 'dev' of http://git.ycjcjy.com/ubpa/pc-admin into dev

李志伟 3 vuotta sitten
vanhempi
commit
fe79fc7621

+ 7
- 0
src/components/QRCode/index.jsx Näytä tiedosto

@@ -47,6 +47,13 @@ export default forwardRef((props, ref) => {
47 47
           resolve()
48 48
         }).catch(reject)
49 49
       })
50
+    },
51
+    getCanvas: () => {
52
+      return new Promise((resolve, reject) => {
53
+        html2canvas(rootRef.current).then((canvas) => {
54
+          resolve(canvas)
55
+        }).catch(reject)
56
+      })
50 57
     }
51 58
   }), [fileName])
52 59
 

+ 1
- 1
src/components/QRCode/style.less Näytä tiedosto

@@ -11,7 +11,7 @@
11 11
   }
12 12
 
13 13
   h3 {
14
-    font-size: 18px;
14
+    font-size: 24px;
15 15
     font-weight: bold;
16 16
     line-height: 3em;
17 17
   }

+ 4
- 15
src/pages/org/org/Edit/components/BasicPage.jsx Näytä tiedosto

@@ -1,10 +1,9 @@
1 1
 import { Form, Input, Button } from 'antd';
2
-import QRCode from '@/components/QRCode'
3 2
 import { history } from 'umi';
4 3
 import React, { useEffect, useState, useCallback, useRef, useMemo } from 'react'
5 4
 import { useModel } from 'umi'
6 5
 import { getDetail, saveOrg, updateOrg } from '@/services/org';
7
-
6
+import QRCode from './QRCode'
8 7
 
9 8
 export default (props) => {
10 9
   const { id } = props;
@@ -13,13 +12,8 @@ export default (props) => {
13 12
   const [form] = Form.useForm();
14 13
   const [formData, setFormData] = useState()
15 14
   const [loading, setLoading] = useState(false)
16
-  const qrcodeRef = useRef()
15
+
17 16
   const qrcodeText = useMemo(() => initialState.report_url + '#resume-work-form?org=' + id, [id, initialState.report_url])
18
-  const downloadQrcode = useCallback(() => {
19
-    if (qrcodeRef.current) {
20
-      qrcodeRef.current.download();
21
-    }
22
-  }, [])
23 17
 
24 18
   const onFinish = (values) => {
25 19
     setLoading(true)
@@ -76,19 +70,14 @@ export default (props) => {
76 70
           <Input type='number' min='0' />
77 71
         </Form.Item>
78 72
         {
79
-          id && <Form.Item label=' ' colon={false} wrapperCol={{ offset: 3 }}>
80
-            <QRCode ref={qrcodeRef} border width={200} content={qrcodeText} title={formData?.orgName} fileName={formData?.orgName} />
73
+          id && <Form.Item label='申报二维码' >
74
+            <QRCode text={qrcodeText} title={formData?.orgName} />
81 75
           </Form.Item>
82 76
         }
83 77
         <Form.Item label=' ' colon={false} >
84 78
           <Button type="primary" htmlType="submit" loading={loading}>
85 79
             保存
86 80
           </Button>
87
-          {id &&
88
-            <Button style={{ marginLeft: '32px' }} onClick={downloadQrcode} >
89
-              下载二维码
90
-            </Button>
91
-          }
92 81
           <Button style={{ marginLeft: '32px' }} onClick={() => { history.go(-1); }} >返回</Button>
93 82
         </Form.Item>
94 83
       </Form>

+ 41
- 0
src/pages/org/org/Edit/components/QRCode.jsx Näytä tiedosto

@@ -0,0 +1,41 @@
1
+import React, { useRef } from 'react'
2
+import { Popover, Button } from 'antd';
3
+import { QrcodeOutlined } from '@ant-design/icons';
4
+import QRCode from '@/components/QRCode'
5
+
6
+const style = {
7
+  display: 'flex',
8
+  alignItem: 'center'
9
+}
10
+
11
+export default (props) => {
12
+  const { text, title } = props
13
+
14
+  const qrcodeRef = useRef()
15
+  const download = () => {
16
+    if (qrcodeRef.current) {
17
+      qrcodeRef.current.getCanvas().then((canvas) => {
18
+        const imageData = canvas.toDataURL('image/png', .8);
19
+        const link = document.createElement('a');
20
+        link.download = `${title}.png`
21
+        link.href = imageData
22
+        link.click()
23
+      });
24
+    }
25
+  }
26
+
27
+  const content = (
28
+    <QRCode ref={qrcodeRef} border content={text} title={title} />
29
+  )
30
+
31
+  return (
32
+    <div style={style}>
33
+      {/* 这个 div 主要用于下载 */}
34
+      <div style={{ zIndex: -1, position: 'fixed', top: '-500vh' }}>{content}</div>
35
+      <Popover content={content} placement="right">
36
+        <QrcodeOutlined style={{ fontSize: '32px' }} />
37
+      </Popover>
38
+      <Button style={{ marginLeft: '16px' }} onClick={download} type='link'>下载</Button>
39
+    </div>
40
+  )
41
+}