123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- import React from 'react';
- import './index.less';
- import Page from '@src/containers/Page';
- import { Checkbox } from 'antd';
- // import Assets from '@src/components/Assets';
- import { getMap, formatDate } from '@src/services/Tools';
- import Button from '../../../components/Button';
- import Money from '../../../components/Money';
- import Icon from '../../../components/Icon';
- import { Order } from '../../../stores/order';
- import { My } from '../../../stores/my';
- import { Main } from '../../../stores/main';
- import { ServiceParamMap, OrderInfoMap } from '../../../../Constant';
- import { Common } from '../../../stores/common';
- const ServiceParamRelation = getMap(Object.keys(ServiceParamMap).map(key => {
- return {
- map: getMap(ServiceParamMap[key].map((row, index) => {
- row.index = index;
- return row;
- }), 'value', 'index'),
- key,
- };
- }), 'key', 'map');
- function formatTitle(record) {
- if (record.productType === 'course_package') {
- return (record.coursePackage || {}).title;
- }
- if (record.productType === 'course') {
- return (record.course || {}).title;
- }
- if (record.productType === 'data') {
- return (record.data || {}).title;
- }
- if (record.productType === 'service') {
- return record.info.label || ((record.serviceInfo || {}).title);
- }
- return '';
- }
- function formatCheckout(checkouts) {
- checkouts.forEach(checkout => {
- checkout.info = OrderInfoMap[checkout.productType];
- if (checkout.productType === 'service') {
- const index = (ServiceParamRelation[checkout.service] && ServiceParamRelation[checkout.service][checkout.param]) || 0;
- checkout.info = Object.assign({}, checkout.info[checkout.service], checkout.serviceInfo.package[index]);
- }
- checkout.title = formatTitle(checkout);
- });
- }
- export default class extends Page {
- initState() {
- return { show: true, showEnd: false, checked: true };
- }
- init() {
- // this.wx = null;
- // Common.readyWechat(window.location.href, ['chooseWXPay']).then(wx => {
- // this.wx = wx;
- // });
- }
- initData() {
- const { id } = this.params;
- Order.getOrder(id).then(order => {
- formatCheckout(order.checkouts);
- const [checkout] = order.checkouts;
- this.setState({ order, checkout });
- });
- Main.getContract('course').then(result => {
- this.setState({ contract: result });
- });
- }
- pay() {
- const { id } = this.params;
- if (this.paying) return;
- this.paying = true;
- this.setState({ paying: true });
- Order.wechatJs(id).then((info) => {
- return Common.readyWechatBridge().then(() => {
- WeixinJSBridge.invoke(
- 'getBrandWCPayRequest', info.request,
- (res) => {
- if (res.err_msg === 'get_brand_wcpay_request:ok') {
- // 使用以上方式判断前端返回,微信团队郑重提示:
- // res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
- this.queryPay(true);
- } else if (res.err_msg === 'get_brand_wcpay_request:cancel' || res.err_msg === 'get_brand_wcpay_request:fail' || res.err_msg === 'chooseWXPay:fail') {
- // 支付失败
- this.paying = false;
- this.setState({ paying: false });
- }
- },
- );
- });
- });
- }
- queryPay(force) {
- const { order } = this.state;
- if (this.time) {
- clearTimeout(this.time);
- }
- if (force) {
- this.times = 0;
- } else {
- this.times = (this.times || 0) + 1;
- }
- this.time = setTimeout(() => {
- Order.query(order.id)
- .then(result => {
- if (result) {
- // 支付成功
- this.paySuccess();
- } else if (order) {
- this.queryPay();
- } else {
- this.setState({ select: null, pay: null, order: null, info: null });
- }
- });
- }, 1000);
- }
- paySuccess() {
- const { order } = this.state;
- const { info } = this.props.user;
- Order.getOrder(order.id).then(result => {
- // 确保开通用的是record记录id
- const [checkout] = result.checkouts;
- formatCheckout(result.checkouts);
- checkout.info.result = checkout.info.result.replace('{email}', info.email).replace('{useExpireDays}', checkout.useExpireDays).replace('{title}', checkout.title);
- if (checkout.service === 'vip') {
- // 查询最后有效期
- My.getVipInfo().then(vip => {
- checkout.info.result = checkout.info.result.replace('{endTime}', formatDate(vip.expireTime, 'YYYY-MM-DD'));
- this.setState({ show: false, showEnd: true, order: result, checkout });
- });
- } else {
- this.setState({ show: false, showEnd: true, order: result, checkout });
- }
- });
- }
- renderView() {
- const { show, showEnd, showContract } = this.state;
- if (showContract) {
- return this.renderContract();
- }
- if (show) {
- return this.renderPay();
- }
- if (showEnd) {
- return this.renderEnd();
- }
- return null;
- }
- renderPay() {
- const { order = {}, contract = {}, checkout = {}, checked } = this.state;
- const { info = {}, productType } = checkout;
- let content = '';
- switch (productType) {
- case 'data':
- content = this.renderData();
- break;
- case 'course_package':
- content = this.renderCoursePackage();
- break;
- case 'course':
- case 'service':
- content = this.renderSingle();
- break;
- default:
- }
- return (
- <div className="detail">
- <div className="title" style={{ marginBottom: 0 }}>
- 商品信息
- </div>
- {content}
- {info.refund_policy && [< div className="title">退款政策</div>, <div className="desc">{info.refund_policy}</div>]}
- {info.copyright_notes && [<div className="title">版权说明</div>, <div className="desc">{info.copyright_notes}</div>]}
- {order.productTypes && order.productTypes.indexOf('course') > 0 && <div className="agree">
- <Checkbox checked={checked} onChange={() => this.setState({ checked: !checked })} />
- 我已阅读并同意 <a onClick={() => this.setState({ showContract: true })}>《{contract.title}》</a>
- </div>}
- <div className="fixed">
- <div className="tip">*若在购买过程中遇到问题,请联系千行小助手协助解决</div>
- <div className="fee">
- 应付: <Money value={order.money} size="lager" />
- </div>
- <Button
- width={110}
- className="f-r"
- radius
- disabled={this.state.paying || !checked}
- onClick={() => {
- this.pay();
- }}
- >
- 立即购买
- </Button>
- </div>
- </div >
- );
- }
- renderData() {
- const { checkout } = this.state;
- return (
- <div className="info data">
- <div className="info-block">
- {checkout.title} <Money className="f-r" value={checkout.money} />
- </div>
- <div className="info-block">
- 开通有效期 <span className="f-r">{checkout.expireDays ? `${checkout.expireDays}天` : '永久'}</span>
- </div>
- </div>
- );
- }
- renderCoursePackage() {
- const { checkout, order } = this.state;
- return (
- <div className="info course-package">
- <div className="info-block">
- {checkout.title} <Money className="f-r" value={checkout.money} />
- </div>
- <div className="info-block">
- {order.checkouts.map(row => {
- if (row.parentId === 0) return null;
- return <div className="info-item">
- {row.title} <span className="f-r">开通有效期: {checkout.expireDays ? `${checkout.expireDays}天` : '付款后立即生效'} 使用有效期: {checkout.useExpireDays ? `${checkout.useExpireDays}天` : '永久'}</span>
- </div>;
- })}
- </div>
- </div>
- );
- }
- renderSingle() {
- const { checkout = {} } = this.state;
- return (
- <div className="info single">
- <div className="info-block">
- {checkout.title} <Money className="f-r" value={checkout.money} />
- </div>
- <div className="info-block">
- 开通有效期 <span className="f-r">{checkout.expireDays ? `${checkout.expireDays}天` : '付款后立即生效'}</span>
- </div>
- <div className="info-block">
- 使用有效期 <span className="f-r">{checkout.useExpireDays ? `${checkout.useExpireDays}天` : '永久'}</span>
- </div>
- </div>
- );
- }
- renderEnd() {
- const { checkout = {} } = this.state;
- const { info = {} } = checkout;
- return (
- <div className="finish">
- <div className="icon">
- <Icon type="check-circle" />
- </div>
- <div className="title">支付成功!</div>
- <div className="desc">{info.result}</div>
- </div>
- );
- }
- renderContract() {
- const { contract } = this.state;
- // 需要一个关闭x在左上角
- return <div dangerouslySetInnerHTML={{ __html: contract.content }} />;
- }
- }
|