-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
171 lines (161 loc) · 5.3 KB
/
service.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
package login
import (
"errors"
"github.com/fighthorse/sampleBookReader/interface/api/service"
"time"
"github.com/fighthorse/sampleBookReader/domain/component/gotoken"
"github.com/fighthorse/sampleBookReader/domain/component/log"
"github.com/fighthorse/sampleBookReader/domain/component/self_errors"
"github.com/fighthorse/sampleBookReader/interface/api/protos"
"github.com/gin-gonic/gin"
)
func loginOutEndpoint(c *gin.Context) {
var token protos.TokenCheck
if err := c.ShouldBind(&token); err != nil {
c.JSON(200, gin.H{"code": -126, "message": "token参数无效", "data": map[string]interface{}{}})
return
}
// token 解析 jwt name
uid, err := gotoken.ParseToken(token.Token, gotoken.LoginSecret)
if err != nil {
c.JSON(200, gin.H{"code": -126, "message": "token无效:" + err.Error(), "data": map[string]interface{}{}})
return
}
// uid 缓存数据
data, ok := service.LoginService.LocalCache.Get(uid)
// 不存在
if !ok {
c.JSON(200, gin.H{"code": -126, "message": "token无效", "data": map[string]interface{}{}})
return
}
service.LoginService.LocalCache.Delete(uid)
log.Info(c.Request.Context(), "loginOutEndpoint", log.Fields{"data": data})
c.JSON(200, gin.H{"code": 0, "message": "ok", "data": map[string]interface{}{}})
}
func submitEndpoint(c *gin.Context) {
var person protos.PersonLogin
if err := c.ShouldBind(&person); err != nil {
c.JSON(200, self_errors.JsonErrExport(self_errors.JsonErr, err, ""))
return
}
if person.Name == "" || person.Pwd == "" {
err := errors.New("用户名称/密码不能为空")
c.JSON(200, self_errors.JsonErrExport(self_errors.ParamsErr, err, ""))
return
}
//verify pwd
id, err := service.LoginService.VerifyUser(c, person.Name, person.Pwd)
if err != nil {
c.JSON(200, gin.H{"code": -1, "message": "" + err.Error(), "data": map[string]interface{}{}})
return
}
if id == 0 {
err := errors.New("服务异常")
c.JSON(200, self_errors.JsonErrExport(self_errors.ParamsErr, err, ""))
}
// login
token, err := gotoken.CreateToken(person.Name, gotoken.LoginSecret)
if err != nil {
c.JSON(200, gin.H{"code": -126, "message": "生产token无效:" + err.Error(), "data": map[string]interface{}{}})
return
}
day := time.Now().AddDate(0, 0, 1).Format("2006-01-02 15:04:05")
ip := c.ClientIP()
data := &protos.Person{
Id: id,
Name: person.Name,
Ip: ip,
Token: token,
Expires: day,
}
service.LoginService.CacheUser(c, data)
c.JSON(200, gin.H{"code": 0, "message": "ok", "data": map[string]interface{}{
"token": token, "exp": day,
}})
}
func registerEndpoint(c *gin.Context) {
var person protos.PersonLogin
if err := c.ShouldBind(&person); err != nil {
c.JSON(200, self_errors.JsonErrExport(self_errors.JsonErr, err, ""))
return
}
if person.Name == "" || person.Pwd == "" {
err := errors.New("用户名称/密码不能为空")
c.JSON(200, self_errors.JsonErrExport(self_errors.ParamsErr, err, ""))
return
}
if person.Pwd != person.Pwd2 {
err := errors.New("密码不一致")
c.JSON(200, self_errors.JsonErrExport(self_errors.ParamsErr, err, ""))
return
}
if len(person.Name) < 4 {
err := errors.New("用户名长度不足4位")
c.JSON(200, self_errors.JsonErrExport(self_errors.ParamsErr, err, ""))
return
}
if len(person.Pwd) < 6 {
err := errors.New("密码长度不足6位")
c.JSON(200, self_errors.JsonErrExport(self_errors.ParamsErr, err, ""))
return
}
//lock
if !service.LoginService.LockByName(c, person.Name) {
err := errors.New("操作速度过快,请稍后重试")
c.JSON(200, self_errors.JsonErrExport(self_errors.ParamsErr, err, ""))
return
}
//verify pwd
sok, err := service.LoginService.VerifyUserName(c, person.Name)
if err != nil {
c.JSON(200, gin.H{"code": -1, "message": "" + err.Error(), "data": map[string]interface{}{}})
return
}
if sok {
c.JSON(200, gin.H{"code": -1, "message": "用户名已存在", "data": map[string]interface{}{}})
return
}
//创建数据
err = service.LoginService.Register(c, person.Name, person.Pwd)
if err != nil {
c.JSON(200, gin.H{"code": -1, "message": "" + err.Error(), "data": map[string]interface{}{}})
return
}
id, err1 := service.LoginService.VerifyUser(c, person.Name, person.Pwd)
if err1 != nil {
c.JSON(200, gin.H{"code": -1, "message": "服务异常", "data": map[string]interface{}{}})
return
}
// login
token, err := gotoken.CreateToken(person.Name, gotoken.LoginSecret)
if err != nil {
c.JSON(200, gin.H{"code": -126, "message": "生产token无效:" + err.Error(), "data": map[string]interface{}{}})
return
}
day := time.Now().AddDate(0, 0, 1).Format("2006-01-02 15:04:05")
ip := c.ClientIP()
data := protos.Person{
Id: id,
Name: person.Name,
Ip: ip,
Token: token,
Expires: day,
}
service.LoginService.CacheUser(c, &data)
c.JSON(200, gin.H{"code": 0, "message": "ok", "data": map[string]interface{}{
"token": token, "exp": day,
}})
}
func checkEndpoint(c *gin.Context) {
var token protos.TokenCheck
if err := c.ShouldBind(&token); err != nil {
c.JSON(200, gin.H{"code": -126, "message": "token参数无效", "data": map[string]interface{}{}})
return
}
data, err := service.LoginService.Check(c, token.Token)
if err != nil {
c.JSON(200, gin.H{"code": -126, "message": err.Error(), "data": map[string]interface{}{}})
return
}
c.JSON(200, gin.H{"code": 0, "message": "ok", "data": data})
}