king
2024-06-28 c8804ceb1fe2dea76f9949c5ea04423876ee2c81
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import React, {Component} from 'react'
import moment from 'moment'
import md5 from 'md5'
import QRCode from 'qrcode.react'
import { notification, Tabs, Modal } from 'antd'
 
import Api from '@/api'
import Utils from '@/utils/utils.js'
import WeiXinPay from '@/assets/img/weixinpay.jpg'
import WeiXinPayLogo from '@/assets/img/weixinpaylogo.jpg'
import WeiXinScan from '@/assets/img/weixinscan.png'
import './index.scss'
 
const { TabPane } = Tabs
 
class Pay extends Component {
  state = {
    orderId: '',
    orderNo: '',
    logo: window.GLOB.doclogo || '',
    name: sessionStorage.getItem('Full_Name') || '',
    copyRight: window.GLOB.copyRight || '',
    icp: window.GLOB.ICP || '',
    total: '',
    unit: '',
    qrcode: '',
    second: 60,
    overdue: false,
    overdone: false,
    appId: ''
  }
 
  UNSAFE_componentWillMount () {
    let param = JSON.parse(window.decodeURIComponent(window.atob(this.props.match.params.param)))
 
    let _appId = param.appId || window.GLOB.WXAppID || window.GLOB.WXminiAppID || ''
 
    this.setState({
      orderId: param.ID,
      appId: _appId
    })
 
    if (param.ID && _appId) {
      this.getOrder(param.ID, _appId)
    } else if (!param.ID) {
      notification.warning({
        top: 92,
        message: '未获取到订单ID!',
        duration: 5
      })
    } else if (!_appId) {
      notification.warning({
        top: 92,
        message: '未获取到应用ID!',
        duration: 5
      })
    }
  }
 
  getOrder = (Id, appId) => {
    let param = {
      func: 's_get_weixin_pay_native',
      ID: Id
    }
 
    param.LTextOut = md5(param.ID + window.GLOB.appkey)
    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
    param.secretkey = Utils.encrypt(param.LTextOut, param.timestamp)
 
    Api.genericInterface(param).then(res => {
      if (res.status) {
        let _total = res.amount ? parseFloat(res.amount.total) : ''
 
        if (isNaN(_total)) {
          _total = ''
        } else if (_total !== '') {
          _total = _total.toFixed(2)
        }
 
        this.setState({
          orderNo: res.out_trade_no,
          total: _total,
          unit: res.amount && res.amount.currency === 'CNY' ? '元' : '元',
        })
 
        // 当前订单支付成功
        if (res.pay_status) {
          this.setState({
            overdone: true
          })
          Modal.success({
            title: '支付成功!',
            content: '请刷新订单页面,查看订单状态。',
            okText: '知道了',
            onOk() {
              window.close()
            }
          })
          return
        }
 
        if (!res.out_trade_no) {
          notification.warning({
            top: 10,
            message: '未获取到订单号!',
            duration: 5
          })
          return
        }
 
        Api.getWxNativePay({ 'out_biz_no': res.out_trade_no, app_id: appId }).then(result => {
          if (result.status && result.code_url) {
            this.setState({
              qrcode: result.code_url
            })
            setTimeout(this.resetSecond, 1000)
          } else {
            this.setState({
              overdue: true
            })
            notification.warning({
              top: 10,
              message: result.message || '未获取到支付码!',
              duration: 5
            })
          }
        })
      } else {
        this.setState({
          overdue: true
        })
        notification.warning({
          top: 10,
          message: res.message,
          duration: 5
        })
      }
    })
  }
 
  resetSecond = () => {
    const { second, orderNo, overdone } = this.state
 
    if (overdone) return
 
    if (second >= 1) {
      this.setState({
        second: second - 1
      })
      setTimeout(this.resetSecond, 1000)
 
      if ((second - 1) % 5 === 0) {
        let param = {
          func: 's_get_weixin_pay_native_status',
          out_trade_no: orderNo
        }
 
        param.LTextOut = md5(orderNo + window.GLOB.appkey)
        param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
        param.secretkey = Utils.encrypt(param.LTextOut, param.timestamp)
 
        Api.genericInterface(param).then(res => {
          if (res.pay_status) {
            this.setState({
              overdone: true
            })
            Modal.success({
              title: '支付成功!',
              content: '请刷新订单页面,查看订单状态。',
              okText: '知道了',
              onOk() {
                window.close()
              }
            })
          }
        })
      }
    } else {
      this.setState({
        overdue: true
      })
    }
  }
 
  resetQrcode = () => {
    const { orderNo, appId } = this.state
 
    if (!orderNo) {
      notification.warning({
        top: 10,
        message: '未获取到订单号!',
        duration: 5
      })
      return
    } else if (!appId) {
      notification.warning({
        top: 10,
        message: '未获取到应用ID!',
        duration: 5
      })
      return
    }
 
    Api.getWxNativePay({ 'out_biz_no': orderNo, app_id: appId }).then(result => {
      if (result.status && result.code_url) {
        this.setState({
          qrcode: result.code_url,
          overdue: false,
          second: 60
        })
        setTimeout(this.resetSecond, 1000)
      } else {
        notification.warning({
          top: 10,
          message: result.message || '未获取到支付码!',
          duration: 5
        })
      }
    })
  }
 
  onChange = () => {
 
  }
 
  render () {
    const { logo, name, orderNo, icp, copyRight, total, unit, qrcode, second, overdue, overdone } = this.state
 
    return (
      <div className="mk-pay-container">
        <div className="mk-pay-box">
          <header>
            <img className="plat-logo" src={logo} alt=""/>
            <div className="user-name">{name}</div>
          </header>
          <div className="pay-tip">
            <span>请您尽快支付,以便订单快速处理!{orderNo ? `订单号:${orderNo}` : ''}</span>
            {total !== '' ? <span className="pay-total">应付金额<span>{total}</span>{unit}</span> : null}
          </div>
          <div className="mk-pay-content">
            <Tabs defaultActiveKey="weixin">
              <TabPane tab={<img src={WeiXinPayLogo} alt=""/>} key="weixin">
                {!overdone ? <div className="qrcode-box">
                  {!overdue ? <p>距离二维码过期还剩<span>{second}</span>秒,过期后请刷新页面重新获取二维码。</p> : null}
                  {overdue ? <p className="overdue">二维码已过期,<span onClick={this.resetQrcode}>刷新</span>页面重新获取二维码。</p> : null}
                  <QRCode
                    value={qrcode}
                    size={250}
                    fgColor="#000000"
                    // imageSettings={{
                    //   src: '',
                    //   height: 60,
                    //   width: 60,
                    //   excavate: true
                    // }}
                  />
                  {overdue ? <div className="overdue-mask"><p onClick={this.resetQrcode}>获取失败 点击重新获取二维码</p></div> : null}
                  <div className="qrcode-tip">
                    <img src={WeiXinScan} alt=""/>
                    <div>
                      <p>请使用微信扫一扫</p>
                      <p>扫描二维码支付</p>
                    </div>
                  </div>
                </div> : null}
                {overdone ? <div className="overdone">
                  <p>支付成功!</p>
                  <p>请刷新订单页面,查看订单状态。</p>
                </div> : null}
                {!overdone ? <img className="weixin-scan" src={WeiXinPay} alt=""/> : null}
              </TabPane>
            </Tabs>
            {/* <div className="mk-pay-type">
              <span className="tip">支付方式:</span>
              <Radio.Group onChange={this.onChange} value="weixin">
                <Radio value="weixin">微信支付</Radio>
              </Radio.Group>
            </div> */}
            
          </div>
          <div className="mk-pay-bottom">
            {copyRight ? <p dangerouslySetInnerHTML={{ __html: copyRight.replace(/\s/ig, '&nbsp;') }}></p> : null}
            {icp ? <p dangerouslySetInnerHTML={{ __html: icp.replace(/\s/ig, '&nbsp;') }}></p> : null}
          </div>
        </div>
      </div>
    )
  }
}
 
export default Pay