settled.js 7.24 KB
/**
 * 商家入驻
 * @author: xuqi<qi.xu@yoho>
 * @date: 2016/3/24
 */

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

var reg = require('../passport/mail-phone-regx');

require('yoho.placeholder');
require('../../plugin/jquery.upload');

//入口页
(function() {
    var $mask,
        $dialog;

    if ($('.settled-page').length === 0) {
        return;
    }

    $mask = $('#mask');
    $dialog = $('#notice-container');

    function showNotice() {
        $mask.removeClass('hide');
        $dialog.removeClass('hide');
    }

    function hideNotice() {
        $mask.addClass('hide');
        $dialog.addClass('hide');
    }

    $('#fake-link').click(showNotice);

    $('#fake-close').click(hideNotice);
    $('#mask').click(hideNotice);
}());

//申请页
(function() {
    var $otherBr,
        $otherNp,
        $otherRp,
        $categoryUl,
        $shopListTr,
        $shops,
        $panel;

    var $postcode,
        $email,
        $phoneNum;

    var categoryHtml = $('#category-tpl').html();

    if ($('.settled-apply-page').length < 0) {
        return;
    }

    $('[placeholder]').placeholder();

    $otherBr = $('#other-brand-relation');
    $otherNp = $('#other-new-period');
    $otherRp = $('#other-replenishment-period');
    $categoryUl = $('#category-list');
    $shopListTr = $('#shop-list');
    $shops = $('#shops');
    $panel = $('#mask, #submit-success');

    $postcode = $('#postcode');
    $email = $('#email');
    $phoneNum = $('#phone-number');

    function initUpload(arr) {
        $.each(arr, function(k, v) {
            $('#' + v.dom).upload({
                auto: true,
                buttonClass: 'upload-btn',
                uploadScript: '/shop/settled/uploadPdf',
                fileObjName: 'fileData',
                fileSizeLimit: 10000,
                buttonText: '上传',
                height: 34,
                width: 150,
                multi: false,
                onUploadComplete: function(file, data) {
                    v.callback(data);
                },
                onError: function(info, fileType, data) {
                    var msg = '图片上传失败!';

                    if (info === 'FILE_SIZE_LIMIT_EXCEEDED') {
                        msg = '图片大小超出限制!';
                    }
                    alert(msg);
                }
            });
        });
    }

    initUpload([
        {
            dom: 'brand-material',
            callback: function(data) {
                console.log(data);
            }
        },
        {
            dom: 'brand-product',
            callback: function(data) {
                console.log(data);
            }
        }
    ]);

    function validate() {
        var pass = true,
            postcodeReg = /[0-9]\d{5}(?!\d)/,
            emailReg = reg.emailRegx,
            phoneReg = reg.phoneRegx['+86'];

        $('.va, .category-item select, .for-other, .category-item').removeClass('error');

        $('.va-null').each(function() {
            if ($(this).val() === '') {
                $(this).addClass('error');
                pass = false;
            }
        });

        if ($postcode.val() === '' || !postcodeReg.test($postcode.val())) {
            $postcode.addClass('error');
            pass = false;
        }

        if ($email.val() === '' || !emailReg.test($email.val())) {
            $email.addClass('error');
            pass = false;
        }

        if ($phoneNum.val() === '' || !(phoneReg.test($phoneNum.val()))) {
            $phoneNum.addClass('error');
            pass = false;
        }

        $('.category-item').each(function() {
            if ($(this).get(0).selectedIndex === 0) {
                $(this).addClass('error');
                pass = false;
            }
        });

        if ($('input[name="seller-role"]:checked').index() === 3 &&
            $otherBr.val() === '') {
            pass = false;
            $otherBr.addClass('error');
        }

        if ($('input[name="new_cycle"]:checked').index() === 5 &&
            $otherNp.val() === '') {
            pass = false;
            $otherNp.addClass('error');
        }

        if ($('input[name="supply_cycle"]:checked').index() === 5 &&
            $otherRp.val() === '') {
            pass = false;
            $otherRp.addClass('error');
        }

        return pass;
    }


    function addCategoryUI() {
        $categoryUl.closest('tr').removeClass('hide');
        $categoryUl.append('<li>' + categoryHtml + '</li>');
    }

    //获取二级分类的方法
    function getCategoryTwo() {
        var id = $(this).val();
        var $categoryTwo = $(this).parent().find('.category-two');
        var html = '<option>选择二级分类</option>';

        if (id === '') {
            $categoryTwo.html(html);
            return;
        }

        $.ajax({
            url: '/shop/settled/getTwoCategory',
            type: 'GET',
            data: {
                id: id
            }
        }).then(function(data) {
            var res = [];
            var key;

            if (data.code === 200) {
                res = data.data;
                for (key = 0; key < res.length; key++) {
                    html += '<option value="' + res[key].id + '" >' + res[key].name + '</option>';
                }
            }
            $categoryTwo.html(html);
        });
    }

    $('input[name="seller_role"]').change(function() {
        if ($(this).index() === 3) {
            $otherBr.removeClass('hide');
        } else {
            $otherBr.addClass('hide');
        }
    });

    $('input[name="new-cycle"]').change(function() {
        if ($(this).index() === 5) {
            $otherNp.removeClass('hide');
        } else {
            $otherNp.addClass('hide');
        }
    });

    $('input[name="supply-cycle"]').change(function() {
        if ($(this).index() === 5) {
            $otherRp.removeClass('hide');
        } else {
            $otherRp.addClass('hide');
        }
    });

    $('input[name="have-store"]').change(function() {
        if ($(this).index() === 1) {
            $shopListTr.addClass('hide');
        } else {
            $shopListTr.removeClass('hide');
        }
    });

    $('.form-container').on('change', '.category-one', getCategoryTwo);

    $('#category-add').click(addCategoryUI);

    $shops.delegate('.shop-add', 'click', function() {
        $shops.append(
            '<li>' +
                '<input class="shop-addres" name="store-address" type="text" placeholder="输入店铺地址">' +
                '<span>月均销售:</span>' +
                '<input class="shop-sales-volume" type="text" name="store-sales-volume" placeholder="输入价格">' +
                '<span>元</span>' +
            '</li>'
        );
    });

    $('#apply-post').click(function() {
        var scrollTo;

        if (validate()) {
            $panel.removeClass('hide');
            $('form').submit();
        } else {
            $('.error').first().focus();
            scrollTo = $('.error').first().offset().top;

            $(window).scrollTop(scrollTo - 10);
        }
    });
}());