index.js 11.1 KB
Newer Older
zhanghaozhe committed
1
/* eslint-disable no-sequences, eqeqeq, jsx-a11y/anchor-is-valid */
zhanghaozhe committed
2 3
import React, { PureComponent } from "react"
import "./index.scss"
xuzhenghua committed
4
import RedeemBar from "../RedeemBar"
zhanghaozhe committed
5 6 7 8 9
import Coupon from "../Coupon"
import { http, getParam } from "src/utils"
import { WithFullSize } from "src/HOCs"
import { Toast } from "antd-mobile"
import { connect } from "react-redux"
xuzhenghua committed
10 11 12

@connect()
class UseCoupon extends PureComponent {
xuzhenghua committed
13 14
  state = {
    selectedCouponId: 0,
zhanghaozhe committed
15
    redeemCode: "",
xuzhenghua committed
16 17 18
    couponList: [],
    valid_coupons: [],
    invalid_coupons: [],
zhanghaozhe committed
19
    courseId: getParam("id"),
xuzhenghua committed
20 21
    showUseButton: false,
    courseCouponExchange: false, // 课程券兑换弹窗
zhanghaozhe committed
22
    courseCouponData: "", // 兑换课程信息
xuzhenghua committed
23 24 25
  }

  componentDidMount() {
zhanghaozhe committed
26
    const { history, location } = this.props
xuzhenghua committed
27

zhanghaozhe committed
28
    const { state } = this.props.location
xuzhenghua committed
29
    if (state && state.from) {
zhanghaozhe committed
30
      if (state.from === "/my") {
xuzhenghua committed
31 32
        this.getMyCoupons()
        this.setState({
zhanghaozhe committed
33
          showUseButton: true,
xuzhenghua committed
34 35 36
        })
      } else {
        if (!this.state.courseId) {
zhanghaozhe committed
37 38 39
          location.state && location.state.from
            ? history.replace(location.state.from)
            : history.goBack()
xuzhenghua committed
40 41 42
        }
        this.getAllCoupons()
      }
xuzhenghua committed
43
    }
xuzhenghua committed
44 45
  }

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

  // 兑换
  exchange = () => {
zhanghaozhe committed
53 54 55 56 57 58 59 60 61 62
    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) => {
xuzhenghua committed
63 64 65 66 67
          const data = res.data
          if (data.code === 200) {
            // 如果从我的页面进来,点击兑换直接兑换成功课程,弹出弹窗
            // 如果从订单页面进来,点击兑换兑换成券
            const coupon = data.data
xuzhenghua committed
68

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

xuzhenghua committed
88 89 90 91 92 93
              if (coupon['ctype'] == 2
                && coupon['limit_course'] != this.state.courseId) {
                this.setState({
                  invalid_coupons: [...this.state.invalid_coupons, coupon],
                  showUseButton: null,
                  redeemCode: ''
xuzhenghua committed
94
                })
xuzhenghua committed
95
              } else {
xuzhenghua committed
96
                this.setState({
xuzhenghua committed
97 98
                  valid_coupons: [...this.state.valid_coupons, coupon],
                  redeemCode: ''
xuzhenghua committed
99
                })
FE committed
100
              }*/
zhanghaozhe committed
101
              Toast.info("兑换成功")
xuzhenghua committed
102
              this.getAllCoupons()
xuzhenghua committed
103
            }
xuzhenghua committed
104 105 106
          } else {
            Toast.info(data.msg)
          }
xuzhenghua committed
107
        })
xuzhenghua committed
108
    } else {
zhanghaozhe committed
109
      Toast.info("请输入兑换码")
xuzhenghua committed
110
    }
xuzhenghua committed
111 112 113 114 115
  }

  getMyCoupons = () => {
    Promise.all([
      http.get(`${API.home}/m/coupon/expansion`),
zhanghaozhe committed
116
      http.get(`${API.home}/m/coupon/all`),
xuzhenghua committed
117 118 119
    ]).then((coupons) => {
      let myCoupons = []
      const [expansionCoupons, allCoupons] = coupons
zhanghaozhe committed
120 121
      const { data: all } = allCoupons
      const { data: expansion } = expansionCoupons
zhanghaozhe committed
122
      if (expansion.code === 200) {
zhanghaozhe committed
123 124
        Array.isArray(expansion.data) &&
          (expansion.data = expansion.data.map(
zhanghaozhe committed
125
            /* eslint-disable-next-line no-sequences */
zhanghaozhe committed
126 127 128
            (item) => ((item.ctype = 4), item)
          )) &&
          (myCoupons = myCoupons.concat(expansion.data))
xuzhenghua committed
129 130 131
      } else {
        Toast.info(expansion.msg)
      }
zhanghaozhe committed
132
      if (all.code === 200) {
xuzhenghua committed
133 134 135 136 137 138
        Array.isArray(all.data) && (myCoupons = myCoupons.concat(all.data))
      } else {
        Toast.info(all.msg)
      }

      this.setState({
zhanghaozhe committed
139
        couponList: myCoupons,
xuzhenghua committed
140 141 142 143 144 145 146
      })
    })
  }

  getAllCoupons = () => {
    Promise.all([
      http.get(`${API.home}/m/coupon/expansion`),
zhanghaozhe committed
147 148 149
      http.post(`${API.home}/m/coupon/select`, {
        course_id: this.state.courseId,
      }),
xuzhenghua committed
150 151
    ]).then((coupons) => {
      const [expansionCoupons, selectCoupons] = coupons
zhanghaozhe committed
152 153
      const { data: select } = selectCoupons
      const { data: expansion } = expansionCoupons
FE committed
154
      let PzCoupon = Array.isArray(expansion.data) ? expansion.data : []
zhanghaozhe committed
155
      if (expansion.code === 200) {
zhanghaozhe committed
156 157 158 159 160 161 162
        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),
          })
xuzhenghua committed
163 164 165 166
      } else {
        Toast.info(expansion.msg)
      }
      if (select.code === 200) {
zhanghaozhe committed
167
        const inuse_coupon = select.data["inuse_coupon"]
xuzhenghua committed
168
        let PzCoupon2 = inuse_coupon
zhanghaozhe committed
169 170
          ? [...inuse_coupon, ...select.data.valid_coupons]
          : select.data.valid_coupons
xuzhenghua committed
171 172 173
        this.setState({
          valid_coupons: PzCoupon.concat(PzCoupon2),
          invalid_coupons: select.data.invalid_coupons,
zhanghaozhe committed
174
          selectedCouponId: inuse_coupon.length ? inuse_coupon[0].id : 0,
xuzhenghua committed
175 176
        })
      } else {
zhanghaozhe committed
177
        Toast.info(select.msg)
xuzhenghua committed
178 179 180 181 182 183 184
      }
    })
  }

  // 立即兑换课程
  toExchangeCourse = (e, code) => {
    e.stopPropagation()
zhanghaozhe committed
185
    http.post(`${API["base-api"]}/pay/miandan/${code}`, {}).then((res) => {
xuzhenghua committed
186 187 188 189
      const data = res.data
      if (data.errno === 200) {
        this.setState({
          courseCouponExchange: true,
zhanghaozhe committed
190
          courseCouponData: res.data.data,
xuzhenghua committed
191 192 193 194 195 196 197 198
        })
        this.getMyCoupons()
      } else {
        Toast.info(data.msg)
      }
    })
  }

zhanghaozhe committed
199 200 201
  useCoupon = (val) => {
    const { history } = this.props
    const coupon = this.state.couponList.find((item) => item.id === val)
xuzhenghua committed
202 203 204

    if (val) {
      if (this.state.showUseButton) {
zhanghaozhe committed
205
        if (coupon["limit_course"] === 0) {
xuzhenghua committed
206 207
          history.push(`/classify`)
        } else {
zhanghaozhe committed
208
          history.push(`/detail?id=${coupon["limit_course"]}`)
xuzhenghua committed
209 210 211
          return false
        }
      } else {
zhanghaozhe committed
212
        const { courseId, selectedCouponId } = this.state
xuzhenghua committed
213

xuzhenghua committed
214
        if (selectedCouponId === val) {
zhanghaozhe committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228
          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)
              }
            })
xuzhenghua committed
229
        } else {
zhanghaozhe committed
230 231 232 233 234 235
          http
            .post(`${API.home}/m/coupon/use`, {
              course_id: this.state.courseId,
              coupon_id: val,
            })
            .then((res) => {
xuzhenghua committed
236 237
              const data = res && res.data
              if (data.code === 200) {
zhanghaozhe committed
238
                this.setState({ selectedCouponId: val })
xuzhenghua committed
239 240 241 242 243 244 245 246
                this.props.history.goBack()
              } else {
                Toast.info(data.msg)
              }
            })
        }
      }
    } else {
zhanghaozhe committed
247
      Toast.info("未知错误")
zhanghaozhe committed
248
      window.location.reload()
xuzhenghua committed
249 250
    }
  }
xuzhenghua committed
251

xuzhenghua committed
252 253
  // 开始学习
  toStudy = (vCourseId, isHaveVideo) => {
zhanghaozhe committed
254
    const { history } = this.props
xuzhenghua committed
255
    if (isHaveVideo == 0) {
zhanghaozhe committed
256
      Toast.info("尚未开课,开课后立即上传课程~", 2)
xuzhenghua committed
257 258
    } else {
      history.push(`/play/video?id=${vCourseId}`)
xuzhenghua committed
259
    }
xuzhenghua committed
260
    this.setState({
zhanghaozhe committed
261
      courseCouponExchange: false,
xuzhenghua committed
262 263 264 265 266 267
    })
  }

  // 关闭弹窗
  closeFreeCourse = () => {
    this.setState({
zhanghaozhe committed
268
      courseCouponExchange: false,
xuzhenghua committed
269 270 271 272 273
    })
  }

  endExpansion = (id, validId) => {
    this.setState({
zhanghaozhe committed
274
      valid_coupons: this.state.valid_coupons.map((item) => {
xuzhenghua committed
275 276 277
        if (item.id === id) {
          delete item.start_amount
          item.id = validId
xuzhenghua committed
278
        }
xuzhenghua committed
279
        return item
zhanghaozhe committed
280
      }),
xuzhenghua committed
281 282
    })
  }
xuzhenghua committed
283

xuzhenghua committed
284
  render() {
zhanghaozhe committed
285 286
    const { state } = this.props.location
    const { showUseButton, selectedCouponId } = this.state
xuzhenghua committed
287
    return (
zhanghaozhe committed
288 289 290 291 292 293
      <div className="use-coupon">
        <RedeemBar
          onChange={this.handleChange}
          exchange={this.exchange}
          redeemCode={this.state.redeemCode}
        />
xuzhenghua committed
294 295 296
        <div className="coupons-area">
          <Content
            coupons={
zhanghaozhe committed
297
              state && state.from && state.from === "/my"
xuzhenghua committed
298 299 300 301 302 303 304 305 306 307
                ? this.state.couponList
                : this.state.valid_coupons
            }
            showUseButton={showUseButton}
            selectedCouponId={selectedCouponId}
            select={this.select}
            useCoupon={this.useCoupon}
            toExchangeCourse={this.toExchangeCourse}
            endExpansion={this.endExpansion}
          />
zhanghaozhe committed
308 309 310 311 312 313 314 315 316 317 318 319
          {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"}
              />
            </>
          )}
xuzhenghua committed
320
        </div>
zhanghaozhe committed
321 322 323 324 325 326 327
        {this.state.courseCouponExchange && (
          <FreeCouponCourse
            toStudy={this.toStudy}
            closeFreeCourse={this.closeFreeCourse}
            courseCouponData={this.state.courseCouponData}
          />
        )}
xuzhenghua committed
328 329 330
      </div>
    )
  }
xuzhenghua committed
331 332
}

zhanghaozhe committed
333
function Content({ coupons, ...rest }) {
xuzhenghua committed
334
  if (coupons.length === 0) {
xuzhenghua committed
335
    return (
zhanghaozhe committed
336
      <div className="empty">
xuzhenghua committed
337 338
        <p>暂无可使用的优惠券</p>
      </div>
xuzhenghua committed
339
    )
xuzhenghua committed
340 341 342
  }
  return (
    <ul>
zhanghaozhe committed
343 344 345 346 347
      {coupons.map((item) => {
        return (
          item.id && <Coupon key={item.id} {...item} id={item.id} {...rest} />
        )
      })}
xuzhenghua committed
348 349
    </ul>
  )
xuzhenghua committed
350 351
}

xuzhenghua committed
352
function FreeCouponCourse(props) {
zhanghaozhe committed
353
  const { toStudy, closeFreeCourse, courseCouponData } = props
xuzhenghua committed
354 355 356
  return (
    <div className="free-coupon-box">
      <div className="free-coupon-content">
zhanghaozhe committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
        <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>
xuzhenghua committed
375
          </div>
zhanghaozhe committed
376
        )}
xuzhenghua committed
377

zhanghaozhe committed
378 379 380 381 382 383 384 385
        <a
          className="toStudy"
          onClick={() =>
            toStudy(courseCouponData.v_course_id, courseCouponData.is_is_start)
          }
        >
          去学习
        </a>
xuzhenghua committed
386 387
      </div>
      <div className="free-coupon-close">
zhanghaozhe committed
388 389 390 391
        <i
          className={"iconfont iconiconfront-2"}
          onClick={() => closeFreeCourse()}
        />
xuzhenghua committed
392 393 394
      </div>
    </div>
  )
xuzhenghua committed
395 396
}

xuzhenghua committed
397
export default WithFullSize(UseCoupon)