forked from lock-upme/OPMS
/
user.go
805 lines (714 loc) · 22 KB
/
user.go
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
package users
import (
"fmt"
"image"
"image/jpeg"
"github.com/virteman/OPMS/controllers"
. "github.com/virteman/OPMS/models/albums"
//. "github.com/virteman/OPMS/models/groups"
. "github.com/virteman/OPMS/models/knowledges"
. "github.com/virteman/OPMS/models/projects"
. "github.com/virteman/OPMS/models/users"
"github.com/virteman/OPMS/utils"
"os"
"strconv"
"strings"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/utils/pagination"
"github.com/oliamb/cutter"
)
//主页
type MainController struct {
controllers.BaseController
}
func (this *MainController) Get() {
this.TplName = "index.tpl"
}
//登录
type LoginUserController struct {
controllers.BaseController
}
func (this *LoginUserController) Get() {
check := this.BaseController.IsLogin
if check {
this.Redirect("/", 302)
return
} else {
this.TplName = "users/login.tpl"
}
}
func (this *LoginUserController) Post() {
username := this.GetString("username")
password := this.GetString("password")
if "" == username {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写用户名"}
this.ServeJSON()
}
if "" == password {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写密码"}
this.ServeJSON()
}
err, users := LoginUser(username, password)
if err == nil {
this.SetSession("userLogin", fmt.Sprintf("%d", users.Id)+"||"+users.Username+"||"+users.Avatar)
//this.SetSession("userPermission", GetPermissions(users.Id))
permission, _ := GetPermissionsAll(users.Id)
this.SetSession("userPermission", permission.Permission)
this.SetSession("userGroupid", permission.Groupid)
//this.SetSession("userPermissionModel", permission.Model)
//this.SetSession("userPermissionModelc", permission.Modelc)
this.Data["json"] = map[string]interface{}{"code": 1, "message": "贺喜你,登录成功"}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "登录失败"}
}
this.ServeJSON()
}
//退出
type LogoutUserController struct {
controllers.BaseController
}
func (this *LogoutUserController) Get() {
this.DelSession("userLogin")
this.DelSession("userPermissionModel")
this.DelSession("userPermissionModelc")
//this.Ctx.WriteString("you have logout")
this.Redirect("/login", 302)
}
//用户管理
type ManageUserController struct {
controllers.BaseController
}
func (this *ManageUserController) Get() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "user-manage") {
this.Abort("401")
}
page, err := this.GetInt("p")
status := this.GetString("status")
keywords := this.GetString("keywords")
if err != nil {
page = 1
}
offset, err1 := beego.AppConfig.Int("pageoffset")
if err1 != nil {
offset = 15
}
condArr := make(map[string]string)
condArr["status"] = status
condArr["keywords"] = keywords
countUser := CountUser(condArr)
paginator := pagination.SetPaginator(this.Ctx, offset, countUser)
_, _, user := ListUser(condArr, page, offset)
this.Data["paginator"] = paginator
this.Data["condArr"] = condArr
this.Data["user"] = user
this.Data["countUser"] = countUser
this.TplName = "users/user-index.tpl"
}
//用户主页
type ShowUserController struct {
controllers.BaseController
}
func (this *ShowUserController) Get() {
idstr := this.Ctx.Input.Param(":id")
if "" == idstr {
idstr = fmt.Sprintf("%d", this.BaseController.UserUserId)
}
id, _ := strconv.Atoi(idstr)
userId := int64(id)
pro, _ := GetProfile(userId)
if pro.Realname == "" {
this.Abort("404")
}
this.Data["pro"] = pro
user, _ := GetUser(userId)
this.Data["user"] = user
this.Data["departName"] = GetDepartsName(pro.Departid)
this.Data["positionName"] = GetPositionsName(pro.Positionid)
//我的项目
_, _, projects := ListMyProject(userId, 1, 10)
this.Data["projects"] = projects
//我的任务
condArr := make(map[string]string)
condArr["acceptid"] = idstr
_, _, tasks := ListProjectTask(condArr, 1, 10)
this.Data["tasks"] = tasks
//我的bug
_, _, tests := ListProjectTest(condArr, 1, 10)
this.Data["tests"] = tests
//知识分享
if this.BaseController.UserUserId != userId {
condArr["userid"] = idstr
}
_, _, knowledges := ListKnowledge(condArr, 1, 3)
this.Data["knowledges"] = knowledges
//相片
if this.BaseController.UserUserId != userId {
condArr["userid"] = idstr
}
_, _, albums := ListAlbum(condArr, 1, 8)
this.Data["albums"] = albums
//公告
//知识分享
condArr["status"] = "1"
_, _, notices := ListNotices(condArr, 1, 5)
this.Data["notices"] = notices
this.TplName = "users/profile.tpl"
}
//头像更换
type AvatarUserController struct {
controllers.BaseController
}
func (this *AvatarUserController) Get() {
this.TplName = "users/avatar.tpl"
}
func (this *AvatarUserController) Post() {
dataX, _ := this.GetInt("dataX")
dataY, _ := this.GetInt("dataY")
dataWidth, _ := this.GetInt("dataWidth")
dataHeight, _ := this.GetInt("dataHeight")
var filepath string
f, h, err := this.GetFile("file")
if err == nil {
defer f.Close()
now := time.Now()
dir := "./static/uploadfile/" + strconv.Itoa(now.Year()) + "-" + strconv.Itoa(int(now.Month())) + "/" + strconv.Itoa(now.Day())
err1 := os.MkdirAll(dir, 0755)
if err1 != nil {
this.Data["json"] = map[string]interface{}{"code": 1, "message": "目录权限不够"}
this.ServeJSON()
return
}
//生成新的文件名
filename := h.Filename
//ext := utils.SubString(filename, strings.LastIndex(filename, "."), 5)
ext := utils.SubString(utils.Unicode(filename), strings.LastIndex(utils.Unicode(filename), "."), 5)
filename = utils.GetGuid() + ext
if err != nil {
this.Data["json"] = map[string]interface{}{"code": 0, "message": err}
this.ServeJSON()
return
} else {
this.SaveToFile("file", dir+"/"+filename)
filepath = strings.Replace(dir, ".", "", 1) + "/" + filename
}
//utils.DoImageHandler(filepath, 200)
} else {
filepath = this.GetString("avatar")
}
dst, _ := utils.LoadImage("." + filepath)
croppedImg, err := cutter.Crop(dst, cutter.Config{
Width: dataWidth,
Height: dataHeight,
Anchor: image.Point{dataX, dataY},
Mode: cutter.TopLeft, // optional, default value
})
filen := strings.Replace(filepath, ".", "-cropper.", -1)
file, err := os.Create("." + filen)
defer file.Close()
err = jpeg.Encode(file, croppedImg, &jpeg.Options{100})
if err == nil {
ChangeUserAvatar(this.BaseController.UserUserId, filen)
this.SetSession("userLogin", fmt.Sprintf("%d", int64(this.BaseController.UserUserId))+"||"+this.BaseController.UserUsername+"||"+filen)
}
this.Data["json"] = map[string]interface{}{"code": 1, "message": "个性头像设置成功"}
this.ServeJSON()
}
//用户状态更改异步操作
type AjaxStatusUserController struct {
controllers.BaseController
}
func (this *AjaxStatusUserController) Post() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "user-edit") {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "无权设置"}
this.ServeJSON()
return
}
id, _ := this.GetInt64("id")
if id <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择用户"}
this.ServeJSON()
return
}
status, _ := this.GetInt("status")
if status <= 0 || status >= 3 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择操作状态"}
this.ServeJSON()
return
}
err := ChangeUserStatus(id, status)
if err == nil {
this.Data["json"] = map[string]interface{}{"code": 1, "message": "用户状态更改成功"}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "用户状态更改失败"}
}
this.ServeJSON()
}
type AjaxSearchUserController struct {
controllers.BaseController
}
func (this *AjaxSearchUserController) Get() {
username := this.GetString("term")
if "" == username {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写用户名"}
this.ServeJSON()
return
}
condArr := make(map[string]string)
condArr["keywords"] = username
_, _, users := ListUser(condArr, 1, 20)
/*
a := make([]map[string]string, 2)
for i := 0; i < 2; i++ {
a[i] = map[string]string{"id": "1", "investor": "2"}
}
*/
newArr := make([]map[string]string, len(users))
for b, _ := range users {
newArr[b] = map[string]string{"value": fmt.Sprintf("%d", users[b].Id), "label": users[b].Profile.Realname}
}
this.Data["json"] = newArr
this.ServeJSON()
}
//添加用户信息
type AddUserController struct {
controllers.BaseController
}
func (this *AddUserController) Get() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "user-add") {
this.Abort("401")
}
condArr := make(map[string]string)
condArr["status"] = "1"
_, _, departs := ListDeparts(condArr, 1, 9)
this.Data["departs"] = departs
_, _, positions := ListPositions(condArr, 1, 9)
this.Data["positions"] = positions
var pro UsersProfile
pro.Sex = 1
this.Data["pro"] = pro
this.TplName = "users/user-form.tpl"
}
func (this *AddUserController) Post() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "user-add") {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "无权设置"}
this.ServeJSON()
return
}
username := this.GetString("username")
if "" == username {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写用户名"}
this.ServeJSON()
return
}
departid, _ := this.GetInt64("depart")
if departid <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择部门"}
this.ServeJSON()
return
}
positionid, _ := this.GetInt64("position")
if positionid <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择职位"}
this.ServeJSON()
return
}
password := this.GetString("password")
realname := this.GetString("realname")
if "" == realname {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写姓名"}
this.ServeJSON()
return
}
sex, _ := this.GetInt("sex")
if sex <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择性别"}
this.ServeJSON()
return
}
birth := this.GetString("birth")
if "" == birth {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择出生日期"}
this.ServeJSON()
return
}
email := this.GetString("email")
if "" == email {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写邮箱"}
this.ServeJSON()
return
}
webchat := this.GetString("webchat")
qq := this.GetString("qq")
phone := this.GetString("phone")
if "" == phone {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写手机号"}
this.ServeJSON()
return
}
tel := this.GetString("tel")
address := this.GetString("address")
emercontact := this.GetString("emercontact")
if "" == emercontact {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写紧急联系人"}
this.ServeJSON()
return
}
emerphone := this.GetString("emerphone")
if "" == emerphone {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写紧急联系人电话"}
this.ServeJSON()
return
}
var err error
//雪花算法ID生成
id := utils.SnowFlakeId()
var pro UsersProfile
pro.Id = id
pro.Realname = realname
pro.Sex = sex
pro.Birth = birth
pro.Email = email
pro.Webchat = webchat
pro.Qq = qq
pro.Phone = phone
pro.Tel = tel
pro.Address = address
pro.Emercontact = emercontact
pro.Emerphone = emerphone
pro.Departid = departid
pro.Positionid = positionid
pro.Ip = this.Ctx.Input.IP()
var user Users
user.Id = id
user.Username = username
user.Password = password
err = AddUserProfile(user, pro)
if err == nil {
//新用户默认权限
/*var per UsersPermissions
per.Id = id
per.Permission = "project-team,team-add,team-delete,project-need,need-add,need-edit,project-task,task-add,task-edit,project-test,test-add,test-edit,checkwork-manage,message-manage,message-delete,leave-manage,leave-add,leave-edit,leave-view,leave-approval,overtime-manage,overtime-add,overtime-edit,overtime-view,overtime-approval,expense-manage,expense-add,expense-edit,expense-view,expense-approval,businesstrip-manage,businesstrip-add,businesstrip-edit,businesstrip-view,businesstrip-approval,goout-manage,goout-add,goout-edit,goout-view,goout-approval,oagood-manage,oagood-add,oagood-edit,oagood-view,oagood-approval,knowledge-manage,knowledge-add,knowledge-edit,album-manage,album-upload,album-edit"
per.Model = "项目管理-project-book||project-manage,考勤管理-checkwork-tasks||checkwork-list,审批管理-approval-suitcase||#,知识分享-knowledge-tasks||knowledge-list,员工相册-album-plane||album-list"
per.Modelc = "请假-approval||leave-manage,加班-approval||overtime-manage,报销-approval||expense-manage,出差-approval||businesstrip-manage,外出-approval||goout-manage,物品-approval||oagood-manage"
AddPermissions(per)*/
/*
var groupUser GroupsUser
groupUser.Id = utils.SnowFlakeId()
groupUser.Groupid = 1468755197309162133
groupUser.Userid = id
err = AddGroupsUser(groupUser)*/
this.Data["json"] = map[string]interface{}{"code": 1, "message": "员工信息添加成功", "id": fmt.Sprintf("%d", id)}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "员工信息添加失败"}
}
this.ServeJSON()
}
//修改用户信息
type EditUserController struct {
controllers.BaseController
}
func (this *EditUserController) Get() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "user-edit") {
this.Abort("401")
}
idstr := this.Ctx.Input.Param(":id")
id, _ := strconv.Atoi(idstr)
pro, err := GetProfile(int64(id))
if err != nil {
this.Abort("404")
}
this.Data["pro"] = pro
user, _ := GetUser(int64(id))
this.Data["user"] = user
condArr := make(map[string]string)
condArr["status"] = "1"
_, _, departs := ListDeparts(condArr, 1, 9)
this.Data["departs"] = departs
_, _, positions := ListPositions(condArr, 1, 9)
this.Data["positions"] = positions
this.TplName = "users/user-form.tpl"
}
func (this *EditUserController) Post() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "user-edit") {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "无权设置"}
this.ServeJSON()
return
}
id, _ := this.GetInt64("id")
if id <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "员工参数出错"}
this.ServeJSON()
return
}
username := this.GetString("username")
if "" == username {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写用户名"}
this.ServeJSON()
return
}
departid, _ := this.GetInt64("depart")
if departid <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择部门"}
this.ServeJSON()
return
}
positionid, _ := this.GetInt64("position")
if positionid <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择职位"}
this.ServeJSON()
return
}
password := this.GetString("password")
realname := this.GetString("realname")
if "" == realname {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写姓名"}
this.ServeJSON()
return
}
sex, _ := this.GetInt("sex")
if sex <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择性别"}
this.ServeJSON()
return
}
birth := this.GetString("birth")
if "" == birth {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择出生日期"}
this.ServeJSON()
return
}
email := this.GetString("email")
if "" == email {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写邮箱"}
this.ServeJSON()
return
}
webchat := this.GetString("webchat")
qq := this.GetString("qq")
phone := this.GetString("phone")
if "" == phone {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写手机号"}
this.ServeJSON()
return
}
tel := this.GetString("tel")
address := this.GetString("address")
emercontact := this.GetString("emercontact")
if "" == emercontact {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写紧急联系人"}
this.ServeJSON()
return
}
emerphone := this.GetString("emerphone")
if "" == emerphone {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写紧急联系人电话"}
this.ServeJSON()
return
}
_, err := GetUser(id)
if err != nil {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "员工不存在"}
this.ServeJSON()
return
}
var pro UsersProfile
pro.Realname = realname
pro.Sex = sex
pro.Birth = birth
pro.Email = email
pro.Webchat = webchat
pro.Qq = qq
pro.Phone = phone
pro.Tel = tel
pro.Address = address
pro.Emercontact = emercontact
pro.Emerphone = emerphone
pro.Departid = departid
pro.Positionid = positionid
err = UpdateProfile(id, pro)
var user Users
user.Username = username
if password != "" {
user.Password = password
}
err = UpdateUser(id, user)
if err == nil {
this.Data["json"] = map[string]interface{}{"code": 1, "message": "信息修改成功", "id": fmt.Sprintf("%d", id)}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "信息修改失败"}
}
this.ServeJSON()
}
type EditUserProfileController struct {
controllers.BaseController
}
func (this *EditUserProfileController) Get() {
userid := this.BaseController.UserUserId
pro, err := GetProfile(userid)
if err != nil {
this.Abort("404")
}
this.Data["pro"] = pro
this.TplName = "users/profile-form.tpl"
}
func (this *EditUserProfileController) Post() {
userid := this.BaseController.UserUserId
realname := this.GetString("realname")
if "" == realname {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写姓名"}
this.ServeJSON()
return
}
sex, _ := this.GetInt("sex")
if sex <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择性别"}
this.ServeJSON()
return
}
birth := this.GetString("birth")
if "" == birth {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择出生日期"}
this.ServeJSON()
return
}
email := this.GetString("email")
if "" == email {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写邮箱"}
this.ServeJSON()
return
}
webchat := this.GetString("webchat")
qq := this.GetString("qq")
phone := this.GetString("phone")
if "" == phone {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写手机号"}
this.ServeJSON()
return
}
tel := this.GetString("tel")
address := this.GetString("address")
emercontact := this.GetString("emercontact")
if "" == emercontact {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写紧急联系人"}
this.ServeJSON()
return
}
emerphone := this.GetString("emerphone")
if "" == emerphone {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写紧急联系人电话"}
this.ServeJSON()
return
}
_, err := GetUser(userid)
if err != nil {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "员工不存在"}
this.ServeJSON()
return
}
var pro UsersProfile
pro.Realname = realname
pro.Sex = sex
pro.Birth = birth
pro.Email = email
pro.Webchat = webchat
pro.Qq = qq
pro.Phone = phone
pro.Tel = tel
pro.Address = address
pro.Emercontact = emercontact
pro.Emerphone = emerphone
err = UpdateProfile(userid, pro)
if err == nil {
this.Data["json"] = map[string]interface{}{"code": 1, "message": "个人资料修改成功", "type": "profile", "id": fmt.Sprintf("%d", userid)}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "修改失败"}
}
this.ServeJSON()
}
type EditUserPasswordController struct {
controllers.BaseController
}
func (this *EditUserPasswordController) Get() {
this.TplName = "users/profile-pwd.tpl"
}
func (this *EditUserPasswordController) Post() {
oldpwd := this.GetString("oldpwd")
if "" == oldpwd {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写当前密码"}
this.ServeJSON()
return
}
newpwd := this.GetString("newpwd")
if "" == newpwd {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写新密码"}
this.ServeJSON()
return
}
confpwd := this.GetString("confpwd")
if "" == confpwd {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写确认密码"}
this.ServeJSON()
return
}
if confpwd != newpwd {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "两次输入密码不一致"}
this.ServeJSON()
return
}
userid := this.BaseController.UserUserId
err := UpdatePassword(userid, oldpwd, newpwd)
if err == nil {
this.Data["json"] = map[string]interface{}{"code": 1, "message": "密码修改成功", "id": fmt.Sprintf("%d", userid)}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "修改失败"}
}
this.ServeJSON()
}
type PermissionController struct {
controllers.BaseController
}
func (this *PermissionController) Get() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "user-permission") {
this.Abort("401")
}
idstr := this.Ctx.Input.Param(":id")
id, err := strconv.Atoi(idstr)
permission := GetPermissions(int64(id))
if err != nil {
this.Abort("404")
}
this.Data["permission"] = permission
this.Data["userid"] = idstr
this.TplName = "users/permission.tpl"
}
func (this *PermissionController) Post() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "user-permission") {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "无权设置"}
this.ServeJSON()
return
}
userid, _ := this.GetInt64("userid")
permission := this.GetString("permission")
model := this.GetString("model")
modelc := this.GetString("modelc")
var per UsersPermissions
per.Permission = permission
per.Model = model
per.Modelc = modelc
err := UpdatePermissions(userid, per)
if err == nil {
this.Data["json"] = map[string]interface{}{"code": 1, "message": "权限设置成功"}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "设置失败"}
}
this.ServeJSON()
}