message.js 2.15 KB
var $ = require('jquery'),
    loading = require('../plugin/loading'),
    dialog = require('./dialog');

var $page = $('.massage-page'),
	page = 1,
    stopLoading = false;

function massageAJAX(page) {
    loading.showLoadingMask();
    $.ajax({
        type: 'POST',
        url: '/home/ajaxMessage',
        dataType: 'html',
        data: {
            page: page
        },
        success: function(data) {
            if (data === ' ') {
                stopLoading = true;
                if (page === 1) {
                    data = '<p style="text-align:center">您没有消息</p>';
                }
            }

            $('.massage-page').append(data);
            window.rePosFooter();
            loading.hideLoadingMask();
        }
    });
}

function scrollHandler() {
    if (!stopLoading && ($(window).scrollTop() + $(window).height() > $('body').height() - 100)) {
        page++;
        massageAJAX(page);
    }
}

$(window).scroll(function() {
    window.requestAnimationFrame(scrollHandler);
});

// 删除消息
$page.on('touchstart', '.del', function() {
    var $good = $(this).closest('.massage-list'),
        $id = $good.data('id');

    dialog.showDialog({
        dialogText: '确定删除这条消息吗?',
        hasFooter: {
            leftBtnText: '取消',
            rightBtnText: '确定'
        }
    }, function() {

        $.ajax({
            type: 'POST',
            url: '/home/ajaxDelMes',
            data: {
                id: $id
            },
            success: function(data) {
                if (data.code === 200) {
                    dialog.showDialog({
                        dialogText: '删除消息成功',
                        autoHide: 2000,
                        fast: true
                    });
                    setTimeout(function() {
                        window.history.go(0);
                    }, 1200);
                } else {
                    dialog.showDialog({
                        dialogText: data.message,
                        autoHide: 2000,
                        fast: true
                    });
                }
            }
        });
    });
});

massageAJAX(page);