index.js 12.1 KB
Newer Older
zhanghaozhe committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import React, { Component } from "react"
import {
  Flex,
  WingBlank,
  WhiteSpace,
  List,
  Radio,
  Toast,
  Checkbox,
} from "antd-mobile"
import { http, getParam, browser } from "src/utils"
import { HeaderBar } from "src/common/index"
import { Link } from "react-router-dom"
import "./index.scss"
import { VList } from "src/common"
import { WithFullSize } from "src/HOCs"
import classnames from "classnames"
xuzhenghua committed
18 19 20

const RadioItem = Radio.RadioItem

zhanghaozhe committed
21
let mockData
xuzhenghua committed
22
if (browser.isWeixin) {
zhanghaozhe committed
23
  mockData = [{ value: 1, label: "微信支付", icon: "iconweixinzhifu" }]
xuzhenghua committed
24
} else {
zhanghaozhe committed
25
  mockData = [
zhanghaozhe committed
26 27
    { value: 1, label: "微信支付", icon: "iconweixinzhifu" },
    { value: 0, label: "支付宝", icon: "iconalipay" },
zhanghaozhe committed
28
  ]
xuzhenghua committed
29 30 31
}

class PayOrder extends Component {
zhanghaozhe committed
32 33 34 35 36 37 38 39 40
  constructor(props) {
    super(props)
    this.state = {
      pay_amount: 0,
      payType: 1,
      stageNumber: 0,
      checkPeriod: false,
      singleMoney: 0,
      periodNumber: 0,
zhanghaozhe committed
41
      orderId: getParam("oid"),
zhanghaozhe committed
42 43
      huabei: false,
      fenqiList: [
zhanghaozhe committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        {
          value: "zhifubao",
          qishu: "3",
          lilv: "2.30%",
          everyMoney: 300,
          feiyong: 2.3,
        },
        {
          value: "weixin",
          qishu: "6",
          lilv: "4.50%",
          everyMoney: 150,
          feiyong: 4.5,
        },
        {
          value: "huabei",
          qishu: "9",
          lilv: "7.50%",
          everyMoney: 100,
          feiyong: 7.5,
        },
zhanghaozhe committed
65 66 67
      ],
      categoryList: [],
      agree: false,
xuzhenghua committed
68
    }
zhanghaozhe committed
69
  }
xuzhenghua committed
70

zhanghaozhe committed
71 72 73 74 75 76 77 78 79
  onChange = (value) => {
    this.setState({
      payType: value,
      checkPeriod: false,
    })
    if (value === 2) {
      this.setState({
        huabei: true,
      })
xuzhenghua committed
80
    }
zhanghaozhe committed
81 82 83
  }
  // 确定购买
  pay = () => {
zhanghaozhe committed
84
    const { payType, orderId } = this.state
zhanghaozhe committed
85 86
    if (!this.state.agree) {
      return
xuzhenghua committed
87
    }
zhanghaozhe committed
88 89 90 91
    if (payType === 0) {
      this.alipayPay(orderId)
    } else if (payType === 1) {
      this.weixinPay(orderId)
xuzhenghua committed
92
    }
zhanghaozhe committed
93 94 95 96 97
  }
  // 微信支付
  weixinPay = (orderId) => {
    // 微信内部-支付
    if (browser.isWeixin) {
zhanghaozhe committed
98 99 100 101
      window.location.href =
        "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx23dac6775ac82877&redirect_uri=" +
        encodeURIComponent(window.location.href + "&aa=bb").toLowerCase() +
        "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
zhanghaozhe committed
102 103
    } else {
      // 微信外部-支付
zhanghaozhe committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117
      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"
              ).toLowerCase()
          } else {
            Toast.info(res.data.msg, 2)
          }
        })
xuzhenghua committed
118
    }
zhanghaozhe committed
119 120 121 122
  }
  // 微信内部支付
  isweixinPay = () => {
    let _this = this
zhanghaozhe committed
123
    let weixin_code = getParam("code")
zhanghaozhe committed
124
    if (weixin_code) {
zhanghaozhe committed
125 126 127 128 129 130 131 132 133 134
      if (getParam("oid") !== undefined) {
        http
          .get(
            `${API["base-api"]}/pay/wxpay/pub_charge/oid/${getParam(
              "oid"
            )}/code/${weixin_code}`
          )
          .then((res) => {
            if (res.data.errno === 0) {
              let data = res.data.data
xuzhenghua committed
135

zhanghaozhe committed
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
              function onBridgeReady() {
                WeixinJSBridge.invoke(
                  "getBrandWCPayRequest",
                  {
                    appId: data.appId, //公众号名称,由商户传入
                    timeStamp: data.timeStamp, //时间戳,自1970年以来的秒数
                    nonceStr: data.nonceStr, //随机串
                    package: data.package,
                    signType: data.signType, //微信签名方式:
                    paySign: data.paySign, //微信签名
                  },
                  function (res) {
                    // eslint-disable-next-line eqeqeq
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                      Toast.info("支付成功", 2)
                      _this.intervalPayStatus = setInterval(function () {
                        http
                          .get(
                            `${API["base-api"]}/m/orderState/oid/${getParam(
                              "oid"
                            )}`
                          )
                          .then((res) => {
                            if (res.data.errno === 401) {
                              clearInterval(_this.intervalPayStatus)
                              _this.intervalPayStatus = null
                              _this.props.history.replace(
                                `/expand/callback?order_id=${getParam("oid")}`
                              )
                            }
                          })
                      }, 1000)
                    } else {
                      alert("支付失败")
                    }
zhanghaozhe committed
171
                  }
zhanghaozhe committed
172 173
                )
              }
zhanghaozhe committed
174

zhanghaozhe committed
175 176 177 178 179 180 181 182 183 184 185 186 187
              if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                  document.addEventListener(
                    "WeixinJSBridgeReady",
                    onBridgeReady,
                    false
                  )
                } else if (document.attachEvent) {
                  document.attachEvent("WeixinJSBridgeReady", onBridgeReady)
                  document.attachEvent("onWeixinJSBridgeReady", onBridgeReady)
                }
              } else {
                onBridgeReady()
zhanghaozhe committed
188
              }
xuzhenghua committed
189
            } else {
zhanghaozhe committed
190
              Toast.info(res.data.msg, 2)
xuzhenghua committed
191
            }
zhanghaozhe committed
192
          })
zhanghaozhe committed
193
      }
xuzhenghua committed
194
    }
zhanghaozhe committed
195 196 197 198 199 200 201
  }
  // 支付完成之后获取状态
  payCallback = () => {
    const _this = this
    // 支付回调
    // 定时器轮训获取订单状态
    _this.intervalPayStatus = setInterval(function () {
zhanghaozhe committed
202 203 204 205 206 207 208 209 210 211
      http
        .get(`${API["base-api"]}/m/orderState/oid/${getParam("oid")}`)
        .then((res) => {
          if (res.data.errno === 401) {
            clearInterval(_this.intervalPayStatus)
            _this.intervalPayStatus = null
            window.location.href =
              "/expand/callback?order_id=" + getParam("oid")
          }
        })
zhanghaozhe committed
212 213
    }, 1000)
  }
xuzhenghua committed
214

zhanghaozhe committed
215 216 217
  onBridgeReady1 = (data) => {
    let _this = this
    data = data || _this.BridgeData
xuzhenghua committed
218

zhanghaozhe committed
219 220
    /* eslint-disable-next-line no-undef */
    WeixinJSBridge.invoke(
zhanghaozhe committed
221 222 223 224 225 226 227 228
      "getBrandWCPayRequest",
      {
        appId: "wx23dac6775ac82877", //公众号名称,由商户传入
        timeStamp: data.timeStamp, //时间戳,自1970年以来的秒数
        nonceStr: data.nonceStr, //随机串
        package: data.package,
        signType: data.signType, //微信签名方式:
        paySign: data.paySign, //微信签名
zhanghaozhe committed
229 230 231 232
      },
      function (res) {
        // eslint-disable-next-line eqeqeq
        if (res.err_msg == "get_brand_wcpay_request:ok") {
zhanghaozhe committed
233
          Toast.info("支付成功", 2)
zhanghaozhe committed
234 235
          _this.payCallback()
        } else {
zhanghaozhe committed
236
          alert("支付失败")
xuzhenghua committed
237
        }
zhanghaozhe committed
238
      }
zhanghaozhe committed
239 240 241 242
    )
  }
  // 支付宝支付
  alipayPay = (orderId) => {
zhanghaozhe committed
243 244 245 246 247 248 249 250 251 252
    http
      .get(`${API["base-api"]}/pay/alipay/wap_charge_new/oid/${orderId}`)
      .then((res) => {
        if (res.data.errno === 0) {
          this.payCallback()
          window.location = res.data.data.url
        } else {
          Toast.info(res.data.msg, 2)
        }
      })
zhanghaozhe committed
253
  }
xuzhenghua committed
254

zhanghaozhe committed
255
  componentDidMount() {
zhanghaozhe committed
256
    if (getParam("weixinpay")) {
zhanghaozhe committed
257 258
      this.payCallback()
    }
xuzhenghua committed
259

zhanghaozhe committed
260
    if (browser.isWeixin) {
zhanghaozhe committed
261
      this.isweixinPay(getParam("oid"))
zhanghaozhe committed
262 263
    }

zhanghaozhe committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    http
      .post(`${API["base-api"]}/m/deposit/detail`, {
        order_id: this.state.orderId,
      })
      .then((res) => {
        const { data } = res
        // eslint-disable-next-line eqeqeq
        if (data.errno == 200) {
          this.setState({
            categoryList: [data.data.course_info],
            salePrice: data.data.course_info.sale_price,
          })
        } else {
          Toast.info(data.msg)
        }
      })
zhanghaozhe committed
280
  }
xuzhenghua committed
281

zhanghaozhe committed
282
  changeAgreement = () => {
zhanghaozhe committed
283
    this.setState((prevState) => ({ agree: !prevState.agree }))
zhanghaozhe committed
284
  }
xuzhenghua committed
285

zhanghaozhe committed
286 287 288 289 290 291 292 293 294 295 296 297
  render() {
    const {
      orderId,
      salePrice,
      payType,
      checkPeriod,
      singleMoney,
      periodNumber,
      categoryList,
      agree,
    } = this.state
    return (
zhanghaozhe committed
298 299 300 301
      <div className="pay-order">
        <HeaderBar title="确认支付" arrow={true}></HeaderBar>
        <WhiteSpace size="sm"></WhiteSpace>
        <div className="order-number">
zhanghaozhe committed
302
          <WingBlank>
zhanghaozhe committed
303
            <Flex justify="between" align="center" style={{ height: "44px" }}>
zhanghaozhe committed
304
              <span>订单号</span>
zhanghaozhe committed
305
              <span className="number">{orderId}</span>
zhanghaozhe committed
306 307 308
            </Flex>
          </WingBlank>
        </div>
zhanghaozhe committed
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 336 337
        <WhiteSpace size="md"></WhiteSpace>
        {categoryList.map((item, index) => {
          const Info = (
            <div className="order-info">
              <p className="order-title text-overflow-one">
                <Link to={`/detail?id=${item.course_id}`}>
                  {item.course_title}
                </Link>
              </p>
              <p className="order-content text-overflow-2">
                {item.simpledescription}
              </p>
              <p className="order-des">
                <span className="order-newprice">¥{item.sale_price}</span>
                <span className={"price-des"}>(预付定金)</span>
              </p>
            </div>
          )
          return (
            <VList
              key={index}
              img={item.image_name}
              id={item.course_id}
              info={Info}
            ></VList>
          )
        })}
        <WhiteSpace size="md"></WhiteSpace>
        <div className="order-number">
zhanghaozhe committed
338
          <WingBlank>
zhanghaozhe committed
339
            <Flex justify="between" align="center" style={{ height: "44px" }}>
zhanghaozhe committed
340
              <span>支付金额</span>
zhanghaozhe committed
341
              <span className="money">{`¥${salePrice}`}</span>
zhanghaozhe committed
342 343 344
            </Flex>
          </WingBlank>
        </div>
xuzhenghua committed
345

zhanghaozhe committed
346 347 348
        <WhiteSpace size="md"></WhiteSpace>
        <List renderHeader={() => "支付方式"} className="pay-type-list">
          {mockData.map((i) => (
zhanghaozhe committed
349
            <RadioItem
zhanghaozhe committed
350 351 352 353 354 355 356
              thumb={
                <i
                  className={`iconfont ${i.icon} ${
                    payType === i.value ? "checked" : ""
                  }`}
                ></i>
              }
zhanghaozhe committed
357 358
              key={i.value}
              checked={payType === i.value}
zhanghaozhe committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
              onChange={() => this.onChange(i.value)}
            >
              {i.value === 2 ? (
                <Flex
                  direction="column"
                  align="start"
                  style={{ width: "100%", marginTop: "6px" }}
                >
                  <Flex
                    direction="row"
                    justify="between"
                    style={{ width: "100%", paddingRight: "30px" }}
                  >
                    <span style={{ color: "#555555", fontSize: "14px" }}>
                      {i.label}
                    </span>
                    {checkPeriod ? (
                      <span
                        style={{
                          color: "#333333",
                          fontSize: "12px",
                        }}
                      >{`${singleMoney}元 × ${periodNumber}期`}</span>
                    ) : null}
zhanghaozhe committed
383
                  </Flex>
zhanghaozhe committed
384 385 386 387 388 389 390 391 392
                  <Flex justify="start">
                    <span style={{ color: "#999999", fontSize: "12px" }}>
                      支付上限受限于您的花呗额度
                    </span>
                  </Flex>
                </Flex>
              ) : (
                i.label
              )}
zhanghaozhe committed
393 394 395 396
            </RadioItem>
          ))}
        </List>
        <Flex>
zhanghaozhe committed
397
          <Flex.Item className={"agreement"}>
zhanghaozhe committed
398 399 400 401 402 403
            <Checkbox.AgreeItem onChange={this.changeAgreement}>
              我已同意定金不退
            </Checkbox.AgreeItem>
          </Flex.Item>
        </Flex>
        {/*<div className='pay-tip'>请在15分钟内完成支付,否则届时系统将关闭该订单。</div>*/}
xuzhenghua committed
404

zhanghaozhe committed
405 406 407 408 409 410
        <div
          className={classnames(["pay-button", !agree && "invalid"])}
          onClick={this.pay}
        >
          确认支付
        </div>
zhanghaozhe committed
411 412 413
      </div>
    )
  }
xuzhenghua committed
414 415 416
}

export default WithFullSize(PayOrder)