index.js 10.5 KB
Newer Older
xuzhenghua committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 44 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 71 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
import React, { Component } from 'react'
import { Flex, List, Toast } from 'antd-mobile'
import { OrderItem } from '@common/index'
import { Link } from 'react-router-dom'
import { http, getParam } from "@/utils"
import { HeaderBar } from '@common/index'


import "./index.scss"

const Item = List.Item

function OrderList(props) {
  const listData = props.list
  return (
    <div>
      {
        listData.map((item, index) => {
          const {is_coupon, course_id, image_name, sale_price, simpledescription, course_title, coupon_num, coupon_desc} = item
          let NewPrice = (<span className='order-newprice'>¥{sale_price}</span>)
          if (props.locationState && props.locationState.group) {
            NewPrice = (<span className='order-newprice'>¥{props.groupPrice}</span>)
          }
          const Info = (
            <div className="order-info">
              <p
                className='order-title'
                style={{
                  overflow: 'hidden',
                  textOverflow: 'ellipsis',
                  whiteSpace: 'nowrap'
                }}
                onClick={() => props.toDetail(course_id)}
              >
                {course_title}
              </p>
              <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'>
                {NewPrice}
                <span className={'price-des'}>(预付定金)</span>
              </p>
            </div>
          )

          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'
                          }
                        }}>
                          <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>
          )
        })
      }
    </div>
  )
}

class Order extends Component {
  constructor(props) {
    super(props)
    this.state = {
      groupPrice: '',
      total: 0.00, // 需要支付总金额
      discount: 0.00, //
      useBalance: false,
      orderList: [],
      info: false,
      offset: 0,
      depositPrice: 0,
      course_id: getParam('oid'),
      user_account: '',
      finalStartTime: '',
    }
  }

  // 提交订单
  submitOrder = () => {
    http.post(`${API["base-api"]}/m/deposit/create`, {
      plat_form: 5,
      source: getParam('source'),   //来源 1-详情页 2-活动页,
      course_id: this.state.course_id,
      is_deduction: this.state.useBalance ? 1 : 0
    }).then(res => {
      const {data} = res
      if (data.errno == 200) {
        if (data.data['pay_jump']) {
          this.props.history.replace(`/expand/callback?order_id=${data.data['order_id']}`)
        } else {
          this.props.history.replace({
            pathname: '/deposit-pay-order',
            search: `?oid=${data.data['order_id']}`
          })
        }
      } else {
        Toast.info(data.msg)
      }
    })
  }

  showInfo = () => {
    this.setState((prevState) => ({
      info: !prevState.info
    }))
  }


  componentDidMount() {
    http.post(`${API["base-api"]}/m/deposit/preorder`, {
      course_id: this.state.course_id
    })
      .then(res => {
        const {data} = res
        if (data.errno == 200) {
          this.setState({
            orderList: [data.data.course],
            depositPrice: data.data.course['sale_price'],
            user_account: data.data['user_account'],
            finalStartTime: data.data['final_start_time']
          })
        } else {
          Toast.info(data.msg)
        }
      })
  };

  toCourseDetail = (id) => {
    const {history} = this.props
    history.push(`/detail?id=${id}`)
  }

  useBalance = () => {
    let {user_account, depositPrice} = this.state
    if (parseFloat(user_account) === 0) {
      return
    }
    let offset = parseFloat(user_account) - parseFloat(depositPrice)
    offset = offset > 0 ? depositPrice : user_account
    this.setState(prevState => ({useBalance: !prevState.useBalance, offset}))
  }

  updateUserAccount = () => {

  }

  render() {
    const {
      orderList,
      discount,
      groupPrice,
      depositPrice,
      user_account,
      useBalance,
      info,
      offset,
      finalStartTime
    } = this.state

    return (
      <div className="order-wrapper">
        <Flex>
          <Flex.Item>
            <HeaderBar title='课程报名' arrow={true}/>
            <div className="order-list">
              <OrderList
                list={orderList}
                courseId={getParam('id')}
                locationState={this.props.location.state}
                groupPrice={groupPrice}
                toDetail={this.toCourseDetail}
              />
            </div>

            <div className="order-balance">
              <List>
                <Item
                  className="order-prefer-text"
                >
                  <Flex justify='between'>
                    <Flex align='center'>
                      <span>余额抵扣</span>
                      <span className="order-balanceprice"> (余额: <i
                        className="order-money">{`${user_account}元`}</i>)</span>
                      <i className="iconfont iconiconfront-22 question-mark"
                         onClick={this.showInfo}></i>
                    </Flex>
                    <Flex>
                      {
                        useBalance ? (
                          <>
                            <span style={{
                              color: '#FF2121',
                              fontSize: '15px',
                              marginRight: "6px"
                            }}>{`-¥${offset}`}</span>
                            <i className={`iconfont icondanseshixintubiao-5 balance-used`}
                               onClick={this.useBalance}></i>
                          </>
                        ) : (
                          <i className='circle-icon' onClick={this.useBalance}></i>
                        )
                      }
                    </Flex>
                  </Flex>
                </Item>
              </List>
            </div>

            <ul className={'deposit-limit-time'}>
              <li>· {finalStartTime}开始支付尾款</li>
              <li>· 代金券只能在支付尾款时使用</li>
            </ul>
            <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>
                  <span className="order-amount-price">{`¥${depositPrice}`}</span>
                </div>
                <div className="order-preprice">
                  <span className="order-preprice-title">已优惠:</span>
                  <span className="order-preprice-price">{${discount}`}</span>
                </div>
              </div>
              <button type="button" className="order-button has-info">
                <span className="order-button-text" onClick={this.submitOrder}>确定订单</span>
              </button>
            </div>
          </Flex.Item>
        </Flex>
        {
          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
        }
      </div>

    )
  }

}

export default Order