goods.js 21.2 KB
Newer Older
D
v1.2.0  
devil_gong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 规格弹窗PC显示
function poptit_pc_show()
{
    $(document.body).css('position', 'static');
    $('.theme-signin-left').scrollTop(0);
    $('.theme-popover-mask').hide();
    $('.theme-popover').slideDown(0);
}
// 规格弹窗关闭
function poptit_close()
{
    if($(window).width() < 1025)
    {
        $(document.body).css('position', 'static');
        $('.theme-signin-left').scrollTop(0);
        $('.theme-popover-mask').hide();
        $('.theme-popover').slideUp(100);
    }
}

G
gongfuxiang 已提交
21 22 23 24 25 26 27 28 29 30
/**
 * 评论记录ajax请求
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  1.0.0
 * @datetime 2019-05-13T21:39:55+0800
 * @param    {[int]}                 page [分页值]
 */
function GoodsCommentsHtml(page)
{
D
devil_gong 已提交
31 32 33 34 35 36 37
    if((page || 1) <= 1)
    {
        $('.goods-page-no-data').removeClass('none');
        $('.goods-page-no-data span').text('加载中...');
    } else {
        $('.goods-page-no-data').addClass('none');
    }
G
gongfuxiang 已提交
38 39 40 41

    $.ajax({
        url: $('.goods-comment').data('url'),
        type:'POST',
D
devil_gong 已提交
42
        data:{"goods_id": $('.goods-comment').data('goods-id'), "page": page || 1},
G
gongfuxiang 已提交
43 44 45
        dataType:'json',
        success:function(result)
        {
D
devil_gong 已提交
46
            $('.goods-page-no-data').addClass('none');
G
gongfuxiang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
            if(result.code == 0)
            {
                var html = '';
                for(var i in result.data.data)
                {
                    html += '<article class="am-comment">';
                    html += '<img src="'+result.data.data[i]['user']['avatar']+'" class="am-comment-avatar" alt="'+result.data.data[i]['user']['user_name_view']+'" />';
                    html += '<div class="am-comment-main">';
                    html += '<header class="am-comment-hd">';
                    html += '<div class="am-comment-meta">';
                    html += '<span class="am-comment-author">'+result.data.data[i]['user']['user_name_view']+'</span>';
                    html += ' 评论于 <time datetime="">'+result.data.data[i]['add_time_time']+'</time>';
                    html += '</div>';
                    html += '</header>';
                    html += '<div class="am-comment-bd">';
                    html += '<p>'+result.data.data[i]['content']+'</p>';

                    // 规格
D
devil_gong 已提交
65
                    if((result.data.data[i]['msg'] || null) != null)
G
gongfuxiang 已提交
66
                    {
D
devil_gong 已提交
67
                        html += '<p class="comment-spec">'+result.data.data[i]['msg']+'</p>';
G
gongfuxiang 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
                    }

                    // 回复
                    if(result.data.data[i]['is_reply'] == 1 && (result.data.data[i]['reply'] || null) != null)
                    {
                        html += '<div class="comment-reply">';
                        html += '<span class="comment-reply-title">管理员回复:</span>';
                        html += '<span class="comment-reply-desc">'+result.data.data[i]['reply']+'</span>';
                        html += '</div>';
                    }
                    html += '</div>';
                    html += '</article>';
                }

                $('.goods-comment-content').html(html);
                $('.goods-page-container').html(PageLibrary(result.data.total, result.data.number, page, 2));
            }

            // 没有数据
            if($('.goods-comment-content article').length <= 0)
            {
                $('.goods-page-no-data').removeClass('none');
D
devil_gong 已提交
90
                $('.goods-page-no-data span').text('没有评论数据');
G
gongfuxiang 已提交
91
            }
D
devil_gong 已提交
92 93 94 95 96
        },
        error:function(result)
        {
            $('.goods-page-no-data').removeClass('none');
            $('.goods-page-no-data span').text('请求出现错误,请稍后再试!');
G
gongfuxiang 已提交
97 98 99 100
        }
    });
}

D
v1.2.0  
devil_gong 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113
/**
 * 购买/加入购物车
 * @author   Devil
 * @blog    http://gong.gg/
 * @version 1.0.0
 * @date    2018-09-13
 * @desc    description
 * @param   {[object]}        e [当前标签对象]
 */
function CartAdd(e)
{
    // 参数
    var type = e.attr('data-type');
D
devil_gong 已提交
114 115 116 117 118 119
    var stock = parseInt($('#text_box').val()) || 1;
    var inventory = parseInt($('.stock-tips .stock').text());
    var min = $('.stock-tips .stock').data('min-limit') || 1;
    var max = $('.stock-tips .stock').data('max-limit') || 0;
    var unit = $('.stock-tips .stock').data('unit') || '';
    if(stock < min)
D
v1.2.0  
devil_gong 已提交
120
    {
D
devil_gong 已提交
121 122 123 124 125 126 127 128 129 130 131
        PromptCenter('最低起购数量'+min+unit);
        return false;
    }
    if(max > 0 && stock > max)
    {
        PromptCenter('最大限购数量'+max+unit);
        return false;
    }
    if(stock > inventory)
    {
        PromptCenter('库存数量'+inventory+unit);
D
v1.2.0  
devil_gong 已提交
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
        return false;
    }

    // 规格
    var spec = [];
    var sku_count = $('.sku-items').length;
    if(sku_count > 0)
    {
        var spec_count = $('.sku-line.selected').length;
        if(spec_count < sku_count)
        {
            $('.sku-items').each(function(k, v)
            {
                if($(this).find('.sku-line.selected').length == 0)
                {
                    $(this).addClass('attr-not-active');
                }
            });
            PromptCenter('请选择规格');
            return false;
        } else {
            $('.iteminfo_parameter .sku-items').removeClass('attr-not-active');
            $('.theme-signin-left .sku-items li.selected').each(function(k, v)
            {
                spec.push({"type": $(this).data('type-value'), "value": $(this).data('value')})
            });
        }
    }

    // 操作类型
    switch(type)
    {
        // 立即购买
        case 'buy' :
            var $form_buy = $('form.buy-form');
            $form_buy.find('input[name="spec"]').val(JSON.stringify(spec));
            $form_buy.find('input[name="stock"]').val(stock);
            $form_buy.find('button[type="submit"]').trigger('click');
            break;

        // 加入购物车
        case 'cart' :
            // 开启进度条
            $.AMUI.progress.start();

            var $button = e;
            $button.attr('disabled', true);

            // ajax请求
            $.ajax({
                url: e.data('ajax-url'),
                type: 'post',
                dataType: "json",
                timeout: 10000,
                data: {"goods_id": $('.goods-detail').data('id'), "stock": stock, "spec": spec},
                success: function(result)
                {
                    poptit_close();
                    $.AMUI.progress.done();
                    $button.attr('disabled', false);

                    if(result.code == 0)
                    {
                        HomeCartNumberTotalUpdate(parseInt(result.data));
                        PromptCenter(result.msg, 'success');
                    } else {
                        PromptCenter(result.msg);
                    }
                },
                error: function(xhr, type)
                {
                    poptit_close();
                    $.AMUI.progress.done();
                    $button.attr('disabled', false);
D
devil_gong 已提交
206
                    PromptCenter('服务器错误');
D
v1.2.0  
devil_gong 已提交
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
                }
            });
            break;

        // 默认
        default :
            PromptCenter('操作参数配置有误');
    }
    return true;
}

/**
 * 获取规格详情
 * @author   Devil
 * @blog    http://gong.gg/
 * @version 1.0.0
 * @date    2018-12-14
 * @desc    description
 */
function GoodsSpecDetail()
{
    // 是否全部选中
    var sku_count = $('.theme-signin-left .sku-items').length;
    var active_count = $('.theme-signin-left .sku-items li.selected').length;
    if(active_count < sku_count)
    {
        return false;
    }

    // 获取规格值
    var spec = [];
    $('.theme-signin-left .sku-items li.selected').each(function(k, v)
    {
        spec.push({"type": $(this).data('type-value'), "value": $(this).data('value')})
    });

    // 开启进度条
    $.AMUI.progress.start();

    // ajax请求
    $.ajax({
        url: $('.goods-detail').data('spec-detail-ajax-url'),
        type: 'post',
        dataType: "json",
        timeout: 10000,
        data: {"id": $('.goods-detail').data('id'), "spec": spec},
        success: function(result)
        {
            $.AMUI.progress.done();
            if(result.code == 0)
            {
                $('.text-info .price-now').text(''+result.data.price);
                $('.sys_item_price').text(result.data.price);
                $('.number-tag input[type="number"]').attr('max', result.data.inventory);
                $('.stock-tips .stock').text(result.data.inventory);
                if(result.data.original_price > 0)
                {
                    $('.sys_item_mktprice').text(''+result.data.original_price);
                    $('.sys_item_mktprice').show();
                } else {
                    $('.sys_item_mktprice').hide();
                }
            } else {
                if($(window).width() < 640)
                {
                    PromptBottom(result.msg, null, null, 50);
                } else {
                    PromptCenter(result.msg);
                }
            }
        },
        error: function(xhr, type)
        {
            $.AMUI.progress.done();
            if($(window).width() < 640)
            {
D
devil_gong 已提交
283
                PromptBottom('服务器错误', null, null, 50);
D
v1.2.0  
devil_gong 已提交
284
            } else {
D
devil_gong 已提交
285
                PromptCenter('服务器错误');
D
v1.2.0  
devil_gong 已提交
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
            }
        }
    });
}

/**
 * 获取规格类型
 * @author   Devil
 * @blog    http://gong.gg/
 * @version 1.0.0
 * @date    2018-12-14
 * @desc    description
 */
function GoodsSpecType()
{
    // 是否全部选中
    var sku_count = $('.theme-signin-left .sku-items').length;
    var active_count = $('.theme-signin-left .sku-items li.selected').length;
    if(active_count <= 0 || active_count >= sku_count)
    {
        return false;
    }

    // 获取规格值
    var spec = [];
    $('.theme-signin-left .sku-items li.selected').each(function(k, v)
    {
        spec.push({"type": $(this).data('type-value'), "value": $(this).data('value')})
    });

    // 开启进度条
    $.AMUI.progress.start();

    // ajax请求
    $.ajax({
        url: $('.goods-detail').data('spec-type-ajax-url'),
        type: 'post',
        dataType: "json",
        timeout: 10000,
        data: {"id": $('.goods-detail').data('id'), "spec": spec},
        success: function(result)
        {
            $.AMUI.progress.done();
            if(result.code == 0)
            {
                var spec_count = spec.length;
                var index = (spec_count > 0) ? spec_count : 0;
                if(index < sku_count)
                {
                    $('.theme-signin-left .sku-items').eq(index).find('li').each(function(k, v)
                    {
                        $(this).removeClass('sku-dont-choose');
338
                        var value = $(this).data('value').toString();
D
v1.2.0  
devil_gong 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
                        if(result.data.indexOf(value) == -1)
                        {
                            $(this).addClass('sku-items-disabled');
                        } else {
                            $(this).removeClass('sku-items-disabled');
                        }
                    });
                }
            } else {
                if($(window).width() < 640)
                {
                    PromptBottom(result.msg, null, null, 50);
                } else {
                    PromptCenter(result.msg);
                }
            }
        },
        error: function(xhr, type)
        {
            $.AMUI.progress.done();
            if($(window).width() < 640)
            {
D
devil_gong 已提交
361
                PromptBottom('服务器错误', null, null, 50);
D
v1.2.0  
devil_gong 已提交
362
            } else {
D
devil_gong 已提交
363
                PromptCenter('服务器错误');
D
v1.2.0  
devil_gong 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
            }
        }
    });
}

/**
 * 商品基础数据恢复
 * @author   Devil
 * @blog    http://gong.gg/
 * @version 1.0.0
 * @date    2018-12-25
 * @desc    description
 */
function GoodsBaseRestore()
{
    $('.text-info .price-now').text(''+$('.text-info .price-now').data('original-price'));
G
gongfuxiang 已提交
380
    $('.sys_item_mktprice').text($('.sys_item_mktprice').data('original-price'));
D
v1.2.0  
devil_gong 已提交
381 382 383 384 385 386 387 388 389 390 391
    $('.sys_item_price').text($('.sys_item_price').data('original-price'));
    $('.number-tag input[type="number"]').attr('max', $('.number-tag input[type="number"]').data('original-max'));
    $('.stock-tips .stock').text($('.stock-tips .stock').data('original-stock'));
}

$(function() {
    // 商品规格选择
    $('.theme-options').each(function()
    {
        $(this).find('ul>li').on('click', function()
        {
392
            // 切换规格购买数量清空
D
devil_gong 已提交
393
            $('#text_box').val($('.stock-tips .stock').data('min-limit') || 1);
394 395
            
            // 规格处理
D
v1.2.0  
devil_gong 已提交
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
            var length = $('.theme-signin-left .sku-items').length;
            var index = $(this).parents('.sku-items').index();

            if($(this).hasClass('selected'))
            {
                $(this).removeClass('selected');

                // 去掉元素之后的禁止
                $('.theme-signin-left .sku-items').each(function(k, v)
                {
                    if(k > index)
                    {
                        $(this).find('li').removeClass('sku-items-disabled').removeClass('selected').addClass('sku-dont-choose');
                    }
                });

                // 数据还原
                GoodsBaseRestore();

            } else {
                if($(this).hasClass('sku-items-disabled') || $(this).hasClass('sku-dont-choose'))
                {
                    return false;
                }
                $(this).addClass('selected').siblings('li').removeClass('selected');
                $(this).parents('.sku-items').removeClass('attr-not-active');

                // 去掉元素之后的禁止
                if(index < length)
                {
                    $('.theme-signin-left .sku-items').each(function(k, v)
                    {
                        if(k > index)
                        {
                            $(this).find('li').removeClass('sku-items-disabled').removeClass('selected').addClass('sku-dont-choose');
                        }
                    });
                }

                // 是否存在规格图片
                var spec_images = $(this).data('type-images') || null;
                if(spec_images != null)
                {
                    $('.jqzoom').attr('src', spec_images);
                    $('.jqzoom').attr('rel', spec_images);
                    $('.img-info img').attr('src', spec_images);
                }

                // 后面是否还有未选择的规格
                if(index < length-1)
                {
                    // 数据还原
                    GoodsBaseRestore();
                }

                // 获取下一个规格类型
                GoodsSpecType();

                // 获取规格详情
                GoodsSpecDetail();
            }
        });
    });

    // 放大镜初始化
G
gongfuxiang 已提交
461 462 463
    $('.jqzoom').imagezoom({
        yzoom: 398
    });
D
v1.2.0  
devil_gong 已提交
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
    $('#thumblist li a').on('mouseover', function() {
        $(this).parents('li').addClass('tb-selected').siblings().removeClass('tb-selected');
        $('.jqzoom').attr('src', $(this).find('img').attr('mid'));
        $('.jqzoom').attr('rel', $(this).find('img').attr('big'));
    });

    //弹出规格选择
    $('.buy-event').on('click', function() {
        if($(window).width() < 1025) {
            // 是否登录
            if(__user_id__ != 0)
            {
                $(document.body).css('position', 'fixed');
                $('.theme-popover-mask').show();
                $('.theme-popover').slideDown(200);

                $('.theme-popover .confirm').attr('data-type', $(this).data('type'));
            }
        } else {
            poptit_pc_show();
        }
    });
    $('.theme-poptit .close, .btn-op .close').on('click', function() {
        poptit_close();
    });

    // 购买
    $('.buy-submit, .cart-submit').on('click', function()
    {
        // 是否登录
        if(__user_id__ != 0)
        {
            if($(window).width() >= 1025)
            {
                CartAdd($(this));
            }
        }
    });
    $('.theme-popover .confirm').on('click', function()
    {
        // 是否登录
        if(__user_id__ != 0)
        {
            if($(window).width() < 1025)
            {
                CartAdd($(this));
            }
        }
    });

    // 收藏
    $('.favor-submit').on('click', function()
    {
        // 是否登录
        if(__user_id__ != 0)
        {
            var $this = $(this);
            // 开启进度条
            $.AMUI.progress.start();

            // ajax请求
            $.ajax({
                url: $(this).data('ajax-url'),
                type: 'post',
                dataType: "json",
                timeout: 10000,
                data: {"id": $('.goods-detail').data('id')},
                success: function(result)
                {
                    poptit_close();
                    $.AMUI.progress.done();

                    if(result.code == 0)
                    {
                        $this.find('.goods-favor-text').text(result.data.text);
                        $this.find('.goods-favor-count').text('('+result.data.count+')');
                        if(result.data.status == 1)
                        {
                            $this.addClass('text-active');
                        } else {
                            $this.removeClass('text-active');
                        }

                        if($(window).width() < 640)
                        {
                            PromptBottom(result.msg, 'success', null, 50);
                        } else {
                            PromptCenter(result.msg, 'success');
                        }
                    } else {
                        if($(window).width() < 640)
                        {
                            PromptBottom(result.msg, null, null, 50);
                        } else {
                            PromptCenter(result.msg);
                        }
                    }
                },
                error: function(xhr, type)
                {
                    poptit_close();
                    $.AMUI.progress.done();
                    if($(window).width() < 640)
                    {
D
devil_gong 已提交
568
                        PromptBottom('服务器错误', null, null, 50);
D
v1.2.0  
devil_gong 已提交
569
                    } else {
D
devil_gong 已提交
570
                        PromptCenter('服务器错误');
D
v1.2.0  
devil_gong 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
                    }
                }
            });
        }
    });

    // 视频
    $('.goods-video-submit-start').on('click', function()
    {
        $('.goods-video-container').removeClass('none').trigger('play');
        $('.goods-video-submit-close').removeClass('none');
        $('.goods-video-submit-start').addClass('none');
    });
    $('.goods-video-submit-close').on('click', function()
    {
        $('.goods-video-container').addClass('none').trigger('pause');
        $('.goods-video-submit-close').addClass('none');
        $('.goods-video-submit-start').removeClass('none');
    });

    //获得文本框对象
D
devil_gong 已提交
592 593 594 595
    var $sotck = $('#text_box');
    var min = $('.stock-tips .stock').data('min-limit') || 1;
    var max = $('.stock-tips .stock').data('max-limit') || 0;
    var unit = $('.stock-tips .stock').data('unit') || '';
D
v1.2.0  
devil_gong 已提交
596

G
gongfuxiang 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
    // 手动输入
    $sotck.on('blur', function()
    {
        var number = parseInt($(this).val());
        var inventory = parseInt($('.stock-tips .stock').text());
        if(number > inventory)
        {
            number = inventory;
        }
        if(number <= 1)
        {
            number = 1;
        }
        $sotck.val(number);
        console.log(number)
    });

D
v1.2.0  
devil_gong 已提交
614
    //数量增加操作
D
devil_gong 已提交
615 616 617 618 619
    $('#add').on('click', function()
    {
        var inventory = parseInt($('.stock-tips .stock').text());
        var number = parseInt($sotck.val())+1;
        if(max > 0 && number > max)
D
v1.2.0  
devil_gong 已提交
620
        {
D
devil_gong 已提交
621 622 623 624 625 626 627
            Prompt('最大限购数量'+max+unit);
            return false;
        }
        if(number > inventory)
        {
            Prompt('库存数量'+inventory+unit);
            return false;
D
v1.2.0  
devil_gong 已提交
628
        }
D
devil_gong 已提交
629
        $sotck.val(number);
D
v1.2.0  
devil_gong 已提交
630 631
    });
    //数量减少操作
D
devil_gong 已提交
632 633
    $('#min').on('click', function()
    {
D
devil_gong 已提交
634
        var value = parseInt($sotck.val())-1 || 1;
D
devil_gong 已提交
635 636 637 638 639 640
        if(value < min)
        {
            Prompt('最低起购数量'+min+unit);
            return false;
        }
        $sotck.val(value);
D
devil_gong 已提交
641 642
    });

G
gongfuxiang 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
    // 评论
    GoodsCommentsHtml(1);
    $(document).on('click', '.goods-page-container .pagelibrary a', function()
    {
        var page = $(this).data('page') || null;
        if(page != null)
        {
            // 获取数据
            GoodsCommentsHtml(page);

            // 回到评论顶部位置
            var top = $('.introduce-main').offset().top;
            $(window).smoothScroll({position: top});
        }
    });

    // 累计评价点击事件
    $('.tm-ind-panel .ind-panel-comment').on('click', function()
    {
        var top = $('.introduce-main').offset().top;
        $(window).smoothScroll({position: top});
        $('.introduce-main .am-tabs').tabs('open', 1);
    });

D
devil_gong 已提交
667 668 669 670 671 672 673
    // tab事件
    $('.introduce-main .am-tabs li').on('click', function()
    {
        var top = $('.introduce-main').offset().top;
        $(window).smoothScroll({position: top});
    });

D
devil_gong 已提交
674
});
D
v1.2.0  
devil_gong 已提交
675

D
devil_gong 已提交
676 677 678 679 680 681 682 683 684 685
// 浏览器窗口实时事件
$(window).resize(function()
{
    // 规格显示/隐藏处理
    if($(window).width() < 1025)
    {
        poptit_close();
    } else {
        poptit_pc_show();
    }
D
v1.2.0  
devil_gong 已提交
686
});