index.js 6.81 KB
Newer Older
xuzhenghua committed
1 2 3 4
import React, {Component} from 'react'
import {connect} from 'react-redux'
import {Checkbox, NavBar, Modal, Toast} from 'antd-mobile'

baiguangyao committed
5
//组件
xuzhenghua committed
6
import ShopCart from './cartList.js'
baiguangyao committed
7 8 9 10

//css
import './card.scss'
import classnames from 'classnames'
xuzhenghua committed
11
import {api, http} from "@/utils";
baiguangyao committed
12

xuzhenghua committed
13
const alert = Modal.alert;
baiguangyao committed
14 15 16 17 18

class Cart extends Component {
    constructor(props) {
        super(props)
        this.state = {
xuzhenghua committed
19
            data: [],
baiguangyao committed
20 21 22 23
            deleteAll: false,
            checkedNum: 0,
            allPrice: 0,
            cartNmu: 0,
xuzhenghua committed
24
            courseIdarr: [] // 选中的课程id
baiguangyao committed
25 26 27
        }

    }
xuzhenghua committed
28 29 30 31


    componentDidMount() {
        this.getList()
baiguangyao committed
32
    }
xuzhenghua committed
33 34 35 36 37 38 39 40 41 42 43

    getList = () => {
        http.get(`${api.home}/m/cart/list`).then((res) => {
            if (res.data.code === 200) {
                this.setState({
                    data: res.data.data.courses
                })
            } else {
                Toast.info(res.data.msg, 2);
            }
        })
.  
baiguangyao committed
44
    }
xuzhenghua committed
45

baiguangyao committed
46 47 48 49 50 51 52 53 54 55 56 57 58
    //全选
    allChange(e) {
        let checked = e.target.checked
        let newData = this.state.data.map((item, i) => {
            return {
                ...item,
                check: checked
            }
        })
        this.setState({
            data: newData
        })
        this.calc(newData)
xuzhenghua committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

        if (checked) {
            let arr = []
            newData.map((item, i) => {
                arr.push(item.course_id)
            })
            this.setState({
                courseIdarr: arr
            })

        } else {
            this.setState({
                courseIdarr: []
            })
        }

baiguangyao committed
75 76 77 78 79
    }

    //点击
    checkChange(id, check) {
        let newData = this.state.data.map((item, i) => {
xuzhenghua committed
80
            if (item.course_id === id) {
baiguangyao committed
81 82 83 84 85 86 87 88 89 90 91 92
                return {
                    ...item,
                    check: check
                }
            } else {
                return item;
            }
        })
        this.setState({
            data: newData
        })
        this.calc(newData)
xuzhenghua committed
93 94 95 96 97 98 99 100 101 102 103 104 105
        let arr = []
        newData.forEach((item, i) => {
            if (item.check) {
                arr.push(item.course_id)
                this.setState({
                    courseIdarr: arr
                })
            } else {
                this.setState({
                    courseIdarr: arr
                })
            }
        })
baiguangyao committed
106
    }
xuzhenghua committed
107

baiguangyao committed
108 109 110 111 112 113 114
    //计算总价
    calc(newData) {
        let allPrice = 0;
        let checkedNum = 0;
        let cartNmu = 0;
        newData.forEach((item, i) => {
            if (item.check) {
.  
baiguangyao committed
115
                i++;
baiguangyao committed
116
                cartNmu += 1;
.  
baiguangyao committed
117
                checkedNum = i;
xuzhenghua committed
118
                allPrice += parseFloat(item.price1);
baiguangyao committed
119 120 121 122 123 124 125 126
            }
        })
        this.setState({
            checkedNum,
            allPrice,
            cartNmu
        })
    }
xuzhenghua committed
127 128 129 130 131 132 133 134 135 136 137

    // 删除
    detail = () => {
        if (this.state.courseIdarr.length > 0) {
            alert('', '确定从购物车中删除?', [
                {text: '取消', onPress: () => console.log('cancel')},
                {
                    text: '确认',
                    onPress: () => {
                        console.log(this.state.courseIdarr)
                        let data = {
xuzhenghua committed
138
                            course_ids: this.state.courseIdarr
xuzhenghua committed
139 140 141 142 143 144 145 146 147 148 149 150 151
                        }
                        http.post(`${api.home}/m/cart/remove`, data).then((res) => {
                            if (res.data.code === 200) {
                                this.getList()
                            } else {
                                Toast.info(res.data.msg, 2);
                            }
                        })
                    }
                }
            ])
        }

baiguangyao committed
152
    }
xuzhenghua committed
153 154


baiguangyao committed
155 156
    render() {
        return (
xuzhenghua committed
157
            <div className="cart-page" style={{overflow: 'hidden'}}>
baiguangyao committed
158
                <NavBar
xuzhenghua committed
159
                    style={{"height": "44px"}}
baiguangyao committed
160 161 162 163
                    className="order-tab"
                    mode="light"
                    icon={<i style={{"fontSize": '24px'}} className="iconfont iconiconfront-68"></i>}
                    rightContent={[
xuzhenghua committed
164 165
                        <i key="0" style={{"fontSize": '24px'}} className="iconfont iconiconfront-56"
                           onClick={this.detail}></i>
baiguangyao committed
166 167 168 169 170 171
                    ]}
                >
                    购物车
                </NavBar>

                <div className="cart-body">
xuzhenghua committed
172
                    <ShopCart checkChange={this.checkChange.bind(this)} data={this.state.data}/>
baiguangyao committed
173 174 175 176 177 178 179

                    {
                        this.state.deleteAll ?
                            <div className="cart-footer">
                                <div>
                                    <Checkbox onChange={(e) => {
                                        this.allChange(e)
xuzhenghua committed
180
                                    }}/>
baiguangyao committed
181 182 183 184 185
                                    <div>全选</div>
                                </div>
                                <div></div>
                                <div className={classnames({
                                    'active': this.state.cartNmu > 0
xuzhenghua committed
186 187 188 189 190 191
                                })}
                                     onClick={() => {
                                         if (this.state.cartNmu > 0) {
                                             this.delete()
                                         }
                                     }}>
baiguangyao committed
192 193 194 195 196 197 198 199
                                    删除<span>({this.state.cartNmu})</span>
                                </div>
                            </div>
                            :
                            <div className="cart-footer">
                                <div className="cart-label">
                                    <Checkbox onChange={(e) => {
                                        this.allChange(e)
xuzhenghua committed
200
                                    }}/>
baiguangyao committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                                    <div>全选</div>
                                </div>
                                <div className="all-pirce">
                                    <p>
                                        <span>合计:</span>
                                        <span>{this.state.allPrice}</span>
                                    </p>
                                </div>
                                <div className={classnames({
                                    'active': this.state.checkedNum > 0
                                })} onClick={() => {
                                    if (this.state.checkedNum > 0) {
                                        this.buy()
                                    }
                                }}>
xuzhenghua committed
216
                                    结算<span> ( {this.state.checkedNum} ) </span>
baiguangyao committed
217 218 219 220
                                </div>
                            </div>
                    }
                </div>
xuzhenghua committed
221

baiguangyao committed
222 223 224 225 226 227 228

            </div>
        )
    }
}

export default connect()(Cart)