red-shop.js 2.88 KB
/*
 * @Author: Targaryen
 * @Date: 2017-03-23 11:31:51
 * @Last Modified by: Targaryen
 * @Last Modified time: 2017-04-01 16:58:34
 */

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

const shopId = $('#shopId').val();

/**
 * 异步检测是否已经收藏
 */
$.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('网络断开连接了~');
    }
});

/**
 * 异步加载人气单品
 */
$.each($goodsContainer, function(index, elem) {
    $.ajax({
        type: 'GET',
        url: location.protocol + '//m.yohobuy.com/product//new/shop/hotlist',
        xhrFields: {
            withCredentials: true
        },
        data: {
            skns: $(elem).data('skns')
        },
        success: function(result) {
            $(elem).html(result);
        }
    });
});

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

    for (let i = 1; i <= num; i++) {
        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)
        });
    }
}

/**
 * 店铺收藏取消收藏操作
 */
$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();
            }
        }
    });
});