forked from zzdboy/GoCMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
379 lines (313 loc) · 9.22 KB
/
admin.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
// +----------------------------------------------------------------------
// | GoCMS 0.1
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.6574.com.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zzdboy <zzdboy1616@163.com>
// +----------------------------------------------------------------------
package controllers
// 管理员设置
import "strconv"
import "github.com/revel/revel"
import "admin/app/models"
type Admin struct {
*revel.Controller
}
//首页
func (c Admin) Index(admin *models.Admin) revel.Result {
title := "管理员管理--GoCMS管理系统"
var page string = c.Params.Get("page")
where := make(map[string]string)
if len(page) > 0 {
Page, err := strconv.ParseInt(page, 10, 64)
if err != nil {
revel.WARN.Println(err)
}
admin_list, pages := admin.GetByAll(0, where, Page, 10)
c.Render(title, admin_list, pages)
} else {
admin_list, pages := admin.GetByAll(0, where, 1, 10)
c.Render(title, admin_list, pages)
}
return c.RenderTemplate("Setting/Admin/Index.html")
}
//添加管理员
func (c Admin) Add(admin *models.Admin) revel.Result {
if c.Request.Method == "GET" {
title := "添加管理员--GoCMS管理系统"
role := new(models.Role)
role_list := role.GetRoleList()
c.Render(title, role_list)
return c.RenderTemplate("Setting/Admin/Add.html")
} else {
var username string = c.Params.Get("username")
if len(username) > 0 {
admin.Username = username
} else {
c.Flash.Error("请输入用户名!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
if admin.HasName() {
c.Flash.Error("用户名“" + username + "”已存在!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
var password string = c.Params.Get("password")
if len(password) > 0 {
admin.Password = password
} else {
c.Flash.Error("请输入密码!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
var pwdconfirm string = c.Params.Get("pwdconfirm")
if len(pwdconfirm) > 0 {
if password != pwdconfirm {
c.Flash.Error("两次输入密码不一致!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
} else {
c.Flash.Error("请输入确认密码!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
var email string = c.Params.Get("email")
if len(email) > 0 {
admin.Email = email
} else {
c.Flash.Error("请输入E-mail!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
if admin.HasEmail() {
c.Flash.Error("E-mail已存在!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
var realname string = c.Params.Get("realname")
if len(realname) > 0 {
admin.Realname = realname
} else {
c.Flash.Error("请输入真实姓名!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
var lang string = c.Params.Get("lang")
if len(lang) > 0 {
admin.Lang = lang
} else {
c.Flash.Error("请选择语言!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
var roleid string = c.Params.Get("roleid")
if len(roleid) > 0 {
Roleid, err := strconv.ParseInt(roleid, 10, 64)
if err != nil {
revel.WARN.Println(err)
}
admin.Roleid = Roleid
} else {
c.Flash.Error("请选择所属角色!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
var status string = c.Params.Get("status")
if len(status) > 0 {
Status, err := strconv.ParseInt(status, 10, 64)
if err != nil {
revel.WARN.Println(err)
}
admin.Status = Status
} else {
c.Flash.Error("请选择状态!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
if admin.Save() {
//******************************************
//管理员日志
if UserID, ok := c.Session["UserID"]; ok {
UserID, err := strconv.ParseInt(UserID, 10, 64)
if err != nil {
revel.WARN.Println(err)
}
admin_info := admin.GetById(UserID)
logs := new(models.Logs)
desc := "添加管理员:" + username + "|^|管理员管理"
logs.Save(admin_info, c.Controller, desc)
}
//*****************************************
c.Flash.Success("添加管理员成功!")
c.Flash.Out["url"] = "/Admin/"
return c.Redirect("/Message/")
} else {
c.Flash.Error("添加管理员失败!")
c.Flash.Out["url"] = "/Admin/Add/"
return c.Redirect("/Message/")
}
}
}
//编辑管理员
func (c Admin) Edit(admin *models.Admin) revel.Result {
if c.Request.Method == "GET" {
title := "编辑管理员--GoCMS管理系统"
role := new(models.Role)
role_list := role.GetRoleList()
var id string = c.Params.Get("id")
if len(id) > 0 {
Id, err := strconv.ParseInt(id, 10, 64)
if err != nil {
revel.WARN.Println(err)
}
admin_info := admin.GetById(Id)
c.Render(title, admin_info, role_list)
} else {
c.Render(title, role_list)
}
return c.RenderTemplate("Setting/Admin/Edit.html")
} else {
var id string = c.Params.Get("id")
if len(id) > 0 {
Id, err := strconv.ParseInt(id, 10, 64)
if err != nil {
revel.WARN.Println(err)
}
var username string = c.Params.Get("username")
if len(username) > 0 {
admin.Username = username
} else {
c.Flash.Error("请输入用户名!")
c.Flash.Out["url"] = "/Admin/Edit/" + id + "/"
return c.Redirect("/Message/")
}
var password string = c.Params.Get("password")
if len(password) > 0 {
admin.Password = password
}
var pwdconfirm string = c.Params.Get("pwdconfirm")
if len(pwdconfirm) > 0 {
if password != pwdconfirm {
c.Flash.Error("两次输入密码不一致!")
c.Flash.Out["url"] = "/Admin/Edit/" + id + "/"
return c.Redirect("/Message/")
}
}
var email string = c.Params.Get("email")
if len(email) > 0 {
admin.Email = email
} else {
c.Flash.Error("请输入E-mail!")
c.Flash.Out["url"] = "/Admin/Edit/" + id + "/"
return c.Redirect("/Message/")
}
var realname string = c.Params.Get("realname")
if len(realname) > 0 {
admin.Realname = realname
} else {
c.Flash.Error("请输入真实姓名!")
c.Flash.Out["url"] = "/Admin/Edit/" + id + "/"
return c.Redirect("/Message/")
}
var lang string = c.Params.Get("lang")
if len(lang) > 0 {
admin.Lang = lang
} else {
c.Flash.Error("请选择语言!")
c.Flash.Out["url"] = "/Admin/Edit/" + id + "/"
return c.Redirect("/Message/")
}
var roleid string = c.Params.Get("roleid")
if len(roleid) > 0 {
Roleid, err := strconv.ParseInt(roleid, 10, 64)
if err != nil {
revel.WARN.Println(err)
}
admin.Roleid = Roleid
} else {
c.Flash.Error("请选择所属角色!")
c.Flash.Out["url"] = "/Admin/Edit/" + id + "/"
return c.Redirect("/Message/")
}
var status string = c.Params.Get("status")
if len(status) > 0 {
Status, err := strconv.ParseInt(status, 10, 64)
if err != nil {
revel.WARN.Println(err)
}
admin.Status = Status
} else {
c.Flash.Error("请选择是否启用!")
c.Flash.Out["url"] = "/Admin/Edit/" + id + "/"
return c.Redirect("/Message/")
}
if admin.Edit(Id) {
//******************************************
//管理员日志
if UserID, ok := c.Session["UserID"]; ok {
UserID, err := strconv.ParseInt(UserID, 10, 64)
if err != nil {
revel.WARN.Println(err)
}
admin_info := admin.GetById(UserID)
logs := new(models.Logs)
desc := "编辑管理员:" + username + "|^|管理员管理"
logs.Save(admin_info, c.Controller, desc)
}
//*****************************************
c.Flash.Success("编辑管理员成功!")
c.Flash.Out["url"] = "/Admin/"
return c.Redirect("/Message/")
} else {
c.Flash.Error("编辑管理员失败!")
c.Flash.Out["url"] = "/Admin/Edit/" + id + "/"
return c.Redirect("/Message/")
}
} else {
c.Flash.Error("编辑管理员失败!")
c.Flash.Out["url"] = "/Admin/Edit/" + id + "/"
return c.Redirect("/Message/")
}
}
}
//删除管理员
func (c Admin) Delete(admin *models.Admin) revel.Result {
var id string = c.Params.Get("id")
data := make(map[string]string)
if len(id) > 0 {
Id, err := strconv.ParseInt(id, 10, 64)
if err != nil {
revel.WARN.Println(err)
}
if admin.DelByID(Id) {
//******************************************
//管理员日志
if UserID, ok := c.Session["UserID"]; ok {
UserID, err := strconv.ParseInt(UserID, 10, 64)
if err != nil {
revel.WARN.Println(err)
}
admin_info := admin.GetById(UserID)
logs := new(models.Logs)
desc := "删除管理员|^|ID:" + id
logs.Save(admin_info, c.Controller, desc)
}
//*****************************************
data["status"] = "1"
data["message"] = "删除成功!"
return c.RenderJson(data)
} else {
data["status"] = "0"
data["message"] = "删除失败!"
return c.RenderJson(data)
}
} else {
data["status"] = "0"
data["message"] = "删除失败!"
return c.RenderJson(data)
}
}