setting.page.js 5.66 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 modifyHead = require('./setting/modifyHead');// flash操作
var tip;// 头像编辑弹框
var $year = $('#year');
var $month = $('#month');
var $day = $('#day');
var birthday = {
    year: $year.val() || '',
    month: $month.val() || '',
    day: $day.val() || ''
};

var headHtml = modifyHead.swfobject('head', '600px', '400px', '../../img/me/head.swf?code=' + Math.random() +
    '&upload_url=' + encodeURIComponent(location.protocol + '//' +
        location.hostname + ':' + location.port + '/me/setting/modifyHead'));

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

        html.push('<div style="width: 600px;height: 400px">');
        html.push(headHtml);
        html.push('</div>');

        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 regRealName = new RegExp(/^[\u4e00-\u9fa5a-zA-Z]{2,12}$/);
        var birthdayForm = $('#birthday');
        var nickForm = $('#nick_name');
        var realNameForm = $('#username');

        // 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();
        !regRealName.test(info.username) ? realNameForm.next().show() : realNameForm.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) || !regRealName.test(info.username)) {
            flag = false;
        }
        return flag;
    }
};

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

// 第一步:身份验证
require('../me/setting/validate');

// 第二步:操作
require('../me/setting/operate');

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

// 编辑头像移入移出切换效果
$('.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() {
    tip = new _dialog({
        className: 'settled-success',
        content: Bll.setIcon(),
        btns: [
            {
                id: 'apply',
                btnClass: ['apply'],
                name: '保存',
                cb: function() {
                    modifyHead.uploadImage();
                }
            },
            {
                id: 'cancel',
                btnClass: ['cancel'],
                name: '取消',
                cb: function() {
                    tip.close();
                }
            }
        ]
    }).show();
});

/**
 * 头像上传处理
 * @param obj
 */
window.receive_image_bytes = function(obj) {
    var result = JSON.parse(obj);

    if (result.code === 200) {
        tip.close();
        $('.show-ico img').attr('src', result.data.image_url.split('?')[0]);
        location.reload();
    } else {
        tip.close();
        new _alert('头像修改失败!').show();
    }
};


// 时间控件切换
$year.on('change', function() {
    birthday.year = $(this).val();
});
$month.on('change', function() {
    birthday.month = $(this).val();
});
$day.on('change', function() {
    birthday.day = $(this).val();
});

$(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.year + '-' + birthday.month + '-' + birthday.day,
            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();
                    }
                }
            });
        }
    });
});