red-shop.js 4.18 KB
/*
 * @Author: Targaryen
 * @Date: 2017-03-23 11:31:51
 * @Last Modified by: Targaryen
 */

/** *****************
 * 红人店铺首页
 ********************/
const Swiper2 = require('yoho-swiper2');
const lazyLoad = require('js/plugin/lazyload');
let tip = require('../../plugin/tip');
let $goodsContainer = $('.index-goods-container');
let $indexGoodsContaniner = $('#indexGoodsContainer');
let $collect = $('#collect');

const shopId = $('#shopId').val();
const lazyLoadParams = {
    q: 75
};

lazyLoad($('.lazy'), lazyLoadParams);

/**
 * 异步检测是否已经收藏
 */
if ($('.not-collect').data('switch') !== true) {
    $.ajax({
        type: 'GET',
        url: location.protocol + '//m.yohobuy.com/product/index/shopFav',
        xhrFields: {
            withCredentials: true
        },
        data: {
            shopId: shopId
        },
        success: function(data) {
            if (data.collect) {
                $collect.attr('class', 'already-collect pull-left');
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });
}

/**
 * 店铺轮播图
 */
if ($('.shop-swiper')) {
    let num = $('.shop-swiper').length;

    for (let i = 1; i <= num; i++) {
        if ($('.shop-swiper-' + i).find('.swiper-slide').length > 1) {
            new Swiper2('.shop-swiper-' + i, {
                lazyLoading: true,
                lazyLoadingInPrevNext: true,
                loop: true,
                autoplay: 3000,
                slideElement: 'li',
                paginationClickable: true,
                pagination: $(this).closest('.shop-swiper-' + i).find('.pagination-inner').get(0)
            });
        }
    }
}

/**
 * 异步加载推荐商品
 */
$.each($goodsContainer, function(index, elem) {
    let url = '/product/new/shop/hotlist';
    let data = {};

    switch (parseInt($(elem).data('type'), 10)) {
        case 0:
            url = '/product/search/shop/goods';
            Object.assign(data, {
                shop_id: shopId,
                order: $(elem).data('order')
            });
            break;
        case 1:
            Object.assign(data, {
                skns: $(elem).data('skns')
            });
            break;
        default:
            break;
    }

    $.ajax({
        type: 'GET',
        url: url,
        xhrFields: {
            withCredentials: true
        },
        data: data,
        success: function(result) {
            let $result = $(result);

            lazyLoad($result.find('img[class=lazy]'), lazyLoadParams);
            $(elem).html($result);
        }
    });
});

/**
 * 异步加载首页全部商品
 */
$.ajax({
    type: 'GET',
    url: '/product/search/shop/goods',
    xhrFields: {
        withCredentials: true
    },
    data: {
        shop_id: shopId,
        order: '0',
        type: 'default'
    },
    success: function(result) {
        let $result = $(result);

        lazyLoad($result.find('img[class=lazy]'), lazyLoadParams);
        $indexGoodsContaniner.find('.container').html($result);
    }
});

/**
 * 店铺收藏取消收藏操作
 */
$collect.on('click', function() {
    let options = {
        id: shopId,
        opt: 'ok',
        type: 'shop'
    };

    if ($collect.hasClass('already-collect')) {
        options.opt = 'cancel';
    }

    $.ajax({
        method: 'get',
        url: location.protocol + '//m.yohobuy.com/product/opt/favoriteBrand',
        xhrFields: {
            withCredentials: true
        },
        data: options,
        success: function(result) {
            if (result.code === 200) {
                if ($collect.hasClass('already-collect')) {
                    $collect.attr('class', 'not-collect pull-left');
                    tip.show('店铺取消收藏成功');
                } else {
                    $collect.attr('class', 'already-collect pull-left');
                    tip.show('店铺收藏成功');
                }
            }

            if (result.code === 400) {
                if ($('#jump-login').length <= 0) {
                    $('body').append('<a href=\'' + result.data + '\'><span id="jump-login"><span></a>');
                }
                $('#jump-login').click();
            }
        }
    });
});