index.js 5.27 KB
Newer Older
zhanghaozhe committed
1
import React, { PureComponent } from 'react'
zhanghaozhe committed
2 3
import './coupon.scss'
import classnames from 'classnames'
zhanghaozhe committed
4 5
import { http } from "@/utils"
import showAlert from '@common/deposit/end-expansion-alert'
zhanghaozhe committed
6 7
import { Toast } from 'antd-mobile'
import { withRouter } from "react-router-dom"
zhanghaozhe committed
8

zhanghaozhe committed
9

zhanghaozhe committed
10
class Coupon extends PureComponent {
zhanghaozhe committed
11

zhanghaozhe committed
12 13 14
  VOUCHER = 1   //代金券
  FREE = 2  //免单券
  EXPAND = 4    //膨胀全
xuzhenghua committed
15

zhanghaozhe committed
16

zhanghaozhe committed
17 18 19 20 21 22
  pick = (e) => {
    let {useCoupon, invalid, start_amount, id, location, history, code} = this.props
    const {state} = location
    if(state && state.from && state.from === '/my'){
      history.push(`/expand/index?deposit_code=${code}`)
      return
zhanghaozhe committed
23
    }
zhanghaozhe committed
24 25 26 27
    if (start_amount) {
      this.stopExpanding(id, e)
    } else {
      !invalid && useCoupon && useCoupon(this.props.id)
zhanghaozhe committed
28 29
    }

zhanghaozhe committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  }

  GoToUse = () => {
    this.props.useCoupon(this.props.id)
  }
  ExchangeCourse = (e) => {
    this.props.toExchangeCourse(e, this.props.code)
  }

  stopExpanding = (id, e) => {
    const {amount, limit_amount} = this.props
    e.stopPropagation()
    showAlert({
      amount,
      limit_amount,
      onConfirm: () => {
        http.post(`${API.home}/m/end_expansion`, {
          id
zhanghaozhe committed
48
        })
zhanghaozhe committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
          .then(res => {
            const {data} = res
            if (data.code == 200) {
              this.props.endExpansion(id, data.data.coupon_id)
            } else {
              Toast.info(data.msg)
            }
          })
      }
    })

  }

  render() {
    let {
      ctype,
      amount,
      course_title,
      showUseButton,
      id,
      format_expire_time,
      start_amount,
      limit_amount
    } = this.props

    let top, bottom

    switch (ctype) {
      case this.VOUCHER:
        top = (
          <>
            <p className='denomination'>{amount} <span></span></p>
            <p className='expire'>有效期至:{format_expire_time}</p>
          </>
zhanghaozhe committed
83

zhanghaozhe committed
84 85 86 87 88 89 90 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
        )
        bottom = (
          <>
            <span className={'limit'}>可用于大于代金券金额的课程</span>
            {
              showUseButton && ctype == this.VOUCHER &&
              <button
                className='use'
                onClick={this.GoToUse}
              >立即使用
              </button>
            }
          </>

        )
        break
      case this.FREE:
        top = (
          <>
            <p className='course-title'>{course_title}</p>
            <p className='expire'>有效期至:{format_expire_time}</p>

          </>
        )
        bottom = (
          <>
            <span className={'limit'}>{`仅适用于《${course_title}》`}</span>
            {
              showUseButton && ctype == this.FREE &&
              <button
                className='use'
                onClick={(e) => this.ExchangeCourse(e)}
              >立即兑换
              </button>
            }
          </>
zhanghaozhe committed
120

zhanghaozhe committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        )
        break
      case this.EXPAND:
        if (!start_amount) {
          top = (
            <>
              <p className='denomination'>{amount} <span></span></p>
              <p className='expire'>有效期至:{format_expire_time}</p>
            </>
          )

        } else {
          top = (
            <>
              <div className="progress-container">
                <p className='denomination'>{amount} <span></span></p>
                <div className={'progress-bar-container'}>
zhanghaozhe committed
138 139 140
                                    <span className={'progress-bar'}
                                          style={{width: `${Math.round((amount - start_amount) / limit_amount * 100)}%`}}
                                    ></span>
zhanghaozhe committed
141 142 143 144 145 146 147 148
                </div>
                <div className={'money'}>
                  <span>{start_amount}</span>
                  <span>{limit_amount}</span>
                </div>
              </div>
            </>
          )
zhanghaozhe committed
149
        }
zhanghaozhe committed
150 151 152 153 154 155 156 157 158
        bottom = (
          <>
            <span className={'limit'}>全场通用</span>
            {
              start_amount
              && <button className='stop-expanding'
                         onClick={this.stopExpanding.bind(this, id)}>立即结束膨胀</button>
            }
          </>
zhanghaozhe committed
159 160

        )
zhanghaozhe committed
161
        break
zhanghaozhe committed
162
    }
zhanghaozhe committed
163

zhanghaozhe committed
164
    return (
zhanghaozhe committed
165 166 167 168 169 170 171 172 173
      <BaseCoupon
        {...this.props}
        top={top}
        VOUCHER={this.VOUCHER}
        FREE={this.FREE}
        EXPAND={this.EXPAND}
        pick={this.pick}
        bottom={bottom}
      />
zhanghaozhe committed
174
    )
zhanghaozhe committed
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
  }
}

function BaseCoupon(
  {
    pick,
    ctype,
    invalid,
    top,
    VOUCHER,
    FREE,
    EXPAND,
    selectedCouponId,
    id,
    bottom
  }) {
  let couponTypes = {
    [VOUCHER]: '代金券',
    [FREE]: '课程券',
    [EXPAND]: '膨胀券'
  }
  return (
    <li className='coupon' onClick={pick}>
      <div className={classnames('coupon-info', invalid ? 'invalid' : `coupon-type${ctype}`)}>
        <p className='type'>{couponTypes[ctype]}</p>
        {top}
        {
          selectedCouponId === id &&
          <i className={classnames('iconfont icondanseshixintubiao-5', {
            check: !invalid
          })}
          />
        }
        <ul>
          {
            new Array(19).fill('a').map((item, index) => {
              return <li key={index}/>
            })
          }
        </ul>
      </div>
      <div className="coupon-des">
        {bottom}
      </div>
    </li>
  )
zhanghaozhe committed
221
}
zhanghaozhe committed
222

zhanghaozhe committed
223
export default withRouter(Coupon)