index.js 10.3 KB
Newer Older
xuzhenghua committed
1 2
import React, {Component} from 'react'
import './index.scss'
xuzhenghua committed
3
import {HeaderBar} from '../../common'
zhanghaozhe committed
4 5
import OrderList from 'src/common/OrderList'
import {http} from "src/utils"
xuzhenghua committed
6
import {Link} from 'react-router-dom'
xuzhenghua committed
7
import {Modal, Toast} from 'antd-mobile'
zhanghaozhe committed
8
import Loading from 'src/common/Loading'
xuzhenghua committed
9 10
import InfiniteScroll from 'react-infinite-scroller'
import {debounce} from 'lodash'
11 12
import {getCourses} from './../detail/actions';
import {connect} from 'react-redux';
xuzhenghua committed
13

xuzhenghua committed
14
const alert = Modal.alert
xuzhenghua committed
15

16
@connect()
xuzhenghua committed
17
class MyOrders extends Component {
xuzhenghua committed
18
    page = 1
xuzhenghua committed
19

xuzhenghua committed
20 21 22
    constructor(props) {
        super(props)
        this.state = {
xuzhenghua committed
23
            data: [],
xuzhenghua committed
24 25 26
            pageNum: 1,
            isLoading: true,
            total: 0,
xuzhenghua committed
27 28 29 30
        }
    }

    componentDidMount() {
xuzhenghua committed
31
        this.getList(this.page)
xuzhenghua committed
32 33
    }

xuzhenghua committed
34
    getMore = debounce(() => {
xuzhenghua committed
35
        if (this.state.data.length % 10 === 0) {
xuzhenghua committed
36 37 38 39
            this.getList(++this.page)
        }
    }, 200)

xuzhenghua committed
40 41
    // 获取订单
    getList = () => {
xuzhenghua committed
42
        http.get(`${API.home}/m/my/orders/${this.page}/10`,).then((res) => {
xuzhenghua committed
43 44
            if (res.data.code === 200) {
                this.setState({
xuzhenghua committed
45
                    data: this.state.data.concat(res.data.data),
xuzhenghua committed
46
                    isLoading: false
xuzhenghua committed
47 48 49 50 51
                })
            } else {
                Toast.info(res.data.msg, 2);
            }
        })
xuzhenghua committed
52

xuzhenghua committed
53 54 55 56 57 58 59 60 61 62 63 64
    }

    // 取消订单
    cancel = (oid) => {
        alert('确认取消订单?', '订单取消后,优惠券和抵扣的余额会返回到您的账户。', [
            {text: '取消', onPress: () => console.log('cancel')},
            {
                text: '确认',
                onPress: () => {
                    let data = {
                        order_id: oid
                    }
zhanghaozhe committed
65
                    http.post(`${API.home}/m/cancel_order`, data).then((res) => {
xuzhenghua committed
66
                        if (res.data.code === 200) {
xuzhenghua committed
67
                            location.reload();
xuzhenghua committed
68 69 70 71 72 73 74 75 76
                        } else {
                            Toast.info(res.data.msg, 2);
                        }
                    })
                }
            }
        ])
    }

77
    toCourseDetail = (id) => {
xuzhenghua committed
78
        const {dispatch, history} = this.props;
xuzhenghua committed
79
        // dispatch(getCourses(id, () => {
xuzhenghua committed
80 81
        history.push(`/detail?id=${id}`);
        return false;
xuzhenghua committed
82
        // }));
83 84
    }

xuzhenghua committed
85 86 87 88

    render() {
        return (
            <div className='myorders-box'>
xuzhenghua committed
89
                <HeaderBar title='我的订单' arrow={true} cart={false}></HeaderBar>
xuzhenghua committed
90 91
                <Loading isLoading={this.state.isLoading}>
                    {
xuzhenghua committed
92
                        this.state.data && JSON.stringify(this.state.data) !== '[{}]' ?
xuzhenghua committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
                            <InfiniteScroll
                                pageStart={0}
                                hasMore={true}
                                loadMore={this.getMore.bind(this)}
                                useWindow={true}
                            >
                                {
                                    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">
xuzhenghua committed
108 109 110
                                                                <p className='order-title text-overflow-one'
                                                                   onClick={() => this.toCourseDetail(item.course_id)}>
                                                                    {item.course_title}
xuzhenghua committed
111 112 113 114
                                                                </p>
                                                                <p className='order-content text-overflow-2'>{item.description}</p>
                                                                <p className='order-des'>
                                                                <span
xuzhenghua committed
115
                                                                    className='order-newprice'>¥{item.amount}</span>
xuzhenghua committed
116 117 118 119 120 121 122
                                                                    <span
                                                                        className='order-price'>¥{item.price0}</span>
                                                                </p>
                                                            </div>
                                                        )
                                                        return (
                                                            <div className="order-wrap" key={index}>
xuzhenghua committed
123 124
                                                                <OrderList
                                                                    info={Info}
125 126 127 128 129
                                                                    src={item.image_name}
                                                                    isSign={item.is_aist}
                                                                    id={item.course_id}
                                                                    toDetail={this.toCourseDetail}
                                                                />
xuzhenghua committed
130 131 132 133 134

                                                                {
                                                                    item.course_expire && item.course_expire!='' &&
                                                                    <span className='course-expire'>{item.course_expire}</span>
                                                                }
xuzhenghua committed
135 136 137 138
                                                            </div>
                                                        )
                                                    })
                                                }
xuzhenghua committed
139

xuzhenghua committed
140
                                                <PayInfo item={item} cancel={this.cancel}/>
xuzhenghua committed
141 142

                                                {
143
                                                    item.type == 5 && item.is_buy == 0 && item.is_overdue == 0 &&
xuzhenghua committed
144 145 146 147 148
                                                    <div className={'expand-pay-wk'}>
                                                        <span className={'expand-pay-time'}>{item.final_end_time}结束付尾款</span>
                                                        <span>还需支付尾款</span>
                                                    </div>
                                                }
149 150 151 152 153 154
                                                {
                                                    item.type == 5 && item.is_buy == 0 && item.is_overdue == 1 &&
                                                    <div className={'expand-pay-wk'}>
                                                        <span>支付尾款时间已过</span>
                                                    </div>
                                                }
xuzhenghua committed
155 156 157 158
                                            </div>
                                        )
                                    })
                                }  </InfiniteScroll> : <div className="cart-tip">
xuzhenghua committed
159 160 161 162
                                <p className='cart-mess'>您还没有订单哦,快去逛逛吧~</p>
                                <Link to='/classify'>去逛逛</Link>
                            </div>
                    }
xuzhenghua committed
163

xuzhenghua committed
164
                </Loading>
xuzhenghua committed
165 166 167 168 169 170 171 172 173 174
            </div>
        )
    }
}


function OrderInfo(props) {
    let btn
    if (props.item.pay_time === '0' && props.item.member_num === 0) {
        btn = <span className='oid-status'>等待支付</span>
xuzhenghua committed
175
    } else if (props.item.pdd_info && (props.item.member_num === props.item.pdd_info.length) && props.item.member_num !== 0) {
xuzhenghua committed
176 177 178 179 180 181 182
        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>
xuzhenghua committed
183 184 185 186
            {
                props.item.type == 4 &&  props.item.course.length == 3 &&
                <span>付费试学</span>
            }
xuzhenghua committed
187 188 189 190 191 192
            {btn}
        </div>
    )
}

function PayInfo(props) {
xuzhenghua committed
193 194 195 196 197 198 199 200 201
    // type 0普通订单 1团购 2小团 3砍价 4单集购买 5定金课定金 6定金课尾款
    let type = ''

    if (props.item.type == 5) {
        type = '(定金)'
    }
    if (props.item.type == 6) {
        type = '(尾款)'
    }
xuzhenghua committed
202 203 204 205
    return (
        <div className='order-btm'>
            <div className='price-info'>
                <p>
xuzhenghua committed
206
                    <span className='payable'>应付{type}</span>
xuzhenghua committed
207 208 209 210
                    <span className='price'>¥{props.item.pay_amount}</span>
                </p>
                <p>
                    <span className='payable'>已优惠:</span>
xuzhenghua committed
211
                    <span className='price'>¥{props.item.discount}</span>
xuzhenghua committed
212 213 214 215 216 217 218
                </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
219
                    <Link to={`/payorder?oid=${props.item.oid}`}>去支付</Link>
xuzhenghua committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                </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