red-shop.js 3.26 KB
/*
 * @Author: Targaryen
 * @Date: 2017-03-23 11:31:51
 * @Last Modified by: Targaryen
 * @Last Modified time: 2017-04-14 11:17:55
 */

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

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

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

    switch (parseInt($(elem).data('type'), 10)) {
        case 0:
            url = '/product/search/search';
            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) {
            $(elem).html(result);
        }
    });
});

/**
 * 异步检测是否已经收藏
 */
$.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++) {
        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();
            }
        }
    });
});