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

import "./order.scss"

const Item = List.Item;

function OrderList(props) {
  const listData = props.list;
  return (
xuzhenghua committed
16
    <div>
.  
baiguangyao committed
17 18
      {
        listData.map((item, index) => {
19
          console.log(item);
20
          const { is_coupon, course_id, image_name, price1, price0, simpledescription, course_title, coupon_num, coupon_desc } = item;
.  
baiguangyao committed
21 22
          const Info = (
            <div className="order-info">
23 24
              <Link to={`/detail?id=${course_id}`}><p className='order-title' style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{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>
.  
baiguangyao committed
25
              <p className='order-des'>
26 27 28
                {
                  props.courseId === undefined ? (<span className='order-newprice'>¥{price1}</span>) : (<span className='order-newprice'>¥{item.pdd_group_info.price}</span>)
                }
29
                <span className='order-price'>¥{price0}</span>
.  
baiguangyao committed
30 31 32
              </p>
            </div>
          )
xuzhenghua committed
33

.  
baiguangyao committed
34
          return (
35 36 37 38 39 40 41 42 43
            <OrderItem {...item} src={image_name} id={course_id} key={index} info={Info}>
              {
                is_coupon === 1 ? (
                  <div className="order-prefer">
                    <List key={index}>
                      <Item
                        arrow="horizontal"
                        onClick={() => { }}
                      >
44
                        <Link to={{
45 46
                                pathname: `/coupons`,
                                search: `?id=${course_id}`,
47 48
                                state: {
                                    from: '/order',
49
                                    a: 'sldfasldjfsl'
50
                              }}}>
51
                          {/* <Link to='coupons' query={{id: course_id}} state={{from: '/order'}}> */}
52 53 54 55 56 57 58 59 60 61 62
                          <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
              }

.  
baiguangyao committed
63 64 65 66 67 68 69 70 71 72 73 74
            </OrderItem>
          )
        })
      }
    </div>
  );
}

class Order extends Component {
  constructor(props) {
    super(props);
    this.state = {
wangshuo committed
75
      perfect: this.props.location.state,
wangshuo committed
76
      user_account: 0.00, // 账户余额
77
      total: 0.00, // 需要支付总金额
wangshuo committed
78
      discount: 0.00, // 
wangshuo committed
79 80
      useBalance: false,
      orderList: [],
wangshuo committed
81 82
      info: false,
      offset: 0,
.  
baiguangyao committed
83 84
    }
  }
wangshuo committed
85 86
  // 提交订单
  submitOrder = () => {
87
    if (this.state.orderList.lengtjh === 0) {
wangshuo committed
88 89 90
      Toast.info('没有要提交的订单!');
      return;
    }
91 92 93
    if (!this.state.perfect) {
      Toast.info('请完善报名信息!');
    }
94 95 96 97 98

    http.post(`${api.home}/m/order/submitOrder`, { is_deduction: this.state.useBalance }).then(res => {
      if(res.data.code !== 200) {
        return;
      }
99 100
      sessionStorage.removeItem('orderUseCacheObj');
      this.props.history.replace(`/payorder?oid=${res.data.data.order_id}`);
101
    });
.  
baiguangyao committed
102
  }
103
  // 勾选取消勾选 是否使用余额
wangshuo committed
104
  useBalance = () => {
wangshuo committed
105 106 107 108
    let useBalanceFlag = this.state.useBalance;
    this.setState({
      useBalance: !useBalanceFlag
    });
109
    if (!useBalanceFlag) {
wangshuo committed
110 111 112
      this.cacheObj = {
        ...this.state
      }
113 114 115
      sessionStorage.setItem('orderUseCacheObj', JSON.stringify(this.state));
    }else{
      sessionStorage.removeItem('orderUseCacheObj');
wangshuo committed
116
    }
117 118 119 120
    this.computedMoney(useBalanceFlag);
  }
  // 勾选取消勾选时:计算金额、优惠金额、优惠券等
  computedMoney = (useBalanceFlag) => {
121 122
    let totalSale = parseFloat(this.cacheObj.total),
      userAccount = parseFloat(this.cacheObj.user_account);
123
    const { discount } = this.state;
124 125
    if (!useBalanceFlag) {
      if (totalSale > userAccount) {
wangshuo committed
126 127
        this.setState({
          offset: userAccount.toFixed(2),
128
          total: (totalSale - userAccount).toFixed(2),
129
          discount: (userAccount + parseFloat(discount)).toFixed(2),
wangshuo committed
130
        });
131
      } else {
wangshuo committed
132 133
        this.setState({
          offset: totalSale.toFixed(2),
134
          total: 0,
135
          discount: (totalSale + parseFloat(discount)).toFixed(2),
wangshuo committed
136 137
        });
      }
138
    } else {
wangshuo committed
139 140
      this.setState({
        offset: this.cacheObj.offset,
141
        total: this.cacheObj.total,
wangshuo committed
142 143 144
        discount: this.cacheObj.discount,
      });
    }
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  };
  // 选择优惠券返回时根据是否勾选计算
  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
164 165 166
  };
  // 展示余额抵扣规则
  showInfo = () => {
167
    this.setState((prevState) => ({
wangshuo committed
168 169
      info: !prevState.info
    }));
wangshuo committed
170 171
  };
  componentDidMount() {
172 173
    let _this = this;
    if(getParam('id') !== undefined) {
174
      Promise.all([http.get(`${api.home}/m/del_cart_order/${getParam('id')}`), http.get(`${api.home}/m/order/preorder`), http.get(`${api.home}/m/course/detail/${getParam('id')}`)]).then(res => {
175

176 177
        let preorder = res[1],
          detail = res[2],
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
          orderList = [];
          console.log(detail);
          if(preorder.data.code !== 200) {
            Toast.info(preorder.data.msg, 2);
            return;
          }
          if(detail.data.code !== 200) {
            Toast.info(preorder.data.msg, 2);
            return;
          }
          const { user_info, user_account, discount } = preorder.data.data;
          const { course_info } = detail.data.data;
          orderList.push(course_info);
          this.cacheObj = {
            perfect: user_info,
            orderList: orderList,
            user_account,
            total: course_info.pdd_group_info.price,
            discount,
          }
          _this.setState({
            perfect: user_info,
            orderList: orderList,
            user_account,
            total: course_info.pdd_group_info.price,
            discount,
          });
      })
    }else{

wangshuo committed
208
    http.get(`${api.home}/m/order/preorder`).then((res) => {
wangshuo committed
209
      if (res.data.code !== 200) {
wangshuo committed
210 211
        return;
      }
212
      const { course, total, user_account, user_info, discount } = res.data.data;
213 214 215 216 217 218 219
      this.cacheObj = {
        perfect: user_info,
        orderList: course,
        user_account,
        total,
        discount,
      }
wangshuo committed
220 221
      this.setState({
        perfect: user_info,
222
        orderList: course,
wangshuo committed
223
        user_account,
224
        total,
wangshuo committed
225
        discount,
wangshuo committed
226
      });
227 228
      if(this.props.history.action === 'PUSH') {
        sessionStorage.removeItem('orderUseCacheObj');
229
      } else{
230 231 232 233 234 235 236 237
        const cacheObj = sessionStorage.getItem('orderUseCacheObj');
        if(cacheObj !== null) {
          this.setState({
            useBalance: true
          });
          this.computedMoneyByCache();
        }
      }
wangshuo committed
238
    })
239
    }
wangshuo committed
240 241 242 243
  };
  render() {
    const {
      perfect,
wangshuo committed
244
      orderList,
wangshuo committed
245
      user_account,
246
      total,
wangshuo committed
247 248
      discount,
      useBalance,
wangshuo committed
249 250
      info,
      offset,
wangshuo committed
251 252 253 254 255 256
    } = this.state;

    return (
      <div className="order-wrapper">
        <Flex>
          <Flex.Item>
wangshuo committed
257
            <HeaderBar title='课程报名' arrow={true}></HeaderBar>
wangshuo committed
258 259 260 261 262 263 264 265 266 267 268 269
            {
              !this.state.perfect &&
              <Link to='/orderinfo' className="order-information">
                <i className="iconfont iconiconfront-6 order-addsize"></i>
                <div className="order-infotext">完善报名信息</div>
                <i className="iconfont iconiconfront-70 order-next"></i>
              </Link>
            }
            {
              this.state.perfect &&
              <div className="order-information2">
                <WingBlank>
wangshuo committed
270 271 272 273 274
                  <Link to={{
                    pathname: '/orderinfo',
                    state: this.state.perfect
                  }
                  } >
275 276
                    <Flex align='center' justify='between' style={{ height: '80px' }}>
                      <i className="iconfont iconiconfront-20 user-icon"></i>
wangshuo committed
277

278 279 280 281
                      <Flex direction='column' justify='between' align='start' className="order-cell">
                        <div className="name">{`姓名:${perfect.real_name}`}</div>
                        <div>{`电话:${perfect.cellphone}`}</div>
                      </Flex>
wangshuo committed
282

283 284 285
                      <Flex align='start' className="order-cell">
                        <div>{`QQ:${perfect.qq}`}</div>
                      </Flex>
wangshuo committed
286

287
                    </Flex>
wangshuo committed
288
                  </Link>
wangshuo committed
289 290 291 292
                </WingBlank>
              </div>
            }
            <div className="order-list">
293
              <OrderList list={orderList} compute={this.computedMoney} courseId={getParam('id')} />
wangshuo committed
294 295 296 297 298 299 300 301 302
            </div>
            <div className="order-balance">
              <List>
                <Item
                  className="order-prefer-text"
                >
                  <Flex justify='between'>
                    <Flex align='center'>
                      <span>余额抵扣</span>
wangshuo committed
303
                      <span className="order-balanceprice"> (余额: <i className="order-money">{`${user_account}元`}</i>)</span>
304
                      <i className="iconfont iconiconfront-22 question-mark" onClick={this.showInfo}></i>
wangshuo committed
305
                    </Flex>
306

307
                    <Flex>
308 309
                      {
                        useBalance ? (
310 311 312 313 314 315 316
                          <>
                            <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>
                        )
317
                      }
318
                    </Flex>
wangshuo committed
319 320 321 322 323 324 325 326 327 328 329
                  </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>
330
                  <span className="order-amount-price">{`¥${total}`}</span>
baiguangyao committed
331
                </div>
wangshuo committed
332 333 334
                <div className="order-preprice">
                  <span className="order-preprice-title">已优惠:</span>
                  <span className="order-preprice-price">{${discount}`}</span>
baiguangyao committed
335
                </div>
wangshuo committed
336 337 338 339 340 341
              </div>
              {
                perfect ? (

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

wangshuo committed
345 346 347 348 349 350 351 352
                    <button type="button" className="order-button">
                      <span className="order-button-text" onClick={this.submitOrder}>提交订单</span>
                    </button>
                  )
              }
            </div>
          </Flex.Item>
        </Flex>
wangshuo committed
353 354 355 356 357 358 359 360 361 362 363 364 365
        {
          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
366 367 368 369
      </div>

    )
  }
.  
baiguangyao committed
370 371 372 373

}

export default Order;