index.js 20.3 KB
Newer Older
.  
baiguangyao committed
1
import React, { Component } from 'react';
2
import { Flex, List, WingBlank, Toast } from 'antd-mobile';
wangshuo committed
3 4
import { OrderItem } from '@/common/index';
import { Link } from 'react-router-dom';
wangshuo committed
5
import { http, getParam } from "@/utils";
6
import { throttle } from 'lodash';
wangshuo committed
7
import {HeaderBar} from '../../common';
8 9
import {connect} from 'react-redux';
import {getCourses} from './../detail/actions';
.  
baiguangyao committed
10 11 12 13 14 15 16 17

import "./order.scss"

const Item = List.Item;

function OrderList(props) {
  const listData = props.list;
  return (
xuzhenghua committed
18
    <div>
.  
baiguangyao committed
19 20
      {
        listData.map((item, index) => {
21 22 23 24 25
          const { is_coupon, 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>);
          }
.  
baiguangyao committed
26 27
          const Info = (
            <div className="order-info">
28 29
              {/* <Link to={`/detail?id=${course_id}`}> */}
                <p
xuzhenghua committed
30 31 32 33
                  className='order-title'
                  style={{
                    overflow: 'hidden',
                    textOverflow: 'ellipsis',
34 35 36 37 38 39 40
                    whiteSpace: 'nowrap'
                  }}
                  onClick={() => props.toDetail(course_id)}
                >
                  {course_title}
                </p>
                {/* </Link> */}
41
              <p className='order-content' style={{ WebkitBoxOrient: 'vertical', WebkitLineClamp: '2', wordBreak: 'break-all', overflow: 'hidden', textOverflow: 'ellipsis', display: '-webkit-box' }}>{simpledescription}</p>
.  
baiguangyao committed
42
              <p className='order-des'>
43
                {/*// 这里根据类型判断 来显示什么价格 拼团还是砍价 还是特训营*/ }
44 45 46 47
                {/*{*/}
                {/*  props.courseId === undefined ? (<span className='order-newprice'>¥{price1}</span>) : (<span className='order-newprice'>¥{price0}</span>)*/}
                {/*}*/}
                {NewPrice}
48
                <span className='order-price'>¥{price0}</span>
.  
baiguangyao committed
49 50
              </p>
            </div>
wangshuo committed
51
          );
xuzhenghua committed
52

.  
baiguangyao committed
53
          return (
xuzhenghua committed
54 55 56 57 58 59
            <OrderItem
              {...item}
              src={image_name}
              id={course_id}
              key={index}
              info={Info}
60 61 62
              isaist={props.isaist}
              toDetail={props.toDetail}
            >
63
              {
wangshuo committed
64
                (props.locationState && (props.locationState.type || props.locationState.simple)) ? (
65 66 67 68 69 70
                  <div className="order-prefer">
                    <List key={index}>
                      <Item
                        arrow="horizontal"
                        onClick={() => { }}
                      >
71
                        <Link to={{
72 73
                                pathname: `/coupons`,
                                search: `?id=${course_id}`,
74
                                state: {
75
                                    from: '/order'
76
                              }}}>
77
                          {/* <Link to='coupons' query={{id: course_id}} state={{from: '/order'}}> */}
78
                          <Flex justify='between'>
xuzhenghua committed
79 80 81 82
                            <span
                              style={{
                                color: '#333',
                                fontSize: '15px'
FE committed
83 84
                              }}
                            >优惠券</span>
xuzhenghua committed
85 86 87 88
                            <span
                              style={{
                                fontSize: '14px',
                                color: '#999999'
FE committed
89 90 91 92
                              }}
                            >
                              {!coupon_desc ? (coupon_num === 0 ? '无' : `${coupon_num}张可用`) : (coupon_desc)}
                            </span>
93 94 95 96 97 98 99 100
                          </Flex>
                        </Link>
                      </Item>
                    </List>
                  </div>
                ) : null
              }

.  
baiguangyao committed
101 102 103 104 105 106 107 108
            </OrderItem>
          )
        })
      }
    </div>
  );
}

109
@connect()
.  
baiguangyao committed
110 111 112 113
class Order extends Component {
  constructor(props) {
    super(props);
    this.state = {
114
      groupPrice: '',
wangshuo committed
115
      perfect: this.props.location.state,
wangshuo committed
116
      user_account: 0.00, // 账户余额
117
      total: 0.00, // 需要支付总金额
xuzhenghua committed
118
      discount: 0.00, //
wangshuo committed
119 120
      useBalance: false,
      orderList: [],
wangshuo committed
121 122
      info: false,
      offset: 0,
xuzhenghua committed
123 124
      full_amount: 0, // 满金额
      cut_amount: 0, // 减金额
wangshuo committed
125
    };
.  
baiguangyao committed
126
  }
wangshuo committed
127 128
  // 提交订单
  submitOrder = () => {
129
    const {total} = this.state;
wangshuo committed
130
    if (this.state.orderList.length === 0) {
wangshuo committed
131 132 133
      Toast.info('没有要提交的订单!');
      return;
    }
134 135 136
    if (!this.state.perfect) {
      Toast.info('请完善报名信息!');
    }
FE committed
137
    const { location: { state ={} } } = this.props;
xuzhenghua committed
138

FE committed
139 140 141 142 143 144 145 146 147 148
    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 => {
xuzhenghua committed
149
        if(Number(res.data.errno) === 200) {
150
          sessionStorage.removeItem('orderUseCacheObj');
151
          if (res.data.data.pay_jump === 1) {
152
            this.props.history.push(`/togroup?id=${res.data.data.oid}`);
153 154
            return;
          }
xuzhenghua committed
155 156
          this.props.history.push(`/payorder?oid=${res.data.data.oid}`, {group: 1});

xuzhenghua committed
157 158 159 160
        }else if(Number(res.data.errno) === 0){
          Toast.info(res.data.data.msg, 2);
          return;
        } else {
161 162 163 164 165 166 167 168
          Toast.info(res.data.msg, 2);
          return;
        }
      });
    }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'
169
      }
170 171 172 173 174 175
      http.get(url).then(res => {
        if(res.data.errno !== 0) {
          Toast.info(res.data.msg, 2);
          return;
        }
        sessionStorage.removeItem('orderUseCacheObj');
176 177 178 179
        if (res.data.data.is_free === 1) {
          this.props.history.replace(`/purchased`);
          return;
        }
180 181 182 183
        this.props.history.replace(`/payorder?oid=${res.data.data.order_id}`);
      });
    }

.  
baiguangyao committed
184
  }
185
  // 勾选取消勾选 是否使用余额
wangshuo committed
186
  useBalance = () => {
wangshuo committed
187 188 189 190
    let useBalanceFlag = this.state.useBalance;
    this.setState({
      useBalance: !useBalanceFlag
    });
191
    if (!useBalanceFlag) {
wangshuo committed
192 193
      this.cacheObj = {
        ...this.state
194
      };
195 196 197
      sessionStorage.setItem('orderUseCacheObj', JSON.stringify(this.state));
    }else{
      sessionStorage.removeItem('orderUseCacheObj');
wangshuo committed
198
    }
199 200 201 202
    this.computedMoney(useBalanceFlag);
  }
  // 勾选取消勾选时:计算金额、优惠金额、优惠券等
  computedMoney = (useBalanceFlag) => {
203 204
    let totalSale = parseFloat(this.cacheObj.total),
      userAccount = parseFloat(this.cacheObj.user_account);
205
    const { discount } = this.state;
206 207
    if (!useBalanceFlag) {
      if (totalSale > userAccount) {
wangshuo committed
208 209
        this.setState({
          offset: userAccount.toFixed(2),
210
          total: (totalSale - userAccount).toFixed(2),
211
          discount: (userAccount + parseFloat(discount)).toFixed(2),
wangshuo committed
212
        });
213
      } else {
wangshuo committed
214 215
        this.setState({
          offset: totalSale.toFixed(2),
216
          total: 0,
217
          discount: (totalSale + parseFloat(discount)).toFixed(2),
wangshuo committed
218 219
        });
      }
220
    } else {
wangshuo committed
221 222
      this.setState({
        offset: this.cacheObj.offset,
223
        total: this.cacheObj.total,
wangshuo committed
224 225 226
        discount: this.cacheObj.discount,
      });
    }
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  };
  // 选择优惠券返回时根据是否勾选计算
  computedMoneyByCache = () => {
    let totalSale = parseFloat(this.cacheObj.total),
      userAccount = parseFloat(this.cacheObj.user_account),
      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
246 247 248
  };
  // 展示余额抵扣规则
  showInfo = () => {
249
    this.setState((prevState) => ({
wangshuo committed
250 251
      info: !prevState.info
    }));
wangshuo committed
252
  };
FE committed
253

wangshuo committed
254 255
  // 公共方法 存储数据
  publicGetData = (res) => {
256
    let { course, total, user_account, user_info, discount } = res;
257
    if(this.props.location.state && this.props.location.state.group === 1) {
258
      total = this.state.groupPrice;
259
    }
wangshuo committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    this.cacheObj = {
      perfect: user_info,
      orderList: course,
      user_account,
      total,
      discount,
    };
    this.setState({
      perfect: user_info,
      orderList: course,
      user_account,
      total,
      discount,
    });
  };
xuzhenghua committed
275 276 277 278 279 280
  fullRules = (data) => {
    this.setState({
      full_amount: data.full_amount,
      cut_amount: data.cut_amount,
    });
  };
wangshuo committed
281 282 283 284
  // 公共方法 本地存储
  publicLocalStorage = () => {
    if(this.props.history.action === 'PUSH') {
      sessionStorage.removeItem('orderUseCacheObj');
FE committed
285 286 287
      this.setState({
        discount: 0.00,
      });
wangshuo committed
288 289 290 291 292 293 294 295 296 297
    } else{
      const cacheObj = sessionStorage.getItem('orderUseCacheObj');
      if(cacheObj !== null) {
        this.setState({
          useBalance: true
        });
        this.computedMoneyByCache();
      }
    }
  };
wangshuo committed
298
  componentDidMount() {
299
    let _this = this;
FE committed
300
    // type: 1,返现课程,simple: 1,正常购买
wangshuo committed
301
    if(this.props.location.state && this.props.location.state.type === 1) {
302
      http.get(`${API['base-api']}/m/order/preorder?type=1`).then((res) => {
wangshuo committed
303
        if(res.data.errno === 200) {
304
          this.publicGetData(res.data.data);
wangshuo committed
305
          this.publicLocalStorage();
wangshuo committed
306 307 308 309
        }else{
          Toast.info(res.data.msg, 2);
        }
      })
wangshuo committed
310
    } else if (this.props.location.state && (this.props.location.state.simple === 1 || this.props.location.state.bargain === 1)) {
311 312 313
      // 普通课程立即报名 不带 type = 1
      http.get(`${API['base-api']}/m/order/preorder`).then(res => {
        if(res.data.errno === 200) {
xuzhenghua committed
314
          console.log(res.data.data);
315
          this.publicGetData(res.data.data);
wangshuo committed
316
          this.publicLocalStorage();
xuzhenghua committed
317 318

          this.fullRules(res.data.data);
wangshuo committed
319 320 321 322
        }else{
          Toast.info(res.data.msg, 2);
        }
      });
323 324
    } else if (this.props.location.state && this.props.location.state.group === 1){
      // 获取一键开团的课程
325
      Promise.all([http.get(`${API.home}/m/course/detail/${getParam('id')}`),http.get(`${API['base-api']}/m/order/preorder`)]).then(resList => {
326 327
        let courseInfo = resList[0],
            orderInfo = resList[1];
328 329 330
        let newData = {};
        if(orderInfo.data.errno === 200) {
          newData = Object.assign({}, orderInfo.data.data, {course: []});
331
        }else{
332
          Toast.info(orderInfo.data.msg, 2);
333 334
          return;
        }
335 336 337 338 339
        if(courseInfo.data.code === 200) {
          newData.course.push(courseInfo.data.data.course_info);
          this.setState({
            groupPrice: courseInfo.data.data.course_info.pdd_group_info.price
          })
340 341 342 343
        }else{
          Toast.info(courseInfo.data.msg, 2);
          return;
        }
344 345 346 347 348
        // this.groupPrice = orderInfo.data.data.course_info.pdd_group_info.price;
        // if(courseInfo.data.data.course.length === 0) {
        //   courseInfo.data.data.course.push(orderInfo.data.data.course_info)
        // }
        this.publicGetData(newData);
349 350
        this.publicLocalStorage();
      });
wangshuo committed
351
    } else {
352 353 354 355 356 357 358 359 360
      // if(getParam('id')) {
      //   Promise.all([http.get(`${API.home}/m/del_cart_order/${getParam('id')}`), http.get(`${API['base-api']}/m/order/preorder`)]).then(res => {
      //     console.log(res);
      //     let preorder = res[1],
      //         orderList = preorder.data.data.course;
      //     if(preorder.data.errno !== 200) {
      //       Toast.info(preorder.data.msg, 2);
      //       return;
      //     }
wangshuo committed
361

362 363 364 365
      //     this.publicGetData(preorder);
      //     this.publicLocalStorage();
      //   })
      // }else{
wangshuo committed
366
        http.get(`${API['base-api']}/m/order/preorder`).then((res) => {
367
          console.log('为了验证什么条件下会走这段代码');
wangshuo committed
368 369 370 371 372
          if (res.data.errno !== 200) {
            Toast.info(res.data.msg, 2);
            return;
          }

373
          this.publicGetData(res.data.data);
wangshuo committed
374 375
          this.publicLocalStorage();
        })
376
      // }
wangshuo committed
377
    }
xuzhenghua committed
378 379
    // else {
    //   if(getParam('id')) {
xuzhenghua committed
380
    //     Promise.all([http.get(`${API.home}/m/del_cart_order/${getParam('id')}`), http.get(`${API.home}/m/order/preorder`)]).then(res => {
xuzhenghua committed
381 382 383 384 385 386 387 388 389 390 391 392
    //       console.log(res);
    //       let preorder = res[1],
    //           orderList = preorder.data.data.course;
    //       if(preorder.data.errno !== 200) {
    //         Toast.info(preorder.data.msg, 2);
    //         return;
    //       }
    //
    //       this.publicGetData(preorder);
    //       this.publicLocalStorage();
    //     })
    //   }else{
xuzhenghua committed
393
    //     http.get(`${API.home}/m/order/preorder`).then((res) => {
xuzhenghua committed
394 395 396 397 398 399 400 401 402 403 404
    //       console.log('为了验证什么条件下会走这段代码');
    //       if (res.data.errno !== 200) {
    //         Toast.info(res.data.msg, 2);
    //         return;
    //       }
    //
    //       this.publicGetData(res);
    //       this.publicLocalStorage();
    //     })
    //   }
    // }
wangshuo committed
405
  };
406 407 408

  toCourseDetail = (id) => {
    const { dispatch, history } = this.props;
xuzhenghua committed
409
    // dispatch(getCourses(id, () => {
410
        history.push(`/detail?id=${id}`)
xuzhenghua committed
411
    // }));
412 413
  }

wangshuo committed
414 415 416
  render() {
    const {
      perfect,
wangshuo committed
417
      orderList,
wangshuo committed
418
      user_account,
419
      total,
wangshuo committed
420 421
      discount,
      useBalance,
wangshuo committed
422 423
      info,
      offset,
xuzhenghua committed
424 425 426
      groupPrice,
      full_amount,
      cut_amount,
wangshuo committed
427 428 429 430 431 432
    } = this.state;

    return (
      <div className="order-wrapper">
        <Flex>
          <Flex.Item>
433
            <HeaderBar title='课程报名' arrow={true} />
xuzhenghua committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
            {/*{*/}
              {/*!this.state.perfect &&*/}
              {/*<Link to={{*/}
                {/*pathname: '/orderinfo',*/}
                {/*state: {type: this.props.location.state ? this.props.location.state.type : null, id: getParam('id')}*/}
              {/*}}*/}
              {/*className="order-information">*/}
                {/*<i className="iconfont iconiconfront-6 order-addsize" />*/}
                {/*<div className="order-infotext">完善报名信息</div>*/}
                {/*<i className="iconfont iconiconfront-70 order-next" />*/}
              {/*</Link>*/}
            {/*}*/}
            {/*{*/}
              {/*this.state.perfect &&*/}
              {/*<div className="order-information2">*/}
                {/*<WingBlank>*/}
                  {/*<Link to={{*/}
                    {/*pathname: '/orderinfo',*/}
                    {/*state: {*/}
                      {/*...this.state.perfect,*/}
                      {/*...this.props.location.state,*/}
                      {/*id: getParam('id')*/}
                    {/*}*/}
                  {/*}*/}
                  {/*} >*/}
                    {/*<Flex align='center' justify='between' style={{ height: '80px' }}>*/}
                      {/*<i className="iconfont iconiconfront-20 user-icon"></i>*/}
wangshuo committed
461

xuzhenghua committed
462 463 464 465
                      {/*<Flex direction='column' justify='between' align='start' className="order-cell">*/}
                        {/*<div className="name">{`姓名:${perfect.real_name}`}</div>*/}
                        {/*<div>{`电话:${perfect.cellphone}`}</div>*/}
                      {/*</Flex>*/}
wangshuo committed
466

xuzhenghua committed
467 468 469
                      {/*<Flex align='start' className="order-cell">*/}
                        {/*<div>{`QQ:${perfect.qq}`}</div>*/}
                      {/*</Flex>*/}
wangshuo committed
470

xuzhenghua committed
471 472 473 474 475
                    {/*</Flex>*/}
                  {/*</Link>*/}
                {/*</WingBlank>*/}
              {/*</div>*/}
            {/*}*/}
wangshuo committed
476
            <div className="order-list">
477 478 479 480 481
              <OrderList
                list={orderList}
                compute={this.computedMoney}
                courseId={getParam('id')}
                locationState={this.props.location.state}
482
                groupPrice={groupPrice}
483 484
                toDetail={this.toCourseDetail}
              />
wangshuo committed
485 486 487 488 489 490 491 492 493
            </div>
            <div className="order-balance">
              <List>
                <Item
                  className="order-prefer-text"
                >
                  <Flex justify='between'>
                    <Flex align='center'>
                      <span>余额抵扣</span>
wangshuo committed
494
                      <span className="order-balanceprice"> (余额: <i className="order-money">{`${user_account}元`}</i>)</span>
495
                      <i className="iconfont iconiconfront-22 question-mark" onClick={this.showInfo}></i>
wangshuo committed
496
                    </Flex>
497

498
                    <Flex>
499 500
                      {
                        useBalance ? (
501
                          <>
xuzhenghua committed
502
                            <span style={{ color: '#FF2121', fontSize: '15px', marginRight: "6px" }}>{`-¥${offset}`}</span>
503 504 505 506 507
                            <i className={`iconfont icondanseshixintubiao-5 balance-used`} onClick={throttle(this.useBalance, 600)}></i>
                          </>
                        ) : (
                          <i className='circle-icon' onClick={throttle(this.useBalance, 600)}></i>
                        )
508
                      }
509
                    </Flex>
wangshuo committed
510 511 512 513
                  </Flex>
                </Item>
              </List>
            </div>
xuzhenghua committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527
            {
              (full_amount > 0 && cut_amount > 0) ? (
                <>
                <div className="full__rules">
                  <WingBlank>
                    <div className="money__off">
                      <span>{full_amount}{cut_amount}</span>
                      <span className='money'>{`-¥${cut_amount}`}</span>
                    </div>
                  </WingBlank>
                </div>
            </>
              ) : (null)
            }
wangshuo committed
528 529 530 531 532 533 534
            <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>
535
                  <span className="order-amount-price">{`¥${total}`}</span>
baiguangyao committed
536
                </div>
wangshuo committed
537 538 539
                <div className="order-preprice">
                  <span className="order-preprice-title">已优惠:</span>
                  <span className="order-preprice-price">{${discount}`}</span>
baiguangyao committed
540
                </div>
wangshuo committed
541 542 543 544 545 546
              </div>
              {
                perfect ? (

                  <button type="button" className="order-button has-info">
                    <span className="order-button-text" onClick={this.submitOrder}>提交订单</span>
baiguangyao committed
547
                  </button>
wangshuo committed
548
                ) : (
xuzhenghua committed
549

wangshuo committed
550 551 552 553 554 555 556 557
                    <button type="button" className="order-button">
                      <span className="order-button-text" onClick={this.submitOrder}>提交订单</span>
                    </button>
                  )
              }
            </div>
          </Flex.Item>
        </Flex>
wangshuo committed
558 559 560 561 562 563 564 565 566 567 568 569 570
        {
          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>
              </div>
            </div>
          ) : null
        }
wangshuo committed
571 572 573 574
      </div>

    )
  }
.  
baiguangyao committed
575 576 577

}

xuzhenghua committed
578
export default Order;