forked from mindoc-org/mindoc
/
SettingController.go
171 lines (139 loc) · 4.29 KB
/
SettingController.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
package controllers
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/astaxie/beego/logs"
"github.com/lifei6671/mindoc/conf"
"github.com/lifei6671/mindoc/graphics"
"github.com/lifei6671/mindoc/models"
"github.com/lifei6671/mindoc/utils"
)
type SettingController struct {
BaseController
}
func (c *SettingController) Index() {
c.TplName = "setting/index.tpl"
if c.Ctx.Input.IsPost() {
email := strings.TrimSpace(c.GetString("email", ""))
phone := strings.TrimSpace(c.GetString("phone"))
description := strings.TrimSpace(c.GetString("description"))
if email == "" {
c.JsonResult(601, "邮箱不能为空")
}
member := c.Member
member.Email = email
member.Phone = phone
member.Description = description
member.RealName = strings.TrimSpace(c.GetString("real_name",""))
if err := member.Update(); err != nil {
c.JsonResult(602, err.Error())
}
c.SetMember(*member)
c.JsonResult(0, "ok")
}
}
func (c *SettingController) Password() {
c.TplName = "setting/password.tpl"
if c.Ctx.Input.IsPost() {
if c.Member.AuthMethod == conf.AuthMethodLDAP {
c.JsonResult(6009, "当前用户不支持修改密码")
}
password1 := c.GetString("password1")
password2 := c.GetString("password2")
password3 := c.GetString("password3")
if password1 == "" {
c.JsonResult(6003, "原密码不能为空")
}
if password2 == "" {
c.JsonResult(6004, "新密码不能为空")
}
if count := strings.Count(password2, ""); count < 6 || count > 18 {
c.JsonResult(6009, "密码必须在6-18字之间")
}
if password2 != password3 {
c.JsonResult(6003, "确认密码不正确")
}
if ok, _ := utils.PasswordVerify(c.Member.Password, password1); !ok {
c.JsonResult(6005, "原始密码不正确")
}
if password1 == password2 {
c.JsonResult(6006, "新密码不能和原始密码相同")
}
pwd, err := utils.PasswordHash(password2)
if err != nil {
c.JsonResult(6007, "密码加密失败")
}
c.Member.Password = pwd
if err := c.Member.Update(); err != nil {
c.JsonResult(6008, err.Error())
}
c.JsonResult(0, "ok")
}
}
// Upload 上传图片
func (c *SettingController) Upload() {
file, moreFile, err := c.GetFile("image-file")
defer file.Close()
if err != nil {
logs.Error("", err.Error())
c.JsonResult(500, "读取文件异常")
}
ext := filepath.Ext(moreFile.Filename)
if !strings.EqualFold(ext, ".png") && !strings.EqualFold(ext, ".jpg") && !strings.EqualFold(ext, ".gif") && !strings.EqualFold(ext, ".jpeg") {
c.JsonResult(500, "不支持的图片格式")
}
x1, _ := strconv.ParseFloat(c.GetString("x"), 10)
y1, _ := strconv.ParseFloat(c.GetString("y"), 10)
w1, _ := strconv.ParseFloat(c.GetString("width"), 10)
h1, _ := strconv.ParseFloat(c.GetString("height"), 10)
x := int(x1)
y := int(y1)
width := int(w1)
height := int(h1)
fmt.Println(x, x1, y, y1)
fileName := "avatar_" + strconv.FormatInt(time.Now().UnixNano(), 16)
filePath := filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+ext)
path := filepath.Dir(filePath)
os.MkdirAll(path, os.ModePerm)
err = c.SaveToFile("image-file", filePath)
if err != nil {
logs.Error("", err)
c.JsonResult(500, "图片保存失败")
}
//剪切图片
subImg, err := graphics.ImageCopyFromFile(filePath, x, y, width, height)
if err != nil {
logs.Error("ImageCopyFromFile => ", err)
c.JsonResult(6001, "头像剪切失败")
}
os.Remove(filePath)
filePath = filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+"_small"+ext)
err = graphics.ImageResizeSaveFile(subImg, 120, 120, filePath)
//err = graphics.SaveImage(filePath,subImg)
if err != nil {
logs.Error("保存文件失败 => ", err.Error())
c.JsonResult(500, "保存文件失败")
}
url := "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
if strings.HasPrefix(url, "//") {
url = string(url[1:])
}
if member, err := models.NewMember().Find(c.Member.MemberId); err == nil {
avater := member.Avatar
member.Avatar = url
err := member.Update()
if err == nil {
if strings.HasPrefix(avater, "/uploads/") {
os.Remove(filepath.Join(conf.WorkingDirectory, avater))
}
c.SetMember(*member)
} else {
c.JsonResult(60001, "保存头像失败")
}
}
c.JsonResult(0, "ok", url)
}