index.js 8.25 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
            })
                .then(res => {
                    const data = res.data
xuzhenghua committed
59
                    if (data.code === 200) {
zhanghaozhe committed
60 61 62 63 64

                        const coupon = data.data

                        if (this.state.showUseButton) {
                            this.setState({
zhanghaozhe committed
65
                                couponList: [...this.state.couponList, coupon],
zhanghaozhe committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
                                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
86
                        Toast.info('兑换成功')
xuzhenghua committed
87
                        this.getMyCoupons()
zhanghaozhe committed
88
                    } else {
zhanghaozhe committed
89
                        Toast.info(data.msg)
zhanghaozhe committed
90 91 92 93 94 95 96
                    }
                })
        } else {
            Toast.info('请输入兑换码')
        }
    }

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

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

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

zhanghaozhe committed
119

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

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

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

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

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

zhanghaozhe committed
147 148 149 150 151 152 153
            } else {

                const {courseId, selectedCouponId} = this.state

                if (selectedCouponId === val) {


zhanghaozhe committed
154
                    http.post(`${API.home}/m/coupon/cancel`, {
zhanghaozhe committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
                        course_id: courseId
                    }).then(res => {
                        const data = res.data
                        if (data.code === 200) {


                            this.setState({
                                selectedCouponId: 0
                            })


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


                } else {
zhanghaozhe committed
173
                    http.post(`${API.home}/m/coupon/use`, {
zhanghaozhe committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
                        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
198
        }
zhanghaozhe committed
199

zhanghaozhe committed
200 201
    }

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

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

zhanghaozhe committed
272
export default WithFullSize(UseCoupon);