1
|
package service
|
1
|
package service
|
2
|
|
2
|
|
3
|
import (
|
3
|
import (
|
|
|
4
|
+ "github.com/GeeTeam/gt3-golang-sdk/geetest"
|
4
|
"github.com/google/uuid"
|
5
|
"github.com/google/uuid"
|
|
|
6
|
+ "github.com/linmadan/egglib-go/utils/json"
|
5
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/query"
|
7
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/query"
|
6
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/dto"
|
8
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/dto"
|
7
|
"time"
|
9
|
"time"
|
|
@@ -17,17 +19,26 @@ import ( |
|
@@ -17,17 +19,26 @@ import ( |
17
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/sms_serve"
|
19
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/sms_serve"
|
18
|
)
|
20
|
)
|
19
|
|
21
|
|
|
|
22
|
+const (
|
|
|
23
|
+ captchaID = "33a2abf9c5df0d6bc3b89fb39280114b"
|
|
|
24
|
+ privateKey = "13320fd2b10199e9a2440a4fbb4d46f7"
|
|
|
25
|
+)
|
|
|
26
|
+
|
20
|
// 组织管理
|
27
|
// 组织管理
|
21
|
type AuthService struct {
|
28
|
type AuthService struct {
|
22
|
}
|
29
|
}
|
23
|
|
30
|
|
24
|
//AuthLogin 用户登录
|
31
|
//AuthLogin 用户登录
|
25
|
-func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{}, error) {
|
32
|
+func (svr AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{}, error) {
|
26
|
var (
|
33
|
var (
|
27
|
authCode string
|
34
|
authCode string
|
28
|
result interface{}
|
35
|
result interface{}
|
29
|
err error
|
36
|
err error
|
|
|
37
|
+ loginPlatform string = domain.LoginPlatformApp
|
30
|
)
|
38
|
)
|
|
|
39
|
+ if loginCommand.DeviceType == domain.DeviceTypeWeb {
|
|
|
40
|
+ loginPlatform = domain.LoginPlatformWeb
|
|
|
41
|
+ }
|
31
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
|
42
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
|
32
|
_, err = creationUserGateway.AuthRefreshIM(allied_creation_user.ReqAuthRefreshIM{
|
43
|
_, err = creationUserGateway.AuthRefreshIM(allied_creation_user.ReqAuthRefreshIM{
|
33
|
Phone: loginCommand.Phone,
|
44
|
Phone: loginCommand.Phone,
|
|
@@ -36,16 +47,20 @@ func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{ |
|
@@ -36,16 +47,20 @@ func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{ |
36
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
47
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
37
|
}
|
48
|
}
|
38
|
switch loginCommand.GrantType {
|
49
|
switch loginCommand.GrantType {
|
39
|
- case "signInPassword":
|
|
|
40
|
- authCode, err = srv.SignInPassword(loginCommand.Phone, loginCommand.Password)
|
|
|
41
|
- case "signInCaptcha":
|
|
|
42
|
- authCode, err = srv.SignInCaptcha(loginCommand.Phone, loginCommand.Captcha)
|
50
|
+ case domain.LoginPwd:
|
|
|
51
|
+ authCode, err = svr.SignInPassword(loginCommand.Phone, loginCommand.Password, loginPlatform)
|
|
|
52
|
+ case domain.LoginSmsCode:
|
|
|
53
|
+ authCode, err = svr.SignInCaptcha(loginCommand.Phone, loginCommand.Captcha, loginPlatform)
|
43
|
default:
|
54
|
default:
|
44
|
err = application.ThrowError(application.TRANSACTION_ERROR, "登录方式无法解析")
|
55
|
err = application.ThrowError(application.TRANSACTION_ERROR, "登录方式无法解析")
|
45
|
}
|
56
|
}
|
46
|
- result, err = srv.GetAuthAccessToken(&command.AccessTokenCommand{
|
57
|
+ if err != nil {
|
|
|
58
|
+ return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
59
|
+ }
|
|
|
60
|
+ result, err = svr.GetAuthAccessToken(&command.AccessTokenCommand{
|
47
|
AuthCode: authCode,
|
61
|
AuthCode: authCode,
|
48
|
SessionMode: loginCommand.SessionMode,
|
62
|
SessionMode: loginCommand.SessionMode,
|
|
|
63
|
+ LoginPlatform: loginPlatform,
|
49
|
})
|
64
|
})
|
50
|
return map[string]interface{}{
|
65
|
return map[string]interface{}{
|
51
|
"access": result,
|
66
|
"access": result,
|
|
@@ -53,7 +68,7 @@ func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{ |
|
@@ -53,7 +68,7 @@ func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{ |
53
|
}
|
68
|
}
|
54
|
|
69
|
|
55
|
//AuthLogin 用户登录
|
70
|
//AuthLogin 用户登录
|
56
|
-func (srv AuthService) AuthLoginPwd(loginCommand *command.LoginPwdCommand) (interface{}, error) {
|
71
|
+func (svr AuthService) AuthLoginPwd(loginCommand *command.LoginPwdCommand) (interface{}, error) {
|
57
|
if err := loginCommand.ValidateCommand(); err != nil {
|
72
|
if err := loginCommand.ValidateCommand(); err != nil {
|
58
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
73
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
59
|
}
|
74
|
}
|
|
@@ -63,11 +78,18 @@ func (srv AuthService) AuthLoginPwd(loginCommand *command.LoginPwdCommand) (inte |
|
@@ -63,11 +78,18 @@ func (srv AuthService) AuthLoginPwd(loginCommand *command.LoginPwdCommand) (inte |
63
|
Password: loginCommand.Password,
|
78
|
Password: loginCommand.Password,
|
64
|
SessionMode: loginCommand.SessionMode,
|
79
|
SessionMode: loginCommand.SessionMode,
|
65
|
}
|
80
|
}
|
66
|
- return srv.AuthLogin(login)
|
81
|
+ if len(loginCommand.CaptchaChallenge) > 0 {
|
|
|
82
|
+ geetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
|
|
|
83
|
+ validateResult := geetest.SuccessValidate(loginCommand.CaptchaChallenge, loginCommand.CaptchaValidate, loginCommand.CaptchaSeccode, "", "")
|
|
|
84
|
+ if !validateResult {
|
|
|
85
|
+ log.Logger.Error("validate captcha fail")
|
|
|
86
|
+ }
|
|
|
87
|
+ }
|
|
|
88
|
+ return svr.AuthLogin(login)
|
67
|
}
|
89
|
}
|
68
|
|
90
|
|
69
|
//AuthLogin 用户登录
|
91
|
//AuthLogin 用户登录
|
70
|
-func (srv AuthService) AuthLoginSms(loginCommand *command.LoginSmsCommand) (interface{}, error) {
|
92
|
+func (svr AuthService) AuthLoginSms(loginCommand *command.LoginSmsCommand) (interface{}, error) {
|
71
|
if err := loginCommand.ValidateCommand(); err != nil {
|
93
|
if err := loginCommand.ValidateCommand(); err != nil {
|
72
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
94
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
73
|
}
|
95
|
}
|
|
@@ -77,11 +99,44 @@ func (srv AuthService) AuthLoginSms(loginCommand *command.LoginSmsCommand) (inte |
|
@@ -77,11 +99,44 @@ func (srv AuthService) AuthLoginSms(loginCommand *command.LoginSmsCommand) (inte |
77
|
Captcha: loginCommand.Code,
|
99
|
Captcha: loginCommand.Code,
|
78
|
SessionMode: loginCommand.SessionMode,
|
100
|
SessionMode: loginCommand.SessionMode,
|
79
|
}
|
101
|
}
|
80
|
- return srv.AuthLogin(login)
|
102
|
+ return svr.AuthLogin(login)
|
|
|
103
|
+}
|
|
|
104
|
+
|
|
|
105
|
+//AuthLoginQrcode 扫码登录
|
|
|
106
|
+func (svr AuthService) AuthLoginQrcode(queryParam *query.QrcodeLoginStatusQuery) (interface{}, error) {
|
|
|
107
|
+ qrmsg := domain.QrcodeMessage{}
|
|
|
108
|
+ err := qrmsg.ParseToken(queryParam.Key)
|
|
|
109
|
+ if err != nil {
|
|
|
110
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
111
|
+ }
|
|
|
112
|
+ qrCache := cache.LoginQrcodeCache{}
|
|
|
113
|
+ qrmsgCache, err := qrCache.Get(qrmsg.Id)
|
|
|
114
|
+ if err != nil {
|
|
|
115
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
116
|
+ }
|
|
|
117
|
+ loginToken := domain.LoginToken{
|
|
|
118
|
+ UserId: qrmsgCache.UserId,
|
|
|
119
|
+ UserBaseId: qrmsgCache.UserBaseId,
|
|
|
120
|
+ Account: qrmsgCache.Account,
|
|
|
121
|
+ Platform: domain.LoginPlatformWeb,
|
|
|
122
|
+ CompanyId: qrmsgCache.CompanyId,
|
|
|
123
|
+ OrgId: qrmsgCache.OrgId,
|
|
|
124
|
+ }
|
|
|
125
|
+ accessToken, err := loginToken.GenerateAccessToken()
|
|
|
126
|
+ if err != nil {
|
|
|
127
|
+ return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
128
|
+ }
|
|
|
129
|
+ _ = accessToken
|
|
|
130
|
+ result, err := svr.getToken(loginToken)
|
|
|
131
|
+ data := map[string]interface{}{
|
|
|
132
|
+ "isLogin": qrmsgCache.IsLogin,
|
|
|
133
|
+ "access": result["token"],
|
|
|
134
|
+ }
|
|
|
135
|
+ return data, nil
|
81
|
}
|
136
|
}
|
82
|
|
137
|
|
83
|
//SendSmsCaptcha 发送验证码短信
|
138
|
//SendSmsCaptcha 发送验证码短信
|
84
|
-func (srv AuthService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand) error {
|
139
|
+func (svr AuthService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand) error {
|
85
|
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
|
140
|
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
|
86
|
err := smsServeGateway.SendSms(smsCodeCommand.Phone)
|
141
|
err := smsServeGateway.SendSms(smsCodeCommand.Phone)
|
87
|
if err != nil {
|
142
|
if err != nil {
|
|
@@ -91,7 +146,7 @@ func (srv AuthService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand |
|
@@ -91,7 +146,7 @@ func (srv AuthService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand |
91
|
}
|
146
|
}
|
92
|
|
147
|
|
93
|
//SignInPassword 使用账号密码校验
|
148
|
//SignInPassword 使用账号密码校验
|
94
|
-func (srv AuthService) SignInPassword(account string, password string) (string, error) {
|
149
|
+func (svr AuthService) SignInPassword(account string, password string, loginPlatform string) (string, error) {
|
95
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
|
150
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
|
96
|
_, err := creationUserGateway.AuthCheckPassword(allied_creation_user.ReqAuthCheckPassword{
|
151
|
_, err := creationUserGateway.AuthCheckPassword(allied_creation_user.ReqAuthCheckPassword{
|
97
|
Password: password,
|
152
|
Password: password,
|
|
@@ -103,7 +158,7 @@ func (srv AuthService) SignInPassword(account string, password string) (string, |
|
@@ -103,7 +158,7 @@ func (srv AuthService) SignInPassword(account string, password string) (string, |
103
|
ltoken := domain.LoginToken{
|
158
|
ltoken := domain.LoginToken{
|
104
|
UserId: 0,
|
159
|
UserId: 0,
|
105
|
Account: account,
|
160
|
Account: account,
|
106
|
- Platform: domain.LoginPlatformApp,
|
161
|
+ Platform: loginPlatform,
|
107
|
CompanyId: 0,
|
162
|
CompanyId: 0,
|
108
|
}
|
163
|
}
|
109
|
authcode, err := ltoken.GenerateAuthCode()
|
164
|
authcode, err := ltoken.GenerateAuthCode()
|
|
@@ -117,7 +172,7 @@ func (srv AuthService) SignInPassword(account string, password string) (string, |
|
@@ -117,7 +172,7 @@ func (srv AuthService) SignInPassword(account string, password string) (string, |
117
|
}
|
172
|
}
|
118
|
|
173
|
|
119
|
//SignInCaptcha 使用手机验证码登录
|
174
|
//SignInCaptcha 使用手机验证码登录
|
120
|
-func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, error) {
|
175
|
+func (svr AuthService) SignInCaptcha(phone string, captcha string, loginPlatform string) (string, error) {
|
121
|
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
|
176
|
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
|
122
|
err := smsServeGateway.CheckSmsCode(phone, captcha)
|
177
|
err := smsServeGateway.CheckSmsCode(phone, captcha)
|
123
|
if err != nil {
|
178
|
if err != nil {
|
|
@@ -126,7 +181,7 @@ func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, erro |
|
@@ -126,7 +181,7 @@ func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, erro |
126
|
ltoken := domain.LoginToken{
|
181
|
ltoken := domain.LoginToken{
|
127
|
UserId: 0,
|
182
|
UserId: 0,
|
128
|
Account: phone,
|
183
|
Account: phone,
|
129
|
- Platform: domain.LoginPlatformApp,
|
184
|
+ Platform: loginPlatform,
|
130
|
CompanyId: 0,
|
185
|
CompanyId: 0,
|
131
|
}
|
186
|
}
|
132
|
authcode, err := ltoken.GenerateAuthCode()
|
187
|
authcode, err := ltoken.GenerateAuthCode()
|
|
@@ -137,7 +192,7 @@ func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, erro |
|
@@ -137,7 +192,7 @@ func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, erro |
137
|
}
|
192
|
}
|
138
|
|
193
|
|
139
|
//GetAuthAccessToken 获取令牌Token
|
194
|
//GetAuthAccessToken 获取令牌Token
|
140
|
-func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessTokenCommand) (interface{}, error) {
|
195
|
+func (svr AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessTokenCommand) (interface{}, error) {
|
141
|
if err := accessTokenCommand.ValidateCommand(); err != nil {
|
196
|
if err := accessTokenCommand.ValidateCommand(); err != nil {
|
142
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
197
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
143
|
}
|
198
|
}
|
|
@@ -146,138 +201,12 @@ func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessToke |
|
@@ -146,138 +201,12 @@ func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessToke |
146
|
if err != nil {
|
201
|
if err != nil {
|
147
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
202
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
148
|
}
|
203
|
}
|
149
|
- phone := ltoken.Account
|
|
|
150
|
- creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
|
|
|
151
|
- userSeachResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
|
|
|
152
|
- CompanyId: ltoken.CompanyId,
|
|
|
153
|
- Phone: phone,
|
|
|
154
|
- })
|
|
|
155
|
- if err != nil {
|
|
|
156
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
157
|
- }
|
|
|
158
|
- if len(userSeachResult.Users) == 0 {
|
|
|
159
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取用户信息失败")
|
|
|
160
|
- }
|
|
|
161
|
- transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
162
|
- if err != nil {
|
|
|
163
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
164
|
- }
|
|
|
165
|
- if err := transactionContext.StartTransaction(); err != nil {
|
|
|
166
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
167
|
- }
|
|
|
168
|
- defer func() {
|
|
|
169
|
- transactionContext.RollbackTransaction()
|
|
|
170
|
- }()
|
|
|
171
|
- var loginAccessRepository domain.LoginAccessRepository
|
|
|
172
|
- if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{
|
|
|
173
|
- "transactionContext": transactionContext,
|
|
|
174
|
- }); err != nil {
|
|
|
175
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
176
|
- }
|
|
|
177
|
- _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
|
|
|
178
|
- "account": phone,
|
|
|
179
|
- "platform": domain.LoginPlatformApp,
|
|
|
180
|
- })
|
|
|
181
|
- if err != nil {
|
|
|
182
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
183
|
- }
|
|
|
184
|
- var currentAccess *domain.LoginAccess
|
|
|
185
|
- if len(lAccess) > 0 {
|
|
|
186
|
- currentAccess = lAccess[0]
|
|
|
187
|
- currentAccess.UpdatedTime = time.Now()
|
|
|
188
|
- } else {
|
|
|
189
|
- currentAccess = &domain.LoginAccess{
|
|
|
190
|
- UserBaseId: int64(userSeachResult.Users[0].UserBaseId),
|
|
|
191
|
- UserId: int64(userSeachResult.Users[0].UserId),
|
|
|
192
|
- Account: userSeachResult.Users[0].UserInfo.Phone,
|
|
|
193
|
- Platform: domain.LoginPlatformApp,
|
|
|
194
|
- OrganizationId: int64(userSeachResult.Users[0].Org.OrgId),
|
|
|
195
|
- AccessToken: "",
|
|
|
196
|
- RefreshToken: "",
|
|
|
197
|
- AccessExpired: 0,
|
|
|
198
|
- RefreshExpired: 0,
|
|
|
199
|
- CreatedTime: time.Now(),
|
|
|
200
|
- UpdatedTime: time.Now(),
|
|
|
201
|
- }
|
|
|
202
|
- if userSeachResult.Users[0].Company != nil {
|
|
|
203
|
- currentAccess.CompanyId = int64(userSeachResult.Users[0].Company.CompanyId)
|
|
|
204
|
- }
|
|
|
205
|
- }
|
|
|
206
|
- //判定当前凭证的companyId,OrganizationId 是否在用户列表中
|
|
|
207
|
- var currentOrgIsOK bool
|
|
|
208
|
-loopUser1:
|
|
|
209
|
- for _, v := range userSeachResult.Users {
|
|
|
210
|
- if v.Company.CompanyId == int(currentAccess.CompanyId) {
|
|
|
211
|
- for _, vv := range v.UserOrg {
|
|
|
212
|
- if vv.OrgID == int(currentAccess.OrganizationId) {
|
|
|
213
|
- currentOrgIsOK = true
|
|
|
214
|
- currentAccess.UserId = int64(v.UserId)
|
|
|
215
|
- currentAccess.UserBaseId = int64(v.UserBaseId)
|
|
|
216
|
- break loopUser1
|
|
|
217
|
- }
|
|
|
218
|
- }
|
|
|
219
|
- }
|
|
|
220
|
- }
|
|
|
221
|
- //记录上一次的登录公司组织信息不可用 ,重置登录记录
|
|
|
222
|
- //使用找到的第一个可用的公司组织
|
|
|
223
|
- if !currentOrgIsOK {
|
|
|
224
|
- loopUser2:
|
|
|
225
|
- for _, v := range userSeachResult.Users {
|
|
|
226
|
- currentAccess.CompanyId = int64(v.Company.CompanyId)
|
|
|
227
|
- for _, vv := range v.UserOrg {
|
|
|
228
|
- currentAccess.UserId = int64(v.UserId)
|
|
|
229
|
- currentAccess.UserBaseId = int64(v.UserBaseId)
|
|
|
230
|
- currentAccess.OrganizationId = int64(vv.OrgID)
|
|
|
231
|
- currentOrgIsOK = true
|
|
|
232
|
- break loopUser2
|
|
|
233
|
- }
|
|
|
234
|
- }
|
|
|
235
|
- }
|
|
|
236
|
-
|
|
|
237
|
- loginToken := domain.LoginToken{
|
|
|
238
|
- UserId: currentAccess.UserId,
|
|
|
239
|
- Account: currentAccess.Account,
|
|
|
240
|
- UserBaseId: currentAccess.UserBaseId,
|
|
|
241
|
- CompanyId: currentAccess.CompanyId,
|
|
|
242
|
- OrgId: currentAccess.OrganizationId,
|
|
|
243
|
- Platform: currentAccess.Platform,
|
|
|
244
|
- }
|
|
|
245
|
-
|
|
|
246
|
- accessTokenStr, err := loginToken.GenerateAccessToken()
|
|
|
247
|
- if err != nil {
|
|
|
248
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
249
|
- }
|
|
|
250
|
- currentAccess.AccessToken = accessTokenStr
|
|
|
251
|
- currentAccess.AccessExpired = loginToken.ExpiresAt
|
|
|
252
|
- refreshTokenStr, err := loginToken.GenerateRefreshToken()
|
|
|
253
|
- if err != nil {
|
|
|
254
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
255
|
- }
|
|
|
256
|
- currentAccess.RefreshToken = refreshTokenStr
|
|
|
257
|
- currentAccess.AccessExpired = loginToken.ExpiresAt
|
|
|
258
|
- //先存数据库
|
|
|
259
|
- _, err = loginAccessRepository.Save(currentAccess)
|
|
|
260
|
- if err != nil {
|
|
|
261
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
262
|
- }
|
|
|
263
|
- if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
264
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
265
|
- }
|
|
|
266
|
- //后处理redis缓存
|
|
|
267
|
- tokenCache := cache.LoginTokenCache{}
|
|
|
268
|
- tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp)
|
|
|
269
|
- tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp)
|
|
|
270
|
- tokenCache.SaveAccessToken(currentAccess)
|
|
|
271
|
- tokenCache.SaveRefreshToken(currentAccess)
|
|
|
272
|
- nowTime := time.Now().Unix()
|
|
|
273
|
- return map[string]interface{}{
|
|
|
274
|
- "refreshToken": accessTokenStr,
|
|
|
275
|
- "accessToken": refreshTokenStr,
|
|
|
276
|
- "expiresIn": currentAccess.AccessExpired - nowTime,
|
|
|
277
|
- }, nil
|
204
|
+ ltoken.Platform = accessTokenCommand.LoginPlatform
|
|
|
205
|
+ result, err := svr.getToken(*ltoken)
|
|
|
206
|
+ return result["token"], err
|
278
|
}
|
207
|
}
|
279
|
|
208
|
|
280
|
-func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.RefreshTokenCommand) (interface{}, error) {
|
209
|
+func (svr AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.RefreshTokenCommand) (interface{}, error) {
|
281
|
if err := refreshTokenCommand.ValidateCommand(); err != nil {
|
210
|
if err := refreshTokenCommand.ValidateCommand(); err != nil {
|
282
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
211
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
283
|
}
|
212
|
}
|
|
@@ -286,113 +215,15 @@ func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.Refre |
|
@@ -286,113 +215,15 @@ func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.Refre |
286
|
if err != nil {
|
215
|
if err != nil {
|
287
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, "refreshToken 不可用,"+err.Error())
|
216
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, "refreshToken 不可用,"+err.Error())
|
288
|
}
|
217
|
}
|
289
|
- token, err := srv.getToken(domain.Operator{}, ltoken)
|
|
|
290
|
- // phone := ltoken.Account
|
|
|
291
|
- // creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
|
|
|
292
|
- // userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
|
|
|
293
|
- // Phone: phone,
|
|
|
294
|
- // })
|
|
|
295
|
- // if err != nil {
|
|
|
296
|
- // return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户信息获取失败,"+err.Error())
|
|
|
297
|
- // }
|
|
|
298
|
- // //判定当前凭证的companyId,OrganizationId 是否在用户列表中
|
|
|
299
|
- // var currentOrgIsOK bool
|
|
|
300
|
- //loopUser1:
|
|
|
301
|
- // for _, v := range userSearchResult.Users {
|
|
|
302
|
- // if v.Company.CompanyId == int(ltoken.CompanyId) {
|
|
|
303
|
- // for _, vv := range v.UserOrg {
|
|
|
304
|
- // if vv.OrgID == int(ltoken.OrgId) {
|
|
|
305
|
- // currentOrgIsOK = true
|
|
|
306
|
- // break loopUser1
|
|
|
307
|
- // }
|
|
|
308
|
- // }
|
|
|
309
|
- // }
|
|
|
310
|
- // }
|
|
|
311
|
- // if !currentOrgIsOK {
|
|
|
312
|
- // return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用")
|
|
|
313
|
- // }
|
|
|
314
|
- //
|
|
|
315
|
- // transactionContext, err := factory.CreateTransactionContext(nil)
|
|
|
316
|
- // if err != nil {
|
|
|
317
|
- // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
318
|
- // }
|
|
|
319
|
- // if err := transactionContext.StartTransaction(); err != nil {
|
|
|
320
|
- // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
321
|
- // }
|
|
|
322
|
- // defer func() {
|
|
|
323
|
- // transactionContext.RollbackTransaction()
|
|
|
324
|
- // }()
|
|
|
325
|
- // var loginAccessRepository domain.LoginAccessRepository
|
|
|
326
|
- // if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{
|
|
|
327
|
- // "transactionContext": transactionContext,
|
|
|
328
|
- // }); err != nil {
|
|
|
329
|
- // return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
330
|
- // }
|
|
|
331
|
- // _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
|
|
|
332
|
- // "account": phone,
|
|
|
333
|
- // "platform": domain.LoginPlatformApp,
|
|
|
334
|
- // })
|
|
|
335
|
- // if err != nil {
|
|
|
336
|
- // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
337
|
- // }
|
|
|
338
|
- // var currentAccess *domain.LoginAccess
|
|
|
339
|
- // if len(lAccess) > 0 {
|
|
|
340
|
- // currentAccess = lAccess[0]
|
|
|
341
|
- // currentAccess.UpdatedTime = time.Now()
|
|
|
342
|
- // } else {
|
|
|
343
|
- // currentAccess = &domain.LoginAccess{
|
|
|
344
|
- // UserBaseId: ltoken.UserBaseId,
|
|
|
345
|
- // UserId: ltoken.UserId,
|
|
|
346
|
- // Account: ltoken.Account,
|
|
|
347
|
- // Platform: domain.LoginPlatformApp,
|
|
|
348
|
- // CompanyId: ltoken.CompanyId,
|
|
|
349
|
- // OrganizationId: ltoken.OrgId,
|
|
|
350
|
- // AccessToken: "",
|
|
|
351
|
- // RefreshToken: "",
|
|
|
352
|
- // AccessExpired: 0,
|
|
|
353
|
- // RefreshExpired: 0,
|
|
|
354
|
- // CreatedTime: time.Now(),
|
|
|
355
|
- // UpdatedTime: time.Now(),
|
|
|
356
|
- // }
|
|
|
357
|
- // }
|
|
|
358
|
- // accessTokenStr, err := ltoken.GenerateAccessToken()
|
|
|
359
|
- // if err != nil {
|
|
|
360
|
- // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
361
|
- // }
|
|
|
362
|
- // currentAccess.AccessToken = accessTokenStr
|
|
|
363
|
- // currentAccess.AccessExpired = ltoken.ExpiresAt
|
|
|
364
|
- // refreshTokenStr, err := ltoken.GenerateRefreshToken()
|
|
|
365
|
- // if err != nil {
|
|
|
366
|
- // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
367
|
- // }
|
|
|
368
|
- // currentAccess.RefreshToken = refreshTokenStr
|
|
|
369
|
- // currentAccess.RefreshExpired = ltoken.ExpiresAt
|
|
|
370
|
- // //先存数据库
|
|
|
371
|
- // _, err = loginAccessRepository.Save(currentAccess)
|
|
|
372
|
- // if err != nil {
|
|
|
373
|
- // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
374
|
- // }
|
|
|
375
|
- // if err := transactionContext.CommitTransaction(); err != nil {
|
|
|
376
|
- // return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
377
|
- // }
|
|
|
378
|
- // //后处理redis缓存
|
|
|
379
|
- // tokenCache := cache.LoginTokenCache{}
|
|
|
380
|
- // tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp)
|
|
|
381
|
- // tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp)
|
|
|
382
|
- // tokenCache.SaveAccessToken(currentAccess)
|
|
|
383
|
- // tokenCache.SaveRefreshToken(currentAccess)
|
|
|
384
|
- // nowTime := time.Now().Unix()
|
|
|
385
|
- // return map[string]interface{}{
|
|
|
386
|
- // "refreshToken": accessTokenStr,
|
|
|
387
|
- // "accessToken": refreshTokenStr,
|
|
|
388
|
- // "expiresIn": currentAccess.AccessExpired - nowTime,
|
|
|
389
|
- // }, nil
|
|
|
390
|
- return token["token"], err
|
218
|
+ token, err := svr.getToken(ltoken)
|
|
|
219
|
+ return map[string]interface{}{
|
|
|
220
|
+ "access": token["token"],
|
|
|
221
|
+ }, err
|
391
|
}
|
222
|
}
|
392
|
|
223
|
|
393
|
//GetUserMenus 获取用户信息
|
224
|
//GetUserMenus 获取用户信息
|
394
|
-func (srv AuthService) GetUserInfo(userInfoCommand *command.UserInfoCommand) (interface{}, error) {
|
|
|
395
|
- user, err := srv.getUserInfo(userInfoCommand.Operator)
|
225
|
+func (svr AuthService) GetUserInfo(userInfoCommand *command.UserInfoCommand) (interface{}, error) {
|
|
|
226
|
+ user, err := svr.getUserInfo(userInfoCommand.Operator)
|
396
|
if err != nil {
|
227
|
if err != nil {
|
397
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
228
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
398
|
}
|
229
|
}
|
|
@@ -402,7 +233,7 @@ func (srv AuthService) GetUserInfo(userInfoCommand *command.UserInfoCommand) (in |
|
@@ -402,7 +233,7 @@ func (srv AuthService) GetUserInfo(userInfoCommand *command.UserInfoCommand) (in |
402
|
}
|
233
|
}
|
403
|
|
234
|
|
404
|
//GetUserMenus 获取用户菜单
|
235
|
//GetUserMenus 获取用户菜单
|
405
|
-func (srv AuthService) GetUserMenus(userMenusCommand *command.UserMenusCommand) (interface{}, error) {
|
236
|
+func (svr AuthService) GetUserMenus(userMenusCommand *command.UserMenusCommand) (interface{}, error) {
|
406
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
|
237
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
|
407
|
userMenusCommand.Operator)
|
238
|
userMenusCommand.Operator)
|
408
|
resultMenu, err := creationUserGateway.UserAccessMenus(allied_creation_user.ReqUserAccessMenus{
|
239
|
resultMenu, err := creationUserGateway.UserAccessMenus(allied_creation_user.ReqUserAccessMenus{
|
|
@@ -417,7 +248,7 @@ func (srv AuthService) GetUserMenus(userMenusCommand *command.UserMenusCommand) |
|
@@ -417,7 +248,7 @@ func (srv AuthService) GetUserMenus(userMenusCommand *command.UserMenusCommand) |
417
|
}
|
248
|
}
|
418
|
|
249
|
|
419
|
//GetUserMenus 获取用户组织
|
250
|
//GetUserMenus 获取用户组织
|
420
|
-func (srv AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (interface{}, error) {
|
251
|
+func (svr AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (interface{}, error) {
|
421
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(userOrgCommand.Operator)
|
252
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(userOrgCommand.Operator)
|
422
|
result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
|
253
|
result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
|
423
|
Offset: 0,
|
254
|
Offset: 0,
|
|
@@ -446,7 +277,7 @@ func (srv AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (inter |
|
@@ -446,7 +277,7 @@ func (srv AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (inter |
446
|
}
|
277
|
}
|
447
|
|
278
|
|
448
|
//OrgSwitch 组织切换
|
279
|
//OrgSwitch 组织切换
|
449
|
-func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (interface{}, error) {
|
280
|
+func (svr AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (interface{}, error) {
|
450
|
if err := switchOrgCommand.ValidateCommand(); err != nil {
|
281
|
if err := switchOrgCommand.ValidateCommand(); err != nil {
|
451
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
282
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
452
|
}
|
283
|
}
|
|
@@ -456,7 +287,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in |
|
@@ -456,7 +287,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in |
456
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, "accessToken 不可用,"+err.Error())
|
287
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, "accessToken 不可用,"+err.Error())
|
457
|
}
|
288
|
}
|
458
|
ltoken.OrgId = switchOrgCommand.OrgId
|
289
|
ltoken.OrgId = switchOrgCommand.OrgId
|
459
|
- token, err := srv.getToken(domain.Operator{}, ltoken)
|
290
|
+ token, err := svr.getToken(ltoken)
|
460
|
if err != nil {
|
291
|
if err != nil {
|
461
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
292
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
462
|
}
|
293
|
}
|
|
@@ -466,7 +297,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in |
|
@@ -466,7 +297,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in |
466
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户不存在")
|
297
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户不存在")
|
467
|
}
|
298
|
}
|
468
|
}
|
299
|
}
|
469
|
- user, err := srv.getUserInfo(domain.Operator{UserId: userId})
|
300
|
+ user, err := svr.getUserInfo(domain.Operator{UserId: userId})
|
470
|
if err != nil {
|
301
|
if err != nil {
|
471
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
302
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
472
|
}
|
303
|
}
|
|
@@ -487,7 +318,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in |
|
@@ -487,7 +318,7 @@ func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (in |
487
|
}
|
318
|
}
|
488
|
|
319
|
|
489
|
// CompanySignUp 企业注册
|
320
|
// CompanySignUp 企业注册
|
490
|
-func (srv AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUpCommand) (interface{}, error) {
|
321
|
+func (svr AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUpCommand) (interface{}, error) {
|
491
|
//TODO:验证码验证测试去掉,后期恢复回来
|
322
|
//TODO:验证码验证测试去掉,后期恢复回来
|
492
|
//smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
|
323
|
//smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
|
493
|
//err := smsServeGateway.CheckSmsCode(companySignUpCommand.Phone, companySignUpCommand.SmsCode)
|
324
|
//err := smsServeGateway.CheckSmsCode(companySignUpCommand.Phone, companySignUpCommand.SmsCode)
|
|
@@ -510,7 +341,7 @@ func (srv AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUp |
|
@@ -510,7 +341,7 @@ func (srv AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUp |
510
|
}
|
341
|
}
|
511
|
|
342
|
|
512
|
// ResetPassword 重置密码(找回密码)
|
343
|
// ResetPassword 重置密码(找回密码)
|
513
|
-func (srv AuthService) ResetPassword(resetPasswordCommand *command.ResetPasswordCommand) (interface{}, error) {
|
344
|
+func (svr AuthService) ResetPassword(resetPasswordCommand *command.ResetPasswordCommand) (interface{}, error) {
|
514
|
if err := resetPasswordCommand.ValidateCommand(); err != nil {
|
345
|
if err := resetPasswordCommand.ValidateCommand(); err != nil {
|
515
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
346
|
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
|
516
|
}
|
347
|
}
|
|
@@ -533,7 +364,7 @@ func (srv AuthService) ResetPassword(resetPasswordCommand *command.ResetPassword |
|
@@ -533,7 +364,7 @@ func (srv AuthService) ResetPassword(resetPasswordCommand *command.ResetPassword |
533
|
return result, err
|
364
|
return result, err
|
534
|
}
|
365
|
}
|
535
|
|
366
|
|
536
|
-func (srv AuthService) getUserInfo(operator domain.Operator) (interface{}, error) {
|
367
|
+func (svr AuthService) getUserInfo(operator domain.Operator) (interface{}, error) {
|
537
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
|
368
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
|
538
|
operator)
|
369
|
operator)
|
539
|
resultUser, err := creationUserGateway.UserGet(allied_creation_user.ReqGetUser{
|
370
|
resultUser, err := creationUserGateway.UserGet(allied_creation_user.ReqGetUser{
|
|
@@ -563,33 +394,46 @@ func (srv AuthService) getUserInfo(operator domain.Operator) (interface{}, error |
|
@@ -563,33 +394,46 @@ func (srv AuthService) getUserInfo(operator domain.Operator) (interface{}, error |
563
|
return user, nil
|
394
|
return user, nil
|
564
|
}
|
395
|
}
|
565
|
|
396
|
|
566
|
-func (srv AuthService) getToken(operator domain.Operator, ltoken domain.LoginToken) (map[string]interface{}, error) {
|
397
|
+func (svr AuthService) getToken(ltoken domain.LoginToken) (map[string]interface{}, error) {
|
567
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
|
398
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
|
568
|
userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
|
399
|
userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
|
569
|
Phone: ltoken.Account,
|
400
|
Phone: ltoken.Account,
|
570
|
})
|
401
|
})
|
571
|
if err != nil {
|
402
|
if err != nil {
|
572
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户信息获取失败,"+err.Error())
|
403
|
+ return nil, application.ThrowError(application.BUSINESS_ERROR, "用户信息获取失败,"+err.Error())
|
573
|
}
|
404
|
}
|
574
|
- //判定当前凭证的companyId,OrganizationId 是否在用户列表中
|
405
|
+ // 1 . 判定当前凭证的companyId,OrganizationId 是否在用户列表中
|
575
|
var currentOrgIsOK bool
|
406
|
var currentOrgIsOK bool
|
576
|
- var currentUserId int64
|
407
|
+ var currentUser allied_creation_user.UserDetail
|
577
|
loopUser1:
|
408
|
loopUser1:
|
578
|
for _, v := range userSearchResult.Users {
|
409
|
for _, v := range userSearchResult.Users {
|
579
|
- //if v.Company.CompanyId == int(ltoken.CompanyId) {
|
|
|
580
|
for _, vv := range v.UserOrg {
|
410
|
for _, vv := range v.UserOrg {
|
581
|
if vv.OrgID == int(ltoken.OrgId) {
|
411
|
if vv.OrgID == int(ltoken.OrgId) {
|
582
|
currentOrgIsOK = true
|
412
|
currentOrgIsOK = true
|
583
|
- currentUserId = int64(v.UserId)
|
413
|
+ currentUser = v
|
584
|
break loopUser1
|
414
|
break loopUser1
|
585
|
}
|
415
|
}
|
586
|
}
|
416
|
}
|
587
|
- //}
|
417
|
+ }
|
|
|
418
|
+ if !currentOrgIsOK {
|
|
|
419
|
+ loopUser2:
|
|
|
420
|
+ for _, v := range userSearchResult.Users {
|
|
|
421
|
+ for _, vv := range v.UserOrg {
|
|
|
422
|
+ ltoken.OrgId = int64(vv.OrgID)
|
|
|
423
|
+ currentOrgIsOK = true
|
|
|
424
|
+ currentUser = v
|
|
|
425
|
+ break loopUser2
|
|
|
426
|
+ }
|
|
|
427
|
+ }
|
588
|
}
|
428
|
}
|
589
|
if !currentOrgIsOK {
|
429
|
if !currentOrgIsOK {
|
590
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用")
|
430
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用")
|
591
|
}
|
431
|
}
|
|
|
432
|
+ ltoken.UserId = int64(currentUser.UserId)
|
|
|
433
|
+ ltoken.UserBaseId = int64(currentUser.UserBaseId)
|
|
|
434
|
+ ltoken.CompanyId = int64(currentUser.Company.CompanyId)
|
592
|
|
435
|
|
|
|
436
|
+ // 2. 更新currentAccess信息
|
593
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
437
|
transactionContext, err := factory.CreateTransactionContext(nil)
|
594
|
if err != nil {
|
438
|
if err != nil {
|
595
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
439
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
@@ -604,35 +448,27 @@ loopUser1: |
|
@@ -604,35 +448,27 @@ loopUser1: |
604
|
if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{
|
448
|
if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{
|
605
|
"transactionContext": transactionContext,
|
449
|
"transactionContext": transactionContext,
|
606
|
}); err != nil {
|
450
|
}); err != nil {
|
607
|
- return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
451
|
+ return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
608
|
}
|
452
|
}
|
609
|
_, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
|
453
|
_, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
|
610
|
"account": ltoken.Account,
|
454
|
"account": ltoken.Account,
|
611
|
- "platform": domain.LoginPlatformApp,
|
455
|
+ "platform": ltoken.Platform,
|
612
|
})
|
456
|
})
|
613
|
if err != nil {
|
457
|
if err != nil {
|
614
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
458
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
615
|
}
|
459
|
}
|
616
|
- var currentAccess *domain.LoginAccess
|
460
|
+ var currentAccess = &domain.LoginAccess{CreatedTime: time.Now()}
|
617
|
if len(lAccess) > 0 {
|
461
|
if len(lAccess) > 0 {
|
618
|
currentAccess = lAccess[0]
|
462
|
currentAccess = lAccess[0]
|
619
|
- currentAccess.UpdatedTime = time.Now()
|
|
|
620
|
- } else {
|
|
|
621
|
- currentAccess = &domain.LoginAccess{
|
|
|
622
|
- UserBaseId: ltoken.UserBaseId,
|
|
|
623
|
- UserId: ltoken.UserId,
|
|
|
624
|
- Account: ltoken.Account,
|
|
|
625
|
- Platform: domain.LoginPlatformApp,
|
|
|
626
|
- CompanyId: ltoken.CompanyId,
|
|
|
627
|
- OrganizationId: ltoken.OrgId,
|
|
|
628
|
- AccessToken: "",
|
|
|
629
|
- RefreshToken: "",
|
|
|
630
|
- AccessExpired: 0,
|
|
|
631
|
- RefreshExpired: 0,
|
|
|
632
|
- CreatedTime: time.Now(),
|
|
|
633
|
- UpdatedTime: time.Now(),
|
|
|
634
|
- }
|
|
|
635
|
}
|
463
|
}
|
|
|
464
|
+ currentAccess.UserId = int64(ltoken.UserId)
|
|
|
465
|
+ currentAccess.UserBaseId = int64(ltoken.UserBaseId)
|
|
|
466
|
+ currentAccess.Account = ltoken.Account
|
|
|
467
|
+ currentAccess.Platform = ltoken.Platform
|
|
|
468
|
+ currentAccess.CompanyId = int64(ltoken.CompanyId)
|
|
|
469
|
+ currentAccess.OrganizationId = ltoken.OrgId
|
|
|
470
|
+ currentAccess.UpdatedTime = time.Now()
|
|
|
471
|
+
|
636
|
accessTokenStr, err := ltoken.GenerateAccessToken()
|
472
|
accessTokenStr, err := ltoken.GenerateAccessToken()
|
637
|
if err != nil {
|
473
|
if err != nil {
|
638
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
474
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
@@ -641,22 +477,24 @@ loopUser1: |
|
@@ -641,22 +477,24 @@ loopUser1: |
641
|
currentAccess.AccessExpired = ltoken.ExpiresAt
|
477
|
currentAccess.AccessExpired = ltoken.ExpiresAt
|
642
|
refreshTokenStr, err := ltoken.GenerateRefreshToken()
|
478
|
refreshTokenStr, err := ltoken.GenerateRefreshToken()
|
643
|
if err != nil {
|
479
|
if err != nil {
|
644
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
480
|
+ return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
645
|
}
|
481
|
}
|
646
|
currentAccess.RefreshToken = refreshTokenStr
|
482
|
currentAccess.RefreshToken = refreshTokenStr
|
647
|
currentAccess.RefreshExpired = ltoken.ExpiresAt
|
483
|
currentAccess.RefreshExpired = ltoken.ExpiresAt
|
648
|
- //先存数据库
|
484
|
+
|
|
|
485
|
+ //存数据库
|
649
|
_, err = loginAccessRepository.Save(currentAccess)
|
486
|
_, err = loginAccessRepository.Save(currentAccess)
|
650
|
if err != nil {
|
487
|
if err != nil {
|
651
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
488
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
652
|
}
|
489
|
}
|
653
|
if err := transactionContext.CommitTransaction(); err != nil {
|
490
|
if err := transactionContext.CommitTransaction(); err != nil {
|
654
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
491
|
+ return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
655
|
}
|
492
|
}
|
656
|
- //后处理redis缓存
|
493
|
+
|
|
|
494
|
+ //redis缓存
|
657
|
tokenCache := cache.LoginTokenCache{}
|
495
|
tokenCache := cache.LoginTokenCache{}
|
658
|
- tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp)
|
|
|
659
|
- tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp)
|
496
|
+ tokenCache.RemoveAccessToken(currentAccess.Account, ltoken.Platform)
|
|
|
497
|
+ tokenCache.RemoveRefreshToken(currentAccess.Account, ltoken.Platform)
|
660
|
tokenCache.SaveAccessToken(currentAccess)
|
498
|
tokenCache.SaveAccessToken(currentAccess)
|
661
|
tokenCache.SaveRefreshToken(currentAccess)
|
499
|
tokenCache.SaveRefreshToken(currentAccess)
|
662
|
nowTime := time.Now().Unix()
|
500
|
nowTime := time.Now().Unix()
|
|
@@ -667,12 +505,12 @@ loopUser1: |
|
@@ -667,12 +505,12 @@ loopUser1: |
667
|
}
|
505
|
}
|
668
|
return map[string]interface{}{
|
506
|
return map[string]interface{}{
|
669
|
"token": token,
|
507
|
"token": token,
|
670
|
- "userId": currentUserId,
|
508
|
+ "userId": currentUser.UserId,
|
671
|
}, nil
|
509
|
}, nil
|
672
|
}
|
510
|
}
|
673
|
|
511
|
|
674
|
//GetCompanyOrgsByUser 获取登录用户的公司组织列表
|
512
|
//GetCompanyOrgsByUser 获取登录用户的公司组织列表
|
675
|
-func (srv AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUserQuery) (interface{}, error) {
|
513
|
+func (svr AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUserQuery) (interface{}, error) {
|
676
|
|
514
|
|
677
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(queryParam.Operator)
|
515
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(queryParam.Operator)
|
678
|
result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
|
516
|
result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
|
|
@@ -708,7 +546,7 @@ func (srv AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUs |
|
@@ -708,7 +546,7 @@ func (srv AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUs |
708
|
}
|
546
|
}
|
709
|
|
547
|
|
710
|
//GetQrcode 获取扫码登录需要的二维码
|
548
|
//GetQrcode 获取扫码登录需要的二维码
|
711
|
-func (srv AuthService) GetQrcode() (interface{}, error) {
|
549
|
+func (svr AuthService) GetQrcode() (interface{}, error) {
|
712
|
qrmsg := domain.QrcodeMessage{}
|
550
|
qrmsg := domain.QrcodeMessage{}
|
713
|
_, err := qrmsg.GenerateImageBase64() //imgBase64
|
551
|
_, err := qrmsg.GenerateImageBase64() //imgBase64
|
714
|
if err != nil {
|
552
|
if err != nil {
|
|
@@ -726,41 +564,8 @@ func (srv AuthService) GetQrcode() (interface{}, error) { |
|
@@ -726,41 +564,8 @@ func (srv AuthService) GetQrcode() (interface{}, error) { |
726
|
return data, nil
|
564
|
return data, nil
|
727
|
}
|
565
|
}
|
728
|
|
566
|
|
729
|
-//QrcodeLoginStatus 询问扫码登录状态
|
|
|
730
|
-func (srv AuthService) QrcodeLoginStatus(queryParam *query.QrcodeLoginStatusQuery) (interface{}, error) {
|
|
|
731
|
- qrmsg := domain.QrcodeMessage{}
|
|
|
732
|
- err := qrmsg.ParseToken(queryParam.Key)
|
|
|
733
|
- if err != nil {
|
|
|
734
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
735
|
- }
|
|
|
736
|
- qrCache := cache.LoginQrcodeCache{}
|
|
|
737
|
- qrmsgCache, err := qrCache.Get(qrmsg.Id)
|
|
|
738
|
- if err != nil {
|
|
|
739
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
740
|
- }
|
|
|
741
|
- loginToken := domain.LoginToken{
|
|
|
742
|
- UserId: qrmsgCache.UserId,
|
|
|
743
|
- UserBaseId: qrmsgCache.UserBaseId,
|
|
|
744
|
- Account: qrmsgCache.Account,
|
|
|
745
|
- Platform: domain.LoginPlatformWeb,
|
|
|
746
|
- CompanyId: qrmsgCache.CompanyId,
|
|
|
747
|
- OrgId: qrmsgCache.OrgId,
|
|
|
748
|
- }
|
|
|
749
|
- accessToken, err := loginToken.GenerateAccessToken()
|
|
|
750
|
- if err != nil {
|
|
|
751
|
- return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
|
|
|
752
|
- }
|
|
|
753
|
- _ = accessToken
|
|
|
754
|
- //TODO 填充token数据
|
|
|
755
|
- data := map[string]interface{}{
|
|
|
756
|
- "isLogin": qrmsgCache.IsLogin,
|
|
|
757
|
- "access": "",
|
|
|
758
|
- }
|
|
|
759
|
- return data, nil
|
|
|
760
|
-}
|
|
|
761
|
-
|
|
|
762
|
//CheckSmsCode 验证手机短信验证码
|
567
|
//CheckSmsCode 验证手机短信验证码
|
763
|
-func (srv AuthService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) (interface{}, error) {
|
568
|
+func (svr AuthService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) (interface{}, error) {
|
764
|
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
|
569
|
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
|
765
|
err := smsServeGateway.CheckSmsCode(smsCodeCommand.Phone, smsCodeCommand.SmsCode)
|
570
|
err := smsServeGateway.CheckSmsCode(smsCodeCommand.Phone, smsCodeCommand.SmsCode)
|
766
|
if err != nil {
|
571
|
if err != nil {
|
|
@@ -780,3 +585,25 @@ func (srv AuthService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) |
|
@@ -780,3 +585,25 @@ func (srv AuthService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) |
780
|
"smsCodeIdentity": uid.String(),
|
585
|
"smsCodeIdentity": uid.String(),
|
781
|
}, nil
|
586
|
}, nil
|
782
|
}
|
587
|
}
|
|
|
588
|
+
|
|
|
589
|
+func (svr *AuthService) CaptchaInit(request *query.CaptchaInitRequest) (interface{}, error) {
|
|
|
590
|
+ var rsp map[string]interface{}
|
|
|
591
|
+ var err error
|
|
|
592
|
+
|
|
|
593
|
+ newGeetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
|
|
|
594
|
+ status, responseBt := newGeetest.PreProcess("", request.UserIp)
|
|
|
595
|
+ var geetestRsp geetest.FailbackRegisterRespnse
|
|
|
596
|
+ json.Unmarshal(responseBt, &geetestRsp)
|
|
|
597
|
+ rspData := map[string]interface{}{
|
|
|
598
|
+ "success": geetestRsp.Success,
|
|
|
599
|
+ "gt": geetestRsp.GT,
|
|
|
600
|
+ "challenge": geetestRsp.Challenge,
|
|
|
601
|
+ "newCaptcha": geetestRsp.NewCaptcha,
|
|
|
602
|
+ }
|
|
|
603
|
+ if status == 0 {
|
|
|
604
|
+ return nil, application.ThrowError(application.BUSINESS_ERROR, "获取图形验证码失败,请重试")
|
|
|
605
|
+ }
|
|
|
606
|
+
|
|
|
607
|
+ rsp = rspData
|
|
|
608
|
+ return rsp, err
|
|
|
609
|
+} |