Browse Source

二维码

李志伟 3 years ago
parent
commit
d101bd0c63
2 changed files with 154 additions and 43 deletions
  1. 147
    43
      src/pages/org/org/Edit.jsx
  2. 7
    0
      src/services/org.js

+ 147
- 43
src/pages/org/org/Edit.jsx View File

@@ -1,27 +1,32 @@
1
-import React, { useEffect, useState } from 'react'
2
-import { Form, Input, Button, Checkbox } from 'antd';
1
+import React, { useEffect, useState, useCallback, useRef } from 'react'
2
+import { useModel } from 'umi'
3
+import { Form, Input, Button,message } from 'antd';
3 4
 import ProCard from '@ant-design/pro-card';
4 5
 import { PageHeaderWrapper } from '@ant-design/pro-layout';
5
-import { getDetail, saveOrg, updateOrg } from '@/services/org';
6
+import QRCode from 'qrcodejs2'
7
+import { getDetail, saveOrg, updateOrg,saveOrgUser,getOrgAdminDetail } from '@/services/org';
8
+import md5 from 'md5';
6 9
 
7 10
 export default (props) => {
8 11
   const { history } = props;
9 12
   const { id } = history.location.query;
13
+  const { initialState } = useModel('@@initialState');
10 14
 
11 15
   const [form] = Form.useForm();
12 16
   const [formData, setFormData] = useState()
13 17
   const [loading, setLoading] = useState(false)
18
+  const qrcodeRef = useRef()
19
+  const linkRef = useRef()
14 20
 
15
-  useEffect(() => {
16
-    if (id) {
17
-      getDetail(id).then(res => {
21
+  const [adminForm] = Form.useForm();
22
+  const [adminFormData, setAdminFormData] = useState()
23
+  const [adminLoading, setAdminLoading] = useState(false)
18 24
 
19
-        setFormData(res);
20
-        form.setFieldsValue(res);
21
-      })
25
+  const downloadQrcode = useCallback(() => {
26
+    if (linkRef.current) {
27
+      linkRef.current.click();
22 28
     }
23
-  }, [id, form])
24
-
29
+  }, [])
25 30
   const onFinish = (values) => {
26 31
     setLoading(true)
27 32
     if (id) {
@@ -40,46 +45,145 @@ export default (props) => {
40 45
       })
41 46
     }
42 47
   };
43
-
44 48
   const onFinishFailed = (errorInfo) => {
45 49
     console.log('Failed:', errorInfo);
46 50
   };
47 51
 
52
+  const onAdminFinish=(values)=>{
53
+    if (!/^1[0-9]{10}$/.test(values.phone)) {
54
+      message.warning('请输入正确的十一位手机号');
55
+      setLoading(false);
56
+      return false;
57
+    }
58
+    var data={...adminFormData,...values,password:md5(values.password),isAdmin:true};
59
+    setAdminLoading(true)
60
+    saveOrgUser(id,data).then(res=>{
61
+      message.success('保存成功');
62
+      history.go(-1);
63
+      setAdminLoading(false)
64
+    }).catch(err=>{
65
+      console.log(err);
66
+      setAdminLoading(false)
67
+    })
68
+  }
69
+
70
+  useEffect(() => {
71
+    if (id) {
72
+      getDetail(id).then(res => {
73
+        setFormData(res);
74
+        form.setFieldsValue(res);
75
+
76
+        // 初始化二维码
77
+        const qrcode = new QRCode(qrcodeRef.current, {
78
+          width: 160,
79
+          height: 160, // 高度
80
+          text: initialState.report_url + '?org=' + id
81
+        })
82
+        const canvas = qrcodeRef.current.querySelector('canvas')
83
+        // 创建下载链接
84
+        const aElement = document.createElement('a');
85
+        linkRef.current = aElement;
86
+        linkRef.current.download = res.orgName + '.png'
87
+        linkRef.current.href = canvas.toDataURL('image/jpg')
88
+      })
89
+      getOrgAdminDetail(id).then((res)=>{
90
+        setAdminFormData(res);
91
+        adminForm.setFieldsValue(res);
92
+      })
93
+    }
94
+  }, [id, form])
95
+
48 96
   return (
49 97
     <PageHeaderWrapper>
50
-      <ProCard bordered>
51
-        <div style={{ maxWidth: '600px' }}>
52
-          <Form
53
-            name="basic"
54
-            form={form}
55
-            labelCol={{ span: 8 }}
56
-            wrapperCol={{ span: 16 }}
57
-            initialValues={formData}
58
-            onFinish={onFinish}
59
-            onFinishFailed={onFinishFailed}
60
-            autoComplete="off"
61
-          >
62
-            <Form.Item
63
-              label="企业名称"
64
-              name="orgName"
65
-              rules={[{ required: true, message: '请填写企业名称!' }]}
98
+      <ProCard tabs={{ type: 'card' }} style={{ minHeight: '700px' }}>
99
+        <ProCard.TabPane key={1} tab="基本信息">
100
+          <div style={{ maxWidth: '600px' }}>
101
+            <Form
102
+              name="basic"
103
+              form={form}
104
+              labelCol={{ span: 8 }}
105
+              wrapperCol={{ span: 16 }}
106
+              initialValues={formData}
107
+              onFinish={onFinish}
108
+              onFinishFailed={onFinishFailed}
109
+              autoComplete="off"
66 110
             >
67
-              <Input />
68
-            </Form.Item>
69
-            <Form.Item
70
-              label="权重"
71
-              name="weight"
72
-              rules={[{ required: true, message: '请填写企业名称!' }]}
111
+              <Form.Item
112
+                label="企业名称"
113
+                name="orgName"
114
+                rules={[{ required: true, message: '请填写企业名称!' }]}
115
+              >
116
+                <Input />
117
+              </Form.Item>
118
+              <Form.Item
119
+                label="权重"
120
+                name="weight"
121
+                rules={[{ required: true, message: '请填写企业名称!' }]}
122
+              >
123
+                <Input type='number' min='0' />
124
+              </Form.Item>
125
+              <Form.Item label=' ' colon={false} wrapperCol={{ offset: 3 }}>
126
+                <div ref={qrcodeRef} />
127
+              </Form.Item>
128
+              <Form.Item label=' ' colon={false} >
129
+                <Button type="primary" htmlType="submit" loading={loading}>
130
+                  保存
131
+                </Button>
132
+                <Button style={{ margin: '0 32px' }} onClick={downloadQrcode} >
133
+                  下载二维码
134
+                </Button>
135
+                <Button onClick={()=>{history.go(-1);}} >返回</Button>
136
+              </Form.Item>
137
+            </Form>
138
+          </div>
139
+        </ProCard.TabPane>
140
+        <ProCard.TabPane disabled={!id} key={2} tab='管理员'>
141
+          <div style={{ maxWidth: '600px' }}>
142
+            <Form
143
+              form={adminForm}
144
+              labelCol={{ span: 8 }}
145
+              wrapperCol={{ span: 16 }}
146
+              initialValues={adminFormData}
147
+              onFinish={onAdminFinish}
73 148
             >
74
-              <Input type='number' min='0' />
75
-            </Form.Item>
76
-            <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
77
-              <Button type="primary" htmlType="submit" loading={loading}>
78
-                保存
79
-              </Button>
80
-            </Form.Item>
81
-          </Form>
82
-        </div>
149
+              <Form.Item
150
+                label="姓名"
151
+                name="userName"
152
+                rules={[{ required: true, message: '请填写管理员姓名!' }]}
153
+              >
154
+                <Input />
155
+              </Form.Item>
156
+              <Form.Item
157
+                label="手机号"
158
+                name="phone"
159
+                rules={[{ required: true, message: '请填写手机号!' }]}
160
+              >
161
+                <Input maxLength={11}/>
162
+              </Form.Item>
163
+              <Form.Item
164
+                label="登录账号"
165
+                name="loginName"
166
+                rules={[{ required: true, message: '请填写登录账号!' }]}
167
+              >
168
+                <Input />
169
+              </Form.Item>
170
+              <Form.Item
171
+                label="密码"
172
+                name="password"
173
+                rules={[{ required: true, message: '请填写密码!' }]}
174
+              >
175
+                <Input />
176
+              </Form.Item>
177
+              <Form.Item label=' ' colon={false} >
178
+                <Button type="primary" htmlType="submit" loading={adminLoading}>
179
+                  保存
180
+                </Button>
181
+                <Button style={{marginLeft:'32px'}} onClick={()=>{history.go(-1);}}>返回</Button>
182
+              </Form.Item>
183
+            </Form>
184
+          </div>
185
+
186
+        </ProCard.TabPane>
83 187
       </ProCard>
84 188
     </PageHeaderWrapper>
85 189
   )

+ 7
- 0
src/services/org.js View File

@@ -11,3 +11,10 @@ export const saveOrg = (data) => request('/org', { data, method: 'post' });
11 11
 
12 12
 // 更新信息
13 13
 export const updateOrg = (id, data) => request(`/org/${id}`, { data, method: 'put' });
14
+
15
+
16
+// 新增管理员
17
+export const saveOrgUser = (id,data) => request(`/org/${id}/user`, { data, method: 'post' });
18
+
19
+// 获取企业管理员详情
20
+export const getOrgAdminDetail = (id) => request(`/org/${id}/admin`);