buyCart.vue 13.7 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 @click="addToCart(foods.category_id, foods.item_id, foods.specfoods[0].food_id, foods.specfoods[0].name, foods.specfoods[0].price, '', $event)">
M
maguohua 已提交
13 14
                <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cart-add"></use>
            </svg>
M
maguohua 已提交
15 16 17 18 19 20 21 22 23 24 25 26
            <transition 
            appear
            @after-appear = 'afterEnter'
            @before-appear="beforeEnter"
            v-for="(item,index) in showMoveDot"
            >
                <span class="move_dot" v-if="item">
                    <svg class="move_liner">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cart-add"></use>
                    </svg>
                </span>
            </transition>
M
maguohua 已提交
27 28
        </section>
        <section v-else class="choose_specification">
M
maguohua 已提交
29 30 31 32 33 34 35 36 37
            <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 已提交
38
                <transition name="fade">
M
maguohua 已提交
39
                    <p class="show_delete_tip" v-if="showDeleteTip">多规格商品只能去购物车删除哦</p>
M
maguohua 已提交
40
                </transition>
M
maguohua 已提交
41 42 43
                <span class="show_chooselist" @click="showChooseList">选规格</span>
            </section>
            <section>
M
maguohua 已提交
44
                <transition name="fade">
M
maguohua 已提交
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
                    <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 已提交
71 72 73
                    </div>
                </transition>
            </section>
M
maguohua 已提交
74 75 76 77 78
        </section>
    </section>
</template>

<script>
M
upata  
maguohua 已提交
79
    import {mapState, mapMutations} from 'vuex'
M
maguohua 已提交
80 81 82
    export default {
    	data(){
            return{
M
maguohua 已提交
83 84 85 86 87 88 89
               showSpecs: false,//控制显示食品规格
               specsIndex: 0, //当前选中的规格索引值
               showDeleteTip: false, //多规格商品点击减按钮,弹出提示框
               showMoveDot: [], //控制下落的小圆点显示隐藏
               elLeft: 0, //当前点击加按钮在网页中的绝对top值
               elBottom: 0, //当前点击加按钮在网页中的绝对left值
               windowHeight: null, //屏幕的高度
M
maguohua 已提交
90 91
            }
        },
M
maguohua 已提交
92 93
        mounted(){
            this.windowHeight = window.innerHeight;
M
maguohua 已提交
94
        },
M
maguohua 已提交
95
        computed: {
M
upata  
maguohua 已提交
96 97 98 99 100 101 102 103 104
            ...mapState([
                'cartList'
            ]),
            /**
             * 监听cartList变化,更新当前商铺的购物车信息shopCart,同时返回一个新的对象
             */
            shopCart: function (){
                return Object.assign({},this.cartList[this.shopId]);
            },
M
maguohua 已提交
105
            //shopCart变化的时候重新计算当前商品的数量
M
maguohua 已提交
106 107 108
            foodNum: function (){
                let category_id = this.foods.category_id;
                let item_id = this.foods.item_id;
M
maguohua 已提交
109 110 111
                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 已提交
112
                        num += item.num;
M
maguohua 已提交
113 114 115 116 117
                    })
                    return num;
                }else {
                    return 0;
                }
M
upata  
maguohua 已提交
118
            },
M
maguohua 已提交
119
        },
M
upata  
maguohua 已提交
120
        props:['foods', 'shopId'],
M
maguohua 已提交
121
        methods: {
M
upata  
maguohua 已提交
122 123 124
            ...mapMutations([
                'ADD_CART','REDUCE_CART',
            ]),
M
maguohua 已提交
125
            //移出购物车
M
maguohua 已提交
126 127
            removeOutCart(category_id, item_id, food_id, name, price, specs){
                if (this.foodNum > 0) {
M
upata  
maguohua 已提交
128
                    this.REDUCE_CART({shopid: this.shopId, category_id, item_id, food_id, name, price, specs});
M
maguohua 已提交
129 130
                }
            },
M
maguohua 已提交
131 132
            //加入购物车,计算按钮位置。
            addToCart(category_id, item_id, food_id, name, price, specs, event){
M
upata  
maguohua 已提交
133
                this.ADD_CART({shopid: this.shopId, category_id, item_id, food_id, name, price, specs});
M
maguohua 已提交
134 135 136 137
                this.elLeft = event.target.getBoundingClientRect().left;
                this.elBottom = event.target.getBoundingClientRect().bottom;
                this.showMoveDot.push(true);

M
maguohua 已提交
138
            },
M
maguohua 已提交
139
            //显示规格列表
M
maguohua 已提交
140 141 142 143
            showChooseList(){
                this.showSpecs = !this.showSpecs;
                this.specsIndex = 0;
            },
M
maguohua 已提交
144 145 146 147 148
            //记录当前所选规格的索引值
            chooseSpecs(index){
                this.specsIndex = index;
            },
            //多规格商品加入购物车
M
maguohua 已提交
149
            addSpecs(category_id, item_id, food_id, name, price, specs){
M
upata  
maguohua 已提交
150
                this.ADD_CART({shopid: this.shopId, category_id, item_id, food_id, name, price, specs});
M
maguohua 已提交
151 152
                this.showChooseList();
            },
M
maguohua 已提交
153
            //点击多规格商品的减按钮,弹出提示
M
maguohua 已提交
154 155 156 157 158 159
            showReduceTip(){
                this.showDeleteTip = true;
                clearTimeout(this.timer);
                this.timer = setTimeout(() => {
                    clearTimeout(this.timer);
                    this.showDeleteTip = false;
M
maguohua 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
                }, 3000);
            },
            beforeEnter(el){
                el.style.transform = `translate3d(0,${39 + this.elBottom - this.windowHeight}px,0)`;
                el.children[0].style.transform = `translate3d(${this.elLeft - 40}px,0,0)`;
            },
            afterEnter(el){
                el.style.transform = `translate3d(0,0,0)`;
                el.children[0].style.transform = `translate3d(0,0,0)`;
                el.style.transition = 'all .55s cubic-bezier(0.3, -0.19, 0.65, -0.15)';
                el.children[0].style.transition = 'all .55s linear';
                //圆点到达目标点后移出
                this.showMoveDot = this.showMoveDot.map(item => false);
                //监听运动结束,通知父级进行后续操作
                el.children[0].addEventListener('transitionend', () => {
                    this.$emit('moveInCart')
                })
M
maguohua 已提交
177 178
            }
        },
M
maguohua 已提交
179 180 181 182 183 184 185 186 187
    }
</script>

<style lang="scss" scoped>
    @import '../../style/mixin.scss';
	.cart_module{
        .cart_button{
            display: flex;
            align-items: center;
M
maguohua 已提交
188 189 190 191 192 193
            .move_dot{
                position: fixed;
                bottom: 40px;
                left: 40px;
                z-index: 11;
            }
M
maguohua 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206
        }
        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 已提交
207 208
        }
        .choose_specification{
M
maguohua 已提交
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
            .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 已提交
235 236 237 238 239 240
            .specs_cover{
                position: fixed;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
M
maguohua 已提交
241
                background-color: rgba(0,0,0,.4);
M
maguohua 已提交
242 243 244
                z-index: 17;
            }
            .specs_list{
M
maguohua 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
                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 已提交
319
            }
M
maguohua 已提交
320 321
        }
    }
M
maguohua 已提交
322
    .showReduce-enter-active, .showReduce-leave-active {
M
maguohua 已提交
323
        transition: all .3s ease-out;
M
maguohua 已提交
324 325 326 327 328 329
    }
    .showReduce-enter, .showReduce-leave-active {
        opacity: 0;
        transform: translateX(1rem);
    }
    .fade-enter-active, .fade-leave-active {
M
maguohua 已提交
330
        transition: all .3s;
M
maguohua 已提交
331 332 333 334
    }
    .fade-enter, .fade-leave-active {
        opacity: 0;
    }
M
maguohua 已提交
335 336 337 338 339 340 341
    .fadeBounce-enter-active, .fadeBounce-leave-active {
        transition: all .3s;
    }
    .fadeBounce-enter, .fadeBounce-leave-active {
        opacity: 0;
        transform: scale(.7);
    }
M
maguohua 已提交
342
    
M
maguohua 已提交
343
</style>
M
maguohua 已提交
344