buyCart.vue 10.8 KB
Newer Older
M
maguohua 已提交
1 2 3 4
 <template>
    <section class="cart_module">
        <section v-if="!foods.specifications.length" class="cart_button">
            <transition name="showReduce">
M
maguohua 已提交
5
                <svg @click="removeOutCart(foods.category_id, foods.item_id, foods.specfoods[0].food_id, foods.specfoods[0].name, foods.specfoods[0].price, '')" v-if="foodNum">
M
maguohua 已提交
6 7 8
                    <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cart-minus"></use>
                </svg>
            </transition>
M
maguohua 已提交
9
            <transition name="fade">
M
maguohua 已提交
10 11
                <span class="cart_num" v-if="foodNum">{{foodNum}}</span>
            </transition>
M
maguohua 已提交
12
            <svg class="cart_add" @click="addToCart(foods.category_id, foods.item_id, foods.specfoods[0].food_id, foods.specfoods[0].name, foods.specfoods[0].price, '')">
M
maguohua 已提交
13 14 15 16
                <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cart-add"></use>
            </svg>
        </section>
        <section v-else class="choose_specification">
M
maguohua 已提交
17 18 19 20 21 22 23 24 25
            <section class="choose_icon_container">
                <transition name="showReduce">
                    <svg class="specs_reduce_icon" v-if="foodNum" @click="showReduceTip">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cart-minus"></use>
                    </svg>
                </transition>
                <transition name="fade">
                    <span class="cart_num" v-if="foodNum">{{foodNum}}</span>
                </transition>
M
maguohua 已提交
26
                <transition name="fade">
M
maguohua 已提交
27
                    <p class="show_delete_tip" v-if="showDeleteTip">多规格商品只能去购物车删除哦</p>
M
maguohua 已提交
28
                </transition>
M
maguohua 已提交
29 30 31
                <span class="show_chooselist" @click="showChooseList">选规格</span>
            </section>
            <section>
M
maguohua 已提交
32
                <transition name="fade">
M
maguohua 已提交
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
                    <div class="specs_cover" @click="showChooseList" v-if="showSpecs"></div>
                </transition>
                <transition name="fadeBounce">
                    <div class="specs_list" v-if="showSpecs">
                        <header class="specs_list_header">
                            <h4 class="ellipsis">{{foods.name}}</h4>
                            <svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" version="1.1"class="specs_cancel" @click="showChooseList">
                                <line x1="0" y1="0" x2="16" y2="16"  stroke="#666" stroke-width="1.2"/>
                                <line x1="0" y1="16" x2="16" y2="0"  stroke="#666" stroke-width="1.2"/>
                            </svg>
                        </header>
                        <section class="specs_details">
                            <h5 class="specs_details_title">{{foods.specifications[0].name}}</h5>
                            <ul>
                                <li v-for="(item, itemIndex) in foods.specifications[0].values" :class="{specs_activity: itemIndex == specsIndex}" @click="chooseSpecs(itemIndex)">
                                    {{item}}
                                </li>
                            </ul>
                        </section>
                        <footer class="specs_footer">
                            <div class="specs_price">
                                <span>¥ </span>
                                <span>{{foods.specfoods[specsIndex].price}}</span>
                            </div>
                            <div class="specs_addto_cart" @click="addSpecs(foods.category_id, foods.item_id, foods.specfoods[specsIndex].food_id, foods.specfoods[specsIndex].name, foods.specfoods[specsIndex].price, foods.specifications[0].values[specsIndex])">加入购物车</div>
                        </footer>
M
maguohua 已提交
59 60 61
                    </div>
                </transition>
            </section>
M
maguohua 已提交
62 63 64 65 66 67 68 69
        </section>
    </section>
</template>

<script>
    export default {
    	data(){
            return{
M
maguohua 已提交
70
               showSpecs: false,
M
maguohua 已提交
71 72
               specsIndex: 0,
               showDeleteTip: false,
M
maguohua 已提交
73 74 75
            }
        },
        created(){
M
maguohua 已提交
76
        
M
maguohua 已提交
77
        },
M
maguohua 已提交
78 79 80 81
        computed: {
            foodNum: function (){
                let category_id = this.foods.category_id;
                let item_id = this.foods.item_id;
M
maguohua 已提交
82 83 84
                if (this.shopCart&&this.shopCart[category_id]&&this.shopCart[category_id][item_id]) {
                    let num = 0;
                    Object.values(this.shopCart[category_id][item_id]).forEach((item,index) => {
M
maguohua 已提交
85
                        num += item.num;
M
maguohua 已提交
86 87 88 89 90 91
                    })
                    return num;
                }else {
                    return 0;
                }
            }
M
maguohua 已提交
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
        },
        props:['foods', 'shopCart'],
        methods: {
            removeOutCart(category_id, item_id, food_id, name, price, specs){
                if (this.foodNum > 0) {
                    this.$emit('reduce', category_id, item_id, food_id, name, price, specs);
                }
            },
            addToCart(category_id, item_id, food_id, name, price, specs){
                this.$emit('add', category_id, item_id, food_id, name, price, specs);
            },
            chooseSpecs(index){
                this.specsIndex = index;
            },
            showChooseList(){
                this.showSpecs = !this.showSpecs;
                this.specsIndex = 0;
            },
            addSpecs(category_id, item_id, food_id, name, price, specs){
                this.$emit('add', category_id, item_id, food_id, name, price, specs);
                this.showChooseList();
            },
            showReduceTip(){
                this.showDeleteTip = true;
                clearTimeout(this.timer);
                this.timer = setTimeout(() => {
                    clearTimeout(this.timer);
                    this.showDeleteTip = false;
                }, 3000)
            }
        },
M
maguohua 已提交
123 124 125 126 127 128 129 130 131
    }
</script>

<style lang="scss" scoped>
    @import '../../style/mixin.scss';
	.cart_module{
        .cart_button{
            display: flex;
            align-items: center;
M
maguohua 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144
        }
        svg{
            @include wh(.8rem, .8rem);
            fill: #3190e8;
        }
        .specs_reduce_icon{
            fill: #999;
        }
        .cart_num{
            @include sc(.6rem, #666);
            min-width: 1rem;
            text-align: center;
            font-family: Helvetica Neue,Tahoma;
M
maguohua 已提交
145 146
        }
        .choose_specification{
M
maguohua 已提交
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
            .choose_icon_container{
                display: flex;
                align-items: center;
                .show_chooselist{
                    display: block;
                    @include sc(.55rem, #fff);
                    padding: .2rem .3rem;
                    background-color: $blue;
                    border-radius: 0.5rem;
                    border: 1px solid $blue;
                }
                .show_delete_tip{
                    position: fixed;
                    top: 50%;
                    left: 15%;
                    width: 70%;
                    transform: translateY(-50%);
                    background-color: rgba(0,0,0,.8);
                    z-index: 18;
                    @include sc(.65rem, #fff);
                    text-align: center;
                    padding: .5rem 0;
                    border: 1px;
                    border-radius: 0.25rem;
                }
            }
M
maguohua 已提交
173 174 175 176 177 178
            .specs_cover{
                position: fixed;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
M
maguohua 已提交
179
                background-color: rgba(0,0,0,.4);
M
maguohua 已提交
180 181 182
                z-index: 17;
            }
            .specs_list{
M
maguohua 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
                position: fixed;
                top: 35%;
                left: 15%;
                width: 70%;
                background-color: #fff;
                z-index: 18;
                border: 1px;
                border-radius: 0.2rem;
                .specs_list_header{
                    h4{
                        @include sc(.7rem, #222);
                        font-weight: normal;
                        text-align: center;
                        padding: .5rem;
                    }
                    .specs_cancel{
                        position: absolute;
                        right: .5rem;
                        top: .5rem;
                    }
                }
                .specs_details{
                    padding: .5rem;
                    .specs_details_title{
                        @include sc(.6rem, #666);
                    }
                    ul{
                        display: flex;
                        flex-wrap: wrap;
                        padding: .4rem 0;
                        li{
                            font-size: .6rem;
                            padding: .3rem .5rem;
                            border: 0.025rem solid #ddd;
                            border-radius: .2rem;
                            margin-right: .5rem;
                        }
                        .specs_activity{
                            border-color: #3199e8;
                            color: #3199e8;
                        }
                    }
                }
                .specs_footer{
                    @include fj;
                    align-items: center;
                    background-color: #f9f9f9;
                    padding: 0.5rem;
                    border: 1px;
                    border-bottom-left-radius: .2rem;
                    border-bottom-right-radius: .2rem;
                    .specs_price{
                        span{
                            color: #ff6000;
                        }
                        span:nth-of-type(1){
                            font-size: .5rem;
                        }
                        span:nth-of-type(2){
                            font-size: .8rem;
                            font-weight: bold;
                            font-family: Helvetica Neue,Tahoma;
                        }
                    }
                    .specs_addto_cart{
                        @include wh(4rem, 1.3rem);
                        background-color: #3199e8;
                        border: 1px;
                        border-radius: 0.15rem;
                        @include sc(.6rem, #fff);
                        text-align: center;
                        line-height: 1.3rem;
                    }
                }
M
maguohua 已提交
257
            }
M
maguohua 已提交
258 259
        }
    }
M
maguohua 已提交
260
    .showReduce-enter-active, .showReduce-leave-active {
M
maguohua 已提交
261
        transition: all .3s ease-out;
M
maguohua 已提交
262 263 264 265 266 267
    }
    .showReduce-enter, .showReduce-leave-active {
        opacity: 0;
        transform: translateX(1rem);
    }
    .fade-enter-active, .fade-leave-active {
M
maguohua 已提交
268
        transition: all .3s;
M
maguohua 已提交
269 270 271 272
    }
    .fade-enter, .fade-leave-active {
        opacity: 0;
    }
M
maguohua 已提交
273 274 275 276 277 278 279
    .fadeBounce-enter-active, .fadeBounce-leave-active {
        transition: all .3s;
    }
    .fadeBounce-enter, .fadeBounce-leave-active {
        opacity: 0;
        transform: scale(.7);
    }
M
maguohua 已提交
280
</style>