singleSuccess.js 9.3 KB
Newer Older
zhanghaozhe committed
1 2 3 4
import React, { Component } from "react"
import "./index.scss"
import { getParam, http, browser } from "src/utils"
import { Toast } from "antd-mobile"
zhanghaozhe committed
5
import { withRouter } from "react-router-dom"
zhanghaozhe committed
6 7 8 9 10 11 12
import { compose } from "redux"
import { connect } from "react-redux"
import {
  differenceInHours,
  differenceInMinutes,
  differenceInSeconds,
} from "date-fns"
FE committed
13 14

class Single extends Component {
zhanghaozhe committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28
  constructor(props) {
    super(props)
    this.state = {
      // status: 1,
      orderId: "",
      nowPrice: "",
      laterPrice: "",
      hour: "",
      min: "",
      sec: "",
      endTime: "",
      groupOrderId: "",
      payType: "0", // 1支付宝 0微信
      // singleBox: false,
FE committed
29
    }
zhanghaozhe committed
30
  }
FE committed
31

zhanghaozhe committed
32
  componentDidMount() {}
wangshuo committed
33

zhanghaozhe committed
34 35 36 37 38 39
  componentWillReceiveProps(nextProps, nextContext) {
    // console.log(nextProps);
    // this.setState({
    //     singleBox: nextProps.singleBox
    // })
  }
FE committed
40

zhanghaozhe committed
41 42 43 44 45 46
  // 选择支付方式
  check = (type) => {
    this.setState({
      payType: type,
    })
  }
FE committed
47

zhanghaozhe committed
48 49 50 51 52
  // 确定购买
  toBuy = () => {
    const videoID = this.props.data.video_id || this.props.data.id
    http.get(`${API.home}/sys/createClassOrder/${videoID}`).then((res) => {
      if (res.data.code === 200) {
FE committed
53
        this.setState({
zhanghaozhe committed
54
          orderId: res.data.data.order_id,
FE committed
55
        })
zhanghaozhe committed
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
        this.state.payType === "1"
          ? this.alipayPay(res.data.data.order_id)
          : this.weixinPay(res.data.data.order_id)
      } else {
        Toast.info(res.data.msg, 2)
      }
    })
  }
  // 微信支付
  weixinPay = (orderId) => {
    // 微信内部-支付
    if (browser.isWeixin) {
      window.location.href =
        "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx23dac6775ac82877&redirect_uri=" +
        encodeURIComponent(
          window.location.href + "&aa=bb&oid=" + orderId
        ).toLowerCase() +
        "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
    } else {
      // 微信外部-支付
      http
        .get(`${API["base-api"]}/pay/wxpay/wap_charge/oid/${orderId}`)
        .then((res) => {
          if (res.data.errno === 0) {
            window.location.href =
              res.data.data.url +
              "&redirect_url=" +
              encodeURIComponent(
                window.location.href + "&weixinpay=1&oid=" + orderId
              ).toLowerCase()
          } else {
            Toast.info(res.data.msg, 2)
          }
FE committed
89 90
        })
    }
zhanghaozhe committed
91 92 93 94 95 96 97 98 99 100
  }
  // 支付宝支付
  alipayPay = (orderId) => {
    const courseId = getParam("id")
    http
      .get(`${API["base-api"]}/pay/alipay/wap_charge_new/oid/${orderId}`)
      .then((res) => {
        if (res.data.errno === 0) {
          window.location = res.data.data.url
          courseId && window.localStorage.setItem("payCourse", courseId)
FE committed
101
        } else {
zhanghaozhe committed
102
          Toast.info(res.data.msg, 2)
FE committed
103
        }
zhanghaozhe committed
104 105
      })
  }
FE committed
106

zhanghaozhe committed
107 108 109 110 111 112 113 114 115 116 117 118
  // 特价购买全集
  toBuyAll = (vcourseId) => {
    if (Number(this.props.isPdd) === 1) {
      this.props.history.push(`/order?id=${this.props.courseId}`, { group: 1 })
    } else {
      let cidArr = JSON.stringify([Number(vcourseId)])
      http
        .get(`${API["base-api"]}/m/cart/addtopreorder/${cidArr}`)
        .then((res) => {
          if (res.data.errno === 0) {
            this.props.history.push("/order?id=" + res.data.data[0], {
              simple: 1,
wangshuo committed
119
            })
zhanghaozhe committed
120 121 122
          } else {
            Toast.info(res.data.msg, 2)
          }
FE committed
123 124
        })
    }
zhanghaozhe committed
125 126 127 128 129 130 131 132 133 134
  }
  // 0元购买全集
  zerobuyReceive = () => {
    Toast.success("购买全集成功", 3)
    this.colse()
  }
  // 0元参团
  zerogroupBuy = () => {
    this.getOrderId()
  }
FE committed
135

zhanghaozhe committed
136 137 138 139
  // 获取订单号-0元参团
  getOrderId = () => {
    let data = {
      course_id: getParam("id"),
FE committed
140
    }
zhanghaozhe committed
141 142 143 144
    http.post(`${API["base-api"]}/pdd/sys`, data).then((res) => {
      if (res.data.errno === 0) {
        this.setState({
          groupOrderId: res.data.data.order_id,
FE committed
145
        })
zhanghaozhe committed
146 147 148 149 150 151
        this.getOrderStatus(res.data.data.order_id)
      } else {
        Toast.info(res.data.msg, 2)
      }
    })
  }
FE committed
152

zhanghaozhe committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  // 获取订单状态-0元参团
  getOrderStatus = (id) => {
    http.get(`${API.home}/m/pdd/order_status/${id}`).then((res) => {
      if (res.data.code === 200) {
        this.getTime(res.data.data.pdd_order_id)
      } else {
        Toast.info(res.data.msg, 2)
      }
    })
  }
  // 获取倒计时-0元参团
  getTime = (id) => {
    http.get(`${API.home}/m/pdd_order_end_time/${id}`).then((res) => {
      if (res.data.code === 200) {
        this.setState({
          endTime: res.data.data.end_time,
          status: 7,
        })
      } else {
        Toast.info(res.data.msg, 2)
      }
    })
  }
FE committed
176

zhanghaozhe committed
177 178 179 180 181 182 183 184 185
  // 邀请好友参团
  toGroup = () => {
    this.props.history.push(`/togroup?id=${this.state.groupOrderId}`)
  }

  toLearn = (vcourseId, videoID) => {
    this.props.boxHide(false)
    this.props.history.push(`/play/video?id=${vcourseId}&video_id=${videoID}`)
  }
wangshuo committed
186

zhanghaozhe committed
187 188 189 190 191 192 193
  // 关闭弹窗
  colse = () => {
    this.props.boxHide(false)
    this.setState({
      status: 1,
    })
  }
wangshuo committed
194

zhanghaozhe committed
195 196 197 198 199 200 201 202 203
  render() {
    if (this.state.endTime) {
      let date = this.state.endTime * 1000
      let now = Date.now()
      setInterval(() => {
        date -= 1000
        let s = differenceInSeconds(new Date(date), now) % 60,
          m = differenceInMinutes(new Date(date), now) % 60,
          h = differenceInHours(new Date(date), now) % 24
FE committed
204
        this.setState({
zhanghaozhe committed
205 206 207
          hour: h,
          min: m,
          sec: s,
FE committed
208
        })
zhanghaozhe committed
209
      }, 1000)
FE committed
210
    }
zhanghaozhe committed
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
    const { singleType } = this.props
    // console.log(this.props);
    const videoID = this.props.data.video_id || this.props.data.id
    return (
      <div className="popup-box">
        {singleType === 2 && (
          <div className="content payment-success">
            <div className="header">
              <i className="iconfont icondanseshixintubiao-5"></i>
              <span>购买成功</span>
            </div>
            <div className="dec">
              · 3天内购买全集,可直接抵扣该集费用,{this.props.nowPrice}元购买。
            </div>
            <div className="dec">
              · 超过3天,按照未购集数/全部集数等比例计费,
              {this.props.laterPrice}元购买全集。
            </div>
            <span
              onClick={() => this.toLearn(this.props.vcourseId, videoID)}
              className="btn btn-18B4ED"
            >
              开始学习
            </span>
            <div
              className="btn btn-FF4000"
              onClick={() => {
                this.toBuyAll(this.props.courseId)
              }}
            >
              {this.props.nowPrice}购买全集
            </div>
          </div>
        )}
        {singleType === 3 && (
          <div className="content zero">
            <div className="header">
              <i className="iconfont icondanseshixintubiao-5"></i>
              <span>购买成功</span>
            </div>
            <div className="dec">· 恭喜您获得0元拼团购买剩余课时的机会。</div>
            <div className="btn btn-FF4000" onClick={this.zerogroupBuy}>
              0元参团
            </div>
          </div>
        )}
        {singleType === 4 && (
          <div className="content zero">
            <div className="header">
              <i className="iconfont icondanseshixintubiao-5"></i>
              <span>购买成功</span>
            </div>
            <div className="dec">· 恭喜您获得0元购买剩余课时的机会。</div>
            <div className="btn btn-FF4000" onClick={this.zerobuyReceive}>
              0元购
            </div>
          </div>
        )}
        {singleType === 6 && (
          <div className="content zero">
            <div className="header">
              <i className="iconfont icondanseshixintubiao-5"></i>
              <span>购买成功</span>
            </div>
            {/*<Link to={`/play/video?id=${this.props.vcourseId}&video_id=${videoID}`} className='btn btn-18B4ED'>去学习</Link>*/}
            <span
              onClick={() => this.toLearn(this.props.vcourseId, videoID)}
              className="btn btn-18B4ED"
            >
              去学习
            </span>
          </div>
        )}
        {singleType === 7 && (
          <div className="content group">
            <div className="header">
              <i className="iconfont icondanseshixintubiao-5"></i>
              <span>参团成功</span>
            </div>
            <div className="group-img">
              <img src={this.props.user.data.avatar} alt="" />
              <img
                src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/images/weekend/train7/ellipsis.png"
                alt=""
              />
              <img
                src="https://julyedu-cdn.oss-cn-beijing.aliyuncs.com/images/weekend/train7/ellipsis.png"
                alt=""
              />
            </div>
            <div className="btn-l btn-FF4000" onClick={this.toGroup}>
              剩余{String(this.state.hour).padStart(2, 0)}
              {String(this.state.min).padStart(2, 0)}
              {String(this.state.sec).padStart(2, 0)}
              邀请好友参团
            </div>
          </div>
        )}
        <i
          onClick={this.colse}
          className={"iconfont iconiconfront-2 close"}
        ></i>
FE committed
313
      </div>
zhanghaozhe committed
314 315
    )
  }
FE committed
316 317 318
}

export default compose(
zhanghaozhe committed
319 320
  connect((state) => ({ user: state.user }), null),
  withRouter
FE committed
321
)(Single)