index.js 11.2 KB
Newer Older
xuzhenghua committed
1
import React, {PureComponent} from 'react';
xuzhenghua committed
2 3 4 5
import './index.scss'

import RedeemBar from "../RedeemBar";
import Coupon from '../Coupon'
xuzhenghua committed
6 7 8 9 10
import {http, getParam} from '@/utils'
import {WithFullSize} from '@/HOCs'
import {Toast} from 'antd-mobile'
import {isEmpty} from 'lodash'
import {connect} from 'react-redux';
xuzhenghua committed
11 12 13 14 15 16 17 18 19 20 21

@connect()
class UseCoupon extends PureComponent {

    state = {
        selectedCouponId: 0,
        redeemCode: '',
        couponList: [],
        valid_coupons: [],
        invalid_coupons: [],
        courseId: getParam('id'),
xuzhenghua committed
22 23 24
        showUseButton: false,
        courseCouponExchange: false, // 课程券兑换弹窗
        courseCouponData: '' // 兑换课程信息
xuzhenghua committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    }

    componentDidMount() {
        const {history, location} = this.props

        const {state} = this.props.location
        if (state && state.from) {
            if (state.from === '/my') {
                this.getMyCoupons();
                this.setState({
                    showUseButton: true
                })
            } else {
                if (!this.state.courseId) {
                    location.state && location.state.from ? history.replace(location.state.from) : history.goBack()
                }
                this.getAllCoupons();
            }
        }
    }

    handleChange = e => {
        let value = e ? e.target.value : ''
        this.setState({redeemCode: value})
    }

xuzhenghua committed
51
    // 兑换
xuzhenghua committed
52
    exchange = () => {
zhanghaozhe committed
53
        const {location: {state = {}}} = this.props;
xuzhenghua committed
54 55 56 57 58 59 60 61 62 63
        if (this.state.redeemCode !== '') {
            http.post(`${API.home}/m/coupon/exchange`, {
                code: this.state.redeemCode
            })
                .then(res => {
                    const data = res.data
                    if (data.code === 200) {

                        const coupon = data.data

xuzhenghua committed
64
                        if (coupon['ctype'] == 2) {
xuzhenghua committed
65
                            this.setState({
xuzhenghua committed
66 67
                                courseCouponData: coupon,
                                courseCouponExchange: true,
xuzhenghua committed
68 69 70
                                redeemCode: ''
                            })
                        } else {
xuzhenghua committed
71
                            if (this.state.showUseButton) {
xuzhenghua committed
72
                                this.setState({
xuzhenghua committed
73 74 75
                                    couponList: [...this.state.couponList, coupon],
                                    redeemCode: ''
                                })
xuzhenghua committed
76
                            } else {
xuzhenghua committed
77 78

                                const coupon = data.data
xuzhenghua committed
79 80 81 82 83
                                this.setState({
                                    valid_coupons: [...this.state.valid_coupons, coupon],
                                    redeemCode: ''
                                })
                            }
xuzhenghua committed
84 85 86 87 88 89 90
                            Toast.info('兑换成功')
                            if (state.from === '/my') {
                                this.getMyCoupons()
                            }
                            if (state.from === '/order') {
                                this.getAllCoupons()
                            }
xuzhenghua committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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
                        }
                    } else {
                        Toast.info(data.msg)
                    }
                })
        } else {
            Toast.info('请输入兑换码')
        }
    }

    getMyCoupons = () => {
        http.get(`${API.home}/m/coupon/all`)
            .then(res => {
                const data = res.data
                if (data.code === 200) {
                    this.setState({
                        couponList: isEmpty(data.data) ? [] : data.data
                    })
                } else {
                    Toast.info(data.msg)
                }
            })
    }

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

                    const inuse_coupon = data.data['inuse_coupon'];


                    this.setState({
                        valid_coupons: inuse_coupon
                            ? [...inuse_coupon, ...data.data.valid_coupons]
                            : data.data.valid_coupons,
                        invalid_coupons: data.data.invalid_coupons,
                        selectedCouponId: inuse_coupon.length ? inuse_coupon[0].id : 0
                    })

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

xuzhenghua committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    // 立即兑换课程
    toExchangeCourse = (e,code)=>{
        e.stopPropagation();
        http.post(`${API['base-api']}/pay/miandan/${code}`, {
        }).then(res => {
            const data = res.data
            if (data.errno === 200) {
                this.setState({
                    courseCouponExchange: true,
                    courseCouponData:res.data.data
                })
                this.getMyCoupons()
            } else {
                Toast.info(data.msg)
            }
        })
    }

xuzhenghua committed
156 157 158 159 160 161 162
    useCoupon = val => {
        const {history, dispatch} = this.props
        const coupon = this.state.couponList.find(item => item.id === val)

        if (val) {
            if (this.state.showUseButton) {

xuzhenghua committed
163
                if (coupon['limit_course'] === 0) {
xuzhenghua committed
164 165
                    history.push(`/classify`)
                } else {
xuzhenghua committed
166 167
                    history.push(`/detail?id=${coupon['limit_course']}`);
                    return false;
xuzhenghua committed
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
                }

            } 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()
        }

    }

xuzhenghua committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    // 开始学习
    toStudy = (vCourseId, isHaveVideo) => {
        const {history} = this.props;
        if (isHaveVideo == 0) {
            Toast.info('尚未开课,开课后立即上传课程~', 2)
        } else {
            history.push(`/play/video?id=${vCourseId}`)
        }
        this.setState({
            courseCouponExchange: false
        })
    }

    // 关闭弹窗
    closeFreeCourse = () => {
        this.setState({
            courseCouponExchange: false
        })
    }

xuzhenghua committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    render() {
        const {state} = this.props.location
        const {showUseButton, selectedCouponId} = this.state
        return (
            <div className='use-coupon'>
                <RedeemBar onChange={this.handleChange}
                           exchange={this.exchange}
                           redeemCode={this.state.redeemCode}/>
                <div className="coupons-area">
                    <Content
                        coupons={
                            state
                            && state.from
                            && state.from === '/my'
                                ? this.state.couponList
                                : this.state.valid_coupons
                        }
                        showUseButton={showUseButton}
                        selectedCouponId={selectedCouponId}
                        select={this.select}
                        useCoupon={this.useCoupon}
xuzhenghua committed
266
                        toExchangeCourse={this.toExchangeCourse}
xuzhenghua committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
                    />
                    {
                        this.state.invalid_coupons.length > 0 &&
                        (
                            <>
                                <div className='invalid-title'>- 不可使用的优惠券 -</div>
                                <Content
                                    coupons={this.state.invalid_coupons}
                                    selectedCouponId={selectedCouponId}
                                    select={this.select}
                                    purpose={'use'}
                                    invalid={'invalid'}
                                />
                            </>
                        )
                    }
                </div>
xuzhenghua committed
284 285 286 287 288
                {
                    this.state.courseCouponExchange &&
                    <FreeCouponCourse toStudy={this.toStudy} closeFreeCourse={this.closeFreeCourse}
                                      courseCouponData={this.state.courseCouponData}/>
                }
xuzhenghua committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
            </div>
        );
    }
}

function Content({coupons, ...rest}) {
    if (coupons.length === 0) {
        return (
            <div className='empty'>
                <p>暂无可使用的优惠券</p>
            </div>
        )
    }
    return (
        <ul>
            {
                coupons.map(item => {
                    return (
                        <Coupon
                            key={item.id}
                            {...item}
                            id={item.id}
                            {...rest}
                        />
                    )
                })
            }
        </ul>
    )
}

xuzhenghua committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
function FreeCouponCourse(props) {
    const {toStudy, closeFreeCourse, courseCouponData} = props
    return (
        <div className="free-coupon-box">
            <div className="free-coupon-content">
                <div className="coures-content-success"><i className={'iconfont icondanseshixintubiao-5'}/></div>
                <div className="coures-content-title">恭喜你课程兑换成功!赶快去学习吧~</div>
                <img className="coures-content-img" src={courseCouponData.image_name} alt=""/>
                {
                    courseCouponData.course_expire != 0 &&
                    <div className="coures-content-tip"><i
                        className={'iconfont icondanseshixintubiao-8'}/><span>课程有效期:自今日起{courseCouponData.course_expire}天内,请在有效期内学习该课程哦~</span>
                    </div>
                }

                <a className='toStudy'
                   onClick={() => toStudy(courseCouponData.v_course_id, courseCouponData.is_is_start)}>去学习</a>
            </div>
            <div className="free-coupon-close">
                <i className={'iconfont iconiconfront-2'} onClick={() => closeFreeCourse()}/>
            </div>
        </div>
    )
}

xuzhenghua committed
345
export default WithFullSize(UseCoupon);