setting.page.js
5.23 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
/**
* [个人中心]个人设置
* @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();
}
}
});
}
});
});