-
Notifications
You must be signed in to change notification settings - Fork 28
/
user.go
executable file
·77 lines (68 loc) · 1.73 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
package handler
import (
"gops/gops-server/model"
"github.com/labstack/echo"
"net/http"
"gops/gops-common"
)
func GetUserList(c echo.Context) error {
page, size := common.GetPageParams(c)
users, err := model.GetUserList(page, size)
if err != nil {
return err
}
return c.JSON(http.StatusOK, &common.ResponseType{0, "", users})
}
func AddUser(c echo.Context) error {
user := new(model.User)
if err := c.Bind(user); err != nil {
return err
}
ctime := common.StampInt64()
cpwd, en := model.EncryptPassword(
user.Username,
user.Password,
common.StampToString(ctime),
)
if en != nil {
return en
}
user.Password = string(cpwd)
user.Ctime = ctime
result, err := user.Create()
if err != nil {
return c.JSON(http.StatusOK, &common.ResponseType{2, "用户名已存在", nil})
}
if result {
return c.JSON(http.StatusOK, &common.ResponseType{1, "新建用户成功", nil})
}
return c.JSON(http.StatusOK, &common.ResponseType{2, "新建用户失败", nil})
}
func ChangeUser(c echo.Context) error {
user := new(model.User)
if err := c.Bind(user); err != nil {
return err
}
user.UserId = c.Param("userid")
user.Mtime = common.StampInt64()
result, err := user.Update()
if err != nil {
return err
}
if result {
return c.JSON(http.StatusOK, &common.ResponseType{1, "修改用户成功", nil})
}
return c.JSON(http.StatusOK, &common.ResponseType{2, "修改用户失败", nil})
}
func RemoveUser(c echo.Context) error {
user := new(model.User)
user.UserId = c.Param("userid")
result, err := user.Delete()
if err != nil {
return err
}
if result {
return c.JSON(http.StatusOK, &common.ResponseType{1, "删除用户成功", nil})
}
return c.JSON(http.StatusOK, &common.ResponseType{2, "删除用户失败", nil})
}