index.js 8.21 KB
Newer Older
zhanghaozhe committed
1 2
import React, { PureComponent } from 'react';
import './coupons.scss'
zhanghaozhe committed
3

zhanghaozhe committed
4
import RedeemBar from "./RedeemBar";
zhanghaozhe committed
5
import Coupon from './Coupon'
zhanghaozhe committed
6
import { http, api, getParam } from '@/utils'
zhanghaozhe committed
7 8
import { WithFullSize } from '@/HOCs'
import { Toast } from 'antd-mobile'
zhanghaozhe committed
9 10
import { isEmpty } from 'lodash'
import { HeaderBar } from "@/common";
zhanghaozhe committed
11 12


zhanghaozhe committed
13
class UseCoupon extends PureComponent {
zhanghaozhe committed
14

zhanghaozhe committed
15
    state = {
zhanghaozhe committed
16
        selectedCouponId: 0,
zhanghaozhe committed
17
        redeemCode: '',
zhanghaozhe committed
18 19 20
        couponList: [],
        valid_coupons: [],
        invalid_coupons: [],
zhanghaozhe committed
21 22
        courseId: getParam('id'),
        showUseButton: false
zhanghaozhe committed
23 24
    }

zhanghaozhe committed
25
    componentDidMount() {
zhanghaozhe committed
26

zhanghaozhe committed
27
        const {history, location} = this.props
zhanghaozhe committed
28

zhanghaozhe committed
29
        const {state} = this.props.location
zhanghaozhe committed
30

zhanghaozhe committed
31

zhanghaozhe committed
32
        if (state && state.from) {
zhanghaozhe committed
33 34 35 36 37 38
            if (state.from === '/my') {
                this.getMyCoupons();
                this.setState({
                    showUseButton: true
                })
            } else {
zhanghaozhe committed
39 40 41
                if (!this.state.courseId) {
                    location.state && location.state.from ? history.replace(location.state.from) : history.goBack()
                }
zhanghaozhe committed
42
                this.getAllCoupons();
zhanghaozhe committed
43 44
            }
        }
zhanghaozhe committed
45 46
    }

zhanghaozhe committed
47 48 49
    handleChange = e => {
        let value = e ? e.target.value : ''
        this.setState({redeemCode: value})
zhanghaozhe committed
50 51
    }

zhanghaozhe committed
52
    exchange = () => {
zhanghaozhe committed
53
        if (this.state.redeemCode !== '') {
zhanghaozhe committed
54
            http.post(`${api.home}/m/coupon/exchange`, {
zhanghaozhe committed
55
                code: this.state.redeemCode
zhanghaozhe committed
56 57 58 59
            })
                .then(res => {
                    const data = res.data
                    if (data.code === 200) {
zhanghaozhe committed
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

                        const coupon = data.data

                        if (this.state.showUseButton) {
                            this.setState({
                                couponList: [...state.couponList, coupon],
                                redeemCode: ''
                            })
                        } else {

                            const coupon = data.data

                            if (coupon['ctype'] == 2
                                && coupon['limit_course'] != this.state.courseId) {
                                this.setState({
                                    invalid_coupons: [...this.state.invalid_coupons, coupon],
                                    showUseButton: null
                                });
                            } else {
                                this.setState({
                                    valid_coupons: [...this.state.valid_coupons, coupon],
                                    redeemCode: ''
                                })
                            }


                        }

zhanghaozhe committed
88 89
                        Toast.info('兑换成功')
                    } else {
zhanghaozhe committed
90
                        Toast.info(data.msg)
zhanghaozhe committed
91 92 93 94 95 96 97
                    }
                })
        } else {
            Toast.info('请输入兑换码')
        }
    }

zhanghaozhe committed
98
    getMyCoupons = () => {
zhanghaozhe committed
99 100 101 102 103
        http.get(`${api.home}/m/coupon/all`)
            .then(res => {
                const data = res.data
                if (data.code === 200) {
                    this.setState({
zhanghaozhe committed
104
                        couponList: isEmpty(data.data) ? [] : data.data
zhanghaozhe committed
105 106
                    })
                } else {
zhanghaozhe committed
107
                    Toast.info(data.msg)
zhanghaozhe committed
108 109 110 111 112
                }
            })
    }

    getAllCoupons = () => {
zhanghaozhe committed
113
        http.post(`${api.home}/m/coupon/select`, {course_id: this.state.courseId})
zhanghaozhe committed
114 115 116 117
            .then(res => {
                const data = res.data
                if (data.code === 200) {

zhanghaozhe committed
118 119
                    const inuse_coupon = data.data['inuse_coupon'];

zhanghaozhe committed
120

zhanghaozhe committed
121
                    this.setState({
zhanghaozhe committed
122 123 124
                        valid_coupons: inuse_coupon
                            ? [...inuse_coupon, ...data.data.valid_coupons]
                            : data.data.valid_coupons,
zhanghaozhe committed
125
                        invalid_coupons: data.data.invalid_coupons,
zhanghaozhe committed
126
                        selectedCouponId: inuse_coupon.length ? inuse_coupon[0].id : 0
zhanghaozhe committed
127
                    })
zhanghaozhe committed
128

zhanghaozhe committed
129
                } else {
zhanghaozhe committed
130
                    Toast.info(data.msg)
zhanghaozhe committed
131 132
                }
            })
zhanghaozhe committed
133 134
    }

zhanghaozhe committed
135 136 137 138
    useCoupon = val => {
        const {history} = this.props
        const coupon = this.state.couponList.find(item => item.id === val)

zhanghaozhe committed
139
        if (val) {
zhanghaozhe committed
140
            if (this.state.showUseButton) {
zhanghaozhe committed
141

zhanghaozhe committed
142 143 144 145 146
                if (coupon['ctype'] === 1) {
                    history.push(`/classify`)
                } else {
                    history.push(`/detail?id=${coupon['limit_course']}`)
                }
zhanghaozhe committed
147

zhanghaozhe committed
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
            } else {

                const {courseId, selectedCouponId} = this.state

                if (selectedCouponId === val) {


                    http.post(`${api.home}/m/coupon/cancel`, {
                        course_id: courseId
                    }).then(res => {
                        const data = res.data
                        if (data.code === 200) {


                            this.setState({
                                selectedCouponId: 0
                            })


                        } else {
                            Toast.info(data.msg)
                        }
                    })


                } else {
                    http.post(`${api.home}/m/coupon/use`, {
                        course_id: this.state.courseId,
                        coupon_id: val
                    })
                        .then(res => {
                            const data = res && res.data
                            if (data.code === 200) {

                                this.setState({selectedCouponId: val})
                                this.props.history.goBack()


                            } else {
                                Toast.info(data.msg)
                            }
                        })

                }


            }

        } else {
            Toast.info('未知错误')
            location.reload()
zhanghaozhe committed
199
        }
zhanghaozhe committed
200

zhanghaozhe committed
201 202
    }

zhanghaozhe committed
203
    render() {
zhanghaozhe committed
204
        const {state} = this.props.location
zhanghaozhe committed
205
        const {showUseButton, selectedCouponId} = this.state
zhanghaozhe committed
206 207
        return (
            <div className='use-coupon'>
zhanghaozhe committed
208 209 210 211
                <HeaderBar title='优惠券' arrow={true}/>
                <RedeemBar onChange={this.handleChange}
                           exchange={this.exchange}
                           redeemCode={this.state.redeemCode}/>
zhanghaozhe committed
212 213
                <div className="coupons-area">
                    <Content
zhanghaozhe committed
214 215 216 217 218 219 220
                        coupons={
                            state
                            && state.from
                            && state.from === '/my'
                                ? this.state.couponList
                                : this.state.valid_coupons
                        }
zhanghaozhe committed
221 222
                        showUseButton={showUseButton}
                        selectedCouponId={selectedCouponId}
zhanghaozhe committed
223
                        select={this.select}
zhanghaozhe committed
224
                        useCoupon={this.useCoupon}
zhanghaozhe committed
225 226
                    />
                    {
zhanghaozhe committed
227
                        this.state.invalid_coupons.length > 0 &&
zhanghaozhe committed
228 229 230 231
                        (
                            <>
                                <div className='invalid-title'>- 不可使用的优惠券 -</div>
                                <Content
zhanghaozhe committed
232
                                    coupons={this.state.invalid_coupons}
zhanghaozhe committed
233
                                    selectedCouponId={selectedCouponId}
zhanghaozhe committed
234 235 236 237 238 239 240 241 242 243 244 245 246
                                    select={this.select}
                                    purpose={'use'}
                                    invalid={'invalid'}
                                />
                            </>
                        )
                    }
                </div>
            </div>
        );
    }
}

zhanghaozhe committed
247
function Content({coupons, ...rest}) {
zhanghaozhe committed
248 249 250 251 252 253 254 255 256 257
    if (coupons.length === 0) {
        return (
            <div className='empty'>
                <p>暂无可使用的优惠券</p>
            </div>
        )
    }
    return (
        <ul>
            {
zhanghaozhe committed
258
                coupons.map(item => {
zhanghaozhe committed
259 260
                    return (
                        <Coupon
zhanghaozhe committed
261
                            key={item.id}
zhanghaozhe committed
262
                            {...item}
zhanghaozhe committed
263
                            id={item.id}
zhanghaozhe committed
264
                            {...rest}
zhanghaozhe committed
265
                        />
zhanghaozhe committed
266 267 268 269 270 271 272
                    )
                })
            }
        </ul>
    )
}

zhanghaozhe committed
273
export default WithFullSize(UseCoupon);