Blame view

public/js/home/order.page.js 14.8 KB
lijing authored
1 2 3 4 5 6
/**
 * 个人中心--我的订单
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/11/10
 */
lijing authored
7 8 9
require('layout/_modal.css');
require('home/order-list.page.css');
lijing authored
10
let $ = require('yoho-jquery'),
lijing authored
11
    lazyLoad = require('yoho-jquery-lazyload'),
12
    tip = require('plugin/tip'),
lijing authored
13 14
    Swiper = require('yoho-swiper');
lijing authored
15
let $navLi = $('#order-nav > li'),
lijing authored
16 17
    $orderContainer = $('#order-container');
lijing authored
18
let $curContainer = $orderContainer.children('.orders:not(.hide)');// 保存当前显示的order-container
lijing authored
19
lijing authored
20
let winH = $(window).height();
lijing authored
21
lijing authored
22
let activeType = $navLi.filter('.active').data('type'); // 当前active的项的index
lijing authored
23
lijing authored
24
let order = {
25
    page: 1,
lijing authored
26 27 28
    end: false
};
lijing authored
29
let inAjax = false;
lijing authored
30
lijing authored
31
let loading = require('plugin/loading');
lijing authored
32
lijing authored
33
let dialog = require('plugin/dialog');
lijing authored
34
王水玲 authored
35
let $reaMask = $('.reason-mask'),
郭成尧 authored
36 37 38
    $refundReaMask = $('.refund-reason-mask'),
    reasonSwiper,
    refundReasonSwiper;
lijing authored
39
40
// 首屏加载标志
lijing authored
41
let firstScreen = $('.firstscreen-orders').children().size() > 0;
42
43 44
require('common');
require('plugin/modal.alert');
姜枫 authored
45
lijing authored
46 47
// 减少计时
function downCount(item) {
lijing authored
48
    let hoursItem = item.find('.hours');
lijing authored
49
lijing authored
50
    let difference = hoursItem.text(), // difference of dates
lijing authored
51 52 53 54 55 56 57 58
        interval;

    /**
     * Main downCount function that calculates everything
     */
    function countdown() {

        // basic math variables
lijing authored
59
        let _second = 1000,
lijing authored
60 61 62 63 64 65 66 67
            _minute = _second * 60,
            _hour = _minute * 60,
            _day = _hour * 24,
            hours,
            minutes,
            seconds;

        // calculate dates
68 69
        hours = Math.floor((difference % _day) / _hour);
        minutes = Math.floor((difference % _hour) / _minute);
lijing authored
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
        seconds = Math.floor((difference % _minute) / _second);

        // fix dates so that it will show two digets
        hours = (String(hours).length >= 2) ? hours : '0' + hours;
        minutes = (String(minutes).length >= 2) ? minutes : '0' + minutes;
        seconds = (String(seconds).length >= 2) ? seconds : '0' + seconds;

        // set to DOM
        item.removeClass('hide');
        if (hours === '00') {
            hoursItem.text('剩余' + minutes + ':' + seconds);
        } else {
            hoursItem.text('剩余' + hours + ':' + minutes + ':' + seconds);
        }

        difference -= 1000;

        if (difference <= 0) {
            clearInterval(interval);// stop timer
            return;
        }
    }
    if (difference !== '' && difference > 0) {
        interval = setInterval(countdown, 1000);// start
    }
}

//  初始化时间
function setTime() {

    $('.order').each(function() {
lijing authored
101
        let item = $(this).find('.count-down');
lijing authored
102 103 104 105 106 107 108 109 110

        if (!$(this).hasClass('hide')) {
            downCount(item);
        }
    });
}

// 加载订单
function getOrders(option) {
lijing authored
111
    let opt = {
lijing authored
112 113 114
        type: activeType,
        page: order.page + 1
    };
lijing authored
115
    let show = option && !option.noLoadingMask;
lijing authored
116
117
    if (firstScreen) {
118 119
        // 如果首屏加载了,则去掉10条记录
        opt.start = 10;
120 121
    }
lijing authored
122 123 124 125 126 127 128 129 130 131 132 133
    if (inAjax) {
        return;
    }

    inAjax = true;
    show && loading.showLoadingMask();

    $.ajax({
        type: 'GET',
        url: '/home/getOrders',
        data: opt,
        success: function(data) {
lijing authored
134
            let num;
zzzzzzz authored
135 136

            if (data) {
lijing authored
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                order.page = opt.page;

                if (opt.page === 1) {
                    $curContainer.html(data);
                    lazyLoad($curContainer.find('.lazy'), {
                        try_again_css: 'order-failure'
                    });
                } else {
                    num = $curContainer.children('.order').length;
                    $curContainer.append(data);

                    // lazyload
                    lazyLoad($curContainer.children('.order:gt(' + (num - 1) + ')').find('.lazy'), {
                        try_again_css: 'order-failure'
                    });
                }

                window.rePosFooter(); // 重新计算底部位置
            } else {
zzzzzzz authored
156 157 158
                if (opt.page > 1) {
                    return;
                }
郭成尧 authored
159 160
                $curContainer.html('<div class="no-order"><div class="icon"></div><span>' +
                    '你还没有订单!</span><a class="walk-way" href="//m.yohobuy.com/product/new">随便逛逛</a></div>');
lijing authored
161 162 163 164 165 166 167 168
                order.end = true;
            }

            inAjax = false;
            show && loading.hideLoadingMask();
            setTime();
        }
    });
169 170 171

    // 还原参数
    firstScreen = false;
lijing authored
172 173 174 175 176 177 178 179
}

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

// 初始化导航
(function() {
lijing authored
180
    let liCount = $navLi.length;
lijing authored
181
182 183
    setTime();
lijing authored
184 185 186 187 188 189 190 191 192 193
    // 默认4个导航项
    if (liCount === 4) {
        return;
    }

    $navLi.width(100 / liCount + '%');
}());

loading.init($('body')); // 满屏loading
郭成尧 authored
194 195 196 197
/**
 * 确认收货
 */
function sureOrder(orderId) {
郭成尧 authored
198 199 200 201 202
    dialog.showDialog({
        dialogText: '请确认是否已经收到货品',
        hasFooter: {
            leftBtnText: '取消',
            rightBtnText: '确定'
郭成尧 authored
203
        }
郭成尧 authored
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    }, function() {
        $.ajax({
            type: 'POST',
            url: '//m.yohobuy.com/home/orders/sure',
            data: {
                orderCode: orderId
            },
            success: function(result) {
                if (result.code === 200) {
                    setTimeout(function() {
                        location.reload();
                    }, 2000);
                }
            }
        });
郭成尧 authored
219 220 221
    });
}
lijing authored
222
// 点击订单区域跳转订单详情页
王水玲 authored
223
$('#order-container').on('click', function(e) {
lijing authored
224
    let $cur = $(e.target),
lijing authored
225 226 227 228 229 230 231 232 233 234 235 236
        $order,
        id,
        url;

    if ($cur.closest('.locHref').length > 0) {
        return;
    }

    $order = $cur.closest('.order');
    id = $order.data('id');

    $reaMask.data('orderId', id);
郭成尧 authored
237
    $refundReaMask.data('orderId', id);
lijing authored
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

    if ($cur.closest('.del').length > 0) {

        // Order delete
        dialog.showDialog({
            dialogText: '确定删除订单吗?',
            hasFooter: {
                leftBtnText: '取消',
                rightBtnText: '确定'
            }
        }, function() {
            $.ajax({
                type: 'GET',
                url: '/home/delOrder',
                data: {
                    id: id
                },
                success: function(data) {
                    dialog.hideDialog();
                    if (data.message) {
                        tip.show(data.message);
                    }
                    if (data.code === 200) {

                        // 删除订单页面刷新
                        window.location.reload();
                    }
                },
                error: function() {
                    tip.show('取消订单失败');
                }
            });
        });
    } else if ($cur.closest('.cancel').length > 0) {
郭成尧 authored
272
        // 取消订单
郭成尧 authored
273 274 275 276 277 278 279 280 281 282
        dialog.showDialog({
            dialogText: '取消订单后,本单享有的优惠可能会一并取消,确定申请吗?',
            hasFooter: {
                leftBtnText: '返回',
                rightBtnText: '确定'
            }
        }, function() {
            dialog.hideDialog();
            $reaMask.css('visibility', 'visible');
        });
郭成尧 authored
283 284
    } else if ($cur.closest('.refund').length > 0) {
        // 申请退款
郭成尧 authored
285
郭成尧 authored
286
        // 埋点
郭成尧 authored
287 288 289 290 291
        if (window._yas && window._yas.sendCustomInfo) {
            window._yas.sendCustomInfo({
                op: 'YB_ORDER_REFUND_C',
                param: JSON.stringify({
                    C_ID: window._ChannelVary[window.cookie('_Channel')],
郭成尧 authored
292
                    ORD_NUM: id + ''
郭成尧 authored
293 294 295
                })
            }, true);
        }
郭成尧 authored
296
郭成尧 authored
297 298 299 300 301 302 303 304 305
        dialog.showDialog({
            dialogText: '申请退款后,本单享有的优惠可能会一并取消,确定申请吗?',
            hasFooter: {
                leftBtnText: '返回',
                rightBtnText: '确定'
            }
        }, function() {
            dialog.hideDialog();
            $refundReaMask.css('visibility', 'visible');
郭成尧 authored
306
        });
lijing authored
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    } else if ($cur.closest('.order-goods').length > 0) {

        // Location to order detail
        url = $order.data('href');
        location.href = url;
    } else if ($cur.closest('.rebuy').length > 0) {
        $.ajax({
            type: 'GET',
            url: '/home/readd',
            data: {
                orderCode: id
            },
            success: function(res) {
                tip.show(res.message);
                location.href = '/cart/index/index';
            },
            error: function(res) {
                tip.show(res.message);
            }
        });
郭成尧 authored
327 328 329 330 331 332 333
    } else if ($cur.closest('.btn-add-change').length > 0) {
        // 修改地址埋点
        if (window._yas && window._yas.sendCustomInfo) {
            window._yas.sendCustomInfo({
                op: 'YB_ORDER_MODIFY_ADDRESS_C',
                param: JSON.stringify({
                    C_ID: window._ChannelVary[window.cookie('_Channel')],
郭成尧 authored
334
                    ORD_NUM: id + ''
郭成尧 authored
335 336 337
                })
            }, true);
        }
郝肖肖 authored
338 339
    } else if ($cur.closest('.after-sales').length) {
        dialog.showDialog({
郝肖肖 authored
340
            dialogText: 'WAP端暂不支持该功能,请至有货APP进行操作',
郝肖肖 authored
341
            hasFooter: {
郝肖肖 authored
342
                centerBtnText: '我知道了'
郝肖肖 authored
343 344
            }
        });
郭成尧 authored
345 346
    } else if ($cur.closest('.sure').length) {
        sureOrder(id);
lijing authored
347 348 349 350
    }
});

function scrollHandler() {
zzzzzzz authored
351
    if (!order.end && $(window).scrollTop() + winH >
lijing authored
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        $(document).height() - 0.25 * $orderContainer.height()) {

        // 下拉请求时不显示mask
        getOrders({
            noLoadingMask: true
        });
    }
}

// srcoll to load more
$(window).scroll(function() {
    window.requestAnimationFrame(scrollHandler);
});

// 初始化请求第一页数据
ccbikai(👎🏻🍜) authored
367
// getOrders();
lijing authored
368 369 370 371 372 373 374

$(function() {
    reasonSwiper = new Swiper('.box-main', {
        direction: 'vertical',
        slidesPerView: 5,
        centeredSlides: true,
        initialSlide: 0,
375
        onSlideChangeStart: function(reasonSwiper) {//eslint-disable-line
郭成尧 authored
376
            let activeIndex = reasonSwiper.activeIndex,
lijing authored
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
                slides = reasonSwiper.slides,
                i = 0;

            if (slides.length !== 1) {
                if (activeIndex === 0) {
                    for (i = 1; i < slides.length; i++) {
                        $(slides[i]).css('transform', '');
                    }
                } else if (activeIndex === slides.length - 1) {
                    for (i = 0; i < activeIndex; i++) {
                        $(slides[i]).css('transform', 'rotateX(' + (30 + (activeIndex - i) * 12) + 'deg)');
                    }
                } else {
                    for (i = 0; i < activeIndex; i++) {
                        $(slides[i]).css('transform', 'rotateX(' + (30 + (activeIndex - i) * 12) + 'deg)');
                    }
                    for (i = activeIndex + 1; i < slides.length; i++) {
                        $(slides[i]).css('transform', '');
                    }
                }
            }
            $(slides[activeIndex]).css('transform', '');
        }
    });
郭成尧 authored
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

    // 申请退款 Swiper
    refundReasonSwiper = new Swiper('.refund-box-main', {
        direction: 'vertical',
        slidesPerView: 5,
        centeredSlides: true,
        initialSlide: 0,
        onSlideChangeStart: function(refundReasonSwiper) {//eslint-disable-line
            let activeIndex = refundReasonSwiper.activeIndex,
                slides = refundReasonSwiper.slides,
                i = 0;

            if (slides.length !== 1) {
                if (activeIndex === 0) {
                    for (i = 1; i < slides.length; i++) {
                        $(slides[i]).css('transform', '');
                    }
                } else if (activeIndex === slides.length - 1) {
                    for (i = 0; i < activeIndex; i++) {
                        $(slides[i]).css('transform', 'rotateX(' + (30 + (activeIndex - i) * 12) + 'deg)');
                    }
                } else {
                    for (i = 0; i < activeIndex; i++) {
                        $(slides[i]).css('transform', 'rotateX(' + (30 + (activeIndex - i) * 12) + 'deg)');
                    }
                    for (i = activeIndex + 1; i < slides.length; i++) {
                        $(slides[i]).css('transform', '');
                    }
                }
            }
            $(slides[activeIndex]).css('transform', '');
        }
    });
lijing authored
434 435
});
436
$reaMask.find('.box-cmp').on('touchend', function() {
郭成尧 authored
437
    let selSolid = reasonSwiper.slides[reasonSwiper.activeIndex],
lijing authored
438 439 440 441 442 443 444 445 446 447 448 449
        reason = $(selSolid).text(),
        reasonId = $(selSolid).data('reasonId');

    $.ajax({
        type: 'GET',
        url: '/home/cancelOrder',
        data: {
            id: $reaMask.data('orderId'),
            reason: reason,
            reasonId: reasonId
        }
    }).then(function(res) {
郭成尧 authored
450
        $reaMask.css('visibility', 'hidden');
lijing authored
451 452 453
        if ($.type(res) !== 'object') {
            return;
        }
郭成尧 authored
454
lijing authored
455 456 457
        if (res.message) {
            tip.show(res.message);
        }
郭成尧 authored
458 459 460 461 462 463

        if (res.code === 200) {
            setTimeout(function() {
                window.location.href = '/home/orders';
            }, 500);
        }
lijing authored
464 465 466 467 468
    }).fail(function() {
        tip.show('网络错误');
    });
});
郭成尧 authored
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
// 申请退款
$refundReaMask.find('.box-cmp').on('touchend', function() {
    let selSolid = refundReasonSwiper.slides[refundReasonSwiper.activeIndex],
        reason = $(selSolid).text(),
        reasonId = $(selSolid).data('reasonId');

    $.ajax({
        type: 'GET',
        url: '/home/refundApply',
        data: {
            id: $refundReaMask.data('orderId'),
            reason: reason,
            reasonId: reasonId
        }
    }).then(function(res) {
郭成尧 authored
484
        $refundReaMask.css('visibility', 'hidden');
郭成尧 authored
485 486 487
        if ($.type(res) !== 'object') {
            return;
        }
郭成尧 authored
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

        if (res.code === 200) {
            dialog.showDialog({
                dialogText: '您的退款申请已提交,请耐心等待退款',
                hasFooter: {
                    leftBtnText: '返回',
                    rightBtnText: '确定'
                }
            }, function() {
                window.location.href = '/home/orders';
            });

            return false;
        }
郭成尧 authored
503 504 505 506 507 508 509 510
        if (res.message) {
            tip.show(res.message);
        }
    }).fail(function() {
        tip.show('网络错误');
    });
});
lijing authored
511 512 513 514 515 516 517 518
$reaMask.on('touchend', function(event) {
    if (event.target.className !== 'reason-mask') {
        return false;
    }

    $reaMask.css('visibility', 'hidden');
    event.stopPropagation();
});
519
郭成尧 authored
520 521
// 申请退款
$refundReaMask.on('touchend', function(event) {
郭成尧 authored
522
    if (event.target.className !== 'refund-reason-mask') {
郭成尧 authored
523 524 525 526 527 528 529
        return false;
    }

    $refundReaMask.css('visibility', 'hidden');
    event.stopPropagation();
});
郭成尧 authored
530
$('.nav-tap').on('click', function(e) {
lijing authored
531
    let $cur = $(e.target);
郭成尧 authored
532
郭成尧 authored
533 534 535
    if ($cur.data('url')) {
        location.replace($cur.data('url'));
    }
郭成尧 authored
536
});