index.js 11.2 KB
Newer Older
zhanghaozhe committed
1 2 3 4
import React, { Component } from "react"
import { Flex, List, Toast } from "antd-mobile"
import { OrderItem } from "src/common/index"
import { Link } from "react-router-dom"
zhanghaozhe committed
5
import { http, getParam } from "src/utils"
zhanghaozhe committed
6
import { HeaderBar } from "src/common/index"
xuzhenghua committed
7 8 9 10 11 12 13 14 15

import "./index.scss"

const Item = List.Item

function OrderList(props) {
  const listData = props.list
  return (
    <div>
zhanghaozhe committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
      {listData.map((item, index) => {
        const {
          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)}
xuzhenghua committed
40
            >
zhanghaozhe committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
              {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>
        )
xuzhenghua committed
62

zhanghaozhe committed
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
        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>
        )
      })}
xuzhenghua committed
117 118 119 120 121 122 123 124
    </div>
  )
}

class Order extends Component {
  constructor(props) {
    super(props)
    this.state = {
zhanghaozhe committed
125 126 127
      groupPrice: "",
      total: 0.0, // 需要支付总金额
      discount: 0.0, //
xuzhenghua committed
128 129 130 131 132
      useBalance: false,
      orderList: [],
      info: false,
      offset: 0,
      depositPrice: 0,
zhanghaozhe committed
133 134 135
      course_id: getParam("oid"),
      user_account: "",
      finalStartTime: "",
xuzhenghua committed
136 137 138 139 140
    }
  }

  // 提交订单
  submitOrder = () => {
zhanghaozhe committed
141 142 143 144 145 146 147 148 149
    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
zhanghaozhe committed
150
        if (data.errno === 200) {
zhanghaozhe committed
151 152 153 154 155 156 157 158 159 160
          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"]}`,
            })
          }
xuzhenghua committed
161
        } else {
zhanghaozhe committed
162
          Toast.info(data.msg)
xuzhenghua committed
163
        }
zhanghaozhe committed
164
      })
xuzhenghua committed
165 166 167 168
  }

  showInfo = () => {
    this.setState((prevState) => ({
zhanghaozhe committed
169
      info: !prevState.info,
xuzhenghua committed
170 171 172 173
    }))
  }

  componentDidMount() {
zhanghaozhe committed
174 175 176 177 178 179
    http
      .post(`${API["base-api"]}/m/deposit/preorder`, {
        course_id: this.state.course_id,
      })
      .then((res) => {
        const { data } = res
zhanghaozhe committed
180
        if (data.errno === 200) {
xuzhenghua committed
181 182
          this.setState({
            orderList: [data.data.course],
zhanghaozhe committed
183 184 185
            depositPrice: data.data.course["sale_price"],
            user_account: data.data["user_account"],
            finalStartTime: data.data["final_start_time"],
xuzhenghua committed
186 187 188 189 190
          })
        } else {
          Toast.info(data.msg)
        }
      })
zhanghaozhe committed
191
  }
xuzhenghua committed
192 193

  toCourseDetail = (id) => {
zhanghaozhe committed
194
    const { history } = this.props
xuzhenghua committed
195 196 197 198
    history.push(`/detail?id=${id}`)
  }

  useBalance = () => {
zhanghaozhe committed
199
    let { user_account, depositPrice } = this.state
xuzhenghua committed
200 201 202 203 204
    if (parseFloat(user_account) === 0) {
      return
    }
    let offset = parseFloat(user_account) - parseFloat(depositPrice)
    offset = offset > 0 ? depositPrice : user_account
zhanghaozhe committed
205 206 207 208
    this.setState((prevState) => ({
      useBalance: !prevState.useBalance,
      offset,
    }))
xuzhenghua committed
209 210
  }

zhanghaozhe committed
211
  updateUserAccount = () => {}
xuzhenghua committed
212 213 214 215 216 217 218 219 220 221 222

  render() {
    const {
      orderList,
      discount,
      groupPrice,
      depositPrice,
      user_account,
      useBalance,
      info,
      offset,
zhanghaozhe committed
223
      finalStartTime,
xuzhenghua committed
224 225 226 227 228 229
    } = this.state

    return (
      <div className="order-wrapper">
        <Flex>
          <Flex.Item>
zhanghaozhe committed
230
            <HeaderBar title="课程报名" arrow={true} />
xuzhenghua committed
231 232 233
            <div className="order-list">
              <OrderList
                list={orderList}
zhanghaozhe committed
234
                courseId={getParam("id")}
xuzhenghua committed
235 236 237 238 239 240 241 242
                locationState={this.props.location.state}
                groupPrice={groupPrice}
                toDetail={this.toCourseDetail}
              />
            </div>

            <div className="order-balance">
              <List>
zhanghaozhe committed
243 244 245
                <Item className="order-prefer-text">
                  <Flex justify="between">
                    <Flex align="center">
xuzhenghua committed
246
                      <span>余额抵扣</span>
zhanghaozhe committed
247 248 249 250 251 252 253 254 255
                      <span className="order-balanceprice">
                        {" "}
                        (余额:{" "}
                        <i className="order-money">{`${user_account}元`}</i>)
                      </span>
                      <i
                        className="iconfont iconiconfront-22 question-mark"
                        onClick={this.showInfo}
                      ></i>
xuzhenghua committed
256 257
                    </Flex>
                    <Flex>
zhanghaozhe committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
                      {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>
                      )}
xuzhenghua committed
278 279 280 281 282 283
                    </Flex>
                  </Flex>
                </Item>
              </List>
            </div>

zhanghaozhe committed
284
            <ul className={"deposit-limit-time"}>
xuzhenghua committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
              <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">
zhanghaozhe committed
303 304 305
                <span className="order-button-text" onClick={this.submitOrder}>
                  确定订单
                </span>
xuzhenghua committed
306 307 308 309
              </button>
            </div>
          </Flex.Item>
        </Flex>
zhanghaozhe committed
310 311 312 313
        {info ? (
          <div
            style={{
              position: "fixed",
xuzhenghua committed
314 315
              top: 0,
              left: 0,
zhanghaozhe committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
              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>
xuzhenghua committed
369
            </div>
zhanghaozhe committed
370 371
          </div>
        ) : null}
xuzhenghua committed
372 373 374 375 376 377
      </div>
    )
  }
}

export default Order