shop-list.page.js 2.58 KB
require('product/shop/shop.page.css');
const $ = require('yoho-jquery');
const tip = require('plugin/tip');
const ProductListWithFilter = require('product/list/product-list-with-filter');

const $shopIntroBtn = $('#shopIntroBtn');
const $introBox = $('#introBox');
const $closeIntroBtn = $('#closeIntroBtn');
const $collectShopIcon = $('#collectShop');

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

const productListWithFilterModel =
    new ProductListWithFilter({
        shop_id: shopId,
        page: 2, // 首页服务端已经渲染
        isShopList: 'Y' // 传给 filter,表明调用哪个接口获取筛选面板的数据
    }, 'product/search/shop/goods');

productListWithFilterModel.getFilter();

/**
 * 添加或者取消收藏处理
 * @param {*} opt
 */
function collectOrNotShop(opt) {
    $.ajax({
        type: 'GET',
        url: location.protocol + '//m.yohobuy.com/product/opt/favoriteBrand',
        data: {
            id: shopId,
            opt: opt,
            type: 'shop'
        },
        xhrFields: {
            withCredentials: true
        },
        success: function(data) {
            if (data.code === 200) {
                $collectShopIcon.toggleClass('coled');

                // 提示
                if (opt === 'ok') {
                    tip.show('添加收藏成功');
                } else {
                    tip.show('取消收藏成功');
                }
            } else if (data.code === 400) {
                location.href = data.data;// 未登录跳转登录页
            } else {
                tip.show(data.message);
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });
}

/**
 * 打开店铺介绍
 */
$shopIntroBtn.on('click', () => {
    $introBox.removeClass('hide');
});

/**
 * 关闭店铺介绍
 */
$closeIntroBtn.on('click', () => {
    $introBox.addClass('hide');
});

/**
 * 添加或者取消收藏操作
 */
$collectShopIcon.on('click', () => {
    if ($collectShopIcon.hasClass('coled')) {
        collectOrNotShop('cancel');
    } else {
        collectOrNotShop('ok');
    }
});

// 获取是否收藏
(function() {
    $.ajax({
        type: 'GET',
        url: location.protocol + '//m.yohobuy.com/product/index/shopFav',
        xhrFields: {
            withCredentials: true
        },
        data: {
            shopId: shopId,
        },
        success: function(data) {
            if (data.collect) {
                $collectShopIcon.addClass('coled');
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });
}());