index.js 18 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5 6 7 8
import React, { Component } from "react"
import { Flex, List, WingBlank, Toast } from "antd-mobile"
import { OrderItem } from "src/common/index"
import { Link } from "react-router-dom"
import { http, getParam } from "src/utils"
import { throttle } from "lodash"
import { HeaderBar } from "../../common"
import { connect } from "react-redux"
.  
baiguangyao committed
9 10 11

import "./order.scss"

zhanghaozhe committed
12
const Item = List.Item
.  
baiguangyao committed
13 14

function OrderList(props) {
zhanghaozhe committed
15
  const listData = props.list
.  
baiguangyao committed
16
  return (
xuzhenghua committed
17
    <div>
zhanghaozhe committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
      {listData.map((item, index) => {
        const {
          course_id,
          image_name,
          price1,
          price0,
          simpledescription,
          course_title,
          coupon_num,
          coupon_desc,
        } = item
        let NewPrice = <span className="order-newprice">¥{price1}</span>
        if (props.locationState && props.locationState.group) {
          NewPrice = <span className="order-newprice">¥{props.groupPrice}</span>
        }
        const Info = (
          <div className="order-info">
            {/* <Link to={`/detail?id=${course_id}`}> */}
            <p
              className="order-title"
              style={{
                overflow: "hidden",
                textOverflow: "ellipsis",
                whiteSpace: "nowrap",
              }}
              onClick={() => props.toDetail(course_id)}
44
            >
zhanghaozhe committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
              {course_title}
            </p>
            {/* </Link> */}
            <p
              className="order-content"
              style={{
                WebkitBoxOrient: "vertical",
                WebkitLineClamp: "2",
                wordBreak: "break-all",
                overflow: "hidden",
                textOverflow: "ellipsis",
                display: "-webkit-box",
              }}
            >
              {simpledescription}
            </p>
            <p className="order-des">
              {/*// 这里根据类型判断 来显示什么价格 拼团还是砍价 还是特训营*/}
              {/*{*/}
              {/*  props.courseId === undefined ? (<span className='order-newprice'>¥{price1}</span>) : (<span className='order-newprice'>¥{price0}</span>)*/}
              {/*}*/}
              {NewPrice}
              <span className="order-price">¥{price0}</span>
            </p>
          </div>
        )
71

zhanghaozhe committed
72 73 74 75 76 77 78 79 80 81 82 83 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 120 121 122 123 124 125 126
        return (
          <OrderItem
            {...item}
            src={image_name}
            id={course_id}
            key={index}
            info={Info}
            isaist={props.isaist}
            toDetail={props.toDetail}
          >
            {props.locationState &&
            (props.locationState.type || props.locationState.simple) ? (
              <div className="order-prefer">
                <List key={index}>
                  <Item arrow="horizontal" onClick={() => {}}>
                    <Link
                      to={{
                        pathname: `/coupons`,
                        search: `?id=${course_id}`,
                        state: {
                          from: "/order",
                        },
                      }}
                    >
                      {/* <Link to='coupons' query={{id: course_id}} state={{from: '/order'}}> */}
                      <Flex justify="between">
                        <span
                          style={{
                            color: "#333",
                            fontSize: "15px",
                          }}
                        >
                          优惠券
                        </span>
                        <span
                          style={{
                            fontSize: "14px",
                            color: "#999999",
                          }}
                        >
                          {!coupon_desc
                            ? coupon_num === 0
                              ? "无"
                              : `${coupon_num}张可用`
                            : coupon_desc}
                        </span>
                      </Flex>
                    </Link>
                  </Item>
                </List>
              </div>
            ) : null}
          </OrderItem>
        )
      })}
.  
baiguangyao committed
127
    </div>
zhanghaozhe committed
128
  )
.  
baiguangyao committed
129 130
}

131
@connect()
.  
baiguangyao committed
132 133
class Order extends Component {
  constructor(props) {
zhanghaozhe committed
134
    super(props)
.  
baiguangyao committed
135
    this.state = {
zhanghaozhe committed
136
      groupPrice: "",
wangshuo committed
137
      perfect: this.props.location.state,
zhanghaozhe committed
138 139 140
      user_account: 0.0, // 账户余额
      total: 0.0, // 需要支付总金额
      discount: 0.0, //
wangshuo committed
141 142
      useBalance: false,
      orderList: [],
wangshuo committed
143 144
      info: false,
      offset: 0,
xuzhenghua committed
145 146
      full_amount: 0, // 满金额
      cut_amount: 0, // 减金额
zhanghaozhe committed
147
    }
.  
baiguangyao committed
148
  }
zhanghaozhe committed
149

wangshuo committed
150 151
  // 提交订单
  submitOrder = () => {
wangshuo committed
152
    if (this.state.orderList.length === 0) {
zhanghaozhe committed
153 154
      Toast.info("没有要提交的订单!")
      return
wangshuo committed
155
    }
156
    if (!this.state.perfect) {
zhanghaozhe committed
157
      Toast.info("请完善报名信息!")
158
    }
zhanghaozhe committed
159 160 161
    const {
      location: { state = {} },
    } = this.props
xuzhenghua committed
162

zhanghaozhe committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176
    if (state.group === 1) {
      let params = state.pdd_order_id
        ? {
            course_id: getParam("id"),
            ischeck: this.state.useBalance,
            pdd_order_id: state.pdd_order_id,
          }
        : {
            course_id: getParam("id"),
            ischeck: this.state.useBalance,
          }
      http.post(`${API["base-api"]}/pdd/m`, params).then((res) => {
        if (Number(res.data.errno) === 200) {
          sessionStorage.removeItem("orderUseCacheObj")
177
          if (res.data.data.pay_jump === 1) {
zhanghaozhe committed
178 179
            this.props.history.push(`/togroup?id=${res.data.data.oid}`)
            return
180
          }
zhanghaozhe committed
181 182 183 184 185 186
          this.props.history.push(`/payorder?oid=${res.data.data.oid}`, {
            group: 1,
          })
        } else if (Number(res.data.errno) === 0) {
          Toast.info(res.data.data.msg, 2)
          return
xuzhenghua committed
187
        } else {
zhanghaozhe committed
188 189
          Toast.info(res.data.msg, 2)
          return
190
        }
zhanghaozhe committed
191 192 193 194 195
      })
    } else {
      let url = `${API["base-api"]}/m/v34/cart/order?ischeck=${this.state.useBalance}`
      if (this.props.location.state && this.props.location.state.type === 1) {
        url += "&type=1"
196
      }
zhanghaozhe committed
197 198 199 200
      http.get(url).then((res) => {
        if (res.data.errno !== 0) {
          Toast.info(res.data.msg, 2)
          return
201
        }
zhanghaozhe committed
202
        sessionStorage.removeItem("orderUseCacheObj")
203
        if (res.data.data.is_free === 1) {
zhanghaozhe committed
204 205
          this.props.history.replace(`/purchased`)
          return
206
        }
zhanghaozhe committed
207 208
        this.props.history.replace(`/payorder?oid=${res.data.data.order_id}`)
      })
209
    }
.  
baiguangyao committed
210
  }
211
  // 勾选取消勾选 是否使用余额
wangshuo committed
212
  useBalance = () => {
zhanghaozhe committed
213
    let useBalanceFlag = this.state.useBalance
wangshuo committed
214
    this.setState({
zhanghaozhe committed
215 216
      useBalance: !useBalanceFlag,
    })
217
    if (!useBalanceFlag) {
wangshuo committed
218
      this.cacheObj = {
zhanghaozhe committed
219 220 221 222 223
        ...this.state,
      }
      sessionStorage.setItem("orderUseCacheObj", JSON.stringify(this.state))
    } else {
      sessionStorage.removeItem("orderUseCacheObj")
wangshuo committed
224
    }
zhanghaozhe committed
225
    this.computedMoney(useBalanceFlag)
226 227 228
  }
  // 勾选取消勾选时:计算金额、优惠金额、优惠券等
  computedMoney = (useBalanceFlag) => {
229
    let totalSale = parseFloat(this.cacheObj.total),
zhanghaozhe committed
230 231
      userAccount = parseFloat(this.cacheObj.user_account)
    const { discount } = this.state
232 233
    if (!useBalanceFlag) {
      if (totalSale > userAccount) {
wangshuo committed
234 235
        this.setState({
          offset: userAccount.toFixed(2),
236
          total: (totalSale - userAccount).toFixed(2),
237
          discount: (userAccount + parseFloat(discount)).toFixed(2),
zhanghaozhe committed
238
        })
239
      } else {
wangshuo committed
240 241
        this.setState({
          offset: totalSale.toFixed(2),
242
          total: 0,
243
          discount: (totalSale + parseFloat(discount)).toFixed(2),
zhanghaozhe committed
244
        })
wangshuo committed
245
      }
246
    } else {
wangshuo committed
247 248
      this.setState({
        offset: this.cacheObj.offset,
249
        total: this.cacheObj.total,
wangshuo committed
250
        discount: this.cacheObj.discount,
zhanghaozhe committed
251
      })
wangshuo committed
252
    }
zhanghaozhe committed
253
  }
254 255 256 257
  // 选择优惠券返回时根据是否勾选计算
  computedMoneyByCache = () => {
    let totalSale = parseFloat(this.cacheObj.total),
      userAccount = parseFloat(this.cacheObj.user_account),
zhanghaozhe committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
      discount = parseFloat(this.cacheObj.discount)
    if (totalSale > userAccount) {
      this.setState({
        offset: userAccount.toFixed(2),
        total: (totalSale - userAccount).toFixed(2),
        discount: (discount + userAccount).toFixed(2),
      })
    } else {
      this.setState({
        offset: totalSale.toFixed(2),
        total: 0,
        discount: totalSale.toFixed(2),
      })
    }
  }
wangshuo committed
273 274
  // 展示余额抵扣规则
  showInfo = () => {
275
    this.setState((prevState) => ({
zhanghaozhe committed
276 277 278
      info: !prevState.info,
    }))
  }
FE committed
279

wangshuo committed
280 281
  // 公共方法 存储数据
  publicGetData = (res) => {
zhanghaozhe committed
282 283 284
    let { course, total, user_account, user_info, discount } = res
    if (this.props.location.state && this.props.location.state.group === 1) {
      total = this.state.groupPrice
285
    }
wangshuo committed
286 287 288 289 290 291
    this.cacheObj = {
      perfect: user_info,
      orderList: course,
      user_account,
      total,
      discount,
zhanghaozhe committed
292
    }
wangshuo committed
293 294 295 296 297 298
    this.setState({
      perfect: user_info,
      orderList: course,
      user_account,
      total,
      discount,
zhanghaozhe committed
299 300
    })
  }
xuzhenghua committed
301 302 303 304
  fullRules = (data) => {
    this.setState({
      full_amount: data.full_amount,
      cut_amount: data.cut_amount,
zhanghaozhe committed
305 306
    })
  }
wangshuo committed
307 308
  // 公共方法 本地存储
  publicLocalStorage = () => {
zhanghaozhe committed
309 310
    if (this.props.history.action === "PUSH") {
      sessionStorage.removeItem("orderUseCacheObj")
FE committed
311
      this.setState({
zhanghaozhe committed
312 313 314 315 316
        discount: 0.0,
      })
    } else {
      const cacheObj = sessionStorage.getItem("orderUseCacheObj")
      if (cacheObj !== null) {
wangshuo committed
317
        this.setState({
zhanghaozhe committed
318 319 320
          useBalance: true,
        })
        this.computedMoneyByCache()
wangshuo committed
321 322
      }
    }
zhanghaozhe committed
323
  }
zhanghaozhe committed
324

wangshuo committed
325
  componentDidMount() {
FE committed
326
    // type: 1,返现课程,simple: 1,正常购买
zhanghaozhe committed
327 328 329 330 331 332 333
    if (this.props.location.state && this.props.location.state.type === 1) {
      http.get(`${API["base-api"]}/m/order/preorder?type=1`).then((res) => {
        if (res.data.errno === 200) {
          this.publicGetData(res.data.data)
          this.publicLocalStorage()
        } else {
          Toast.info(res.data.msg, 2)
wangshuo committed
334 335
        }
      })
zhanghaozhe committed
336 337 338 339 340
    } else if (
      this.props.location.state &&
      (this.props.location.state.simple === 1 ||
        this.props.location.state.bargain === 1)
    ) {
341
      // 普通课程立即报名 不带 type = 1
zhanghaozhe committed
342 343 344 345 346
      http.get(`${API["base-api"]}/m/order/preorder`).then((res) => {
        if (res.data.errno === 200) {
          console.log(res.data.data)
          this.publicGetData(res.data.data)
          this.publicLocalStorage()
xuzhenghua committed
347

zhanghaozhe committed
348 349 350
          this.fullRules(res.data.data)
        } else {
          Toast.info(res.data.msg, 2)
wangshuo committed
351
        }
zhanghaozhe committed
352 353 354 355 356
      })
    } else if (
      this.props.location.state &&
      this.props.location.state.group === 1
    ) {
357
      // 获取一键开团的课程
zhanghaozhe committed
358 359 360 361
      Promise.all([
        http.get(`${API.home}/m/course/detail/${getParam("id")}`),
        http.get(`${API["base-api"]}/m/order/preorder`),
      ]).then((resList) => {
362
        let courseInfo = resList[0],
zhanghaozhe committed
363 364 365 366 367 368 369
          orderInfo = resList[1]
        let newData = {}
        if (orderInfo.data.errno === 200) {
          newData = Object.assign({}, orderInfo.data.data, { course: [] })
        } else {
          Toast.info(orderInfo.data.msg, 2)
          return
370
        }
zhanghaozhe committed
371 372
        if (courseInfo.data.code === 200) {
          newData.course.push(courseInfo.data.data.course_info)
373
          this.setState({
zhanghaozhe committed
374
            groupPrice: courseInfo.data.data.course_info.pdd_group_info.price,
375
          })
zhanghaozhe committed
376 377 378
        } else {
          Toast.info(courseInfo.data.msg, 2)
          return
379
        }
zhanghaozhe committed
380 381 382
        this.publicGetData(newData)
        this.publicLocalStorage()
      })
wangshuo committed
383
    } else {
zhanghaozhe committed
384 385 386 387 388 389
      http.get(`${API["base-api"]}/m/order/preorder`).then((res) => {
        console.log("为了验证什么条件下会走这段代码")
        if (res.data.errno !== 200) {
          Toast.info(res.data.msg, 2)
          return
        }
wangshuo committed
390

zhanghaozhe committed
391 392 393
        this.publicGetData(res.data.data)
        this.publicLocalStorage()
      })
wangshuo committed
394
    }
zhanghaozhe committed
395
  }
396 397

  toCourseDetail = (id) => {
zhanghaozhe committed
398
    const { history } = this.props
zhanghaozhe committed
399
    history.push(`/detail?id=${id}`)
400 401
  }

wangshuo committed
402 403 404
  render() {
    const {
      perfect,
wangshuo committed
405
      orderList,
wangshuo committed
406
      user_account,
407
      total,
wangshuo committed
408 409
      discount,
      useBalance,
wangshuo committed
410 411
      info,
      offset,
xuzhenghua committed
412 413 414
      groupPrice,
      full_amount,
      cut_amount,
zhanghaozhe committed
415
    } = this.state
wangshuo committed
416 417 418 419 420

    return (
      <div className="order-wrapper">
        <Flex>
          <Flex.Item>
zhanghaozhe committed
421
            <HeaderBar title="课程报名" arrow={true} />
wangshuo committed
422
            <div className="order-list">
423 424 425
              <OrderList
                list={orderList}
                compute={this.computedMoney}
zhanghaozhe committed
426
                courseId={getParam("id")}
427
                locationState={this.props.location.state}
428
                groupPrice={groupPrice}
429 430
                toDetail={this.toCourseDetail}
              />
wangshuo committed
431 432 433
            </div>
            <div className="order-balance">
              <List>
zhanghaozhe committed
434 435 436
                <Item className="order-prefer-text">
                  <Flex justify="between">
                    <Flex align="center">
wangshuo committed
437
                      <span>余额抵扣</span>
zhanghaozhe committed
438 439 440 441 442 443 444 445 446
                      <span className="order-balanceprice">
                        {" "}
                        (余额:{" "}
                        <i className="order-money">{`${user_account}元`}</i>)
                      </span>
                      <i
                        className="iconfont iconiconfront-22 question-mark"
                        onClick={this.showInfo}
                      ></i>
wangshuo committed
447
                    </Flex>
448

449
                    <Flex>
zhanghaozhe committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
                      {useBalance ? (
                        <>
                          <span
                            style={{
                              color: "#FF2121",
                              fontSize: "15px",
                              marginRight: "6px",
                            }}
                          >{`-¥${offset}`}</span>
                          <i
                            className={`iconfont icondanseshixintubiao-5 balance-used`}
                            onClick={throttle(this.useBalance, 600)}
                          ></i>
                        </>
                      ) : (
                        <i
                          className="circle-icon"
                          onClick={throttle(this.useBalance, 600)}
                        ></i>
                      )}
470
                    </Flex>
wangshuo committed
471 472 473 474
                  </Flex>
                </Item>
              </List>
            </div>
zhanghaozhe committed
475 476
            {full_amount > 0 && cut_amount > 0 ? (
              <>
xuzhenghua committed
477 478 479
                <div className="full__rules">
                  <WingBlank>
                    <div className="money__off">
zhanghaozhe committed
480 481 482 483
                      <span>
                        {full_amount}{cut_amount}
                      </span>
                      <span className="money">{`-¥${cut_amount}`}</span>
xuzhenghua committed
484 485 486
                    </div>
                  </WingBlank>
                </div>
zhanghaozhe committed
487 488
              </>
            ) : null}
wangshuo committed
489 490 491 492 493 494 495
            <div className="order-bar">
              <div className="order-course">
                <span className="order-course-text">{`${orderList.length}门课程`}</span>
              </div>
              <div className="order-bar-text">
                <div className="order-amount">
                  <span className="order-amount-title">合计:</span>
496
                  <span className="order-amount-price">{`¥${total}`}</span>
baiguangyao committed
497
                </div>
wangshuo committed
498 499 500
                <div className="order-preprice">
                  <span className="order-preprice-title">已优惠:</span>
                  <span className="order-preprice-price">{${discount}`}</span>
baiguangyao committed
501
                </div>
wangshuo committed
502
              </div>
zhanghaozhe committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
              {perfect ? (
                <button type="button" className="order-button has-info">
                  <span
                    className="order-button-text"
                    onClick={this.submitOrder}
                  >
                    提交订单
                  </span>
                </button>
              ) : (
                <button type="button" className="order-button">
                  <span
                    className="order-button-text"
                    onClick={this.submitOrder}
                  >
                    提交订单
                  </span>
                </button>
              )}
wangshuo committed
522 523 524
            </div>
          </Flex.Item>
        </Flex>
zhanghaozhe committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
        {info ? (
          <div
            style={{
              position: "fixed",
              top: 0,
              left: 0,
              width: "100%",
              height: "100%",
              backgroundColor: "rgba(0, 0, 0, 0.8)",
              zIndex: "99",
            }}
          >
            <div
              style={{
                padding: "20px",
                backgroundColor: "#FFF",
                width: "300px",
                height: "170px",
                margin: "0 auto",
                position: "absolute",
                left: "50%",
                top: "50%",
                transform: "translate(-50%, -50%)",
              }}
            >
              <Flex
                direction="column"
                justify="between"
                align="center"
                style={{ height: "100%" }}
              >
                <p style={{ fontSize: "16px", color: "#333333" }}>
                  余额抵扣说明
                </p>
                <p
                  style={{
                    lineHeight: "20px",
                    fontSize: "13px",
                    color: "#666666",
                  }}
                >
                  分销课程或者参与七月在线的相关活动,可获得资金奖励。账户资金可直接提现,也可抵扣课程费用。
                </p>
                <div
                  onClick={this.showInfo}
                  style={{
                    width: "260px",
                    height: "30px",
                    lineHeight: "30px",
                    textAlign: "center",
                    borderRadius: "3px",
                    border: "1px solid #0099FF",
                    color: "#0099FF",
                    fontSize: "15px",
                  }}
                >
                  知道了
                </div>
              </Flex>
wangshuo committed
584
            </div>
zhanghaozhe committed
585 586
          </div>
        ) : null}
wangshuo committed
587 588 589
      </div>
    )
  }
.  
baiguangyao committed
590 591
}

zhanghaozhe committed
592
export default Order