order-ensure.js 13.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
/* eslint-disable vars-on-top */
/**
 * 订单确认
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/11/12
 */

require('common.js');

var lazyLoad = require('yoho-jquery-lazyload'),
    tip = require('plugin/tip'),
    cookie = require('yoho-cookie');

var loading = require('plugin/loading'),
    order = require('../order-info');

var $invoice = $('.invoice'),
    $couponUse = $('.coupon-use.used'),
    $addressWrap = $('.address-wrap'),
    $coinCheck = $('.coin-check'),
    $coinLi = $('li.coin'),
    $subBlock = $('.sub-block'),
    $ticketsMobile = $('#mobile'),
    payType,
    queryString = $.queryString(),
    orderInfo = order.orderInfo,
    isSubmiting,
    dispatchInfo,
    total,
    activityId = $('#activity-id').val(),
    isYohoCoinClick = $coinLi.data('yoho-coin-click') * 1, // 判断有货币是否可以单击
    productSku = $('#product-sku').val();

var orderCont = cookie.get('order-info') && JSON.parse(cookie.get('order-info'));
var invoiceCont = {
        7: '服装',
        1: '图书',
        9: '配件',
        11: '日用品',
        3: '办公用品',
        6: '体育用品',
        10: '数码产品'
    },
    invoicesType = {
        1: '纸质',
        2: '电子'
    };

// cookie 参数
var actCkOpthn = {
    path: '/cart/index'
};

lazyLoad();

// 初始化发票信息
function invoiceInit() {
    if (orderCont.invoiceType) {
        $('.invoice-type').text(invoiceCont[orderCont.invoiceType] + '(' + invoicesType[orderCont.invoicesType] + ')');
    } else {
        $('.invoice-type').text('服装(电子)');
    }
}

function getQueryParam() {
    var queryArray = location.search.substr(1).split('&'),
        i,
        subArr = [],
        obj = {};

    for (i = 0; i < queryArray.length; i++) {
        subArr = queryArray[i].split('=');
        obj[subArr[0]] = subArr[1];
        subArr = [];
    }

    return obj;
}

function isLimitGood() {
    return getQueryParam().limitproductcode;
}

if (window.getUid() !== orderInfo('uid')) {
    order.init();
    window.location.reload();
}

if ($couponUse.data('name') !== orderInfo('couponName')) {
    orderInfo('couponCode', null);
    orderInfo('couponName', null);
}

// 来自购物车的链接默认不使用优惠券
if (document.referrer && document.referrer.indexOf('/cart/index/index') !== -1) {
    orderInfo('couponCode', null);
    orderInfo('couponName', null);
}

isLimitGood() && (function() {
    var a = [];

    var data = getQueryParam();

    data.type = 'limitcode';

    a.push(data);
    orderInfo('skuList', JSON.stringify(a));
    orderInfo('limitUrlSufix', location.search);
}());

if (queryString.cartType || queryString.carttype || !orderInfo('cartType')) {
    orderInfo('cartType', queryString.cartType || queryString.carttype || 'ordinary');
}

// function dispacthTapEvt(e) {
//     var $cur = $(e.target).closest('li');

//     if ($cur.length === 0 || $cur.hasClass('chosed')) {
//         return;
//     }

//     $cur.siblings('li.chosed').removeClass('chosed');
//     $cur.addClass('chosed');
// }

$('.checkbox').on('touchstart', function(e) {
    var $this = $(this);

    if ($(e.target).closest('.coin-check').length && !isYohoCoinClick) {
        return true;
    }

    if ($this.hasClass('icon-cb-radio')) {
        $this.removeClass('icon-cb-radio').addClass('icon-radio');
        return;
    }
    if ($this.hasClass('icon-radio')) {
        $this.removeClass('icon-radio').addClass('icon-cb-radio');
    }
});

$invoice.on('touchend', '.checkbox', function() {
    var $this = $(this);

    if ($this.hasClass('icon-cb-radio')) {
        $invoice.addClass('focus');
        orderInfo('invoiceText', '');
        orderInfo('invoiceType', '7');
        orderInfo('receiverMobile', $('.user-mobile').val());
        orderInfo('invoicesType', '2');
        orderInfo('invoiceTitle', '个人');
    }
    if ($this.hasClass('icon-radio')) {
        $invoice.removeClass('focus');
        orderInfo('invoiceText', null);
        orderInfo('invoiceType', null);
        orderInfo('receiverMobile', null);
        orderInfo('invoicesType', null);
        orderInfo('invoiceTitle', null);
    }

    orderCont = cookie.get('order-info') && JSON.parse(cookie.get('order-info'));
    invoiceInit();
});


function updateDeliverId(id) {
    var $moreJit = $('.more-jit a').get(0),
        url;

    if ($moreJit) {
        url = $moreJit.href;
    } else {
        return;
    }

    if (url.indexOf('deliveryId') < 0) {
        $moreJit.href = url + '&deliveryId=' + id;
    } else {
        $moreJit.href = url.replace(/deliveryId=(\d)/, 'deliveryId=' + id);
    }
}

function orderCompute() {
    var yohoCoin = orderInfo('yohoCoin'),
        deliverId = orderInfo('deliveryId'),
        data = {
            cartType: orderInfo('cartType') || 'ordinary',
            deliveryId: deliverId, // 配送方式id
            paymentType: orderInfo('paymentType'),
            yohoCoin: yohoCoin,
            sku: productSku,
            activityId: activityId
        };

    loading.showLoadingMask();
    $.ajax({
        method: 'POST',
        url: '/cart/index/seckill/compute',
        data: data
    }).then(function(res) {
        if ($.type(res) !== 'object') {
            window.location.reload();
        } else {
            if (typeof res.last_order_amount !== 'undefined') {
                res.last_order_amount = (+res.last_order_amount).toFixed(2);
            }

            if (res.use_yoho_coin) {
                $coinCheck.find('em').html('- ¥ ' + res.use_yoho_coin);
                $coinCheck.find('em').show();
            }
            $coinLi.find('.msg').html(res.yohoCoinCompute.yohoCoinMsg);
            isYohoCoinClick = res.yohoCoinCompute.yohoCoinClick * 1;
            $('.coin').data('yoho-coin', res.yohoCoinCompute.yohoCoin);

            total = '';
            if (res.promotion_formula_list) {
                $.each(res.promotion_formula_list, function(index, value) {
                    total += '<li>' +
                        '<p>' + value.promotion + '</p>' +
                        '<span>' + value.promotion_amount + '</span>' +
                        '</li>';
                });
                $('.price-cost span').html('¥' + res.last_order_amount);
                $('.bill span').html('¥' + res.last_order_amount);
                $('.total').html(total);
            }

            updateDeliverId(deliverId);
        }
    }).fail(function() {
        window.location.reload();
    }).always(function() {
        loading.hideLoadingMask();
    });
}

function submitOrder() {
    var invoiceText = $invoice.find('[name="invoice-title"]').val() || orderInfo('invoiceText'),
        msg = $('#msg').find('input').val() || orderInfo('msg');

    if (isSubmiting) {
        return false;
    }

    // if (orderInfo('invoice')) {
    //     if (!invoiceText) {
    //         tip.show('请输入发票抬头');
    //         return;
    //     }
    //     if (invoiceText.length > 30) {
    //         tip.show('发票抬头不得超过30个汉字');
    //         return;
    //     }
    // }

    if (msg) {
        if (msg.length > 40) {
            tip.show('留言不得超过40个汉字');
            return;
        }
    }
    loading.showLoadingMask();
    isSubmiting = true;
    $.ajax({
        method: 'POST',
        url: '/cart/index/seckill/submit',
        data: {
            activityId: activityId,
            sku: productSku,
            addressId: orderInfo('addressId'),
            cartType: orderInfo('cartType') || 'ordinary',
            deliveryWay: orderInfo('deliveryId'),
            deliveryTime: orderInfo('deliveryTimeId'),
            invoiceTitle: orderInfo('invoice') ? invoiceText : null,
            invoiceTypeId: orderInfo('invoice') ? ($invoice.find('.invoice-type').val() ||
                orderInfo('invoiceType')) : null,
            remark: msg,
            paymentId: orderInfo('paymentTypeId'), // 支付方式id
            paymentType: orderInfo('paymentType'), // 支付方式
            useYohoCoin: orderInfo('yohoCoin'),
            cartToken: $('#cart-token').val()
        }
    }).then(function(res) {
        var url;

        if (!res) {
            tip.show('系统繁忙,请稍后再试!');
            return;
        }
        if (res.code === 200) {
            if (res.data.payment_status || (payType === 2)) {
                url = '/home/orderDetail?order_code=' + res.data.order_code;
            } else {
                url = '/home/orders/paynew?order_code=' + res.data.order_code;
            }
            cookie.set('order-info', '', actCkOpthn);
            window.location.href = url;
        } else if (res.message) {
            tip.show(res.message);
        }
    }).fail(function() {
        tip.show('系统繁忙,请稍后再试!');
    }).always(function() {
        isSubmiting = false;
        loading.hideLoadingMask();
    });
}

// 界面点击,状态存 cookie
if (!orderInfo('addressId')) {
    orderInfo('addressId', $addressWrap.data('id'));
}

// 配送方式 发生改变
$('.delivery-id').on('touchend', 'li', function() {
    orderInfo('deliveryId', $(this).data('id'));

    // 实付金额发生变化,使用有货币为0
    orderInfo('yohoCoin', 0);
    $('.coin').find('.checkbox').removeClass('icon-cb-radio').addClass('icon-radio');

    orderCompute();
});

$('.payment-type').on('touchend', 'li', function() {
    var $paymentType = $('.icon-cb-radio', this);

    orderInfo('paymentTypeId', $paymentType.data('id')); // 支付方式id
    orderInfo('paymentType', $paymentType.data('payment-type')); // 支付方式
});

$('.dispatch-time').on('touchend', 'li', function() {
    orderInfo('deliveryTimeId', $(this).data('id'));
});

$('.yoho-coin-help-dialog-bg, .yoho-coin-footer').on('touchend', function(e) {
    e.preventDefault();
    $('.yoho-coin-help-dialog-bg').addClass('hide');
    $('.yoho-coin-help-dialog').addClass('hide');
});

$('.coin').on('touchend', function(e) {
    var $this = $(this);

    if ($(e.target).closest('.yoho-coin-help').length) {
        $('.yoho-coin-help-dialog-bg').removeClass('hide');
        $('.yoho-coin-help-dialog').removeClass('hide');
        return true;
    }

    if ($(e.target).closest('.coin-check').length <= 0) {
        return false;
    }

    if (!isYohoCoinClick) {
        return true;
    }

    if ($this.find('.checkbox').hasClass('icon-cb-radio')) {
        orderInfo('yohoCoin', $this.data('yoho-coin'));
        $this.find('.can-use').hide();
    } else {
        orderInfo('yohoCoin', 0);
        $this.find('.coin-check em').hide();
        $this.find('.can-use').show();
        $this.find('.used').hide();
    }
    orderCompute();
});

$invoice.on('touchend', '.checkbox', function(e) {
    var $this = $(this);

    orderInfo('invoice', $this.hasClass('icon-cb-radio'));
    e.preventDefault();
    e.stopPropagation();
});

$invoice.find('[name="invoice-title"]').on('blur', function() {
    orderInfo('invoiceText', $(this).val());
}).end().find('.invoice-type').on('change', function() {
    orderInfo('invoiceType', $(this).val());
});

$('#msg').find('textarea').on('blur', function() {
    orderInfo('msg', $(this).val());
});

$('form').on('submit', function() {
    return false;
});

// xwg 2016/3/21 13:22
$('.dispatch').on('touchend', 'h3', function() {
    if ($(this).siblings('ul').is(':hidden')) {
        $('.dispatch h3').removeClass('border-none');
        $(this).addClass('border-none');
        $('.down').removeClass('hide');
        $('.up').addClass('hide');
        $('.up', this).removeClass('hide');
        $('.down', this).addClass('hide');
        $('.dispatch ul').hide();
        $(this).siblings('ul').show();
    } else {
        $(this).removeClass('border-none');
        $('.down', this).removeClass('hide');
        $('.up', this).addClass('hide');
        $(this).siblings('ul').hide();
    }
});

$subBlock.on('touchstart', 'li', function() {
    // 送货时间提示语li,不响应事件
    if ($(this).hasClass('dispatch-time-info')) {
        return true;
    }

    $.each($(this).parents('ul').find('i'), function() {
        $(this).parents('ul').find('i').removeClass('icon-cb-radio').addClass('icon-radio');
    });
    $(this).parents('ul').hide();
    $('.down').removeClass('hide');
    $('.up').addClass('hide');
    $('.dispatch h3').removeClass('border-none');
    dispatchInfo = $(this).find('span').html();
    $(this).parents('.sub-block').find('h3 span').html(dispatchInfo);
    if ($(this).find('i').hasClass('icon-cb-radio')) {
        $(this).find('i').addClass('icon-radio');
    } else if ($(this).find('i').hasClass('icon-radio')) {
        $(this).find('i').addClass('icon-cb-radio');
    }
});


$('.bill a').on('touchstart', function() {
    var $paymentType = $('.payment-type .icon-cb-radio');

    orderInfo('paymentTypeId', $paymentType.data('id')); // 支付方式id
    orderInfo('paymentType', $paymentType.data('payment-type')); // 支付方式

    payType = $paymentType.data('payment-type');
    submitOrder();
});

function phoneHidden(phone) {
    phone = phone || '';
    return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
}

$('.info-phone').html(phoneHidden($('.info-phone').html()));

if (orderInfo('address') && orderInfo('address').is_support === 'N') {
    orderInfo('deliveryId', $('.delivery-id .icon-cb-radio').data('id'));
    orderCompute();
}

// 校验手机号
$ticketsMobile.blur(function function_name() {
    var reg = /^[0123456789]{1,30}$/;
    var mobile = $ticketsMobile.val();

    if (!reg.test(mobile)) {
        tip.show('手机号码不正确!');
    }
});

// 初始化发票信息内容
invoiceInit();