index.js 19.1 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'>
FE committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92
                            <span 
                              style={{ 
                                color: '#333', 
                                fontSize: '15px' 
                              }}
                            >优惠券</span>
                            <span 
                              style={{ 
                                fontSize: '14px', 
                                color: '#999999' 
                              }}
                            >
                              {!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,
wangshuo committed
123
    };
.  
baiguangyao committed
124
  }
wangshuo committed
125 126
  // 提交订单
  submitOrder = () => {
127
    const {total} = this.state;
wangshuo committed
128
    if (this.state.orderList.length === 0) {
wangshuo committed
129 130 131
      Toast.info('没有要提交的订单!');
      return;
    }
132 133 134
    if (!this.state.perfect) {
      Toast.info('请完善报名信息!');
    }
FE committed
135 136 137 138 139 140 141 142 143 144 145
    const { location: { state ={} } } = this.props;
    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 => {
FE committed
146
        console.log(res);
147 148
        if(res.data.errno === 200) {
          sessionStorage.removeItem('orderUseCacheObj');
149
          if (res.data.data.pay_jump === 1) {
150
            this.props.history.push(`/togroup?id=${res.data.data.oid}`);
151 152
            return;
          }
xuzhenghua committed
153 154
          this.props.history.push(`/payorder?oid=${res.data.data.oid}`, {group: 1});

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

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

wangshuo committed
249 250
  // 公共方法 存储数据
  publicGetData = (res) => {
251
    let { course, total, user_account, user_info, discount } = res;
252
    if(this.props.location.state && this.props.location.state.group === 1) {
253
      total = this.state.groupPrice;
254
    }
wangshuo committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    this.cacheObj = {
      perfect: user_info,
      orderList: course,
      user_account,
      total,
      discount,
    };
    this.setState({
      perfect: user_info,
      orderList: course,
      user_account,
      total,
      discount,
    });
  };
  // 公共方法 本地存储
  publicLocalStorage = () => {
    if(this.props.history.action === 'PUSH') {
      sessionStorage.removeItem('orderUseCacheObj');
FE committed
274 275 276
      this.setState({
        discount: 0.00,
      });
wangshuo committed
277 278 279 280 281 282 283 284 285 286
    } else{
      const cacheObj = sessionStorage.getItem('orderUseCacheObj');
      if(cacheObj !== null) {
        this.setState({
          useBalance: true
        });
        this.computedMoneyByCache();
      }
    }
  };
wangshuo committed
287
  componentDidMount() {
288
    let _this = this;
FE committed
289
    // type: 1,返现课程,simple: 1,正常购买
wangshuo committed
290
    if(this.props.location.state && this.props.location.state.type === 1) {
291
      http.get(`${API['base-api']}/m/order/preorder?type=1`).then((res) => {
wangshuo committed
292
        if(res.data.errno === 200) {
293
          this.publicGetData(res.data.data);
wangshuo committed
294
          this.publicLocalStorage();
wangshuo committed
295 296 297 298
        }else{
          Toast.info(res.data.msg, 2);
        }
      })
wangshuo committed
299
    } else if (this.props.location.state && (this.props.location.state.simple === 1 || this.props.location.state.bargain === 1)) {
300 301 302
      // 普通课程立即报名 不带 type = 1
      http.get(`${API['base-api']}/m/order/preorder`).then(res => {
        if(res.data.errno === 200) {
303
          this.publicGetData(res.data.data);
wangshuo committed
304
          this.publicLocalStorage();
wangshuo committed
305 306 307 308
        }else{
          Toast.info(res.data.msg, 2);
        }
      });
309 310
    } else if (this.props.location.state && this.props.location.state.group === 1){
      // 获取一键开团的课程
311
      Promise.all([http.get(`${API.home}/m/course/detail/${getParam('id')}`),http.get(`${API['base-api']}/m/order/preorder`)]).then(resList => {
312 313
        let courseInfo = resList[0],
            orderInfo = resList[1];
314 315 316
        let newData = {};
        if(orderInfo.data.errno === 200) {
          newData = Object.assign({}, orderInfo.data.data, {course: []});
317
        }else{
318
          Toast.info(orderInfo.data.msg, 2);
319 320
          return;
        }
321 322 323 324 325
        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
          })
326 327 328 329
        }else{
          Toast.info(courseInfo.data.msg, 2);
          return;
        }
330 331 332 333 334
        // 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);
335 336
        this.publicLocalStorage();
      });
wangshuo committed
337
    } else {
338 339 340 341 342 343 344 345 346
      // 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
347

348 349 350 351
      //     this.publicGetData(preorder);
      //     this.publicLocalStorage();
      //   })
      // }else{
wangshuo committed
352
        http.get(`${API['base-api']}/m/order/preorder`).then((res) => {
353
          console.log('为了验证什么条件下会走这段代码');
wangshuo committed
354 355 356 357 358
          if (res.data.errno !== 200) {
            Toast.info(res.data.msg, 2);
            return;
          }

359
          this.publicGetData(res.data.data);
wangshuo committed
360 361
          this.publicLocalStorage();
        })
362
      // }
wangshuo committed
363
    }
xuzhenghua committed
364 365
    // else {
    //   if(getParam('id')) {
xuzhenghua committed
366
    //     Promise.all([http.get(`${API.home}/m/del_cart_order/${getParam('id')}`), http.get(`${API.home}/m/order/preorder`)]).then(res => {
xuzhenghua committed
367 368 369 370 371 372 373 374 375 376 377 378
    //       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
379
    //     http.get(`${API.home}/m/order/preorder`).then((res) => {
xuzhenghua committed
380 381 382 383 384 385 386 387 388 389 390
    //       console.log('为了验证什么条件下会走这段代码');
    //       if (res.data.errno !== 200) {
    //         Toast.info(res.data.msg, 2);
    //         return;
    //       }
    //
    //       this.publicGetData(res);
    //       this.publicLocalStorage();
    //     })
    //   }
    // }
wangshuo committed
391
  };
392 393 394

  toCourseDetail = (id) => {
    const { dispatch, history } = this.props;
xuzhenghua committed
395
    // dispatch(getCourses(id, () => {
396
        history.push(`/detail?id=${id}`)
xuzhenghua committed
397
    // }));
398 399
  }

wangshuo committed
400 401 402
  render() {
    const {
      perfect,
wangshuo committed
403
      orderList,
wangshuo committed
404
      user_account,
405
      total,
wangshuo committed
406 407
      discount,
      useBalance,
wangshuo committed
408 409
      info,
      offset,
410
      groupPrice
wangshuo committed
411 412 413 414 415 416
    } = this.state;

    return (
      <div className="order-wrapper">
        <Flex>
          <Flex.Item>
417
            <HeaderBar title='课程报名' arrow={true} />
wangshuo committed
418 419
            {
              !this.state.perfect &&
420 421
              <Link to={{
                pathname: '/orderinfo',
wangshuo committed
422
                state: {type: this.props.location.state ? this.props.location.state.type : null, id: getParam('id')}
423 424
              }}
              className="order-information">
425
                <i className="iconfont iconiconfront-6 order-addsize" />
wangshuo committed
426
                <div className="order-infotext">完善报名信息</div>
427
                <i className="iconfont iconiconfront-70 order-next" />
wangshuo committed
428 429 430 431 432 433
              </Link>
            }
            {
              this.state.perfect &&
              <div className="order-information2">
                <WingBlank>
wangshuo committed
434 435
                  <Link to={{
                    pathname: '/orderinfo',
436 437
                    state: {
                      ...this.state.perfect,
438
                      ...this.props.location.state,
439 440
                      id: getParam('id')
                    }
wangshuo committed
441 442
                  }
                  } >
443 444
                    <Flex align='center' justify='between' style={{ height: '80px' }}>
                      <i className="iconfont iconiconfront-20 user-icon"></i>
wangshuo committed
445

446 447 448 449
                      <Flex direction='column' justify='between' align='start' className="order-cell">
                        <div className="name">{`姓名:${perfect.real_name}`}</div>
                        <div>{`电话:${perfect.cellphone}`}</div>
                      </Flex>
wangshuo committed
450

451 452 453
                      <Flex align='start' className="order-cell">
                        <div>{`QQ:${perfect.qq}`}</div>
                      </Flex>
wangshuo committed
454

455
                    </Flex>
wangshuo committed
456
                  </Link>
wangshuo committed
457 458 459 460
                </WingBlank>
              </div>
            }
            <div className="order-list">
461 462 463 464 465
              <OrderList
                list={orderList}
                compute={this.computedMoney}
                courseId={getParam('id')}
                locationState={this.props.location.state}
466
                groupPrice={groupPrice}
467 468
                toDetail={this.toCourseDetail}
              />
wangshuo committed
469 470 471 472 473 474 475 476 477
            </div>
            <div className="order-balance">
              <List>
                <Item
                  className="order-prefer-text"
                >
                  <Flex justify='between'>
                    <Flex align='center'>
                      <span>余额抵扣</span>
wangshuo committed
478
                      <span className="order-balanceprice"> (余额: <i className="order-money">{`${user_account}元`}</i>)</span>
479
                      <i className="iconfont iconiconfront-22 question-mark" onClick={this.showInfo}></i>
wangshuo committed
480
                    </Flex>
481

482
                    <Flex>
483 484
                      {
                        useBalance ? (
485
                          <>
xuzhenghua committed
486
                            <span style={{ color: '#FF2121', fontSize: '15px', marginRight: "6px" }}>{`-¥${offset}`}</span>
487 488 489 490 491
                            <i className={`iconfont icondanseshixintubiao-5 balance-used`} onClick={throttle(this.useBalance, 600)}></i>
                          </>
                        ) : (
                          <i className='circle-icon' onClick={throttle(this.useBalance, 600)}></i>
                        )
492
                      }
493
                    </Flex>
wangshuo committed
494 495 496 497 498 499 500 501 502 503 504
                  </Flex>
                </Item>
              </List>
            </div>
            <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>
505
                  <span className="order-amount-price">{`¥${total}`}</span>
baiguangyao committed
506
                </div>
wangshuo committed
507 508 509
                <div className="order-preprice">
                  <span className="order-preprice-title">已优惠:</span>
                  <span className="order-preprice-price">{${discount}`}</span>
baiguangyao committed
510
                </div>
wangshuo committed
511 512 513 514 515 516
              </div>
              {
                perfect ? (

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

wangshuo committed
520 521 522 523 524 525 526 527
                    <button type="button" className="order-button">
                      <span className="order-button-text" onClick={this.submitOrder}>提交订单</span>
                    </button>
                  )
              }
            </div>
          </Flex.Item>
        </Flex>
wangshuo committed
528 529 530 531 532 533 534 535 536 537 538 539 540
        {
          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
541 542 543 544
      </div>

    )
  }
.  
baiguangyao committed
545 546 547 548

}

export default Order;