new-detail.js 17.5 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
require('product/detail/detail.page.css');
let $ = require('yoho-jquery'),
    tip = require('plugin/tip');
let productId = $('#productId').val();
let goodsId = $('#goodsId').val();
let productSkn = $('#productSkn').val();
let brandId = $('#brand-id').val();
let shopId = $('#shopId').val();
let bundleType = $('#bundleType').val();
let yoho = require('yoho-app');

require('common');

// 领券
require('./detail/brand-coupon');

// 页面底部异步加载的部分
require('./detail/desc');

require('common/invite-share');

// 为你优选
require('./detail/recommend-for-you-product-desc');

let detailFuns = require('./detail/functions');

let productName = $('.goods-name .name').text();
let productPic = $('.banner-container').find('img:first').attr('src');

if (productPic && productPic.indexOf('http:') < 0) {
    productPic = `http:${productPic}`;
}
let shareData = {
    title: `推荐这个很酷的潮流好物给你:${productName}`,
    link: location.href,
    desc: '最近很多 KOL 在用!据说要抢!',
    desCircle: `我发现了一个最近很火的潮流好物!${productName}`,
    imgUrl: productPic
};

let nowShareData = {
    title: `推荐这本城市指南给你:${productName}`,
    link: location.href.split('?')[0],
    desc: '最近很多 KOL 在推!据说是探索城市必备神器',
    desCircle: `最近被刷屏的这本城市指南终于可以买到了!${productName}`,
    imgUrl: productPic
};

const DOWNLOAD_URL = '//union.yoho.cn/union/app-downloads.html';

yoho.ready(function() {
    if (yoho.isMarsApp) {
        yoho.invokeMethod('set.shareInfo', shareData);
    } else if (yoho.isNowApp) {
        yoho.invokeMethod('set.shareInfo', nowShareData);
    }
});

window.rePosFooter && window.rePosFooter();

setTimeout(() => {
    let Swiper = require('yoho-swiper');

    // 顶部swiper
    let swiper = new Swiper('.banner-swiper', {
        preloadImages: false,
        lazyLoading: true,
        lazyLoadingInPrevNext: true,
        lazyLoadingOnTransitionStart: true,
        paginationClickable: true,
        pagination: '.banner-top .pagination-inner',
        nextButton: '.my-swiper-button-next',
        prevButton: '.my-swiper-button-prev',
        spaceBetween: 3
    });

    $('.banner-swiper').click(function() {
        $('.banner-top').toggleClass('hover');
        swiper.onResize();
    });
    $('.banner-top-single').click(function() {
        $('.banner-top-single').toggleClass('hover');
    });
}, 500);


// 动态加载数据
(function() {
    function renderInfo(data) {
        if (!data) {
            return;
        }
        let dbClass = 'data-bind';
        let infoDataHbs = require('product/detail/infodata.hbs'),
            priceDataHbs = require('product/detail/pricedata.hbs');

        // 商品价格
        if (data.goodsPrice) {
            $('.goods-price>.current-price').text(data.goodsPrice.currentPrice);
            $('.goods-price>.previous-price').text(data.goodsPrice.previousPrice);
        }

        // 限购码
        if (!data.noLimitGoodsBtn && !data.storeGood) { // 线下店限制时,不显示限购吗
            if (data.canGetLimitCode) {
                $('.data-can-get-limit-code').removeClass(dbClass);
            }
            if (data.codeEmpty) {
                $('.data-code-empty').removeClass(dbClass);
            }
            if (data.gotCode) {
                $('.data-got-code').removeClass(dbClass);
            }
        }

        if (data.studentPrice) {
            data.studentPrice = Math.round(data.studentPrice.toString().replace('¥', ''));
        }

        if (data.tickets && data.cartInfo) {
            let $downloadBtn = $('#download-go');
            let ticketsUrl;

            if ($downloadBtn.length) {
                ticketsUrl = $downloadBtn.attr('href');
            }

            if (!ticketsUrl) {
                let mktCode = window._getMktCode ? window._getMktCode() : '';

                ticketsUrl = `${DOWNLOAD_URL}?union_type=${mktCode}&openby:yohobuy={"action":"go.productDetail","params":{"product_skn":${data.productSkn}}}`;  // eslint-disable-line
            }

            data.cartInfo.ticketsUrl = ticketsUrl;
        }

        $('#placeholder-pricedata').replaceWith(priceDataHbs(data));
        $('#placeholder-infodata').replaceWith(infoDataHbs(data));

        setTimeout(function() {
            $('#isDepositAdvance').on('touchstart', function() {
                tip.show('定金预售商品只能在有货App购买');
                return false;
            });
        }, 200);

        // 没有线下店标示的商品绑定秒杀js
        if (!($('.cart-bar.store-good').length > 0)) {
            $(window).trigger('seckill', [data]);
        }
    }

    if (productId && (goodsId || productSkn)) {
        setTimeout(() => {
            $.ajax({
                type: 'GET',
                url: '/product/detail/newinfo',
                data: {
                    id: productId,
                    goodsId: goodsId,
                    productSkn: productSkn,
                    bundleType: bundleType,
                    from: 'detail'
                },
                success: (data) => {
                    // 如果当前是秒杀商品,且不在秒杀路径下,跳到该商品的秒杀详情页
                    let reg = /\/product\/show_([\d]+)/;
                    let reg2 = /\/product\/([\d]+)/;
                    let regPro = /\/product\/pro_([\d]+)_([\d]+)/;
                    let regSeckill = /\/product\/seckill/;

                    // let regProSeckill = /\/product\/seckill\/pro_([\d]+)_([\d]+)/;
                    let thisHref = window.location.href;
                    let thisRefer = document.referrer;

                    if (!regSeckill.test(thisRefer)) {
                        if (data.isSecKill === 'Y' && (reg.test(thisHref) || regPro.test(thisHref) ||
                            reg2.test(thisHref))) {
                            window.location.replace('/product/seckill/show_' + productSkn + '.html');
                        }
                    }

                    // 如果秒杀商品没有吊牌价,显示原销售价
                    if (data.isSecKill === 'Y' && !data.cartInfo.price) {
                        $('.previous-price').text(data.cartInfo.salePrice);
                    }

                    // 渲染动态数据
                    renderInfo(data);

                    // 引入like事件
                    require('./detail/like');

                    // 引入店铺埋点
                    require('./detail/store');

                    // 设定购物bar
                    let $cartBar = $('.cart-bar');
                    let timer = setInterval(function() {
                        if ($cartBar) {
                            window.reMarginFooter('.cart-bar');
                            clearInterval(timer);
                        } else {
                            $cartBar = $('.cart-bar');
                        }
                    }, 500);

                    // 限购
                    let dialog = require('plugin/dialog');

                    let chosePanel = require('common/chose-panel-new');

                    if (window.cookie('tmp-cart-info') && yoho.isLogin()) {
                        let preInfo = window.cookie('tmp-cart-info').split('_');

                        if (preInfo[1] === $('#productSkn').val()) {
                            detailFuns.addToCart(preInfo[0], preInfo[1], preInfo[2]);
                        } else {
                            window.setCookie('tmp-cart-info', '');
                        }
                    }

                    $('#addtoCart, #ticketsToCart').on('click', function() {
                        let productCode = $('#limitProductCode').val();
                        let seckill = $('.seckill-time').length;

                        $('.cart-bar').hide();
                        chosePanel.show({
                            data: data,
                            disableNum: productCode || seckill
                        }).then(result => {
                            $('.cart-bar').show();
                            if (result && result.sku) {
                                if (result.modes.tickets) { // 门票
                                    detailFuns.addTickets(result.sku.skuId, result.buyNum);
                                    return;
                                }
                                if (result.modes.limitCode) { // 限购
                                    detailFuns.addLimitCode(productCode, result.sku.skuId, productSkn, result.buyNum);
                                    return;
                                }
                                if (result.modes.seckill) { // 秒杀
                                    window.location.href = '/cart/index/seckill?skn=' + productSkn +
                                        '&sku=' + result.sku.skuId;
                                    return;
                                }
                                if (result.buyNow) { // 立即购买
                                    let nextUrl = location.origin + '/cart/index/buynow/orderensure?product_sku=' +
                                        result.sku.skuId + '&buy_number=' + result.buyNum;

                                    if (yoho.isMarsApp && !yoho.isLogin()) {
                                        yoho.goLogin(nextUrl);
                                    } else {
                                        location.href = nextUrl;
                                    }
                                    return;
                                }
                                detailFuns.addToCart(result.sku.skuId, productSkn, result.buyNum);
                            }
                        }, () => {
                            $('.cart-bar').show();
                        });

                        // 统计代码:用于统计用户加入购物车的动作
                        if (window._yas && window._yas.sendCustomInfo) {
                            window._yas.sendCustomInfo({
                                pd: productId,
                                by: 1
                            }, true);
                        }

                        return false;
                    });
                    $('#limit-sale').on('touchend', function(e) {
                        let loginUrl = $('#loginUrl').val(),
                            time,
                            ifr;

                        e.stopPropagation();

                        if (loginUrl) {
                            window.location = loginUrl;
                        } else {
                            dialog.showDialog({
                                dialogText: '进入有货APP,获取限购码',
                                hasFooter: {
                                    rightBtnText: '打开Yoho!Buy有货APP'
                                }
                            }, function() {
                                let appUrl = $('input[name="limitCodeUrl"]').val();

                                ifr = document.createElement('iframe');
                                ifr.src = appUrl;
                                ifr.style.display = 'none';
                                document.body.appendChild(ifr);
                                window.location.href = appUrl;

                                time = Date.now();
                                window.setTimeout(function() {
                                    document.body.removeChild(ifr);
                                    if (Date.now() - time < 3200) {
                                        window.location.href = 'http://a.app.qq.com/o/simple.jsp?pkgname=com.yoho';
                                    }
                                }, 3000);

                            }, null, true);

                            $('.dialog-wrapper').off('touchstart').on('touchstart', function(para) {
                                para.stopPropagation();
                                if ($(para.target).hasClass('dialog-wrapper')) {
                                    dialog.hideDialog();
                                }
                            });
                        }
                    });

                    window.rePosFooter && window.rePosFooter();
                }
            });
        }, 150);
    }
}());

/*
 *   获取商品 相关数据: 1. 促销 2. feedback 3. 店铺, 并初始化
 */
+ (function(skn, productId, brandId) { // eslint-disable-line
    // 模版: 促销, feedback, 店铺
    let promotionT = require('product/detail/promotion.hbs');
    let feedbackT = require('product/detail/feedbacks.hbs');
    let enterStoreT = require('product/detail/enterStore.hbs');

    // placeholder
    let $feedbackStore = $('#placeholder-feedback-store');
    let $promotion = $('#placeholder-promotion');

    /**
     *  渲染 feedback, 店铺, 替换placeholder
     *  @param data
     *  {
     *      enterStore, feedback
     *  }
     *  @param $placeholder
     */
    function renderFeedbackStore(data, $placeholder) {
        let feedbackHtml = feedbackT({
            feedbacks: data.feedbacks
        });

        let enterStoreHtml = enterStoreT({
            enterStore: data.enterStore
        });

        return $placeholder.replaceWith([feedbackHtml, enterStoreHtml]);
    }


    /**
     *  渲染 促销, 替换placeholder
     *  @param data
     *  {
     *      promotion
     *  }
     *  @param $placeholder
     */
    function renderPromotion(data, $placeholder) {
        let promotionHtml = promotionT({
            promotion: data.promotion
        });

        return $placeholder.replaceWith(promotionHtml);
    }

    function promotionInit() {
        let $goodsDiscount = $('#goodsDiscount');
        let $discountFirstItem = $goodsDiscount.find('.first-item');
        let $discountFolder = $goodsDiscount.find('.discount-folder');
        let $discountArrow = $goodsDiscount.find('.first-item .iconfont');

        // 初始化goods-discount
        if ($discountFolder.children().length === 0) {
            $discountFolder.css('display', 'none');
            $discountArrow.html('');
        }

        // goods-discount下拉按钮点击事件
        if ($goodsDiscount.length && $discountFolder.children().length > 0) {
            $goodsDiscount.on('click', function() {
                if ($discountFolder.is(':hidden')) {
                    $discountFirstItem.removeClass('short-text');
                    $discountArrow.removeClass('icon-down').addClass('icon-up').html('&#xe608;');
                    $discountFolder.slideDown();
                } else {
                    $discountFirstItem.addClass('short-text');
                    $discountArrow.removeClass('icon-up').addClass('icon-down').html('&#xe609;');
                    $discountFolder.slideUp();
                }
            });
        }
    }

    // boot
    setTimeout(() => {
        $.get('/product/detail/sknData.json', {
            skn: skn,
            productId: productId,
            brandId: brandId,
            shopId: shopId,
            bundleType: bundleType,
            gid: goodsId
        }).done(function(info) {
            // step1: render dom
            renderFeedbackStore(info, $feedbackStore);
            renderPromotion(info, $promotion);

            if (info.bundleDatas && info.bundleDatas.length) {
                require('./detail/bundle').init(info.bundleDatas);
                $('.bundle').on('touchstart', function() {
                    if (window._yas && window._yas.sendCustomInfo) {
                        window._yas.sendCustomInfo({
                            op: 'YB_GDS_PACKAGE_C',
                            param: JSON.stringify({
                                C_ID: window._ChannelVary[window.cookie('_Channel')],
                                PRD_ID: productId
                            })
                        }, true);
                    }
                });
            }

            // step2: init js
            require('./detail/comments-consults');
            require('./detail/consultform'); // TODO
            promotionInit();
            window.rePosFooter && window.rePosFooter();
        });
    }, 200);

    // 加载详情页的返回顶部按钮并注册事件

    $(document).scroll(function() {
        if (($(document).scrollTop() >= $(document).height() - $(window).height() - 120) &&
        $('.back-to-top').length > 0) {
            let top = $('.back-to-top').offset().top - $('.float-top').offset().top +
                ($('.back-to-top').height() - $('.float-top').height()) / 2;
            let left = $('.back-to-top').offset().left - $('.float-top').offset().left;

            $('.float-top').addClass('animation').css({
                transform: 'translate(' + left + 'px, ' + top + 'px) scale(0, 0)',
            });
        } else if ($('.product-detail').length && $(document).scrollTop() >= $('.product-detail').offset().top) {
            if (!$('.float-top').hasClass('hover') || $('.float-top').hasClass('animation')) {
                $('.float-top').addClass('hover').removeClass('animation').removeAttr('style');
            }
        } else {
            if ($('.float-top').hasClass('hover')) {
                $('.float-top').removeClass('hover');
            }
        }
    });

    // 加载详情页的返回顶部按钮并注册事件-end
}(productSkn, productId, brandId));

$('.rec-word .unfurled').on('click', function() {
    $('.rec-word .unfurled').hide();
    $('.rec-word .word-list').show();
});

$('.rec-word .fold').on('click', function() {
    $('.rec-word .unfurled').show();
    $('.rec-word .word-list').hide();
});