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 (