import React, { Component } from 'react' import { getCoinRechargeItemsGuest, addCoinRechargeOrderGuest, addPayOrderGuest } from '@/services/common' import styles from './Recharge.less' import iconGold from '@/assets/icon_gold.png' import iconDefaultAvatar from '@/assets/icon_default_avatar.png' import { notification, Modal } from 'antd' import Login from './Login' class Recharge extends Component { constructor (props) { super(props) this.state = { choosenItem: {}, visible: false, userInfo: {}, } } componentDidMount () { const userString = localStorage.userInfo const { no, amount, res } = this.props.location.query if (userString) { const json = JSON.parse(userString) this.setState({ userInfo: json }) } if (res === '1') { notification.success({ message: '充值成功', description:

订单号: {no}

支付金额: {amount}

, duration: 0 }) } this.getCoinRechargeItemsGuest() } // 获取充值项目 getCoinRechargeItemsGuest = async () => { const _state = {} try { const { code, data } = await getCoinRechargeItemsGuest() if (code === 'OK' && data) { const { list } = data const payAliPc = list && list.find(item => item.payWay === 204) _state.payAliPc = (payAliPc && payAliPc.items) || [] } } catch (e) { console.log(e) } this.setState(_state) } onChoose = (item) => () => { this.setState({ choosenItem: item }) } // 充值 onRecharge = async (e) => { e.preventDefault() const { choosenItem, userInfo } = this.state if (!choosenItem || !choosenItem.itemId) { notification.error({message: '请选择充值金额'}) return } if (!userInfo || !userInfo.userId) { notification.error({ message: '请先登录' }) return } if (this.state.uploading) { return } this.setState({ uploading: true }) // 创建金币充值订单 const { code, data } = await addCoinRechargeOrderGuest({ itemId: choosenItem.itemId, userId: userInfo.userId }) if (code === 'OK' && data) { const { orderId } = data this.addPayOrderGuest(orderId) } } addPayOrderGuest = async (orderId) => { const { code, data } = await addPayOrderGuest({ orderId }) if (code === 'OK' && data) { const { payParams } = data const node = document.createElement('div') node.innerHTML = payParams document.body.appendChild(node) if (document.forms[0]) { document.forms[0].submit(); } } this.setState({ uploading: false }) } // 登录 onLogin = () => { this.setState({ visible: true }) } onModalCancel = () => { this.setState({ visible: false }) } // 登录成功 onLoginOk = (data) => { localStorage.userInfo = JSON.stringify(data) this.setState({ visible: false, userInfo: data }) } // 退出登录 onLoginOut = () => { localStorage.removeItem('userInfo') this.setState({ userInfo: {} }) } render () { const { payAliPc, choosenItem, visible, userInfo, uploading, payParams } = this.state return (
{ payAliPc && payAliPc.map((item) => (
{item.itemCoin}
¥ {item.itemPrice}
)) }
充值
) } } export default Recharge