address.page.js 2.07 KB
/**
 * Created by PhpStorm.
 * User: Targaryen
 * Date: 2016/8/17
 * Time: 13:55
 */
/**
 * 地址管理
 * @author: bikai<kai.bi@yoho.cn>
 * @date: 2015/11/17
 */

let $ = require('yoho-jquery'),
    tip = require('plugin/tip'),
    loading = require('plugin/loading');

let $action = $('.action'),
    $addAddress = $('.add-address'),
    $footer = $('#yoho-footer'),
    $confim = $('.confim-mask'),
    $pageWrap = $('.page-wrap'),
    $addressItem = $('.address-item'),
    deleteId;

require('common');

$pageWrap.first().css('min-height', function() {
    return $(window).height() - $('#yoho-header').height() - $footer.height();
});
window.rePosFooter();

$confim.on('touchend', '.cancel', function() {
    deleteId = null;
    $confim.fadeOut();
    return false;
}).on('touchend', '.confim', function() {
    loading.showLoadingMask();
    $confim.fadeOut();
    $.ajax({
        method: 'POST',
        url: '/home/delAddress',
        data: {
            id: deleteId,
            _csrf: $('input[name=_csrf]').val()
        }
    }).then(function(res) {
        if ($.type(res) !== 'object') {
            res = {};
        }
        if (res.code !== 200) {
            tip.show(res.message || '网络出了点问题~');
            loading.hideLoadingMask();
        } else {
            window.location.reload();
        }
    }).fail(function() {
        tip.show('网络出了点问题~');
        loading.hideLoadingMask();
    }).always(function() {
        deleteId = null;
    });
    return false;
});


// 添加地址
if ($addressItem.length < 5) {
    $addAddress.attr('href', $addAddress.data('href'));
}
$addAddress.on('touchend', function() {
    if ($addressItem.length >= 5) {
        tip.show('您最多添加5个收货地址');
        return false;
    }
});

// 删除
$action.on('touchend', '.del', function() {
    deleteId = $(this).data('id');
    $confim.fadeIn();
    return false;
});

$action.on('touchstart', 'a,span', function() {
    $(this).css('background', '#eee');
}).on('touchend touchcancel', 'a,span', function() {
    $(this).css('background', 'transparent');
});