consultform.js 2.3 KB
/**
 *  我要咨询提交页面
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/12/01
 */
var $ = require('yoho-jquery'),
    tip = require('../../plugin/tip'),
    loading = require('../../plugin/loading');

var $consultForm = $('.consult-form'),
    $submit = $('#submit'),
    $content = $('#content'),
    productId = $('#product_id').val(),
    isSubmiting;

$submit.on('touchend', function() {
    $content.blur();
    $consultForm.submit();
    return false;
}).on('touchstart', function() {
    $(this).addClass('highlight');
}).on('touchend touchcancel', function() {
    $(this).removeClass('highlight');
});

$content.on('focus', function() {
    if ($content.val() === '请输入咨询内容') {
        $content.val('');
    }
}).on('blur', function() {
    if ($content.val() === '') {
        $content.val('请输入咨询内容');
    }
});
var getUrlParam = function (paramName) {
    var reg = "[\\?|\\&]+" + paramName + "=([^&]*)[^&]?";
    var exp = (window.location.href + "").match(reg);
    return exp ? exp[1] : "";
}

// 提交表单请求
$consultForm.on('submit', function() {
    var content;

    if (isSubmiting) {
        return false;
    }

    // 简单的表单校验
    content = $content.val();
    if (!content || content === '请输入咨询内容') {
        tip.show('咨询内容不能为空');
        return false;
    }

    isSubmiting = true;
    loading.showLoadingMask();
    $.ajax({
        method: 'POST',
        url: '/product/detail/consultsubmit',
        data: {
            product_id: productId,
            content: content
        }
    }).then(function(res) {
        if ($.type(res) !== 'object') {
            res = {};
        }
        if (res.code !== 200) {
            tip.show(res.message || '网络出了点问题~');
            isSubmiting = false;
            loading.hideLoadingMask();
        } else {
            tip.show('提交成功~');
            setTimeout(function() {
                var from = getUrlParam('from');
                if (from) {
                    window.location = decodeURIComponent(from);
                } else {
                    window.history.go(-1);
                }
            }, 3000);
        }
    }).fail(function() {
        tip.show('网络出了点问题~');
        isSubmiting = false;
    });
    return false;
});