-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathuser.go
71 lines (64 loc) · 1.71 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
package request
import (
"log"
"net/http"
"com.lc.go.codepush/server/config"
"com.lc.go.codepush/server/model"
"com.lc.go.codepush/server/model/constants"
"com.lc.go.codepush/server/utils"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/google/uuid"
)
type User struct{}
type loginUser struct {
UserName *string `json:"userName" binding:"required"`
Password *string `json:"password" binding:"required"`
}
func (User) Login(ctx *gin.Context) {
loginUser := loginUser{}
if err := ctx.ShouldBindBodyWith(&loginUser, binding.JSON); err == nil {
user := model.GetOne[model.User]("user_name", &loginUser.UserName)
if user == nil || *user.Password != *loginUser.Password {
panic("UserName or Psssword error")
}
uuid, _ := uuid.NewUUID()
timeNow := utils.GetTimeNow()
expireTime := *timeNow + (config.GetConfig().TokenExpireTime * 24 * 60 * 60 * 1000)
token := uuid.String()
del := false
tokenInfo := model.Token{
Uid: user.Id,
Token: &token,
ExpireTime: &expireTime,
Del: &del,
}
err := model.Create[model.Token](&tokenInfo)
if err != nil {
panic("create token error")
}
ctx.JSON(http.StatusOK, gin.H{
"token": token,
})
} else {
log.Panic(err.Error())
}
}
type changePasswordReq struct {
Password *string `json:"password" binding:"required"`
}
func (User) ChangePassword(ctx *gin.Context) {
req := changePasswordReq{}
if err := ctx.ShouldBindBodyWith(&req, binding.JSON); err == nil {
uid := ctx.MustGet(constants.GIN_USER_ID).(int)
err := model.User{}.ChangePassword(uid, *req.Password)
if err != nil {
panic(err.Error())
}
ctx.JSON(http.StatusOK, gin.H{
"success": true,
})
} else {
log.Panic(err.Error())
}
}