complaints.page.js 3.91 KB
/**
 * 我的投诉
 * @author: yyqing<yanqing.yang@yoho.cn>
 * @date: 2016/3/18
 */

var $ = require('yoho-jquery');

var $cancelBtn = $('.cancel-btn'),
    $title = $('#title'),
    $customer = $('#customer'),
    $complaintsType = $('#complaintsType'),
    $orderCode = $('#orderCode'),
    $content = $('#content');

var ajaxing = false;
var rule = {
    title: {
        dom: $title,
        min: 1,
        max: 10,
        onshow: '请输入您的投诉主题',
        onfocus: '请输入主题',
        onmin: '投诉主题不能为空',
        onmax: '输入投诉主题不能超过10个字'
    },
    customer: {
        dom: $customer,
        min: 1,
        max: 10,
        onshow: '请填写您要投诉的对象,比如:客服00*号、发货员...',
        onfocus: '请填写您要投诉的对象',
        onmin: '投诉对象不能为空',
        onmax: '输入投诉对象不能超过10个字'
    },
    orderCode: {
        dom: $orderCode,
        regexp: /^([0-9,]{9,})$/, // 一个和多个订单
        onshow: '如果多个是多个订单号,请用英文逗号隔开,没有则不填',
        onfocus: '请输入订单号',
        onerror: '您输入的订单格式不对,如果多个是多个订单号,请用英文逗号隔开'
    },
    content: {
        dom: $content,
        min: 1,
        max: 130,
        onshow: '输入您投诉的内容',
        onfocus: '请输入您投诉的内容',
        onmin: '投诉内容不能为空',
        onmax: '输入的投诉内容不能超过130个字'
    }
};

function preventResubmit() {
    if (!ajaxing) {
        ajaxing = true;
        setTimeout(function() {
            ajaxing = false;
        }, 1000);
        return false;
    }
    return true;
}

function validate(info) {
    var $tips = info.dom.siblings('.option-tip'),
        val = $.trim(info.dom.val()),
        len = val.length;

    if (info.min && len < info.min) {
        $tips.text(info.onmin);
        return false;
    } else if (info.max && len > info.max) {
        $tips.text(info.onmax);
        return false;
    }

    if (info.regexp && !info.regexp.test(val)) {
        $tips.text(info.onerror);
        return false;
    }

    $tips.text('输入正确');
    return true;
}

function validateForm() {
    var res = {},
        focus;

    $.each(rule, function(key, info) {
        if (validate(info)) {
            res[key] = $.trim(info.dom.val());
        } else {
            if (!focus) {
                focus = key;
                info.dom.focus();
            }
        }
    });
    res.complaintsType = $complaintsType.val();

    if (focus) {
        return false;
    }
    return res;
}

function addComplaint() {
    var sendParm = validateForm();

    if (preventResubmit() || !sendParm) {
        return;
    }

    $.ajax({
        type: 'POST',
        url: '/home/complaints/submit',
        data: sendParm
    }).then(function(jsonData) {
        ajaxing = false;
        if (jsonData.code === 200) {
            window.location.reload();
        } else {
            alert(jsonData.message);
        }
    });
}

function cancelComplaint(id, dom) {
    var $dom = dom;

    if (preventResubmit()) {
        return;
    }

    $.ajax({
        type: 'POST',
        url: '/home/complaints/cancel',
        data: {
            id: id
        }
    }).then(function(jsonData) {
        ajaxing = false;
        if (jsonData.code === 200) {
            $dom.prev().remove();
            $dom.text('问题已撤销');
        } else {
            alert(jsonData.message);
        }
    });
}

$.each(rule, function(key, info) {
    info.dom.focus(function() {
        info.dom.siblings('.option-tip').text(info.onfocus);
    }).blur(function() {
        validate(info);
    });
});

$('#complaint-submit').click(function() {
    addComplaint();
});

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

    if (data && data.id) {
        cancelComplaint(data.id, $(this).parent());
    }
});