settled.js 8.95 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();

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

    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: v.url,
                fileObjName: 'fileData',
                fileSizeLimit: 10000,
                buttonText: '上传',
                height: 34,
                width: 150,
                multi: false,
                onUploadComplete: function(file, data) {
                    v.callback(JSON.parse(data));
                },
                onError: function(info, fileType, data) {
                    var msg = '图片上传失败!';

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

    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);
        });
    }

    // 初始化上传插件
    initUpload([
        {
            dom: 'upload-brand-material',
            url: '/shop/settled/uploadPdf',
            callback: function(data) {
                if (data.code === 200) {
                    $('#brand-material').val(data.list[0].relUrl);
                    $('.brand-tip').html('上传成功');
                    alert('上传成功!');
                } else {
                    $('.brand-tip').html('(点击上传品牌资料,ppt / pdf,大小不超过10M, 提示上传成功后即可提交)');
                    alert(data.message);
                }
            }
        },
        {
            dom: 'upload-goods-material',
            url: '/shop/settled/uploadExcel',
            callback: function(data) {
                if (data.code === 200) {
                    $('#goods-material').val(data.list[0].relUrl);
                    $('.goods-tip').html('上传成功');
                    alert('上传成功!');
                } else {
                    $('.goods-tip').html('(点击上传产品列表,excel表格,大小不超过10M, 提示上传成功后即可提交)');
                    alert(data.message);
                }
            }
        }
    ]);

    // 提交成功
    if (location.search.indexOf('state=1') > -1) {
        successDialog = new Dialog({
            className: 'settled-success',
            closeIcon: false,
            content: '<h1 class="main-title">恭喜您已提交成功</h1><h2 class="sub-title">我们将尽快与您联系</h2>',
            btns: [
                {
                    id: 'apply-success-ok',
                    btnClass: ['apply-success-ok'],
                    name: '确认',
                    cb: function() {
                        location.href = '/shop/settled/index';
                    }
                }
            ]
        });
        successDialog.show();
        return;
    }

    $('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);
        }
    });
}());