index_20190429105048.js 6.35 KB
Newer Older
.  
baiguangyao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Checkbox, NavBar } from 'antd-mobile'
//组件
import ShopCard  from './cardList.js'
import Loading from '@base/Loading'

//css
import './card.scss'
import classnames from 'classnames'

const mockData = [
    {
        title: '三月面试求职班',
        imgUrl: 'https://julyedu-img-public.oss-cn-beijing.aliyuncs.com/Public/Image/4c5ccac604.jpg',
        content: '涵盖ML主流算法及其应用-文字超过一行换行',
        newprice: '980',
        price: '1280',
        id: '110',
        check: false
    },
    {
        title: '三月面试求职班',
        imgUrl: 'https://julyedu-img-public.oss-cn-beijing.aliyuncs.com/Public/Image/4c5ccac604.jpg',
        content: '涵盖ML主流算法及其应用-文字超过一行换行',
        newprice: '980',
        price: '1280',
        id: '110',
        check: false
    },
    {
        title: '三月面试求职班',
        imgUrl: 'https://julyedu-img-public.oss-cn-beijing.aliyuncs.com/Public/Image/4c5ccac604.jpg',
        content: '涵盖ML主流算法及其应用-文字超过一行换行',
        newprice: '980',
        price: '1280',
        id: '110',
        check: false
    }
]

class Cart extends Component {
    constructor(props) {
        super(props)
        this.state = {
            deleteAll: false,
            loading: false,
            checkedNum: 0,
            allPrice: 0,
            cartNmu: 0,
            data: mockData
        }

        this.checkChange = this.checkChange.bind(this);
        this.getCartList = this.getCartList.bind(this);
        this.changeStock = this.changeStock.bind(this);
    }
    //获取购物车列表
    getCartList() {
       
    }
    //全选
    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)
    }

    //点击
    checkChange(id, check) {
        let newData = this.state.data.map((item, i) => {
            if (item.id === id) {
                return {
                    ...item,
                    check: check
                }
            } else {
                return item;
            }
        })
        this.setState({
            data: newData
        })
        this.calc(newData)
    }
    //计算总价
    calc(newData) {
        let allPrice = 0;
        let checkedNum = 0;
        let cartNmu = 0;
        newData.forEach((item, i) => {
            if (item.check) {
                cartNmu += 1;
                checkedNum += i;
                allPrice += parseFloat(item.newprice);
            }
        })
        this.setState({
            checkedNum,
            allPrice,
            cartNmu
        })
    }
    //购买
    buy() {
       
    }
    deleteAll() {
        
    }
    //装载组件
    componentDidMount() {
        this.getCartList()
    }
    render() {
        return (
            <div className="cart-page" style={{ overflow: 'hidden' }}>
                <NavBar
                    style={{ "height": "44px" }}
                    className="order-tab"
                    mode="light"
                    icon={<i style={{"fontSize": '24px'}} className="iconfont iconiconfront-68"></i>}
                    rightContent={[
                        <i key="0" style={{ "fontSize": '24px' }} className="iconfont iconiconfront-56"></i>
                    ]}
                >
                    购物车
                </NavBar>

                <div className="cart-body">
                    {
                        this.state.loading ?
                            <Loading />
                            :
                            <ShopCard getCartList={this.getCartList.bind(this)} changeStock={this.changeStock.bind(this)} checkChange={this.checkChange.bind(this)}  data={this.state.data}/>

                    }

                    {
                        this.state.deleteAll ?
                            <div className="cart-footer">
                                <div>
                                    <Checkbox onChange={(e) => {
                                        this.allChange(e)
                                    }} />
                                    <div>全选</div>
                                </div>
                                <div></div>
                                <div className={classnames({
                                    'active': this.state.cartNmu > 0
                                })} 
                                onClick={() => {
                                    if (this.state.cartNmu > 0) {
                                        this.delete()
                                    }
                                }}>
                                    删除<span>({this.state.cartNmu})</span>
                                </div>
                            </div>
                            :
                            <div className="cart-footer">
                                <div className="cart-label">
                                    <Checkbox onChange={(e) => {
                                        this.allChange(e)
                                    }} />
                                    <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()
                                    }
                                }}>
                                    去结算<span>({this.state.checkedNum})</span>
                                </div>
                            </div>
                    }
                </div>
                

            </div>
        )
    }
}

export default connect()(Cart)