index.js 6.75 KB
Newer Older
xuzhenghua committed
1 2
import React, {Component} from 'react'
import './index.scss'
xuzhenghua committed
3
import {HeaderBar} from '../../common'
xuzhenghua committed
4 5
import OrderList from '@/common/OrderList'
import {http, api} from "@/utils"
xuzhenghua committed
6
import {Link} from 'react-router-dom'
xuzhenghua committed
7
import {Modal, Toast} from 'antd-mobile'
xuzhenghua committed
8
import Loading from '@/common/Loading'
xuzhenghua committed
9

xuzhenghua committed
10
const alert = Modal.alert
xuzhenghua committed
11 12 13 14 15 16


class MyOrders extends Component {
    constructor(props) {
        super(props)
        this.state = {
xuzhenghua committed
17 18
            data: [],
            isLoading: true
xuzhenghua committed
19 20 21 22 23 24 25 26 27
        }
    }

    componentDidMount() {
        this.getList()
    }

    // 获取订单
    getList = () => {
zhanghaozhe committed
28
        http.get(`${API.home}/m/my/orders/1/1`,).then((res) => {
xuzhenghua committed
29 30
            if (res.data.code === 200) {
                this.setState({
xuzhenghua committed
31 32
                    data: res.data.data,
                    isLoading: false
xuzhenghua committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
                })
            } else {
                Toast.info(res.data.msg, 2);
            }
        })
    }

    // 取消订单
    cancel = (oid) => {
        alert('确认取消订单?', '订单取消后,优惠券和抵扣的余额会返回到您的账户。', [
            {text: '取消', onPress: () => console.log('cancel')},
            {
                text: '确认',
                onPress: () => {
                    let data = {
                        order_id: oid
                    }
zhanghaozhe committed
50
                    http.post(`${API.home}/m/cancel_order`, data).then((res) => {
xuzhenghua committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
                        if (res.data.code === 200) {
                            this.getList()
                        } else {
                            Toast.info(res.data.msg, 2);
                        }
                    })
                }
            }
        ])
    }


    render() {

        return (
            <div className='myorders-box'>
xuzhenghua committed
67
                <HeaderBar title='我的订单' arrow={true} cart={false}></HeaderBar>
xuzhenghua committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
                <Loading isLoading={this.state.isLoading}>
                    {
                        this.state.data && this.state.data.length > 0 ?
                            this.state.data.map((item, index) => {
                                return (
                                    <div className="order-body" key={index}>
                                        <OrderInfo item={item}/>
                                        {
                                            item.course && item.course.length > 0 && item.course.map((item, index) => {
                                                const Info = (
                                                    <div className="order-info">
                                                        <p className='order-title text-overflow-one'>
                                                            <Link
                                                                to={`/detail?id=${item.course_id}`}>{item.course_title}</Link>
                                                        </p>
                                                        <p className='order-content text-overflow-2'>{item.description}</p>
                                                        <p className='order-des'>
                                                            <span className='order-newprice'>¥{item.pay_amount}</span>
                                                            <span className='order-price'>¥{item.price0}</span>
                                                        </p>
                                                    </div>
                                                )
                                                return (
                                                    <div className="order-wrap" key={index}>
FE committed
92
                                                        <OrderList info={Info} src={item.image_name} isSign={item.is_aist}
xuzhenghua committed
93 94 95 96 97
                                                                   id={item.course_id}></OrderList>
                                                    </div>
                                                )
                                            })
                                        }
xuzhenghua committed
98

xuzhenghua committed
99 100 101 102 103 104 105 106 107
                                        <PayInfo item={item} cancel={this.cancel}/>
                                    </div>
                                )
                            }) : <div className="cart-tip">
                                <p className='cart-mess'>您还没有订单哦,快去逛逛吧~</p>
                                <Link to='/classify'>去逛逛</Link>
                            </div>
                    }
                </Loading>
xuzhenghua committed
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
            </div>
        )
    }
}


function OrderInfo(props) {
    let btn
    if (props.item.pay_time === '0' && props.item.member_num === 0) {
        btn = <span className='oid-status'>等待支付</span>
    } else if ((props.item.member_num === props.item.pdd_info.length) && props.item.member_num !== 0) {
        btn = <span className='oid-success'>拼团成功</span>
    } else {
        btn = <span className='oid-success'>支付成功</span>
    }
    return (
        <div className='order-head'>
            <span className='oid-num'>订单号:{props.item.oid}</span>
            {btn}
        </div>
    )
}

function PayInfo(props) {
    return (
        <div className='order-btm'>
            <div className='price-info'>
                <p>
                    <span className='payable'>应付:</span>
                    <span className='price'>¥{props.item.pay_amount}</span>
                </p>
                <p>
                    <span className='payable'>已优惠:</span>
                    <span className='price'>¥{props.item.coupon_amount}</span>
                </p>
            </div>

            {
                props.item.pay_time === '0' && props.item.member_num === 0 &&
                <div className='btm-right'>
                    <button className='cancel' onClick={event => props.cancel(props.item.oid)}>取消订单</button>
xuzhenghua committed
149
                    <Link to={`/payorder?oid=${props.item.oid}`}>去支付</Link>
xuzhenghua committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
                </div>
            }

            {
                props.item.member_num !== 0 &&
                <div className='btm-right'>
                    <div className="group">
                        {
                            props.item.pdd_info && props.item.pdd_info.length > 0 && props.item.pdd_info.map((item, index) => {
                                return (
                                    <img
                                        src={item.user_avatar === '' ? 'https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/tinypng-spreadtrain8/ellipsis.png' : item.user_avatar}
                                        key={index} alt=""/>
                                )
                            })
                        }
                    </div>
                </div>
            }
        </div>
    )
}

export default MyOrders