Commit 20e6cbae by zhanghaozhe

获取和兑换优惠券

parent cbd0c8a1
...@@ -5,12 +5,12 @@ import classnames from 'classnames' ...@@ -5,12 +5,12 @@ import classnames from 'classnames'
class Coupon extends PureComponent { class Coupon extends PureComponent {
pick = () => { pick = () => {
let {purpose, select, invalid} = this.props let {select, invalid} = this.props
purpose === 'use' && !invalid && select && select(this.props.id) !invalid && select && select(this.props.id)
} }
GoToUse = () => { GoToUse = () => {
this.props.toUse(this.props.id) this.props.useCoupon(this.props.id)
} }
render() { render() {
...@@ -19,15 +19,12 @@ class Coupon extends PureComponent { ...@@ -19,15 +19,12 @@ class Coupon extends PureComponent {
amount, amount,
format_expire_time, format_expire_time,
limit_course, limit_course,
selected,
invalid, invalid,
purpose,
course_title, course_title,
id, id,
selectedCouponId selectedCouponId
} = this.props } = this.props
return ( return (
<li className='coupon' onClick={this.pick}> <li className='coupon' onClick={this.pick}>
<div className={classnames('coupon-info', invalid ? 'invalid' : `coupon-type${ctype}`)}> <div className={classnames('coupon-info', invalid ? 'invalid' : `coupon-type${ctype}`)}>
...@@ -39,9 +36,9 @@ class Coupon extends PureComponent { ...@@ -39,9 +36,9 @@ class Coupon extends PureComponent {
} }
<p className='expire'>有效期至:{format_expire_time}</p> <p className='expire'>有效期至:{format_expire_time}</p>
{ {
purpose === 'use' && selectedCouponId === id &&
<i className={classnames('iconfont icondanseshixintubiao-5', { <i className={classnames('iconfont icondanseshixintubiao-5', {
check: (selected && !invalid) && (selectedCouponId === id) check: !invalid
})} })}
/> />
} }
...@@ -58,7 +55,6 @@ class Coupon extends PureComponent { ...@@ -58,7 +55,6 @@ class Coupon extends PureComponent {
limit_course === 0 ? '可用于大于代金券金额的课程' : `仅适用于《${course_title}》` limit_course === 0 ? '可用于大于代金券金额的课程' : `仅适用于《${course_title}》`
}</span> }</span>
{ {
purpose === 'exchange' &&
<button <button
className='use' className='use'
onClick={this.GoToUse} onClick={this.GoToUse}
......
...@@ -7,52 +7,16 @@ import { http, api } from '@/utils' ...@@ -7,52 +7,16 @@ import { http, api } from '@/utils'
import { WithFullSize } from '@/HOCs' import { WithFullSize } from '@/HOCs'
import { Toast } from 'antd-mobile' import { Toast } from 'antd-mobile'
const mockData = {
valid: [
{
type: 1,
couponName: '代金券',
expire_time: '2017-12-31',
limit: '可用于大于代金券金额的课程',
denomination: '5'
},
{
type: 2,
couponName: '代金券',
expire_time: '2017-12-31',
limit: '可用于大于代金券金额的课程',
denomination: '5'
},
],
invalid: [
{
type: 1,
couponName: '代金券',
expire_time: '2017-12-31',
limit: '可用于大于代金券金额的课程',
denomination: '5'
},
{
type: 1,
couponName: '代金券',
expire_time: '2017-12-31',
limit: '可用于大于代金券金额的课程',
denomination: '5'
},
]
}
class UseCoupon extends PureComponent { class UseCoupon extends PureComponent {
state = { state = {
selected: true,
selectedCouponId: 0, selectedCouponId: 0,
exchangeCode: '', exchangeCode: '',
couponList: [], couponList: [],
valid_coupons: [], valid_coupons: [],
invalid_coupons: [], invalid_coupons: [],
inuse_coupon: [] courseId: 139,
} }
componentDidMount() { componentDidMount() {
...@@ -71,12 +35,8 @@ class UseCoupon extends PureComponent { ...@@ -71,12 +35,8 @@ class UseCoupon extends PureComponent {
} }
select = val => { select = val => {
this.setState(state => { this.setState({
console.log(state.selected, state.selectedCouponId) selectedCouponId: val === this.state.selectedCouponId ? 0 : val
return {
selected: !state.selected,
selectedCouponId: val
}
}) })
} }
...@@ -97,7 +57,7 @@ class UseCoupon extends PureComponent { ...@@ -97,7 +57,7 @@ class UseCoupon extends PureComponent {
}) })
Toast.info('兑换成功') Toast.info('兑换成功')
} else { } else {
Toast.fail(data.msg) Toast.info(data.msg)
} }
}) })
} else { } else {
...@@ -111,35 +71,52 @@ class UseCoupon extends PureComponent { ...@@ -111,35 +71,52 @@ class UseCoupon extends PureComponent {
const data = res.data const data = res.data
if (data.code === 200) { if (data.code === 200) {
this.setState({ this.setState({
couponList: data.data, couponList: data.data
selectedCouponId: data.data[0].id
}) })
} else { } else {
Toast.fail(data.msg) Toast.info(data.msg)
} }
}) })
} }
getAllCoupons = () => { getAllCoupons = () => {
http.post(`${api.home}/m/coupon/select`, {course_id: 139}) http.post(`${api.home}/m/coupon/select`, {course_id: this.state.courseId})
.then(res => { .then(res => {
const data = res.data const data = res.data
if (data.code === 200) { if (data.code === 200) {
const inuse_coupon = data.data.inuse_coupon;
this.setState({ this.setState({
valid_coupons: data.data.valid_coupons, valid_coupons: inuse_coupon
? [...inuse_coupon, ...data.data.valid_coupons]
: data.data.valid_coupons,
invalid_coupons: data.data.invalid_coupons, invalid_coupons: data.data.invalid_coupons,
inuse_coupon: data.data.inuse_coupon selectedCouponId: inuse_coupon ? inuse_coupon[0].id : 0
}) })
} else { } else {
Toast.fail(data.msg) Toast.info(data.msg)
} }
}) })
} }
selectCoupon = () => { useCoupon = (val) => {
if (val) {
http.post(`${api.home}/m/coupon/use`, {
course_id: this.state.courseId,
coupon_id: val
})
.then(res => {
const data = res && res.data
if (data.code === 200) {
} else {
Toast.info(data.msg)
}
})
}
} }
render() { render() {
...@@ -151,12 +128,18 @@ class UseCoupon extends PureComponent { ...@@ -151,12 +128,18 @@ class UseCoupon extends PureComponent {
exchangeCode={this.state.exchangeCode}/> exchangeCode={this.state.exchangeCode}/>
<div className="coupons-area"> <div className="coupons-area">
<Content <Content
coupons={state && state.from && state.from === '/my' ? this.state.couponList : this.state.valid_coupons} coupons={
state
&& state.from
&& state.from === '/my'
? this.state.couponList
: this.state.valid_coupons
}
showUseButton={false} showUseButton={false}
selected={this.state.selected} selected={this.state.selected}
selectedCouponId={this.state.selectedCouponId} selectedCouponId={this.state.selectedCouponId}
select={this.select} select={this.select}
purpose={'use'} useCoupon={this.useCoupon}
/> />
{ {
this.state.invalid_coupons.length > 0 && this.state.invalid_coupons.length > 0 &&
...@@ -165,7 +148,6 @@ class UseCoupon extends PureComponent { ...@@ -165,7 +148,6 @@ class UseCoupon extends PureComponent {
<div className='invalid-title'>- 不可使用的优惠券 -</div> <div className='invalid-title'>- 不可使用的优惠券 -</div>
<Content <Content
coupons={this.state.invalid_coupons} coupons={this.state.invalid_coupons}
selected={this.state.selected}
selectedCouponId={this.state.selectedCouponId} selectedCouponId={this.state.selectedCouponId}
select={this.select} select={this.select}
purpose={'use'} purpose={'use'}
...@@ -180,7 +162,7 @@ class UseCoupon extends PureComponent { ...@@ -180,7 +162,7 @@ class UseCoupon extends PureComponent {
} }
} }
function Content({coupons, selected, ...rest}) { function Content({coupons, ...rest}) {
if (coupons.length === 0) { if (coupons.length === 0) {
return ( return (
<div className='empty'> <div className='empty'>
...@@ -196,7 +178,6 @@ function Content({coupons, selected, ...rest}) { ...@@ -196,7 +178,6 @@ function Content({coupons, selected, ...rest}) {
<Coupon <Coupon
key={item.id} key={item.id}
{...item} {...item}
selected={selected}
id={item.id} id={item.id}
{...rest} {...rest}
/> />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment