forked from yuanjack/champloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
98 lines (89 loc) · 2.01 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
package main
import (
"net/http"
"strconv"
"time"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
func GetUsers(username string, r render.Render) {
var users []User
db.Order("id desc").Find(&users)
data := map[string]interface{}{"username": username, "users": users}
r.HTML(200, "user", data)
}
func EditUsers(req *http.Request, user User, r render.Render) {
req.ParseForm()
user.IsAdmin = req.PostForm.Get("isadmin") == "on"
if user.Name == "" || user.Password == "" {
sendFailMsg(r, "保存失败,用户名或密码不能为空.", "")
return
}
user.CreatedAt = time.Now()
err := db.Save(&user).Error
if user.Id > 0 {
sendSuccessMsg(r, "")
} else {
sendFailMsg(r, "保存失败."+err.Error(), "")
}
}
func DeleteUser(params martini.Params, r render.Render) {
id, err := strconv.Atoi(params["id"])
if err != nil {
r.JSON(200, ActionMessage{
Success: false,
Message: "id参数错误",
})
return
}
err = db.Delete(&User{Id: id}).Error
if err != nil {
r.JSON(200, ActionMessage{
Success: false,
Message: "删除出错." + err.Error(),
})
return
}
r.JSON(200, ActionMessage{
Success: true,
Message: "成功",
})
}
func ToggleSetAdmin(params martini.Params, r render.Render) {
id, err := strconv.Atoi(params["id"])
if err != nil {
r.JSON(200, ActionMessage{
Success: false,
Message: "id参数错误",
})
return
}
action := params["action"]
var user User
if action == "enable" {
db.First(&user, id)
user.IsAdmin = true
err = db.Debug().Save(user).Error
} else {
db.First(&user, id)
user.IsAdmin = false
err = db.Save(user).Error
}
if err != nil {
r.JSON(200, ActionMessage{
Success: false,
Message: "保存出错." + err.Error(),
})
return
}
r.JSON(200, ActionMessage{
Success: true,
Message: "成功",
})
}
func UserSetting(username string, r render.Render) {
var user User
db.First(&user, User{Name: username})
data := map[string]interface{}{"username": username, "user": user}
r.HTML(200, "setting", data)
}