|
@@ -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
|