auth.go
5.8 KB
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
package auth
import (
"encoding/json"
"fmt"
"github.com/GeeTeam/gt3-golang-sdk/geetest"
"github.com/tiptok/gocomm/common"
"github.com/tiptok/gocomm/pkg/cache"
"github.com/tiptok/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/cachex"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/auth"
"time"
)
type AuthService struct {
}
func (svr *AuthService) Login(header *protocol.RequestHeader, request *protocolx.LoginRequest) (rsp *protocolx.LoginResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
UserRepository, _ = factory.CreateUserRepository(transactionContext)
)
rsp = &protocolx.LoginResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var user *domain.Users
if user, err = UserRepository.FindOne(map[string]interface{}{"phone": request.UserName}); err != nil || user == nil {
err = protocol.NewCustomMessage(1, "用户不存在!")
return
}
if user.Passwd != request.Password {
err = protocol.NewCustomMessage(1, "密码有误!")
return
}
token, _ := common.GenerateToken(fmt.Sprintf("%v", user.Id), user.Passwd)
rsp.Access = map[string]interface{}{
"accessToken": "Bearer " + token,
"expiresIn": domain.TokenExpire,
}
cache.Delete(cachex.UserRoleAccessCacheKey(user.Id))
err = transactionContext.CommitTransaction()
return
}
func (svr *AuthService) Logout(header *protocol.RequestHeader, request *protocolx.LogoutRequest) (rsp *protocolx.LogoutResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.LogoutResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
err = transactionContext.CommitTransaction()
return
}
func (svr *AuthService) Profile(header *protocol.RequestHeader, request *protocolx.ProfileRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
UserRepository, _ = factory.CreateUserRepository(transactionContext)
RoleRepository, _ = factory.CreateRoleRepository(transactionContext)
RoleAccessDao, _ = dao.NewRoleAccessDao(transactionContext)
AccessRepository, _ = factory.CreateAccessRepository(transactionContext)
)
rsp = &protocolx.ProfileResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
var user *domain.Users
if user, err = UserRepository.FindOne(map[string]interface{}{"id": request.UserId}); err != nil {
err = protocol.NewCustomMessage(1, "用户不存在")
return
}
_, roles, _ := RoleRepository.Find(map[string]interface{}{"inRoleIds": user.Roles})
rspMap := map[string]interface{}{
"menus": struct{}{},
}
rspMap["user"] = map[string]interface{}{
"name": user.Name,
"id": user.Id,
"phone": user.Phone,
"adminType": user.AdminType,
"roles": utils.LoadCustomField(roles, "Id", "RoleName"),
}
accessIds, _ := RoleAccessDao.GetRoleAccess(user.Roles...)
if len(accessIds) > 0 {
_, accesses, _ := AccessRepository.Find(map[string]interface{}{"inAccessIds": accessIds})
rspMap["menus"] = accesses
}
rsp = rspMap
err = transactionContext.CommitTransaction()
return
}
func (svr *AuthService) CaptchaInit(header *protocol.RequestHeader, request *protocolx.CaptchaInitRequest) (rsp interface{}, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.CaptchaInitResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
return
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
const (
captchaID = "33a2abf9c5df0d6bc3b89fb39280114b"
privateKey = "13320fd2b10199e9a2440a4fbb4d46f7"
)
newGeetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
_, responseBt := newGeetest.PreProcess("", request.UserIp)
var geetestRsp geetest.FailbackRegisterRespnse
json.Unmarshal(responseBt, &geetestRsp)
rspData := map[string]interface{}{
"success": geetestRsp.Success,
"gt": geetestRsp.GT,
"challenge": geetestRsp.Challenge,
"newCaptcha": geetestRsp.NewCaptcha,
}
rsp = rspData
err = transactionContext.CommitTransaction()
return
}
func (svr *AuthService) ChangePassword(header *protocol.RequestHeader, request *protocolx.ChangePasswordRequest) (rsp *protocolx.ChangePasswordResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
rsp = &protocolx.ChangePasswordResponse{}
if err = request.ValidateCommand(); err != nil {
err = protocol.NewCustomMessage(2, err.Error())
}
if err = transactionContext.StartTransaction(); err != nil {
log.Error(err)
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
err = transactionContext.CommitTransaction()
return
}
func NewAuthService(options map[string]interface{}) *AuthService {
svr := &AuthService{}
return svr
}