-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuserinfo.js
More file actions
416 lines (400 loc) · 12.1 KB
/
userinfo.js
File metadata and controls
416 lines (400 loc) · 12.1 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
window.onload=function(){
var button=getElement(".changeButton"); //为控制按钮添加单击事件
for(var i=0;i<button.length;i++){
button[i].onclick=control;
}
var showImageDialog1=getElement(".show_image")[0];//更改头像的大的对话框
var showImageDialog2=getElement(".show_image")[1];//上传头像的大对话框
var imageDialog1=getElement(".imageDialog")[0]; //更改头像的小的对话框
var imageDialog2=getElement(".imageDialog")[1]; //上传头像的小对话框
postionImageDialog();
var close1=getElement("#close1");//更改头像的小的对话框的关闭按钮
var close2=getElement("#close2");//上传头像的小的对话框的关闭按钮
//控制对话框的关闭
close1.onclick=function(){
showImageDialog1.style.display="none";
}
close2.onclick=function(){
showImageDialog2.style.display="none";
}
var tb=getElement("table")[0];
var imageList=tb.getElementsByTagName("img");
for(var i=0;i<imageList.length;i++){
imageList[i].onclick=changeIcon;
}
/*alert(imageList[0].getAttribute("src"));*/
//控制上传图片
var loadImage=getElement(".loadImage")[0];
var ok=getElement(".uploadIconControl")[0];
var cancel=getElement(".uploadIconControl")[1];
var inputFile=getElement(".inputfile")[0];
inputFile.onchange=function(){
var filter=[".png",".gif",".jpg",".bmp",".jpeg",".ico"];
var filepath=this.value;
var isReady=false;
for(var i=0;i<filter.length;i++){
if((filepath.indexOf(filter[i]))==-1){
continue;
}else{
isReady=true;
break;
}
}
if(!isReady){
alert("格式不支持!");
return;
}
var fm=document.forms["uploadiconForm"];
var formData=new FormData(fm);
var xhr=new XMLHttpRequest();
xhr.open("POST","/bbs/imageupload",true);
xhr.send(formData);
var url="";
xhr.onload=function(){
if(xhr.readyState==4&&xhr.status==200){
var json=xhr.responseText;
if(json=="")return;
var data=window.eval("("+json+")");
var state=data["state"];
url=data["url"];
if(state=="SUCCESS"){
if(loadImage.childNodes[0]==undefined){
var img=document.createElement("img");
img.src="/bbs/images/"+url;
img.style.width="80px";
img.style.height="80px"
loadImage.append(img);
}else{
var img=loadImage.childNodes[0];
img.src="/bbs/images/"+url;
}
}else{
alert("图片上传失败!");
}
}
}
ok.onclick=function(){
if(url=="")return;
var upurl="/bbs/changeicon";
var xhr=new XMLHttpRequest();
var param="iconname="+url;
xhr.open("post", upurl,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(param);
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var json=xhr.responseText;
if(json=="")return;
var data=window.eval("("+json+")");
if(data["state"]==0){
alert("头像更改成功!");
showImageDialog1.style.display="none";
window.location=location;
}else if(data["state"]==1){//session过期
alert("登录过期!");
window.location.href="/bbs/index.jsp";
}
}
}
}
}
cancel.onclick=function(){
showImageDialog2.style.display="none";
}
var inputData=getElement(".input_data");
var changeUsername=inputData[0];
var changePasswordOld=inputData[1];
var changePasswordNew=inputData[2];
var changeNickName=inputData[3];
var changeSexM=inputData[4];
var changeSexW=inputData[5];
var changeDateYear=inputData[6];
var changeDateMouth=inputData[7];
var changeDateDay=inputData[8];
var changeEmail=inputData[9];
var submit=getElement(".submit"); //提交按钮
for(var i=0;i<submit.length;i++){
submit[i].onclick=mySubmit;
}
function mySubmit(){
var Index=this.getAttribute("_sub");
if(Index=="1"){ //提交用户名
if(changeUsername.value=="")return;
var match=/^[\w]{6,12}$/g;
if(!match.test(changeUsername.value)){
alert("用户名必须是数字,字母,下划线组成,长度为6-12位");
}else{
var param="type=changeusername&value="+changeUsername.value;
sendMsg(param,Index);
}
}else if(Index=="2"){//提交密码
var old=changePasswordOld.value;
var New=changePasswordNew.value;
var match=/^[\w]{6,8}$/;
if(!match.test(changePasswordNew.value)){
alert("密码必须由6-8字母,数字,下划线组成");
}else{
var str=old+","+New;
var param="type=changepassword&value="+str;
sendMsg(param,Index);
}
}else if(Index=="3"){//提交昵称
var match=/^[\w\u4e00-\u9fa5]{5,25}$/g;
if(!match.test(changeNickName.value)){
alert("昵称必须由中文字符,字母,数字组成,长度为5-25位");
}else{
var param="type=changenickname&value="+changeNickName.value;
sendMsg(param,Index);
}
}else if(Index=="4"){//提交性别
if(!changeSexM.checked&&!changeSexW.checked){
alert("你还没有选择!");
return;
}
var value="";
if(changeSexM.checked){
value="男";
}else if(changeSexW.checked){
value="女";
}
var param="type=setsex&value="+value;
sendMsg(param,Index);
}else if(Index=="5"){//提交出生日期
var year=changeDateYear.value;
var mouth=changeDateMouth.value;
var day=changeDateDay.value;
var format=year+"-"+mouth+"-"+day;
var param="type=setbirthday&value="+format;
sendMsg(param,Index);
}else if(Index=="6"){//提交email
var match=/^\w+@([0-9a-zA-Z]+[-0-9a-zA-Z]*)(\.[0-9a-zA-Z]+[-0-9a-zA-Z]*)+$/g;
if(!match.test(changeEmail.value)){
alert("请输入正确的邮箱");
}else{
var email=changeEmail.value;
var param="type=changeemail&value="+email;
sendMsg(param,Index);
}
}else {
return;
}
function sendMsg(param,type){
var xhr=new XMLHttpRequest();
var url="/bbs/changefield";
xhr.open("POST", url,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(param);
xhr.onreadystatechange=function(){
if(xhr.readyState===4&&xhr.status==200){
var json=xhr.responseText;
if(json=="")return;
var data=window.eval("("+json+")");
switch(type){
case "1":
if(data["state"]==0){
alert("用户名修改成功");
changeUsername.value="";
window.location=location;
}else if(data["state"]==1){
alert("登录过期");
window.location.href="/bbs/index.jsp";
}else {
alert("用户名被注册!");
}
break;
case "2":
if(data["state"]==0){
alert("更改密码成功");
window.location=location;
changePasswordNew.value="";
changePasswordOld.value="";
}else if(data["state"]==1){
alert("登录过期");
window.location.href="/bbs/index.jsp";
}else {
alert("密码错误!");
}
break;
case "3":
if(data["state"]==0){
alert("昵称修改成功");
window.location=location;
changeNickName.value="";
}else if(data["state"]==1){
alert("登录过期");
window.location.href="/bbs/index.jsp";
}else {
alert("昵称修改失败!");
}
break;
case "4":
if(data["state"]==0){
alert("设置性别成功");
window.location=location;
}else if(data["state"]==1){
alert("登录过期");
window.location.href="/bbs/index.jsp";
}else {
alert("设置性别失败!");
}
break;
case "5":
if(data["state"]==0){
alert("设置出生日期成功");
window.location=location;
}else if(data["state"]==1){
alert("登录过期");
window.location.href="/bbs/index.jsp";
}else {
alert("设置出生日期失败!");
}
break;
case "6":
if(data["state"]==0){
alert("设置邮箱成功");
window.location=location;
changeEmail.value="";
}else if(data["state"]==1){
alert("登录过期");
window.location.href="/bbs/index.jsp";
}else {
alert("设置邮箱失败!");
}
break;
default:return;
}
}
}
}
}
var hidden=getElement(".hidden");
/*var inputText;*/
/*alert(inputData.length);
alert(changeDateYear);
alert(changeNickName);
alert(changeSexW);*/
function changeIcon(){ //更改头像
var str=this.getAttribute("src");
var start=str.lastIndexOf("/");
var iconName=str.substring(start+1,str.length);
var url="/bbs/changeicon";
var xhr=new XMLHttpRequest();
var param="changeIcon=1&iconname="+iconName;
xhr.open("post", url,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(param);
xhr.onreadystatechange=function(){
if(xhr.readyState==4&&xhr.status==200){
var json=xhr.responseText;
if(json=="")return;
var data=window.eval("("+json+")");
if(data["state"]==0){
alert("头像更改成功!");
showImageDialog1.style.display="none";
window.location=location;
}else if(data["state"]==1){//session过期
alert("登录过期!");
window.location.href="/bbs/index.jsp";
}
}
}
}
var isBlockOfUser=false;
var isBlockOfPw=false;
var isBlockOfNickName=false;
var isBlockOfSex=false;
var isBlockOfDate=false;
var isBlockOfEmail=false;
function control(){
var buttonIndex=this.getAttribute("_index");
if (buttonIndex=="")return;
if(buttonIndex=="1"){ //改变头像
showImageDialog1.style.display="block";
}else if(buttonIndex=="2"){ //上传头像
showImageDialog2.style.display="block";
}else if(buttonIndex=="3"){//更改用户名
if(isBlockOfUser){
hidden[0].style.display="none";
this.value="更改用户名";
isBlockOfUser=false;
}else{
hidden[0].style.display="block";
this.value="收起";
isBlockOfUser=true;
}
}else if(buttonIndex=="4"){//更改密码
if(isBlockOfPw){
hidden[1].style.display="none";
this.value="更改密码";
isBlockOfPw=false;
}else{
hidden[1].style.display="block";
this.value="收起";
isBlockOfPw=true;
}
}else if(buttonIndex=="5"){//更改昵称
if(isBlockOfNickName){
hidden[2].style.display="none";
this.value="更改昵称";
isBlockOfNickName=false;
}else{
hidden[2].style.display="block";
this.value="收起";
isBlockOfNickName=true;
}
}else if(buttonIndex=="6"){//设置性别
if(isBlockOfSex){
hidden[3].style.display="none";
this.value="设置性别";
isBlockOfSex=false;
}else{
hidden[3].style.display="block";
this.value="收起";
isBlockOfSex=true;
}
}else if(buttonIndex=="7"){//更改出生日期
if(isBlockOfDate){
hidden[4].style.display="none";
this.value="更改出生日期";
isBlockOfDate=false;
}else{
hidden[4].style.display="block";
this.value="收起";
isBlockOfDate=true;
}
}else if(buttonIndex="8"){//更改邮箱
if(isBlockOfEmail){
hidden[5].style.display="none";
this.value="更改邮箱";
isBlockOfEmail=false;
}else{
hidden[5].style.display="block";
this.value="收起";
isBlockOfEmail=true;
}
}else {
return;
}
}
function getElement(attr){
if(typeof(attr)=="string"){
if(attr.indexOf("#")==0){
attr=attr.slice(1);
return document.getElementById(attr);
}else if(attr.indexOf(".")==0){
attr=attr.slice(1);
return document.getElementsByClassName(attr);
}else{
return document.getElementsByTagName(attr);
}
}else{
return null;
}
}
function postionImageDialog(){ //首先对图片对话框进行定位
imageDialog1.style.left=window.screen.width/3+"px";
imageDialog1.style.top=window.screen.height/3.5+"px";
imageDialog2.style.left=window.screen.width/3+"px";
imageDialog2.style.top=window.screen.height/3.5+"px";
}
}