index.html 23.6 KB
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 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 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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
<!DOCTYPE html>
<html lang="zh">
{include file="public/head"/}
<!--swiper引入-->
<link rel="stylesheet" href="__CDN__/assets/store/js/Swiper-3.4.2/css/swiper.min.css">
<script type="text/javascript" src="__CDN__/assets/store/js/Swiper-3.4.2/js/swiper.min.js"></script>
<style>
    body{
        background: rgba(249,249,249,1);
    }
    .content{
        width: 100%;
        font-size: 0;
        background: rgba(249,249,249,1);
    }

    /*购物车样式*/
    .content .shoppingCartBox{
        width: 100%;
        background: rgba(249,249,249,1);
    }

    /*购物车标题部分样式*/
    .content .shoppingCartBox .shoppingCartTitleBox{
        width: 1200px;
        background: rgba(255,255,255,1);
        margin: 0 auto 12px auto;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: 500;
        color: rgba(61,68,77,1);
        border-radius:8px;
    }
    .content .shoppingCartBox .shoppingCartTitleBox .titleBox{
        width: 1200px;
        height: 62px;
        line-height: 62px;
    }
    .content .shoppingCartBox .shoppingCartTitleBox .titleBox span:first-child{
        display: inline-block;
        float: left;
        width: 6px;
        height: 20px;
        margin-top: 21px;
        background: rgba(0,159,142,1);
        border-radius: 2px;
    }
    .content .shoppingCartBox .shoppingCartTitleBox .titleBox span:nth-child(2){
        height: 22px;
        line-height: 22px;
        float: left;
        margin-top: 20px;
        padding-left: 12px;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: 500;
        color: rgba(0,159,142,1);
    }
    .content .shoppingCartBox .shoppingCartTitleBox .shoppingCartTitle{
        width: 100%;
        height: 70px;
        padding: 0 30px;
        line-height: 70px;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: 500;
        color: rgba(61,68,77,1);
    }
    .content .shoppingCartBox .shoppingCartTitleBox .shoppingCartTitle table{
        width: 100%;
        height: 100%;
    }

    /*购物车商品部分样式*/
    .content .shopListBox{
        width: 1200px;
        margin: 0 auto;
    }
    .content .shopListBox .shopBox{
        margin-bottom: 12px;
    }
    .content .shopListBox .shopBox .title{
        width: 100%;
        height: 39px;
        padding: 0 30px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 500;
        color: rgba(61,68,77,1);
        border-bottom: 1px solid rgba(238,238,238,1);
        background: rgba(255,255,255,1);
        border-radius: 8px 8px 0 0;
    }
    .content .shopListBox .shopBox .title p{
        display: inline-block;
        height: 39px;
        line-height: 39px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight:500;
        color: rgba(61,68,77,1);
    }
    .content .shopListBox .shopBox .title p:first-child{
        width: 276px;
    }
    .content .shopListBox .shopBox .title p:nth-child(2){
        width: 130px;
    }
    .content .shopListBox .shopBox .title p:nth-child(3){
        color:rgba(234,50,43,1);
    }
    .content .shopListBox .shopBox .title p:nth-child(3) img{
        margin-top: -5px;
    }
    .content .shopListBox .shopBox .title p:nth-child(3) span{
        /*vertical-align: middle;*/
    }
    .content .shopListBox .shopBox ul{
        width: 100%;
        background: rgba(255,255,255,1);
        border-radius: 0 0 8px 8px;
    }
    .content .shopListBox .shopBox ul li{
        list-style-type: none;
        width: 100%;
        height: 120px;
        padding: 0 30px;

    }
    .content .shopListBox .shopBox ul li div{
        display: inline-block;
    }
    .content .shopListBox .shopBox ul li .radioBox{
        width: 40px;
        height: 100%;
        line-height: 120px;
        border-bottom: transparent;
    }
    .content .shopListBox .shopBox ul li .imgBox{
        width: 110px;
        height: 100%;
        line-height: 120px;
        display: inline-block;
    }
    .content .shopListBox .shopBox ul li .imgBox img{
        width: 72px;
        height: 72px;
    }
    .content .shopListBox .shopBox ul li .borderBox{
        width: 1100px;
        height: 100%;
        float: right;
        border-bottom:1px solid rgba(238,238,238,1);
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 500;
        color: rgba(91,94,99,1);

    }
    .content .shopListBox .shopBox ul li .commodityNameBox{
        width: 412px;
        height: 100%;
    }
    .content .shopListBox .shopBox ul li .commodityNameBox p{
        margin: 0;
    }
    .content .shopListBox .shopBox ul li .unitPriceBox{
        width: 109px;
        height: 100%;
        text-align: center;
    }
    .content .shopListBox .shopBox ul li .numberBox{
        width: 183px;
        height: 100%;
        text-align: center;
    }
    .content .shopListBox .shopBox ul li .numberBox .input-group-btn{
        width: 30px;
        height: 30px;
    }
    .content .shopListBox .shopBox ul li .numberBox .input-group-btn button{
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
        border:1px solid rgba(238,238,238,1);
        color: #5B5E63;
    }
    .content .shopListBox .shopBox ul li .numberBox .input-group-btn:first-child{
        float: left;
    }
    .content .shopListBox .shopBox ul li .numberBox .contInput{
        width: 56px;
        height: 30px;
        text-align: center;
        color: #5B5E63;
    }
    .content .shopListBox .shopBox ul li .subtotalBox{
        width: 150px;
        height: 100%;
        text-align: center;
    }
    .content .shopListBox .shopBox ul li .operationBox{
        width: 110px;
        height: 100%;
        text-align: right;
    }
    .content .shopListBox .shopBox ul li .operationBox .btnSpan{
        cursor: pointer;
    }
    .content .shopListBox .shopBox ul li .operationBox .btnSpan span{
        vertical-align: middle;
    }

    /*结算订单部分样式*/
    .content .submitBox {
        width: 1200px;
        height: 52px;
        background: rgba(255, 255, 255, 1);
        border-radius: 8px;
        margin: 60px auto 50px auto;
        padding-left: 30px;
    }
    .content .submitBox .myCheckBox{
        display: inline-block;
        width: 66px;
        height: 52px;
        line-height: 52px;
        margin: 0;
        font-size:12px;
        font-family:PingFang SC;
        font-weight:500;
        color:rgba(140,145,152,1);
    }
    .content .submitBox .myCheckBox #checkAll_bottom{
        vertical-align: middle;
        margin: -2px 6px 0 0;
    }
    .content .submitBox .myCheckBox span{
        /*vertical-align: middle;*/
    }
    .content .submitBox p {
        display: inline-block;
        width: 100px;
        height: 52px;
        line-height: 52px;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 500;
        color: rgba(140, 145, 152, 1);
        margin: 0;
    }
    .content .submitBox .btnP{
        cursor: pointer;
    }
    .content .submitBox .totalPrice {
        width: 730px;
        text-align: right;
        padding-right: 21px;
    }
    .content .submitBox .totalPrice span {
        font-size: 16px;
        color: rgba(234, 50, 43, 1);
    }
    .content .submitBox .totalPrice #totalCount{
        font-size: 12px;
        color: rgba(61,68,77,1);
    }
    .content .submitBox .btn {
        width: 170px;
        height: 52px;
        background: rgba(0, 159, 142, 1);
        border: 0;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: 500;
        line-height: 22px;
        color: rgba(255, 255, 255, 1);
        border-radius: 0;
        margin-top: -12px;
    }
</style>
<body>
{include file="public/header"/}
<!--主要内容-->
<div class="content">
    {include file="public/nav"}
    <div class="shoppingCartBox">
        <!--购物车头部部分-->
        <div class="shoppingCartTitleBox">
            <div class="titleBox">
                <span></span>
                <span>我的购物车</span>
            </div>
            <div class="shoppingCartTitle">
                <table>
                    <tr>
                        <td style="width: 155px">
                            <div style="display:inline-block;height: 70px;line-height: 70px">
                                <input id="checkAll_top" type="checkbox" style="margin: 0 10px 0 0;vertical-align: middle">
                            </div>

                            <span style="vertical-align: middle">全选</span>
                        </td>
                        <td style="width: 415px">商品</td>
                        <td style="width: 117px;text-align: center">单价</td>
                        <td style="width: 186px;text-align: center">数量</td>
                        <td style="width: 152px;text-align: center">小计</td>
                        <td style="text-align: right">操作</td>
                    </tr>
                </table>
            </div>
        </div>

        <!--购物车商品部分-->
        <div class="shopListBox">
            {foreach name="$data" item="vo"}
            <div class="shopBox" data-free_freight="{$vo.money}" data-freight="{$vo.freight}">
                <div class="title">
                    <p>{$vo.name}</p>
                    <p>运费:{eq name="$vo.freight" value="0"}免运费{else /}¥{$vo.freight}{/eq}</p>
                    {neq name="$vo.freight" value="0"}
                    <p>
                        <img src="__CDN__/assets/store/images/discountImg.png" alt="img">
                        <span>满{$vo.money}元免运费</span>
                    </p>
                    {/neq}
                </div>
                <ul>
                    {foreach name="$vo.car" item="c"}
                    <li>
                        <div class="radioBox"><input class="commodityCheck" type="checkbox" data-car_id="{$c.id}" data-goods_id="{$c.goods_id}" data-number="{$c.number}" value="{$c.id}"></div>
                        <div class="borderBox">
                            <div class="imgBox">
                                <img src="{$c.goods.thumbnail}" alt="img">
                            </div>
                            <div class="commodityNameBox">
                                <p style="width: 196px">{$c.goods.goodsname}</p>
                            </div>
                            <div class="unitPriceBox">
                                ¥{$c.goods.price}
                            </div>
                            <div class="numberBox">
                                <div class="input-group">
                                    <div class="input-group-btn">
                                        <button type="button" class="btn btn-default car-decrease" data-goods_id="{$c.goods_id}" data-type="2">-</button>
                                    </div>
                                    <input type="text" class="form-control contInput" value="{$c.number}">
                                    <div class="input-group-btn">
                                        <button type="button" class="btn btn-default car-add" data-goods_id="{$c.goods_id}" data-type="1">+</button>
                                    </div>
                                </div>
                            </div>
                            <div class="subtotalBox">
                                ¥{$c.goods.price*$c.number}
                            </div>
                            <div class="operationBox">
                                <span class="btnSpan" onclick="del(this,{$c.id})">
                                    <img src="__CDN__/assets/store/images/icon_del.png" alt="">
                                    <span>删除</span>
                                </span>
                            </div>
                        </div>
                    </li>
                    {/foreach}
                </ul>
            </div>
            {/foreach}
        </div>

        <!--结算订单部分-->
        <div class="submitBox">
            <div class="myCheckBox">
                <input id="checkAll_bottom" type="checkbox">
                <span>全选</span>
            </div>
            <p class="btnP" onclick="delSelect()">删除选中商品</p>
            <p class="btnP" onclick="clearFun()">清空购物车</p>
            <p class="totalPrice">已选择&nbsp;<span id="totalCount">0</span>&nbsp;件商品&nbsp;&nbsp;&nbsp;总价:<span style="color: #EA322B">¥</span><span id="totalPrice">0.00</span></p>
            <button type="button" class="btn" onclick="settlement()">去结算</button>
        </div>
    </div>
</div>
{include file="public/footer"/}
{include file="public/js"/}
<script>
    $(function () {
        initLocation();
    });

    //定位当前位置
    function initLocation(){
        // 百度地图API功能
        var map = new BMap.Map("allmap");
        var point = new BMap.Point(116.331398,39.897445);
        map.centerAndZoom(point,12);
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function(r){
            $('#locationText').html(r.address.province + "" + r.address.city);
            if(this.getStatus() == BMAP_STATUS_SUCCESS){
                var mk = new BMap.Marker(r.point);
                map.addOverlay(mk);
                map.panTo(r.point);
                //省份名称转换为id
                $.ajax({
                    url:"{:url('index/sundry/get_province_id')}",
                    type:"POST",
                    data:{"province_name":r.address.province},
                    async:false,
                    success:function(res){
                        if(res.code == 1){
                            province_id = res.data.province_id;
                            lng = r.point.lng;
                            lat = r.point.lat;
                        }else{
                            toast(res.msg);
                        }
                    },
                    error:function(res){
                        toast('与服务器断开连接');
                    }
                });
            } else {
                toast('failed'+this.getStatus());
            }
        },{enableHighAccuracy: true})
    }

    //去结算
    function settlement() {
        if($("input[class='commodityCheck']:checked").length == 0){
            toast('请选择要结算的商品');
            return false;
        }
        var data = [];
        for(var i=0;i<$("input[class='commodityCheck']:checked").length;i++){
            // var obj = {};
            // var goods_id = $("input[class='commodityCheck']:checked").eq(i).attr('data-goods_id');
            // var number = $("input[class='commodityCheck']:checked").eq(i).attr('data-number');
            // obj.goods_id = goods_id;
            // obj.number = number;
            // data.push(obj);
            var car_id = $("input[class='commodityCheck']:checked").eq(i).attr('data-car_id');
            data.push(car_id);
        }
        // data = JSON.stringify(data);
        var str = "{:url('index/order/confirm',array('data'=>'DATA','province_id'=>'PROVINCE_ID'))}";
        // window.location.href = str.replace("DATA",data).replace("PROVINCE_ID",province_id);
        var car_ids = data.join(',');
        window.location.href = str.replace("DATA",car_ids).replace("PROVINCE_ID",province_id);
    }

    //单个删除按钮
    function del(_this,car_id) {
        $.ajax({
            url:"{:url('index/car/del')}",
            type:"POST",
            data:{'car_ids':car_id},
            success:function(res){
                if(res.code == 1){
                    $(_this).closest('li').remove();
                    for(var i=$('.shopBox').length;i>-1;i--){
                        console.log($($('.shopBox')[i]).find('li').length);
                        if($($('.shopBox')[i]).find('li').length == 0){
                            $($('.shopBox')[i]).remove();
                        }
                    }
                    contTotalPrice();
                    toast('操作成功');
                }
            },
            error:function(res){
                toast('与服务器断开连接');
            }
        });
    }

    //删除选中商品
    function delSelect() {
        var commodityCheck = $("input[class='commodityCheck']:checked");
        var car_ids = [];
        $(commodityCheck).each(function (key, vo) {
            car_ids.push(commodityCheck.eq(key).val());
        });
        if(car_ids.length < 1){
            toast('请至少选择一个商品删除~');
            return false;
        }
        car_ids = car_ids.toString();
        $.ajax({
            url:"{:url('index/car/del')}",
            type:"POST",
            data:{"car_ids":car_ids},
            success:function(res){
                if(res.code == 1){
                    $("input[class='commodityCheck']:checked").closest('li').remove();
                    for(var i=$('.shopBox').length;i>-1;i--){
                        console.log($($('.shopBox')[i]).find('li').length);
                        if($($('.shopBox')[i]).find('li').length == 0){
                            $($('.shopBox')[i]).remove();
                        }
                    }
                    contTotalPrice();
                    toast('操作成功');
                }else{
                    toast(res.msg);
                }
            },
            error:function(res){
                toast('与服务器断开连接');
            }
        });
    }

    //清空购物车
    function clearFun() {
        var commodityCheck = $("input[class='commodityCheck']");
        var car_ids = [];
        $(commodityCheck).each(function (key, vo) {
            car_ids.push(commodityCheck.eq(key).val());
        });
        car_ids = car_ids.toString();
        $.ajax({
            url:"{:url('index/car/del')}",
            type:"POST",
            data:{"car_ids":car_ids},
            success:function(res){
                if(res.code == 1){
                    $('.shopBox').remove();
                    contTotalPrice();
                    toast('操作成功');
                }else{
                    toast(res.msg);
                }
            },
            error:function(res){
                toast('与服务器断开连接');
            }
        });
    }

    //头部全选按钮
    $('#checkAll_top').on('change', function() {
        if(this.checked) {
            $('#checkAll_bottom').prop('checked', true)
        } else {
            $('#checkAll_bottom').prop('checked', false)
        }
        checkedAll(this)
    });

    //尾部全选按钮
    $('#checkAll_bottom').on('change', function() {
        if(this.checked) {
            $('#checkAll_top').prop('checked', true)
        } else {
            $('#checkAll_top').prop('checked', false)
        }
        checkedAll(this)
    });

    //全选、反选
    function checkedAll(_this) {
        if(_this.checked) {
            $('.shopBox').find('.commodityCheck').prop('checked', true);
        } else {
            $('.shopBox').find('.commodityCheck').prop('checked', false);
        }
        contTotalPrice();
    }

    //单个商品选择按钮
    $('.commodityCheck').on('change', function() {
        if($('.shopBox ul li').length == $("input[class='commodityCheck']:checked").length){
            $('#checkAll_top').prop('checked', true);
            $('#checkAll_bottom').prop('checked', true);
        }else{
            $('#checkAll_top').prop('checked', false);
            $('#checkAll_bottom').prop('checked', false);
        }
        contTotalPrice();
    });

    //数量减
    $('.car-decrease').on('click', function() {
        var self = $(this);
        var goods_id = self.attr('data-goods_id');
        var type = self.attr('data-type');
        $.ajax({
            url:"{:url('index/car/add')}",
            type:"POST",
            data:{'goods_id':goods_id,'type':type,'province_id':province_id},
            success:function(res){
                if(res.code == 1){
                    var val = parseInt(self.closest('li').find('.contInput').val());
                    (val===1)?1:(val--);
                    $(self.closest('li').find('.contInput')[0]).val(val);
                    contPrice(self, val);
                    toast('操作成功');
                }else{
                    toast(res.msg);
                }
            },
            error:function(res){
                toast('与服务器断开连接');
            }
        });
    });

    //数量加
    $('.car-add').on('click', function() {
        var self = $(this);
        var goods_id = self.attr('data-goods_id');
        var type = self.attr('data-type');
        $.ajax({
            url:"{:url('index/car/add')}",
            type:"POST",
            data:{'goods_id':goods_id,'type':type,'province_id':province_id},
            success:function(res){
                if(res.code == 1){
                    var val = parseInt(self.closest('li').find('.contInput').val());
                    val++;
                    $(self.closest('li').find('.contInput')[0]).val(val);
                    contPrice(self, val);
                    toast('操作成功');
                }else{
                    toast(res.msg);
                }
            },
            error:function(res){
                toast('与服务器断开连接');
            }
        });
    });

    //计算小计
    function contPrice(_this, val){
        var unitPrice = parseInt($($(_this).closest('li').find('.unitPriceBox')[0]).html().split('¥')[1]);
        $($(_this).closest('li').find('.subtotalBox')[0]).html('¥' + (unitPrice * val).toFixed(2));
        contTotalPrice();
    }

    //计算总数、总价
    function contTotalPrice() {
        var totalCount = 0;
        var totalPrice = 0;
        var totalFreight = 0;
        /*for(var i=0;i<$('.shopBox ul li').length;i++){
            if($($('.shopBox ul li')[i]).find('.commodityCheck')[0].checked==true){
                var subtotalNum = parseInt($($('.shopBox ul li')[i]).find('.contInput').val());
                totalCount += subtotalNum;

                var subtotal =  parseInt($($('.shopBox ul li')[i]).find('.subtotalBox').html().split('¥')[1]);

                var freight = parseInt($($('.shopBox')[i]).attr('data-freight'));//运费
                var free_freight = parseInt($($('.shopBox')[i]).attr('data-free_freight'));//满免运费
                if(subtotal < free_freight){
                    subtotal += freight;
                }
                console.log(subtotal);
                console.log(free_freight);
                console.log(freight);
                totalPrice += subtotal;
            }
        }*/
        for(var i1=0;i1<$('.shopBox').length;i1++){
            var freight = parseInt($($('.shopBox')[i1]).attr('data-freight'));//运费
            var free_freight = parseInt($($('.shopBox')[i1]).attr('data-free_freight'));//满免运费
            for(var i2=0;i2<$('.shopBox').eq(i1).find('ul li').length;i2++){
                if($('.shopBox').eq(i1).find('ul li').eq(i2).find('.commodityCheck')[0].checked==true) {
                    var subtotalNum = parseInt($('.shopBox').eq(i1).find('ul li').eq(i2).find('.contInput').val());
                    totalCount += subtotalNum;

                    var subtotal = parseInt($('.shopBox').eq(i1).find('ul li').eq(i2).find('.subtotalBox').html().split('¥')[1]);
                    totalPrice += subtotal;
                }
            }
            if (totalPrice < free_freight) {
                totalFreight += freight;
                totalPrice += totalFreight;
            }
            if($("input[class='commodityCheck']:checked").length == 0){
                totalPrice -= totalFreight;
            }
        }
        $('#totalCount').html(totalCount.toFixed(0));
        $('#totalPrice').html(totalPrice.toFixed(2));
    }
</script>
</body>
</html>