setting.page.js
5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* [个人中心]个人设置
* @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) ||
typeof (info.area_code) === 'undefined') {
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]);
} 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();
}
}
});
}
});
});