order-detail.js 2.78 KB
/**
 * 订单详情页
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/11/16
 */

var $ = require('jquery'),
    lazyLoad = require('yoho.lazyload'),
    Hammer = require('yoho.hammer'),
    dialog = require('./dialog'),
    tip = require('../plugin/tip'),
    Swiper = require('yoho.iswiper');

var orderId = $('#order-detail').data('id'),
    $ownerInfo = $('.owner-info'),
    $reaMask = $('.reason-mask'),
    reasonSwiper;

var optHammer;

lazyLoad({
    try_again_css: 'order-failure'
});

//订单删除
optHammer = new Hammer(document.getElementsByClassName('opt')[0]);
optHammer.on('tap', function(e) {
    var $cur = $(e.target);

    if ($cur.hasClass('btn-del')) {

        //删除订单
        dialog.showDialog({
            dialogText: '确定删除订单吗?',
            hasFooter: {
                leftBtnText: '取消',
                rightBtnText: '确定'
            }
        }, function() {
            $.ajax({
                type: 'GET',
                url: '/home/delOrder',
                data: {
                    id: orderId
                }
            }).then(function(res) {
                $('#dialog-wrapper').hide();
                if ($.type(res) !== 'object') {
                    return;
                }
                if (res.message) {
                    tip.show(res.message);
                }
                setTimeout(function() {
                    window.location.href = '/home/orders';
                }, 2000);
            }).fail(function() {
                tip.show('网络错误');
            });
        });
    } else if ($cur.hasClass('btn-cancel')) {
        $reaMask.css('visibility', 'visible');
    }
});

if ($ownerInfo.data('changeable') === true) {
    $ownerInfo.find('.rest').show();
    $ownerInfo.on('touchend', function() {
        location.href = $ownerInfo.data('url');
    });
}

$(function() {
    reasonSwiper = new Swiper('.box-main', {
        paginationClickable: true,
        direction: 'vertical',
        slidesPerView: 4,
        effect: 'coverflow',
        centeredSlides: true
    });
});

$reaMask.find('.box-cmp').on('touchend', function(e) {
    var selSolid = reasonSwiper.slides[reasonSwiper.activeIndex],
        reason = $(selSolid).text(),
        reasonId = $(selSolid).data('reasonId');

    $.ajax({
        type: 'GET',
        url: '/home/cancelOrder',
        data: {
            id: orderId,
            reason: reason,
            reasonId: reasonId
        }
    }).then(function(res) {
        $reaMask.fadeOut();
        if ($.type(res) !== 'object') {
            return;
        }
        if (res.message) {
            tip.show(res.message);
        }
        setTimeout(function() {
            window.location.href = '/home/orders';
        }, 2000);
    }).fail(function() {
        tip.show('网络错误');
    });
});