/
UserController.go
847 lines (744 loc) · 24.2 KB
/
UserController.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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
package HomeControllers
import (
"fmt"
"path/filepath"
"github.com/astaxie/beego"
"strings"
"time"
"os"
"github.com/TruthHun/DocHub/helper"
"github.com/TruthHun/DocHub/helper/conv"
"github.com/TruthHun/DocHub/models"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/validation"
)
type UserController struct {
BaseController
}
func (this *UserController) Prepare() {
this.BaseController.Prepare()
this.Xsrf()
}
//会员中心
func (this *UserController) Get() {
uid, _ := this.GetInt(":uid")
path := this.GetString(":splat")
params := conv.Path2Map(path)
//排序
sort := "new"
if param, ok := params["sort"]; ok {
sort = param
}
//页码
p := 1
if page, ok := params["p"]; ok {
p = helper.Interface2Int(page)
if p < 1 {
p = 1
}
}
switch sort {
case "dcnt":
sort = "dcnt"
case "score":
sort = "score"
case "vcnt":
sort = "vcnt"
case "ccnt":
sort = "ccnt"
default:
sort = "new"
}
//显示风格
style := "list"
if s, ok := params["style"]; ok {
style = s
}
if style != "th" {
style = "list"
}
//cid:collect folder id ,收藏夹id
cid := 0
if s, ok := params["cid"]; ok {
cid = helper.Interface2Int(s)
}
if p < 1 {
p = 1
}
if uid < 1 {
uid = this.IsLogin
}
this.Data["Uid"] = uid
if uid <= 0 {
this.Redirect("/user/login", 302)
return
}
listRows := 16
user, rows, err := models.NewUser().GetById(uid)
if err != nil {
helper.Logger.Error(err.Error())
}
if rows == 0 {
this.Redirect("/", 302)
return
}
if cid > 0 {
sql := fmt.Sprintf("select Title,Cnt from %v where Id=? limit 1", models.GetTableCollectFolder())
var params []orm.Params
orm.NewOrm().Raw(sql, cid).Values(¶ms)
if len(params) == 0 {
this.Redirect(fmt.Sprintf("/user/%v/collect", uid), 302)
return
}
this.Data["Folder"] = params[0]
fields := "di.Id,di.`Uid`, di.`Cid`, di.`TimeCreate`, di.`Dcnt`, di.`Vcnt`, di.`Ccnt`, di.`Score`, di.`Status`, di.`ChanelId`, di.`Pid`,c.Title Category,u.Username,d.Title,ds.`Md5`, ds.`Ext`, ds.`ExtCate`, ds.`ExtNum`, ds.`Page`, ds.`Size`"
sqlFormat := `
select %v from %v di left join %v u on di.Uid=u.Id
left join %v clt on clt.Did=di.Id
left join %v d on d.Id=di.Id
left join %v c on c.Id=di.cid
left join %v ds on ds.Id=di.DsId
where %v order by %v limit %v,%v
`
sql = fmt.Sprintf(sqlFormat,
fields,
models.GetTableDocumentInfo(),
models.GetTableUser(),
models.GetTableCollect(),
models.GetTableDocument(),
models.GetTableCategory(),
models.GetTableDocumentStore(),
fmt.Sprintf("clt.Cid=%v", cid),
"clt.Id desc",
(p-1)*listRows, listRows,
)
var data []orm.Params
orm.NewOrm().Raw(sql).Values(&data)
this.Data["Lists"] = data
this.Data["Page"] = helper.Paginations(6, helper.Interface2Int(params[0]["Cnt"]), listRows, p, fmt.Sprintf("/user/%v/doc/cid/%v", user["Id"], cid), "sort", sort, "style", style)
} else {
this.Data["Lists"], _, _ = models.GetDocList(uid, 0, 0, 0, p, listRows, sort, 1, 0)
this.Data["Page"] = helper.Paginations(6, helper.Interface2Int(user["Document"]), listRows, p, fmt.Sprintf("/user/%v/doc", user["Id"]), "sort", sort, "style", style)
}
this.Data["Tab"] = "doc"
this.Data["Cid"] = cid
this.Data["User"] = user
this.Data["PageId"] = "wenku-user"
this.Data["IsUser"] = true
this.Data["Sort"] = sort
this.Data["Style"] = style
this.Data["P"] = p
this.Data["Seo"] = models.NewSeo().GetByPage("PC-Ucenter-Doc", "文档列表-会员中心-"+user["Username"].(string), "会员中心,文档列表,"+user["Username"].(string), "文档列表-会员中心-"+user["Username"].(string), this.Sys.Site)
this.Data["Ranks"], _, err = models.NewUser().UserList(1, 8, "i.Document desc", "u.Id,u.Username,u.Avatar,u.Intro,i.Document", "i.Status=1")
if err != nil {
helper.Logger.Error(err.Error())
}
this.TplName = "index.html"
}
//金币记录
func (this *UserController) Coin() {
uid, _ := this.GetInt(":uid")
p, _ := this.GetInt("p", 1)
if p < 1 {
p = 1
}
if uid < 1 {
uid = this.IsLogin
}
if uid <= 0 {
this.Redirect("/user/login", 302)
return
}
listRows := 16
lists, _, _ := models.GetList(models.GetTableCoinLog(), p, listRows, orm.NewCondition().And("Uid", uid), "-Id")
if p > 1 { // 当页码大于0,则以 JSON 返回数据
this.ResponseJson(true, "数据获取成功", lists)
}
user, rows, err := models.NewUser().GetById(uid)
if err != nil {
helper.Logger.Error(err.Error())
}
if rows == 0 {
this.Redirect("/", 302)
return
}
this.Data["Lists"] = lists
this.Data["User"] = user
this.Data["PageId"] = "wenku-user"
this.Data["Tab"] = "coin"
this.Data["IsUser"] = true
this.Data["Ranks"], _, err = models.NewUser().UserList(1, 8, "i.Document desc", "u.Id,u.Username,u.Avatar,u.Intro,i.Document", "i.Status=1")
if err != nil {
helper.Logger.Error(err.Error())
}
this.Data["Seo"] = models.NewSeo().GetByPage("PC-Ucenter-Coin", "财富记录—会员中心-"+user["Username"].(string), "会员中心,财富记录,"+user["Username"].(string), "财富记录—会员中心-"+user["Username"].(string), this.Sys.Site)
this.TplName = "coin.html"
}
// 收藏夹
func (this *UserController) Collect() {
this.Data["Tab"] = "collect"
action := this.GetString("action")
uid, _ := this.GetInt(":uid")
p, _ := this.GetInt("p", 1)
if p < 1 {
p = 1
}
if uid < 1 {
uid = this.IsLogin
}
if uid <= 0 {
this.Redirect("/user/login", 302)
return
}
listRows := 100
lists, _, _ := models.GetList(models.GetTableCollectFolder(), p, listRows, orm.NewCondition().And("Uid", uid), "-Id")
if p > 1 { // 页码大于1,以 JSON 返回数据
this.ResponseJson(true, "数据获取成功", lists)
}
user, rows, err := models.NewUser().GetById(uid)
if err != nil {
helper.Logger.Error(err.Error())
}
if rows == 0 {
this.Redirect("/", 302)
return
}
this.Data["Lists"] = lists
this.Data["User"] = user
this.Data["PageId"] = "wenku-user"
this.Data["IsUser"] = true
this.Data["Uid"] = uid
this.Data["Ranks"], _, err = models.NewUser().UserList(1, 8, "i.Document desc", "u.Id,u.Username,u.Avatar,u.Intro,i.Document", "i.Status=1")
if err != nil {
helper.Logger.Error(err.Error())
}
this.TplName = "collect.html"
this.Data["Seo"] = models.NewSeo().GetByPage("PC-Ucenter-Folder", "收藏夹—会员中心-"+user["Username"].(string), "会员中心,收藏夹,"+user["Username"].(string), "收藏夹—会员中心-"+user["Username"].(string), this.Sys.Site)
if action == "edit" {
this.Data["Edit"] = true
} else {
this.Data["Edit"] = false
}
}
//用户登录
func (this *UserController) Login() {
if this.IsLogin > 0 {
this.Redirect("/user", 302)
return
}
// GET 请求
if this.Ctx.Request.Method == "GET" {
this.Data["Seo"] = models.NewSeo().GetByPage("PC-Login", "会员登录", "会员登录", "会员登录", this.Sys.Site)
this.Data["IsUser"] = true
this.Data["PageId"] = "wenku-reg"
this.TplName = "login.html"
return
}
type Post struct {
Email, Password string
}
var post struct {
Email, Password string
}
this.ParseForm(&post)
valid := validation.Validation{}
res := valid.Email(post.Email, "Email")
if !res.Ok {
this.ResponseJson(false, "登录失败,邮箱格式不正确")
}
ModelUser := models.NewUser()
users, rows, err := ModelUser.UserList(1, 1, "", "", "u.`email`=? and u.`password`=?", post.Email, helper.MD5Crypt(post.Password))
if rows == 0 || err != nil {
if err != nil {
helper.Logger.Error(err.Error())
}
this.ResponseJson(false, "登录失败,邮箱或密码不正确")
}
user := users[0]
this.IsLogin = helper.Interface2Int(user["Id"])
if this.IsLogin > 0 {
//查询用户有没有被封禁
if info := ModelUser.UserInfo(this.IsLogin); info.Status == false { //被封禁了
this.ResponseJson(false, "登录失败,您的账号已被管理员禁用")
}
this.BaseController.SetCookieLogin(this.IsLogin)
this.ResponseJson(true, "登录成功")
}
this.ResponseJson(false, "登录失败,未知错误!")
}
//用户退出登录
func (this *UserController) Logout() {
this.ResetCookie()
if v, ok := this.Ctx.Request.Header["X-Requested-With"]; ok && v[0] == "XMLHttpRequest" {
this.ResponseJson(true, "退出登录成功")
}
this.Redirect("/", 302)
}
//会员注册[GET/POST]
func (this *UserController) Reg() {
if this.IsLogin > 0 {
this.Redirect("/user", 302)
return
}
if this.Ctx.Request.Method == "GET" {
this.Data["IsUser"] = true
this.Data["Seo"] = models.NewSeo().GetByPage("PC-Login", "会员注册", "会员注册", "会员注册", this.Sys.Site)
this.Data["PageId"] = "wenku-reg"
if this.Sys.IsCloseReg {
this.TplName = "regclose.html"
} else {
this.TplName = "reg.html"
}
return
}
if this.Sys.IsCloseReg {
this.ResponseJson(false, "注册失败,站点已关闭注册功能")
}
//先验证邮箱验证码是否正确
email := this.GetString("email")
code := this.GetString("code")
if this.Sys.CheckRegEmail {
sessEmail := fmt.Sprintf("%v", this.GetSession("RegMail"))
sessCode := fmt.Sprintf("%v", this.GetSession("RegCode"))
if sessEmail != email || sessCode != code {
this.ResponseJson(false, "邮箱验证码不正确,请重新输入或重新获取")
}
}
// 注册
err, uid := models.NewUser().Reg(
email,
this.GetString("username"),
this.GetString("password"),
this.GetString("repassword"),
this.GetString("intro"),
)
if err != nil {
this.ResponseJson(false, err.Error())
}
models.Regulate(models.GetTableSys(), "CntUser", 1, "Id=1") //站点用户数量增加
this.IsLogin = uid
this.SetCookieLogin(uid)
this.ResponseJson(true, "会员注册成功")
}
// 发送邮件
func (this *UserController) SendMail() {
beego.Info(this.Ctx.GetCookie("_xsrf"))
//发送邮件的类型:注册(reg)和找回密码(findpwd)
t := this.GetString("type")
if t != "reg" && t != "findpwd" {
this.ResponseJson(false, "邮件发送类型不正确")
}
valid := validation.Validation{}
email := this.GetString("email")
res := valid.Email(email, "mail")
if res.Error != nil || !res.Ok {
this.ResponseJson(false, "邮箱格式不正确")
}
//检测邮箱是否已被注册
ModelUser := models.NewUser()
user := ModelUser.GetUserField(orm.NewCondition().And("email", email))
//注册邮件
if t == "reg" {
if user.Id > 0 {
this.ResponseJson(false, "该邮箱已经被注册会员")
}
code := helper.RandStr(6, 0)
err := models.NewEmail().SendMail(email, fmt.Sprintf("%v会员注册验证码", this.Sys.Site), strings.Replace(this.Sys.TplEmailReg, "{code}", code, -1))
if err != nil {
helper.Logger.Error("邮件发送失败:%v", err.Error())
this.ResponseJson(false, "邮件发送失败,请联系管理员检查邮箱配置是否正确")
}
this.SetSession("RegMail", email)
this.SetSession("RegCode", code)
this.ResponseJson(true, "邮件发送成功,请打开邮箱查看验证码")
}
// 找回密码
if user.Id == 0 {
this.ResponseJson(false, "邮箱不存在")
}
code := helper.RandStr(6, 0)
err := models.NewEmail().SendMail(email, fmt.Sprintf("%v找回密码验证码", this.Sys.Site), strings.Replace(this.Sys.TplEmailFindPwd, "{code}", code, -1))
if err != nil {
helper.Logger.Error("邮件发送失败:%v", err.Error())
this.ResponseJson(false, "邮件发送失败,请联系管理员检查邮箱配置是否正确")
}
this.SetSession("FindPwdMail", email)
this.SetSession("FindPwdCode", code)
this.ResponseJson(true, "邮件发送成功,请打开邮箱查看验证码")
}
//会员签到,增加金币
func (this *UserController) Sign() {
if this.IsLogin == 0 {
this.ResponseJson(false, "签到失败,请先登录")
}
var data = models.Sign{
Uid: this.IsLogin,
Date: time.Now().Format("20060102"),
}
_, err := orm.NewOrm().Insert(&data)
if err != nil {
this.ResponseJson(false, "签到失败,您今天已签到")
}
if err = models.Regulate(models.GetTableUserInfo(), "Coin", this.Sys.Sign, fmt.Sprintf("Id=%v", this.IsLogin)); err == nil {
log := models.CoinLog{
Uid: this.IsLogin,
Coin: this.Sys.Sign,
Log: fmt.Sprintf("签到成功,获得 %v 个金币", this.Sys.Sign),
}
models.NewCoinLog().LogRecord(log)
}
this.ResponseJson(true, fmt.Sprintf("恭喜您,今日签到成功,领取了 %v 个金币", this.Sys.Sign))
}
// 检测用户是否已登录
func (this *UserController) CheckLogin() {
if this.BaseController.IsLogin > 0 {
this.ResponseJson(true, "已登录")
}
this.ResponseJson(false, "您当前处于未登录状态,请先登录")
}
// 创建收藏夹
func (this *UserController) CreateCollectFolder() {
if this.IsLogin == 0 {
this.ResponseJson(false, "您当前未登录,请先登录")
}
cover := ""
timestamp := int(time.Now().Unix())
//文件在文档库中未存在,则接收文件并做处理
f, fh, err := this.GetFile("Cover")
if err == nil {
defer f.Close()
slice := strings.Split(fh.Filename, ".")
ext := slice[len(slice)-1]
dir := fmt.Sprintf("./uploads/%v/%v/", time.Now().Format("2006-01-02"), this.IsLogin)
os.MkdirAll(dir, 0777)
file := helper.MD5Crypt(fmt.Sprintf("%v-%v-%v", timestamp, this.IsLogin, fh.Filename)) + "." + ext
tmpFile := dir + file
err = this.SaveToFile("Cover", tmpFile)
if err != nil {
helper.Logger.Error(err.Error())
this.ResponseJson(false, "封面保存失败")
}
defer os.RemoveAll(tmpFile)
if err = helper.CropImage(tmpFile, helper.CoverWidth, helper.CoverHeight); err != nil {
helper.Logger.Error(err.Error())
this.ResponseJson(false, "封面裁剪失败")
}
//将图片移动到OSS
var cs *models.CloudStore
if cs, err = models.NewCloudStore(false); err != nil {
helper.Logger.Error(err.Error())
this.ResponseJson(false, "连接云存储失败")
}
if err = cs.Upload(tmpFile, file); err != nil {
helper.Logger.Error(err.Error())
} else {
cover = file
}
}
// 收藏夹
folder := models.CollectFolder{
Uid: this.IsLogin,
Title: this.GetString("Title"),
Description: this.GetString("Description"),
TimeCreate: int(time.Now().Unix()),
Cnt: 0,
Cover: cover,
}
// 收藏夹 Id 大于0,则表示编辑收藏夹
folder.Id, _ = this.GetInt("Id")
if folder.Id > 0 { // 编辑收藏夹
cols := []string{"Title", "Description"}
if len(cover) > 0 {
cols = append(cols, "Cover")
}
if _, err = orm.NewOrm().Update(&folder, cols...); err == nil {
this.ResponseJson(true, "收藏夹编辑成功")
}
} else { // 创建收藏夹
if _, err = orm.NewOrm().Insert(&folder); err == nil { //收藏夹数量+1
models.Regulate(models.GetTableUserInfo(), "Collect", 1, "Id=?", this.IsLogin)
this.ResponseJson(true, "收藏夹创建成功")
}
}
if err != nil {
helper.Logger.Error(err.Error())
this.ResponseJson(false, "操作失败,请重试")
}
this.ResponseJson(true, "操作成功")
}
// 找回密码
func (this *UserController) FindPwd() {
if this.IsLogin > 0 {
this.Redirect("/user", 302)
return
}
if this.Ctx.Request.Method == "GET" {
this.Data["Seo"] = models.NewSeo().GetByPage("PC-Findpwd", "找回密码", "找回密码", "找回密码", this.Sys.Site)
this.Data["IsUser"] = true
this.Data["PageId"] = "wenku-reg"
this.TplName = "findpwd.html"
return
}
rules := map[string][]string{
"username": {"required", "mincount:2", "maxcount:16"},
"email": {"required", "email"},
"code": {"required", "len:6"},
"password": {"required", "mincount:6"},
"repassword": {"required", "mincount:6"},
}
params, errs := helper.Valid(this.Ctx.Request.Form, rules)
if len(errs) > 0 {
if _, ok := errs["username"]; ok {
this.ResponseJson(false, "用户名限2-16个字符")
}
if _, ok := errs["email"]; ok {
this.ResponseJson(false, "邮箱格式不正确")
}
if _, ok := errs["code"]; ok {
this.ResponseJson(false, "请输入6位验证码")
}
if _, ok := errs["password"]; ok {
this.ResponseJson(false, "密码长度,至少6个字符")
}
if _, ok := errs["repassword"]; ok {
this.ResponseJson(false, "密码长度,至少6个字符")
}
}
//校验验证码和邮箱是否匹配
if fmt.Sprintf("%v", this.GetSession("FindPwdMail")) != params["email"].(string) || fmt.Sprintf("%v", this.GetSession("FindPwdCode")) != params["code"].(string) {
this.ResponseJson(false, "验证码不正确,修改密码失败")
}
pwd := helper.MD5Crypt(params["password"].(string))
repwd := helper.MD5Crypt(params["repassword"].(string))
if pwd != repwd {
this.ResponseJson(false, "确认密码和密码不一致")
}
user := models.NewUser().GetUserField(orm.NewCondition().And("Email", params["email"]))
if user.Id == 0 || user.Username != params["username"].(string) {
this.ResponseJson(false, "重置密码失败,用户名与邮箱不匹配")
}
_, err := models.UpdateByIds("user", "Password", pwd, user.Id)
if err != nil {
helper.Logger.Error(err.Error())
this.ResponseJson(false, "重置密码失败,请刷新页面重试")
}
this.DelSession("FindPwdMail")
this.DelSession("FindPwdCode")
this.ResponseJson(true, "重置密码成功,请重新登录")
}
//删除文档
func (this *UserController) DocDel() {
if this.IsLogin == 0 {
this.ResponseJson(false, "请先登录")
}
docid, _ := this.GetInt(":doc")
if docid == 0 {
this.ResponseJson(false, "删除失败,文档不存在")
}
err := models.NewDocumentRecycle().RemoveToRecycle(this.IsLogin, true, docid)
if err != nil {
helper.Logger.Error("删除失败:%v", err.Error())
this.ResponseJson(false, "删除失败,文档不存在")
}
this.ResponseJson(true, "删除成功")
}
//文档编辑
func (this *UserController) DocEdit() {
if this.IsLogin == 0 {
this.Redirect("/user", 302)
}
docId, _ := this.GetInt(":doc")
if docId == 0 {
this.Redirect("/user", 302)
}
info := models.DocumentInfo{Id: docId}
err := orm.NewOrm().Read(&info)
if err != nil {
helper.Logger.Error(err.Error())
this.Redirect("/user", 302)
}
if info.Uid != this.IsLogin { // 文档所属用户id与登录的用户id不一致
this.Redirect("/user", 302)
}
doc := models.Document{Id: docId}
// POST
if this.Ctx.Request.Method == "POST" {
ruels := map[string][]string{
"Title": {"required", "unempty"},
"Chanel": {"required", "gt:0", "int"},
"Pid": {"required", "gt:0", "int"},
"Cid": {"required", "gt:0", "int"},
"Tags": {"required"},
"Intro": {"required"},
"Price": {"required", "int"},
}
params, errs := helper.Valid(this.Ctx.Request.Form, ruels)
if len(errs) > 0 {
this.ResponseJson(false, "参数错误")
}
doc.Title = params["Title"].(string)
doc.Keywords = params["Tags"].(string)
doc.Description = params["Intro"].(string)
info.Pid = params["Pid"].(int)
info.Cid = params["Cid"].(int)
info.ChanelId = params["Chanel"].(int)
info.Price = params["Price"].(int)
info.TimeUpdate = int(time.Now().Unix())
orm.NewOrm().Update(&doc, "Title", "Keywords", "Description")
orm.NewOrm().Update(&info, "Pid", "Cid", "ChanelId", "Price")
//原分类-1
models.Regulate(models.GetTableCategory(), "Cnt", -1, fmt.Sprintf("Id in(%v,%v,%v)", info.ChanelId, info.Cid, info.Pid))
//新分类+1
models.Regulate(models.GetTableCategory(), "Cnt", 1, fmt.Sprintf("Id in(%v,%v,%v)", params["Chanel"], params["Cid"], params["Pid"]))
this.ResponseJson(true, "文档编辑成功")
}
// GET
err = orm.NewOrm().Read(&doc)
if err != nil {
helper.Logger.Error(err.Error())
this.Redirect("/user", 302)
}
cond := orm.NewCondition().And("status", 1)
data, _, _ := models.GetList(models.GetTableCategory(), 1, 2000, cond, "sort")
this.Data["User"], _, _ = models.NewUser().GetById(this.IsLogin)
this.Data["Ranks"], _, err = models.NewUser().UserList(1, 8, "i.Document desc", "u.Id,u.Username,u.Avatar,u.Intro,i.Document", "i.Status=1")
this.Data["IsUser"] = true
this.Data["Cates"], _ = conv.InterfaceToJson(data)
this.Data["json"] = data
this.Data["PageId"] = "wenku-user"
this.Data["Info"] = info
this.Data["Doc"] = doc
this.Data["Tab"] = "doc"
this.TplName = "edit.html"
}
//删除收藏(针对收藏夹)
func (this *UserController) CollectFolderDel() {
cid, _ := this.GetInt(":cid")
if cid > 0 && this.IsLogin > 0 {
err := models.NewCollect().DelFolder(cid, this.IsLogin)
if err != nil {
helper.Logger.Error(err.Error())
this.ResponseJson(false, err.Error())
}
this.ResponseJson(true, "收藏夹删除成功")
}
this.ResponseJson(false, "删除失败,参数错误")
}
//取消收藏(针对文档)
func (this *UserController) CollectCancel() {
cid, _ := this.GetInt(":cid")
did, _ := this.GetInt(":did")
if err := models.NewCollect().Cancel(did, cid, this.IsLogin); err != nil {
helper.Logger.Error(err.Error())
this.ResponseJson(false, "移除收藏失败,可能您为收藏该文档")
}
this.ResponseJson(true, "移除收藏成功")
}
//更换头像
func (this *UserController) Avatar() {
if this.IsLogin == 0 {
this.ResponseJson(false, "请先登录")
}
dir := fmt.Sprintf("./uploads/%v/%v", time.Now().Format("2006-01-02"), this.IsLogin)
os.MkdirAll(dir, 0777)
f, fh, err := this.GetFile("Avatar")
if err != nil {
helper.Logger.Error("用户(%v)更新头像失败:%v", this.IsLogin, err.Error())
this.ResponseJson(false, "头像文件上传失败")
}
defer f.Close()
ext := strings.ToLower(strings.TrimLeft(filepath.Ext(fh.Filename), "."))
if !(ext == "jpg" || ext == "jpeg" || ext == "png" || ext == "gif") {
this.ResponseJson(false, "头像图片格式只支持jpg、jpeg、png和gif")
}
tmpFile := dir + "/" + helper.MD5Crypt(fmt.Sprintf("%v-%v-%v", fh.Filename, this.IsLogin, time.Now().Unix())) + "." + ext
saveFile := helper.MD5Crypt(tmpFile) + "." + ext
err = this.SaveToFile("Avatar", tmpFile)
if err != nil {
helper.Logger.Error("用户(%v)头像保存失败:%v", this.IsLogin, err.Error())
this.ResponseJson(false, "头像文件保存失败")
}
//头像裁剪
if err = helper.CropImage(tmpFile, helper.AvatarWidth, helper.AvatarHeight); err != nil {
helper.Logger.Error("图片裁剪失败:%v", err.Error())
}
var cs *models.CloudStore
if cs, err = models.NewCloudStore(false); err != nil {
helper.Logger.Error(err.Error())
this.ResponseJson(false, "内部服务错误:云存储连接失败")
}
err = cs.Upload(tmpFile, saveFile)
if err != nil {
helper.Logger.Error(err.Error())
this.ResponseJson(false, "头像文件保存失败")
}
os.RemoveAll(tmpFile)
//查询数据库用户数据
var user = models.User{Id: this.IsLogin}
orm.NewOrm().Read(&user)
oldAvatar := user.Avatar
user.Avatar = saveFile
rows, err := orm.NewOrm().Update(&user, "Avatar")
if rows > 0 && err == nil {
this.ResponseJson(true, "头像更新成功")
go cs.Delete(oldAvatar)
}
if err != nil {
helper.Logger.Error(err.Error())
}
this.ResponseJson(false, "头像更新失败")
}
//编辑个人信息
func (this *UserController) Edit() {
if this.IsLogin == 0 {
this.ResponseJson(false, "请先登录")
}
changepwd := false
cols := []string{"Intro"}
rules := map[string][]string{
"OldPassword": {"required"},
"NewPassword": {"required"},
"RePassword": {"required"},
"Intro": {"required"},
}
params, errs := helper.Valid(this.Ctx.Request.Form, rules)
if len(errs) > 0 {
this.ResponseJson(false, "参数不正确")
}
var user = models.User{Id: this.IsLogin}
orm.NewOrm().Read(&user)
if len(params["OldPassword"].(string)) > 0 || len(params["NewPassword"].(string)) > 0 || len(params["RePassword"].(string)) > 0 {
if len(params["NewPassword"].(string)) < 6 || len(params["RePassword"].(string)) < 6 {
this.ResponseJson(false, "密码长度必须至少6个字符")
}
opwd := helper.MD5Crypt(params["OldPassword"].(string))
npwd := helper.MD5Crypt(params["NewPassword"].(string))
rpwd := helper.MD5Crypt(params["RePassword"].(string))
if user.Password != opwd {
this.ResponseJson(false, "原密码不正确")
}
if npwd != rpwd {
this.ResponseJson(false, "确认密码和新密码必须一致")
}
if opwd == npwd {
this.ResponseJson(false, "确认密码不能与原密码相同")
}
user.Password = rpwd
cols = append(cols, "Password")
changepwd = true
}
user.Intro = params["Intro"].(string)
affected, err := orm.NewOrm().Update(&user, cols...)
if err != nil {
helper.Logger.Error(err.Error())
this.ResponseJson(false, "设置失败,请刷新页面重试")
}
if affected == 0 {
this.ResponseJson(true, "设置失败,可能您未对内容做更改")
}
if changepwd {
this.ResetCookie()
this.ResponseJson(true, "设置成功,您设置了新密码,请重新登录")
}
this.ResponseJson(true, "设置成功")
}