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

xuzhenghua committed
4
import RedeemBar from "../RedeemBar"
xuzhenghua committed
5
import Coupon from '../Coupon'
xuzhenghua committed
6 7 8 9
import { http, getParam } from '@/utils'
import { WithFullSize } from '@/HOCs'
import { Toast } from 'antd-mobile'
import { connect } from 'react-redux'
xuzhenghua committed
10 11 12 13

@connect()
class UseCoupon extends PureComponent {

xuzhenghua committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  state = {
    selectedCouponId: 0,
    redeemCode: '',
    couponList: [],
    valid_coupons: [],
    invalid_coupons: [],
    courseId: getParam('id'),
    showUseButton: false,
    courseCouponExchange: false, // 课程券兑换弹窗
    courseCouponData: '' // 兑换课程信息
  }

  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()
      }
xuzhenghua committed
42
    }
xuzhenghua committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  }

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

  // 兑换
  exchange = () => {
    const {location: {state = {}}} = this.props
    if (this.state.redeemCode !== '') {
      http.post(`${API.home}/m/coupon/exchange`, {
        code: this.state.redeemCode,
        type: state.from.substr(1)
      })
        .then(res => {
          const data = res.data
          if (data.code === 200) {
            // 如果从我的页面进来,点击兑换直接兑换成功课程,弹出弹窗
            // 如果从订单页面进来,点击兑换兑换成券
            const coupon = data.data
xuzhenghua committed
64 65

            if (state.from === '/my') {
xuzhenghua committed
66
              if (coupon['ctype'] == 2) {
xuzhenghua committed
67
                this.setState({
xuzhenghua committed
68 69 70
                  courseCouponData: coupon,
                  courseCouponExchange: true,
                  redeemCode: ''
xuzhenghua committed
71
                })
xuzhenghua committed
72 73 74 75 76 77 78 79
              } else {
                this.setState({
                  couponList: [...this.state.couponList, coupon],
                  redeemCode: ''
                })
                Toast.info('兑换成功')
                this.getMyCoupons()
              }
xuzhenghua committed
80
            }
xuzhenghua committed
81
            if (state.from === '/order') {
FE committed
82
              /*const coupon = data.data
xuzhenghua committed
83

xuzhenghua committed
84 85 86 87 88 89
              if (coupon['ctype'] == 2
                && coupon['limit_course'] != this.state.courseId) {
                this.setState({
                  invalid_coupons: [...this.state.invalid_coupons, coupon],
                  showUseButton: null,
                  redeemCode: ''
xuzhenghua committed
90
                })
xuzhenghua committed
91
              } else {
xuzhenghua committed
92
                this.setState({
xuzhenghua committed
93 94
                  valid_coupons: [...this.state.valid_coupons, coupon],
                  redeemCode: ''
xuzhenghua committed
95
                })
FE committed
96
              }*/
xuzhenghua committed
97 98
              Toast.info('兑换成功')
              this.getAllCoupons()
xuzhenghua committed
99
            }
xuzhenghua committed
100 101 102 103

          } else {
            Toast.info(data.msg)
          }
xuzhenghua committed
104
        })
xuzhenghua committed
105 106
    } else {
      Toast.info('请输入兑换码')
xuzhenghua committed
107
    }
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
  }

  getMyCoupons = () => {
    Promise.all([
      http.get(`${API.home}/m/coupon/expansion`),
      http.get(`${API.home}/m/coupon/all`)
    ]).then((coupons) => {
      let myCoupons = []
      const [expansionCoupons, allCoupons] = coupons
      const {data: all} = allCoupons
      const {data: expansion} = expansionCoupons
      if (expansion.code == 200) {
        Array.isArray(expansion.data)
        && (expansion.data = expansion.data.map(item => (item.ctype = 4, item)))
        && (myCoupons = myCoupons.concat(expansion.data))
      } else {
        Toast.info(expansion.msg)
      }
      if (all.code == 200) {
        Array.isArray(all.data) && (myCoupons = myCoupons.concat(all.data))
      } else {
        Toast.info(all.msg)
      }

      this.setState({
        couponList: myCoupons
      })
    })
  }

  getAllCoupons = () => {
    Promise.all([
      http.get(`${API.home}/m/coupon/expansion`),
      http.post(`${API.home}/m/coupon/select`, {course_id: this.state.courseId})
    ]).then((coupons) => {
      const [expansionCoupons, selectCoupons] = coupons
      const {data: select} = selectCoupons
      const {data: expansion} = expansionCoupons
FE committed
146
      let PzCoupon = Array.isArray(expansion.data) ? expansion.data : []
xuzhenghua committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
      if (expansion.code == 200) {
        Array.isArray(expansion.data)
        && (expansion.data = expansion.data.map(item => (item.ctype = 4, item)))
        && (this.setState({
          valid_coupons: this.state.valid_coupons.concat(expansion.data)
        }))
      } else {
        Toast.info(expansion.msg)
      }
      if (select.code === 200) {
        const inuse_coupon = select.data['inuse_coupon']
        let PzCoupon2 = inuse_coupon
            ? [...inuse_coupon, ...select.data.valid_coupons]
            : select.data.valid_coupons
        this.setState({
          valid_coupons: PzCoupon.concat(PzCoupon2),
          invalid_coupons: select.data.invalid_coupons,
          selectedCouponId: inuse_coupon.length ? inuse_coupon[0].id : 0
        })
xuzhenghua committed
166

xuzhenghua committed
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 199 200 201 202
      } else {
        Toast.info(data.msg)
      }
    })
  }

  // 立即兑换课程
  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)
      }
    })
  }

  useCoupon = val => {
    const {history} = this.props
    const coupon = this.state.couponList.find(item => item.id === val)

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

        if (coupon['limit_course'] === 0) {
          history.push(`/classify`)
        } else {
          history.push(`/detail?id=${coupon['limit_course']}`)
          return false
        }
xuzhenghua committed
203

xuzhenghua committed
204
      } else {
xuzhenghua committed
205

xuzhenghua committed
206
        const {courseId, selectedCouponId} = this.state
xuzhenghua committed
207

xuzhenghua committed
208
        if (selectedCouponId === val) {
xuzhenghua committed
209 210


xuzhenghua committed
211 212 213 214 215
          http.post(`${API.home}/m/coupon/cancel`, {
            course_id: courseId
          }).then(res => {
            const data = res.data
            if (data.code === 200) {
xuzhenghua committed
216 217


xuzhenghua committed
218 219 220
              this.setState({
                selectedCouponId: 0
              })
xuzhenghua committed
221 222


xuzhenghua committed
223 224 225 226
            } else {
              Toast.info(data.msg)
            }
          })
xuzhenghua committed
227 228


xuzhenghua committed
229 230 231 232 233 234 235 236
        } 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) {
xuzhenghua committed
237

xuzhenghua committed
238 239
                this.setState({selectedCouponId: val})
                this.props.history.goBack()
xuzhenghua committed
240 241


xuzhenghua committed
242 243 244 245
              } else {
                Toast.info(data.msg)
              }
            })
xuzhenghua committed
246

xuzhenghua committed
247
        }
xuzhenghua committed
248 249


xuzhenghua committed
250
      }
xuzhenghua committed
251

xuzhenghua committed
252 253 254 255
    } else {
      Toast.info('未知错误')
      location.reload()
    }
xuzhenghua committed
256

xuzhenghua committed
257
  }
xuzhenghua committed
258

xuzhenghua committed
259 260 261 262 263 264 265
  // 开始学习
  toStudy = (vCourseId, isHaveVideo) => {
    const {history} = this.props
    if (isHaveVideo == 0) {
      Toast.info('尚未开课,开课后立即上传课程~', 2)
    } else {
      history.push(`/play/video?id=${vCourseId}`)
xuzhenghua committed
266
    }
xuzhenghua committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    this.setState({
      courseCouponExchange: false
    })
  }

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

  endExpansion = (id, validId) => {
    this.setState({
      valid_coupons: this.state.valid_coupons.map(item => {
        if (item.id === id) {
          delete item.start_amount
          item.id = validId
xuzhenghua committed
285
        }
xuzhenghua committed
286 287 288 289
        return item
      })
    })
  }
xuzhenghua committed
290 291


xuzhenghua committed
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
  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}
            toExchangeCourse={this.toExchangeCourse}
            endExpansion={this.endExpansion}
          />
          {
            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>
        {
          this.state.courseCouponExchange &&
          <FreeCouponCourse toStudy={this.toStudy} closeFreeCourse={this.closeFreeCourse}
                            courseCouponData={this.state.courseCouponData}/>
        }
      </div>
    )
  }
xuzhenghua committed
340 341 342
}

function Content({coupons, ...rest}) {
xuzhenghua committed
343
  if (coupons.length === 0) {
xuzhenghua committed
344
    return (
xuzhenghua committed
345 346 347
      <div className='empty'>
        <p>暂无可使用的优惠券</p>
      </div>
xuzhenghua committed
348
    )
xuzhenghua committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  }
  return (
    <ul>
      {
        coupons.map(item => {
          return (
              item.id &&
            <Coupon
              key={item.id}
              {...item}
              id={item.id}
              {...rest}
            />
          )
        })
      }
    </ul>
  )
xuzhenghua committed
367 368
}

xuzhenghua committed
369
function FreeCouponCourse(props) {
xuzhenghua committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  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
392 393
}

xuzhenghua committed
394
export default WithFullSize(UseCoupon)