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

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

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

var Alert = dialog.Alert;

require('yoho-jquery-placeholder');
require('../plugins/jquery.qupload');

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

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

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

    function showNotice() {
        var wh = $(window).height();

        $mask.removeClass('hide');
        $dialog.css({
            'max-height': wh - 60
        }).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,
        $upKey,
        $styleCount;

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

    var Dialog = require('../common/dialog').Dialog;
    var successDialog,
        SWFUpload = window.SWFUpload,
        upKey;

    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');
    $upKey = $('#upload-key');
    $styleCount = $('#style-count');
    upKey = $upKey.val();
    $upKey.remove();

    function initUpload(arr) {
        $.each(arr, function(k, v) {
            $('#' + v.dom).qupload({
                uploadKey: upKey,
                button_image_url: '',
                button_text: '<span class="btn_upload_text">上传</span>',
                button_text_style:
                    '.btn_upload_text{color:#000;margin-left:30px;' +
                    'line-height: 2em;font-size:14px;}',
                button_text_left_padding: 32,
                button_text_top_padding: 8,
                button_width: 160,
                button_height: 35,
                button_action: SWFUpload.BUTTON_ACTION.SELECT_FILE, // 单选
                file_size_limit: '10240',
                file_types: v.type,
                uploadSuccessed: function(serverData) {
                    var data = JSON.parse(serverData);

                    if (data.code !== 12) {
                        new Alert(data.message).show();
                        return false;
                    }
                    v.callback(data.data.hit.Filedata.return_file_path);
                }
            });
        });
    }

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

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

        if ($styleCount.val() === '' || !(styleCountReg.test($styleCount.val()))) {
            $styleCount.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/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',
            type: '*.ppt;*.pdf;*.zip;*.rar',
            callback: function(data) {
                $('#brand-material').val(data);
                $('.brand-tip').html('上传成功');
                new Alert('上传成功!').show();
            }
        },
        {
            dom: 'upload-goods-material',
            type: '*.xls;*.xlsx;*.zip;*.rar',
            callback: function(data) {
                $('#goods-material').val(data);
                $('.goods-tip').html('上传成功');
                new Alert('上传成功!').show();
            }
        }
    ]);

    // 提交成功
    // 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';
    //                 }
    //             }
    //         ]
    //     });
    //     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');

            $.ajax({
                url: '/shop/applysave',
                type: 'post',
                data: $('form').serialize()
            }).then(function(data) {

                if (data.code && data.code === 200) {
                    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.yohobuy.com/settled';
                                }
                            }
                        ]
                    });
                    successDialog.show();
                    return;
                } else {
                    new Alert(data.message).show();
                }
            });

        } else {
            $('.error').first().focus();
            scrollTo = $('.error').first().offset().top;
            $(window).scrollTop(scrollTo - 10);
        }
    });

    if (navigator.userAgent.indexOf('MSIE') >= 0) {
        $('[placeholder]').focus(function() {
            var $input = $(this);

            $input.removeClass('placeholder');
        }).blur(function() {
            var $input = $(this);

            if ($input.val() === '') {
                $input.addClass('placeholder');
            }
        });
    }
}());