setting.page.js 4.85 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');
var tip;

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 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() {
    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]);
    } else {
        tip.close();
        new _alert('头像修改失败!').show();
    }
};

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