-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
322 lines (281 loc) · 10.2 KB
/
profile.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
package controllers
import (
"github.com/astaxie/beego/validation"
"github.com/dombine/mm-wiki/app/models"
"strings"
)
type ProfileController struct {
BaseController
}
func (this *ProfileController) Info() {
user, err := models.UserModel.GetUserByUserId(this.UserId)
if err != nil {
this.ErrorLog("获取我的资料失败: " + err.Error())
this.ViewError("获取资料失败")
}
logDocuments, err := models.LogDocumentModel.GetLogDocumentsByUserIdAndLimit(this.UserId, 0, 10)
if err != nil {
this.ErrorLog("查找用户活动出错:" + err.Error())
this.ViewError("查找用户活动出错!", "/main/index")
}
docIds := []string{}
for _, logDocument := range logDocuments {
docIds = append(docIds, logDocument["document_id"])
}
documents, err := models.DocumentModel.GetAllDocumentsByDocumentIds(docIds)
if err != nil {
this.ErrorLog("查找用户活动出错: " + err.Error())
this.ViewError("查找用户活动出错", "/main/index")
}
for _, logDocument := range logDocuments {
for _, document := range documents {
if document["document_id"] == logDocument["document_id"] {
logDocument["document_id"] = document["document_id"]
logDocument["document_name"] = document["name"]
logDocument["document_type"] = document["type"]
logDocument["update_time"] = document["update_time"]
}
}
}
this.Data["logDocuments"] = logDocuments
this.Data["count"] = len(logDocuments)
this.Data["user"] = user
this.viewLayout("profile/info", "profile")
}
func (this *ProfileController) Edit() {
user, err := models.UserModel.GetUserByUserId(this.UserId)
if err != nil {
this.ErrorLog("获取我的资料失败: " + err.Error())
this.ViewError("获取资料失败")
}
this.Data["user"] = user
this.viewLayout("profile/edit", "profile")
}
func (this *ProfileController) Modify() {
if !this.IsPost() {
this.ViewError("请求方式有误!", "/system/profile/info")
}
givenName := strings.TrimSpace(this.GetString("given_name", ""))
email := strings.TrimSpace(this.GetString("email", ""))
mobile := strings.TrimSpace(this.GetString("mobile", ""))
phone := strings.TrimSpace(this.GetString("phone", ""))
department := strings.TrimSpace(this.GetString("department", ""))
position := strings.TrimSpace(this.GetString("position", ""))
location := strings.TrimSpace(this.GetString("location", ""))
im := strings.TrimSpace(this.GetString("im", ""))
v := validation.Validation{}
if givenName == "" {
this.jsonError("姓名不能为空!")
}
if email == "" {
this.jsonError("邮箱不能为空!")
}
if !v.Email(email, "email").Ok {
this.jsonError("邮箱格式不正确!")
}
if mobile == "" {
this.jsonError("手机号不能为空!")
}
//if !v.Mobile(mobile, "mobile").Ok {
// this.jsonError("手机号格式不正确!")
//}
//if phone != "" && !v.Phone(phone, "phone").Ok {
// this.jsonError("电话格式不正确!")
//}
_, err := models.UserModel.Update(this.UserId, map[string]interface{}{
"given_name": givenName,
"email": email,
"mobile": mobile,
"phone": phone,
"department": department,
"position": position,
"location": location,
"im": im,
})
if err != nil {
this.ErrorLog("修改我的资料失败:" + err.Error())
this.jsonError("修改我的资料失败")
}
this.InfoLog("修改我的资料成功")
this.jsonSuccess("我的资料修改成功", nil, "/system/profile/info")
}
func (this *ProfileController) FollowUser() {
// follow users
followUsers, err := models.FollowModel.GetFollowsByUserIdAndType(this.UserId, models.Follow_Type_User)
if err != nil {
this.ErrorLog("获取我的关注用户列表失败: " + err.Error())
this.ViewError("获取关注用户列表失败", "/system/profile/info")
}
userIds := []string{}
for _, followUser := range followUsers {
userIds = append(userIds, followUser["object_id"])
}
users, err := models.UserModel.GetUsersByUserIds(userIds)
if err != nil {
this.ErrorLog("获取我的关注用户列表失败: " + err.Error())
this.ViewError("获取关注用户列表失败", "/system/profile/info")
}
for _, user := range users {
user["follow_id"] = "0"
for _, followUser := range followUsers {
if followUser["object_id"] == user["user_id"] {
user["follow_id"] = followUser["follow_id"]
break
}
}
}
// fans users
followedUsers, err := models.FollowModel.GetFollowsByObjectIdAndType(this.UserId, models.Follow_Type_User)
if err != nil {
this.ErrorLog("获取关注用户列表失败: " + err.Error())
this.ViewError("获取关注用户列表失败", "/system/profile/info")
}
followedUserIds := []string{}
for _, followedUser := range followedUsers {
followedUserIds = append(followedUserIds, followedUser["user_id"])
}
fansUsers, err := models.UserModel.GetUsersByUserIds(followedUserIds)
if err != nil {
this.ErrorLog("获取关注用户列表失败: " + err.Error())
this.ViewError("获取关注用户列表失败", "/system/profile/info")
}
this.Data["users"] = users
this.Data["fansUsers"] = fansUsers
this.Data["followCount"] = len(users)
this.Data["fansCount"] = len(fansUsers)
this.Data["user"] = this.User
this.viewLayout("profile/follow_user", "profile")
}
func (this *ProfileController) FollowDoc() {
page, _ := this.GetInt("page", 1)
number, _ := this.GetRangeInt("number", 10, 10, 100)
limit := (page - 1) * number
// follow docs limit
followDocuments, err := models.FollowModel.GetFollowsByUserIdTypeAndLimit(this.UserId, models.Follow_Type_Doc, limit, number)
if err != nil {
this.ErrorLog("获取关注文档列表失败: " + err.Error())
this.ViewError("获取关注文档列表失败", "/system/profile/info")
}
count, err := models.FollowModel.CountFollowsByUserIdAndType(this.UserId, models.Follow_Type_Doc)
if err != nil {
this.ErrorLog("获取关注文档列表失败: " + err.Error())
this.ViewError("获取关注文档列表失败", "/system/profile/info")
}
docIds := []string{}
for _, followDocument := range followDocuments {
docIds = append(docIds, followDocument["object_id"])
}
documents, err := models.DocumentModel.GetDocumentsByDocumentIds(docIds)
if err != nil {
this.ErrorLog("获取我的关注用户列表失败: " + err.Error())
this.ViewError("获取关注用户列表失败", "/system/profile/info")
}
for _, followDocument := range followDocuments {
for _, document := range documents {
if document["document_id"] == followDocument["object_id"] {
followDocument["document_id"] = document["document_id"]
followDocument["document_name"] = document["name"]
followDocument["update_time"] = document["update_time"]
}
}
}
autoFollowDoc := "0"
autoFollowConfig, _ := models.ConfigModel.GetConfigByKey(models.Config_Key_AutoFollowDoc)
if len(autoFollowConfig) > 0 && autoFollowConfig["value"] == "1" {
autoFollowDoc = "1"
}
this.Data["followDocuments"] = followDocuments
this.Data["count"] = len(documents)
this.Data["user"] = this.User
this.Data["autoFollowDoc"] = autoFollowDoc
this.SetPaginator(number, count)
this.viewLayout("profile/follow_doc", "profile")
}
func (this *ProfileController) Activity() {
page, _ := this.GetInt("page", 1)
number, _ := this.GetRangeInt("number", 15, 10, 100)
limit := (page - 1) * number
keyword := strings.TrimSpace(this.GetString("keyword", ""))
var logDocuments = []map[string]string{}
var err error
var count int64
if keyword != "" {
logDocuments, err = models.LogDocumentModel.GetLogDocumentsByUserIdKeywordAndLimit(this.UserId, keyword, limit, number)
count, err = models.LogDocumentModel.CountLogDocumentsByUserIdAndKeyword(this.UserId, keyword)
} else {
logDocuments, err = models.LogDocumentModel.GetLogDocumentsByUserIdAndLimit(this.UserId, limit, number)
count, err = models.LogDocumentModel.CountLogDocumentsByUserId(this.UserId)
}
if err != nil {
this.ErrorLog("我的活动查找失败:" + err.Error())
this.ViewError("我的活动查找失败!", "/system/main/index")
}
userIds := []string{}
docIds := []string{}
for _, logDocument := range logDocuments {
userIds = append(userIds, logDocument["user_id"])
docIds = append(docIds, logDocument["document_id"])
}
users, err := models.UserModel.GetUsersByUserIds(userIds)
if err != nil {
this.ErrorLog("我的活动查找失败:" + err.Error())
this.ViewError("我的活动查找失败!", "/system/main/index")
}
docs, err := models.DocumentModel.GetAllDocumentsByDocumentIds(docIds)
if err != nil {
this.ErrorLog("我的活动查找失败:" + err.Error())
this.ViewError("我的活动查找失败!", "/system/main/index")
}
for _, logDocument := range logDocuments {
logDocument["username"] = ""
for _, user := range users {
if logDocument["user_id"] == user["user_id"] {
logDocument["username"] = user["username"]
logDocument["given_name"] = user["given_name"]
break
}
}
for _, doc := range docs {
if logDocument["document_id"] == doc["document_id"] {
logDocument["document_name"] = doc["name"]
logDocument["document_type"] = doc["type"]
break
}
}
}
this.Data["logDocuments"] = logDocuments
this.Data["keyword"] = keyword
this.SetPaginator(number, count)
this.viewLayout("profile/activity", "profile")
}
func (this *ProfileController) Password() {
this.viewLayout("profile/password", "profile")
}
func (this *ProfileController) SavePass() {
pwd := strings.TrimSpace(this.GetString("pwd", ""))
pwdNew := strings.TrimSpace(this.GetString("pwd_new", ""))
pwdConfirm := strings.TrimSpace(this.GetString("pwd_confirm", ""))
if (pwd == "") || (pwdNew == "") || (pwdConfirm == "") {
this.jsonError("密码不能为空!")
}
p := models.UserModel.EncodePassword(pwd)
if p != this.User["password"] {
this.jsonError("当前密码错误")
}
if pwdConfirm != pwdNew {
this.jsonError("确认密码和新密码不一致")
}
_, err := models.UserModel.Update(this.UserId, map[string]interface{}{
"password": models.UserModel.EncodePassword(pwdNew),
})
// 阻止日志记录 password
this.Ctx.Request.PostForm.Del("pwd")
this.Ctx.Request.PostForm.Del("pwd_new")
this.Ctx.Request.PostForm.Del("pwd_confirm")
if err != nil {
this.ErrorLog("修改密码失败:" + err.Error())
this.jsonError("修改密码失败")
}
this.InfoLog("修改密码成功")
this.jsonSuccess("修改密码成功, 下次登录时生效", nil, "/system/profile/password")
}