setting.page.js 5.23 KB
/**
 * [个人中心]个人设置
 * @author: jiangmin
 * @date: 2016/07/11
 */
var cascadingAddress = require('../plugins/cascading-address');
var dialog = require('../plugins/dialog');
var _dialog = dialog.Dialog;
var _alert = dialog.Alert;

var Bll = {
    setIcon: function() {
        var html = [];

        html.push('<form id="upload_form"  method="post" action="" onsubmit="return checkForm()">');
        html.push('<div  class="post-picture inline-block">');
        html.push('<div class="choose-avatar"></div>');
        html.push('<div class="post-file"><input id="avatar" name="avatar" type="file" value="点击上传"></div>');
        html.push('<p class="post-limit">支持JPG、GIF、PNG、JPEG、BMP格式,文件小于3M</p>');
        html.push('</div>');
        html.push(' <div class="show-picture inline-block" ><img id="post-picture">');
        html.push(' </div>');
        html.push('<div class="preview-picture inline-block">');
        html.push('<div class="small-preview"></div>');
        html.push('</div>');
        html.push('</form>');

        return html.join('');
    },
    validate: function(info) {
        var regBirth = new RegExp(/^[1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]-[0-3]{0,1}[0-9]$/);
        var regName = new RegExp(/^[\u4e00-\u9fa5_-a-zA-Z0-9]+$/);
        var birthdayForm = $('#birthday');
        var nickForm = $('#nick_name');
        var addressForm = $('.form-group-address');
        var flag = true;

        !regBirth.test(info.birthday) ? birthdayForm.next().show() : birthdayForm.next().hide();
        !regName.test(info.nick_name) ? nickForm.next().show() : nickForm.next().hide();
        typeof (info.area_code) === 'undefined' ?
            addressForm.css('margin-bottom', '20px').find('.error-tips').show() :
            addressForm.css('margin-bottom', '70px').find('.error-tips').hide();
        if (!regBirth.test(info.birthday) || !regName.test(info.nick_name) || typeof (info.area_code) === 'undefined') {
            flag = false;
        }
        return flag;
    }
};

require('./me');
require('../plugins/check');
require('../me/setting/step1');
require('../me/setting/step2');
require('../me/setting/step3');


// 编辑头像移入移出切换效果
$('.user-icon').hover(function() {
    $(this).find('.show-ico ').addClass('hide').end().find('.edit-ico').removeClass('hide');
}, function() {
    $(this).find('.show-ico').removeClass('hide').end().find('.edit-ico').addClass('hide');
});

// 编辑头像打开弹框
$(document).on('click', '.edit-ico', function() {
    var tip = new _dialog({
        className: 'settled-success',
        title: '自定义头像',
        content: Bll.setIcon(),
        btns: [
            {
                id: 'apply',
                btnClass: ['apply'],
                name: '保存',
                cb: function() {
                    tip.close();
                }
            },
            {
                id: 'cancel',
                btnClass: ['cancel'],
                name: '取消',
                cb: function() {
                    tip.close();
                }
            }
        ]
    }).show();
});


$(document).on('change', '#avatar', function() {
    /* var oFile = $("#avatar")[0].files[0];
     var oImage = document.getElementById('post-picture');
     var oReader = new FileReader();
     oReader.onload = function (e) {
     oImage.src = e.target.result;
     $(".post-picture").hide();
     $("#post-picture").show();
     };
     oReader.readAsDataURL(oFile);*/
});


$(function() {
    var address = cascadingAddress({el: '#address'});
    var areaCode = $('#area_code').val();

    if (areaCode) {
        address.setAddress(areaCode);
    }


    // 设置性别
    $('.input-radio').check({
        type: 'radio',
        group: 'genders',
        onChange: function(ele, checked, value) {
            var gender = $('#gender').val();

            checked ? $('#gender').val(value) : $('#gender').val(gender);
        }
    });

    /**
     * 保存修改
     */
    $(document).on('click', '#save-settings', function() {
        var area = address.getAreaIds();

        var body = {
            nick_name: $('#nick_name').val(),
            username: $('#username').val(),
            gender: $('#gender').val(),
            birthday: $('#birthday').val(),
            area_code: area.split(',')[2],

            // todo 手机号码老接口必填
            mobile: $('#infoMobile').val(),
            full_address: $('#full_address').val(),

            // todo 邮编老接口必填字段
            zip_code: $('#zip_code').val() || '210000'
        };

        if (Bll.validate(body)) {
            $.ajax({
                type: 'POST',
                url: '/me/setting/editUserInfo',
                dataType: 'json',
                data: body,
                success: function(data) {
                    var len = 0;

                    data.forEach(function(x) {
                        if (x.code === 200) {
                            len++;
                        }
                    });
                    if (len === 2) {
                        new _alert('修改成功!').show();
                    } else {
                        new _alert('修改失败!').show();
                    }
                }
            });
        }
    });
});