张延森 5 years ago
parent
commit
77a36361a2

+ 166
- 0
src/components/MiniQRCode/index.jsx View File

@@ -0,0 +1,166 @@
1
+import React, { PureComponent } from 'react'
2
+import PropTypes from 'prop-types'
3
+import classNames from 'classnames'
4
+import { Button, Modal, Tooltip } from 'antd'
5
+import { fetch, apis } from '../../utils/request'
6
+
7
+import Style from './style.less'
8
+
9
+const fetchQRCode = fetch(apis.image.qrcode)
10
+
11
+function encodeParam (o) {
12
+  return Object.keys(o).map((k) => {
13
+    const v = encodeURIComponent(o[k] || '')
14
+    return `${k}=${v}`
15
+  }).join('&')
16
+}
17
+
18
+class MiniQRCode extends PureComponent {
19
+  constructor(props) {
20
+    super(props)
21
+
22
+    this.state = {
23
+      previewVisible: false,
24
+      qrcode: undefined,
25
+      loading: false,
26
+    }
27
+  }
28
+
29
+  componentDidMount() {
30
+    if (this.props.targetId && this.props.value) {
31
+      const params = this.getQRCodeParams(this.props)
32
+
33
+      if (!params) return;
34
+
35
+      this.setState({
36
+        qrcode: {
37
+          img: this.props.value,
38
+          params,
39
+        }
40
+      })
41
+    }
42
+  }
43
+
44
+  componentDidUpdate(prevProps) {
45
+    const preParams = this.getQRCodeParams(prevProps)
46
+    const curParams = this.getQRCodeParams(this.props)
47
+
48
+    // 需要重新生成二维码
49
+    if (preParams !== curParams) {
50
+      this.createQRCode()
51
+      return
52
+    }
53
+
54
+    // 如果 value 有了更新
55
+    const { img } = this.state.qrcode || {}
56
+    if (prevProps.value !== this.props.value && this.props.value !== img) {
57
+      this.setState({
58
+        qrcode: {
59
+          img: this.props.value,
60
+          params: curParams,
61
+        }
62
+      })
63
+    }
64
+  }
65
+
66
+  getQRCodeParams = (props) => {
67
+    const { targetId, sceneParams = {}, page, params = {} } = props
68
+
69
+    if (!targetId || !page) {
70
+      return;
71
+    }
72
+
73
+    const sceneObj = {
74
+      id: targetId,
75
+      ...sceneParams,
76
+    }
77
+
78
+    return JSON.stringify({
79
+      scene: encodeParam(sceneObj),
80
+      page,
81
+      ...params,
82
+    })
83
+  }
84
+
85
+  createQRCode = () => {
86
+    const params = this.getQRCodeParams(this.props)
87
+
88
+    if (!params) {
89
+      return;
90
+    }
91
+
92
+    this.setState({ loading: true }, () => {
93
+      fetchQRCode({ data: { params } }).then((img) => {
94
+        this.setState({ qrcode: { img, params }, loading: false }, () => {
95
+          if (this.props.onChange) {
96
+            this.props.onChange(img)
97
+          }
98
+        })
99
+      })
100
+    })
101
+  }
102
+
103
+  handleThumbClick = () => {
104
+    this.setState({ previewVisible: true })
105
+  }
106
+
107
+  handleCancel = () => {
108
+    this.setState({ previewVisible: false })
109
+  }
110
+
111
+  handleBtnClick = () => {
112
+    this.createQRCode()
113
+  }
114
+
115
+  render() {
116
+    const { loading, qrcode = {}, previewVisible } = this.state
117
+    const { className, style, size } = this.props
118
+    const icon = !qrcode.img ? 'plus' : 'redo'
119
+    const btnText = !qrcode.img ? '生成' : '重新生成'
120
+    const clsList = classNames(Style.miniQRCode, className)
121
+    const styleUsed = style || {}
122
+    const thumbSize = size || 120
123
+    const thumbStyle = {
124
+      width: `${thumbSize}px`,
125
+      height: `${thumbSize}px`,
126
+    }
127
+    const btnSize = thumbSize > 96 ? 'default' : 'small'
128
+
129
+    return (
130
+      <div className={clsList} style={styleUsed}>
131
+        {
132
+          qrcode.img && (
133
+            <Tooltip title={qrcode.params}>
134
+              <div className={Style.thumb} onClick={this.handleThumbClick} style={thumbStyle}>
135
+                <img src={qrcode.img} alt=""/>
136
+              </div>
137
+            </Tooltip>
138
+          )
139
+        }
140
+        <div className={Style.body}>
141
+          <Button loading={loading} size={btnSize} icon={icon} onClick={this.handleBtnClick}>{btnText}</Button>
142
+        </div>
143
+        <Modal visible={previewVisible} footer={null} onCancel={this.handleCancel}>
144
+          <img alt="preview" style={{ width: '100%' }} src={qrcode.img} />
145
+        </Modal>
146
+      </div>
147
+    );
148
+  }
149
+}
150
+
151
+MiniQRCode.propTypes = {
152
+  value: PropTypes.string,
153
+  onChange: PropTypes.func,
154
+  targetId: PropTypes.oneOfType([
155
+    PropTypes.string,
156
+    PropTypes.number,
157
+  ]),
158
+  page: PropTypes.string,
159
+  sceneParams: PropTypes.object,
160
+  params: PropTypes.object,
161
+  className: PropTypes.string,
162
+  style: PropTypes.object,
163
+  size: PropTypes.number,
164
+}
165
+
166
+export default MiniQRCode

+ 20
- 0
src/components/MiniQRCode/style.less View File

@@ -0,0 +1,20 @@
1
+.miniQRCode {
2
+  background: transparent;
3
+  display: flex;
4
+  align-items: center;
5
+
6
+  .thumb {
7
+    flex: none;
8
+    padding: 8px;
9
+    margin-right: 12px;
10
+
11
+    img {
12
+      width: 100%;
13
+      height: 100%;
14
+    }
15
+  }
16
+
17
+  .body {
18
+    flex: auto;
19
+  }
20
+}

+ 4
- 2
src/pages/house/edit/components/base.jsx View File

@@ -9,6 +9,7 @@ import apis from '../../../../services/apis';
9 9
 import BuildSelect from '../../../../components/SelectButton/BuildSelect';
10 10
 import request from '../../../../utils/request';
11 11
 import SalesBatchHelpDoc from './SalesBatchHelpDoc';
12
+import MiniQRCode from '@/components/MiniQRCode';
12 13
 
13 14
 const { MonthPicker, RangePicker, WeekPicker } = DatePicker;
14 15
 const { TextArea } = Input;
@@ -117,9 +118,10 @@ const Base = props => {
117 118
     {
118 119
       label: '扫码查看房源列表',
119 120
       name: 'qrCode',
120
-      type: FieldTypes.ImageUploader,
121
+      // type: FieldTypes.ImageUploader,
121 122
       value: saleBatchData.qrCode,
122
-      props: {disabled: true },
123
+      // props: { disabled: true },
124
+      render: <MiniQRCode targetId={saleBatchData.salesBatchId} page="onlineSelling/pages/houseList/index" />
123 125
     },
124 126
     {
125 127
       label: '说明',

+ 5
- 0
src/services/apis.js View File

@@ -14,6 +14,11 @@ export default {
14 14
       method: 'POST',
15 15
       action: 'upload',
16 16
     },
17
+    qrcode: {
18
+      url: `${prefix}/qrcode`,
19
+      method: 'POST',
20
+      action: 'upload',
21
+    }
17 22
   },
18 23
   user: {
19 24
     updatePassword: {