export-data.js

var timeBtnClick = (function() {
    function _todayClick() {
        $('.select-time .today').on('click', function() {
            $('#export-date-start-time').val(countDate.getDate(0));
            $('#export-date-end-time').val(countDate.getDate(-1));
        });
    }

    function _yesterdayClick() {
        $('.select-time .yesterday').on('click', function(){
            $('#export-date-start-time').val(countDate.getDate(1));
            $('#export-date-end-time').val(countDate.getDate(0));
        });
    }

    function _sevenClick() {
        $('.select-time .recent-seven-days').on('click', function(){
            $('#export-date-start-time').val(countDate.getDate(7));
            $('#export-date-end-time').val(countDate.getDate(0));
        });
    }

    function _thirtyClick() {
        $('.select-time .recent-thirty-days').on('click', function(){
            $('#export-date-start-time').val(countDate.getDate(30));
            $('#export-date-end-time').val(countDate.getDate(0));
        });
    }

    return {
        init: function() {
            _todayClick();
            _yesterdayClick();
            _sevenClick();
            _thirtyClick();
        }
    };
})();

var countDate = (function() {
    function getDate(days) {
        var now = new Date(),
            newDate = new Date(now.getTime() - 86400000 * days),
            yyyy = newDate.getFullYear(),
            mm = (newDate.getMonth() + 1) < 9 ? '0' + (newDate.getMonth() + 1) : newDate.getMonth() + 1,
            dd = newDate.getDate() < 9 ? '0' + newDate.getDate() : newDate.getDate();
        return (yyyy + '-' + mm + '-' + dd + ' 00:00');
    }
    return {
        getDate: getDate
    };
})();

var inputInit = (function() {
    function _initPlaceholder() {
        $('.export-date-time').placeholder();
    }
    function _initTimepicker() {
        $('.export-date-time').datetimepicker({
            dateFormat : "yy-mm-dd",
            timeFormat : "HH:mm"
        }).placeholder();
    }
    return {
        init: function() {
            _initPlaceholder();
            _initTimepicker();
        }
    };
})();

var exportBtnClick = (function() {
    function _initExportBtnClick() {
        $('.export-data-container').on('click', ".export-data-btn:not('.disabled-btn')", function(){
            $this = $(this);
            $('.error-msg').hide();
            $this.find('.loading-icon').hide();
            var startTime = $('#export-date-start-time').val(),
                endTime = $('#export-date-end-time').val(),
                dataType = $this.data('type'),
                timeErrorMsg = $('.time-error-msg');
            if (startTime > endTime) {
                timeErrorMsg.html(reminder_message.please_choose_correct_time);
                timeErrorMsg.show();
                return;
            } else {
                timeErrorMsg.hide();
            }

            if (!startTime || !endTime) {
                timeErrorMsg.html(reminder_message.please_choose_time);
                timeErrorMsg.show();
                return;
            } else {
                timeErrorMsg.hide();
            }
            $('.export-data-btn').addClass('disabled-btn');
            $this.find('.loading-icon').css('display', 'inline-block');
            ifContinue = true;
            check_export($this, dataType, startTime, endTime, ifContinue);
        });
    }
    function check_export($this, dataType, startTime, endTime, ifContinue) {
        var taskId = $this.data('task-id') ? $this.data('task-id') : '',
            xlsFileLink = $('#xls-file-link'),
            loadingIcon = $this.find('.loading-icon'),
            exportDataBtn = $('.export-data-btn');
        if (ifContinue) {
            $.ajax({
                type: 'GET',
                url: '/export-data?startTime=' + startTime + '&endTime=' + endTime + '&dataType=' + dataType + '&taskId=' + taskId,
                dataType: 'JSON',
                success: function(data){
                    if (data.error_msg) {
                        if_continue = false;
                        $this.data('task-id', '');
                        $('.time-error-msg').show();
                        loadingIcon.hide();
                        exportDataBtn.removeClass('disabled-btn');
                    }

                    if (data.xls_name) {
                        ifContinue = false;
                        xlsFileLink.attr('href', '/media/' + data.xls_name);
                        xlsFileLink[0].click();
                        xlsFileLink.removeAttr('href');
                        $this.data('task-id', '');
                        loadingIcon.hide();
                        exportDataBtn.removeClass('disabled-btn');
                    } else if (data.task_id) {
                        $this.data('task-id', data.task_id);
                    }

                },
                error: function(e){
                    if_continue = false;
                    $this.data('task-id', '');
                    $this.next('.error-msg').show();
                    loadingIcon.hide();
                    exportDataBtn.removeClass('disabled-btn');
                }
            });
            t = setTimeout(function(){
                console.log("Looping to query.");
                check_export($this, dataType, startTime, endTime, ifContinue);
            }, 1000);
        }


    }
    return {
        init: _initExportBtnClick
    };
})();

$(document).ready(function(){
    timeBtnClick.init();
    inputInit.init();
    exportBtnClick.init();
});

 

转载于:https://www.cnblogs.com/Brittany-yan/p/5287351.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值