message.page.js 3.25 KB
/**
 * 我的信息
 * @author: yyqing<yanqing.yang@yoho.cn>
 * @date: 2016/3/21
 */
var $ = require('yoho-jquery');

var Dialog = require('../common/dialog');

var $container = $('#message-main-container'),
    $msgCheck = $container.find('.msg-check'),
    $readBtn = $container.find('.read-choose-btn'),
    $checked;

var $pickBtn = $('.pick-coupon-btn');

var operationId = [];

var pickBusy = false;

var Alert = Dialog.Alert,
    Confirm = Dialog.confirm;

function hasNew() {
    var newMsg = false;

    $checked = $container.find('.msg-check:checked');
    operationId = [];

    $checked.each(function() {
        var $par = $(this).parent().parent();

        operationId.push($(this).val());
        if ($par.hasClass('new-msg')) {
            newMsg = true;
        }
    });

    if (newMsg) {
        $readBtn.addClass('has-new');
    } else {
        $readBtn.removeClass('has-new');
    }
}

function msgOperation(type, data) {
    var url;

    switch (type) {
        case 'del':
            url = '/home/message/del';
            break;
        case 'read':
            url = '/home/message/read';
            break;
        default:
            break;
    }

    if (!url) {
        return;
    }

    $.ajax({
        type: 'GET',
        url: url,
        data: data
    }).then(function(jsonData) {
        if (jsonData.code === 200) {
            if (type === 'read') {
                $checked.removeAttr('checked');
                $checked.parent().parent().removeClass('new-msg');
                hasNew();
            } else {
                window.location.reload();
            }
        } else {
            new Alert(jsonData.message);
        }
    });
}

$container.on('change', 'input[type="checkbox"]', function() {
    var checked = $(this).attr('checked');

    if ($(this).hasClass('choose-all')) {
        if (checked === 'checked') {
            $msgCheck.attr('checked', true);
        } else {
            $msgCheck.removeAttr('checked');
        }
    }
    hasNew();
});

$container.on('click', '.del-btn', function() {
    var data = $(this).data();

    new Confirm({
        content: '您确定要删除这条短消息?',
        cb: function() {
            msgOperation('del', data);
        }
    }).show();
});

$container.on('click', '.del-choose-btn', function() {
    if (!operationId.length) {
        new Alert('请选中您要删除的消息');
        return;
    }

    new Confirm({
        content: '确定要删除您选中的消息?',
        cb: function() {
            msgOperation('del', {
                id: operationId.join(',')
            });
        }
    }).show();
});

$container.on('click', '.read-choose-btn', function() {
    if ($(this).hasClass('has-new')) {
        msgOperation('read', {
            id: operationId.join(',')
        });
    }
});

$pickBtn.click(function() {
    var data = $(this).data();

    if (pickBusy) {
        return;
    }
    pickBusy = true;
    $.ajax({
        type: 'GET',
        url: '/home/message/pickCoupon',
        data: data
    }).then(function(jsonData) {
        var msg = jsonData.message;

        if (jsonData.code === 200) {
            msg = '领取成功';
            $(this).removeClass('pick-coupon-btn');
        }

        pickBusy = false;

        new Alert(msg);
    });
});