service.go
27.6 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
package service
import (
"github.com/google/uuid"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/dto"
"time"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/auth/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/cache"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/sms_serve"
)
// 组织管理
type AuthService struct {
}
//AuthLogin 用户登录
func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{}, error) {
var (
authCode string
result interface{}
err error
)
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
_, err = creationUserGateway.AuthRefreshIM(allied_creation_user.ReqAuthRefreshIM{
Phone: loginCommand.Phone,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
switch loginCommand.GrantType {
case "signInPassword":
authCode, err = srv.SignInPassword(loginCommand.Phone, loginCommand.Password)
case "signInCaptcha":
authCode, err = srv.SignInCaptcha(loginCommand.Phone, loginCommand.Captcha)
default:
err = application.ThrowError(application.TRANSACTION_ERROR, "登录方式无法解析")
}
result, err = srv.GetAuthAccessToken(&command.AccessTokenCommand{
AuthCode: authCode,
SessionMode: loginCommand.SessionMode,
})
return map[string]interface{}{
"access": result,
}, err
}
//AuthLogin 用户登录
func (srv AuthService) AuthLoginPwd(loginCommand *command.LoginPwdCommand) (interface{}, error) {
if err := loginCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
login := &command.LoginCommand{
GrantType: "signInPassword",
Phone: loginCommand.Username,
Password: loginCommand.Password,
SessionMode: loginCommand.SessionMode,
}
return srv.AuthLogin(login)
}
//AuthLogin 用户登录
func (srv AuthService) AuthLoginSms(loginCommand *command.LoginSmsCommand) (interface{}, error) {
if err := loginCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
login := &command.LoginCommand{
GrantType: "signInCaptcha",
Phone: loginCommand.Phone,
Captcha: loginCommand.Code,
SessionMode: loginCommand.SessionMode,
}
return srv.AuthLogin(login)
}
//SendSmsCaptcha 发送验证码短信
func (srv AuthService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand) error {
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
err := smsServeGateway.SendSms(smsCodeCommand.Phone)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
//SignInPassword 使用账号密码校验
func (srv AuthService) SignInPassword(account string, password string) (string, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
_, err := creationUserGateway.AuthCheckPassword(allied_creation_user.ReqAuthCheckPassword{
Password: password,
Phone: account,
})
if err != nil {
return "", application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
ltoken := domain.LoginToken{
UserId: 0,
Account: account,
Platform: domain.LoginPlatformApp,
CompanyId: 0,
}
authcode, err := ltoken.GenerateAuthCode()
if err != nil {
return "", application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
//result := map[string]string{
// "authCode": authcode,
//}
return authcode, nil
}
//SignInCaptcha 使用手机验证码登录
func (srv AuthService) SignInCaptcha(phone string, captcha string) (string, error) {
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
err := smsServeGateway.CheckSmsCode(phone, captcha)
if err != nil {
return "", application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
ltoken := domain.LoginToken{
UserId: 0,
Account: phone,
Platform: domain.LoginPlatformApp,
CompanyId: 0,
}
authcode, err := ltoken.GenerateAuthCode()
if err != nil {
return "", application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return authcode, nil
}
//GetAuthAccessToken 获取令牌Token
func (srv AuthService) GetAuthAccessToken(accessTokenCommand *command.AccessTokenCommand) (interface{}, error) {
if err := accessTokenCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
ltoken := &domain.LoginToken{}
err := ltoken.ParseToken(accessTokenCommand.AuthCode)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
phone := ltoken.Account
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
userSeachResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
CompanyId: ltoken.CompanyId,
Phone: phone,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if len(userSeachResult.Users) == 0 {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "获取用户信息失败")
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var loginAccessRepository domain.LoginAccessRepository
if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
_, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
"account": phone,
"platform": domain.LoginPlatformApp,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var currentAccess *domain.LoginAccess
if len(lAccess) > 0 {
currentAccess = lAccess[0]
currentAccess.UpdatedTime = time.Now()
} else {
currentAccess = &domain.LoginAccess{
UserBaseId: int64(userSeachResult.Users[0].UserBaseId),
UserId: int64(userSeachResult.Users[0].UserId),
Account: userSeachResult.Users[0].UserInfo.Phone,
Platform: domain.LoginPlatformApp,
OrganizationId: int64(userSeachResult.Users[0].Org.OrgId),
AccessToken: "",
RefreshToken: "",
AccessExpired: 0,
RefreshExpired: 0,
CreatedTime: time.Now(),
UpdatedTime: time.Now(),
}
if userSeachResult.Users[0].Company != nil {
currentAccess.CompanyId = int64(userSeachResult.Users[0].Company.CompanyId)
}
}
//判定当前凭证的companyId,OrganizationId 是否在用户列表中
var currentOrgIsOK bool
loopUser1:
for _, v := range userSeachResult.Users {
if v.Company.CompanyId == int(currentAccess.CompanyId) {
for _, vv := range v.UserOrg {
if vv.OrgID == int(currentAccess.OrganizationId) {
currentOrgIsOK = true
currentAccess.UserId = int64(v.UserId)
currentAccess.UserBaseId = int64(v.UserBaseId)
break loopUser1
}
}
}
}
//记录上一次的登录公司组织信息不可用 ,重置登录记录
//使用找到的第一个可用的公司组织
if !currentOrgIsOK {
loopUser2:
for _, v := range userSeachResult.Users {
currentAccess.CompanyId = int64(v.Company.CompanyId)
for _, vv := range v.UserOrg {
currentAccess.UserId = int64(v.UserId)
currentAccess.UserBaseId = int64(v.UserBaseId)
currentAccess.OrganizationId = int64(vv.OrgID)
currentOrgIsOK = true
break loopUser2
}
}
}
loginToken := domain.LoginToken{
UserId: currentAccess.UserId,
Account: currentAccess.Account,
UserBaseId: currentAccess.UserBaseId,
CompanyId: currentAccess.CompanyId,
OrgId: currentAccess.OrganizationId,
Platform: currentAccess.Platform,
}
accessTokenStr, err := loginToken.GenerateAccessToken()
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
currentAccess.AccessToken = accessTokenStr
currentAccess.AccessExpired = loginToken.ExpiresAt
refreshTokenStr, err := loginToken.GenerateRefreshToken()
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
currentAccess.RefreshToken = refreshTokenStr
currentAccess.AccessExpired = loginToken.ExpiresAt
//先存数据库
_, err = loginAccessRepository.Save(currentAccess)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
//后处理redis缓存
tokenCache := cache.LoginTokenCache{}
tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp)
tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp)
tokenCache.SaveAccessToken(currentAccess)
tokenCache.SaveRefreshToken(currentAccess)
nowTime := time.Now().Unix()
return map[string]interface{}{
"refreshToken": accessTokenStr,
"accessToken": refreshTokenStr,
"expiresIn": currentAccess.AccessExpired - nowTime,
}, nil
}
func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.RefreshTokenCommand) (interface{}, error) {
if err := refreshTokenCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
ltoken := domain.LoginToken{}
err := ltoken.ParseToken(refreshTokenCommand.RefreshToken)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "refreshToken 不可用,"+err.Error())
}
token, err := srv.getToken(domain.Operator{}, ltoken)
// phone := ltoken.Account
// creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
// userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
// Phone: phone,
// })
// if err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户信息获取失败,"+err.Error())
// }
// //判定当前凭证的companyId,OrganizationId 是否在用户列表中
// var currentOrgIsOK bool
//loopUser1:
// for _, v := range userSearchResult.Users {
// if v.Company.CompanyId == int(ltoken.CompanyId) {
// for _, vv := range v.UserOrg {
// if vv.OrgID == int(ltoken.OrgId) {
// currentOrgIsOK = true
// break loopUser1
// }
// }
// }
// }
// if !currentOrgIsOK {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用")
// }
//
// transactionContext, err := factory.CreateTransactionContext(nil)
// if err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// if err := transactionContext.StartTransaction(); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// defer func() {
// transactionContext.RollbackTransaction()
// }()
// var loginAccessRepository domain.LoginAccessRepository
// if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{
// "transactionContext": transactionContext,
// }); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// }
// _, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
// "account": phone,
// "platform": domain.LoginPlatformApp,
// })
// if err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// var currentAccess *domain.LoginAccess
// if len(lAccess) > 0 {
// currentAccess = lAccess[0]
// currentAccess.UpdatedTime = time.Now()
// } else {
// currentAccess = &domain.LoginAccess{
// UserBaseId: ltoken.UserBaseId,
// UserId: ltoken.UserId,
// Account: ltoken.Account,
// Platform: domain.LoginPlatformApp,
// CompanyId: ltoken.CompanyId,
// OrganizationId: ltoken.OrgId,
// AccessToken: "",
// RefreshToken: "",
// AccessExpired: 0,
// RefreshExpired: 0,
// CreatedTime: time.Now(),
// UpdatedTime: time.Now(),
// }
// }
// accessTokenStr, err := ltoken.GenerateAccessToken()
// if err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// currentAccess.AccessToken = accessTokenStr
// currentAccess.AccessExpired = ltoken.ExpiresAt
// refreshTokenStr, err := ltoken.GenerateRefreshToken()
// if err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// currentAccess.RefreshToken = refreshTokenStr
// currentAccess.RefreshExpired = ltoken.ExpiresAt
// //先存数据库
// _, err = loginAccessRepository.Save(currentAccess)
// if err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// if err := transactionContext.CommitTransaction(); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// //后处理redis缓存
// tokenCache := cache.LoginTokenCache{}
// tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp)
// tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp)
// tokenCache.SaveAccessToken(currentAccess)
// tokenCache.SaveRefreshToken(currentAccess)
// nowTime := time.Now().Unix()
// return map[string]interface{}{
// "refreshToken": accessTokenStr,
// "accessToken": refreshTokenStr,
// "expiresIn": currentAccess.AccessExpired - nowTime,
// }, nil
return token["token"], err
}
//GetUserMenus 获取用户信息
func (srv AuthService) GetUserInfo(userInfoCommand *command.UserInfoCommand) (interface{}, error) {
user, err := srv.getUserInfo(userInfoCommand.Operator)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return map[string]interface{}{
"user": user,
}, nil
}
//GetUserMenus 获取用户菜单
func (srv AuthService) GetUserMenus(userMenusCommand *command.UserMenusCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
userMenusCommand.Operator)
resultMenu, err := creationUserGateway.UserAccessMenus(allied_creation_user.ReqUserAccessMenus{
UserId: int(userMenusCommand.Operator.UserId),
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return map[string]interface{}{
"accessMenus": resultMenu.Menus,
}, nil
}
//GetUserMenus 获取用户组织
func (srv AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(userOrgCommand.Operator)
result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
Offset: 0,
Limit: 100,
UserBaseId: userOrgCommand.Operator.UserBaseId,
UserType: domain.UserTypeEmployee,
EnableStatus: domain.UserStatusEnable,
PullRealTime: true,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
var res = make([]interface{}, 0)
for i := range result.Users {
for j := range result.Users[i].UserOrg {
org := result.Users[i].UserOrg[j]
res = append(res, map[string]interface{}{
"orgId": org.OrgID,
"orgName": org.OrgName,
})
}
}
return map[string]interface{}{
"orgs": res,
}, nil
}
//OrgSwitch 组织切换
func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (interface{}, error) {
if err := switchOrgCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
ltoken := domain.LoginToken{}
err := ltoken.ParseToken(switchOrgCommand.Operator.Token)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "accessToken 不可用,"+err.Error())
}
ltoken.OrgId = switchOrgCommand.OrgId
token, err := srv.getToken(domain.Operator{}, ltoken)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
var userId int64
if v, ok := token["userId"]; ok {
if userId, ok = v.(int64); !ok {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户不存在")
}
}
user, err := srv.getUserInfo(domain.Operator{UserId: userId})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
switchOrgCommand.Operator)
resultMenu, err := creationUserGateway.UserAccessMenus(allied_creation_user.ReqUserAccessMenus{
UserId: int(userId),
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
var res = map[string]interface{}{
"user": user,
"accessMenus": resultMenu.Menus,
"token": token["token"],
}
return res, nil
}
// CompanySignUp 企业注册
func (srv AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUpCommand) (interface{}, error) {
//TODO:验证码验证测试去掉,后期恢复回来
//smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
//err := smsServeGateway.CheckSmsCode(companySignUpCommand.Phone, companySignUpCommand.SmsCode)
//if err != nil {
// return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
//}
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
_, err := creationUserGateway.AuthCompanySignUp(allied_creation_user.ReqAuthCompanySignUp{
CompanyName: companySignUpCommand.CompanyName,
Phone: companySignUpCommand.Phone,
Password: companySignUpCommand.Password,
Contacts: companySignUpCommand.Contacts,
IndustryCategory: companySignUpCommand.IndustryCategory,
Scale: companySignUpCommand.Scale,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return companySignUpCommand, err
}
// ResetPassword 重置密码(找回密码)
func (srv AuthService) ResetPassword(resetPasswordCommand *command.ResetPasswordCommand) (interface{}, error) {
if err := resetPasswordCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
//var phone string
pcc := cache.PhoneCheckCache{}
var item = &cache.PhoneCheckItem{}
if err := pcc.Get(resetPasswordCommand.SmsCodeIdentity, item); err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.BUSINESS_ERROR, "验证码已失效")
}
// 2.重置密码
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
result, err := creationUserGateway.AuthResetPassword(allied_creation_user.ReqAuthResetPassword{
Phone: item.Phone,
Password: resetPasswordCommand.Password,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return result, err
}
func (srv AuthService) getUserInfo(operator domain.Operator) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
operator)
resultUser, err := creationUserGateway.UserGet(allied_creation_user.ReqGetUser{
UserId: int(operator.UserId),
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
var user = map[string]interface{}{
"userId": resultUser.UserId,
"userInfo": map[string]interface{}{
"userName": resultUser.UserInfo.UserName,
"userPhone": resultUser.UserInfo.Phone,
"userAvatar": resultUser.UserInfo.Avatar,
//"userCode": resultUser.UserInfo.UserCode,
"email": resultUser.UserInfo.Email,
},
"department": resultUser.Department,
"company": map[string]interface{}{
"companyId": resultUser.Company.CompanyId,
"companyName": resultUser.Company.CompanyName,
"logo": resultUser.Company.Log,
},
"im": resultUser.IM,
"org": resultUser.Org,
}
return user, nil
}
func (srv AuthService) getToken(operator domain.Operator, ltoken domain.LoginToken) (map[string]interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
Phone: ltoken.Account,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户信息获取失败,"+err.Error())
}
//判定当前凭证的companyId,OrganizationId 是否在用户列表中
var currentOrgIsOK bool
var currentUserId int64
loopUser1:
for _, v := range userSearchResult.Users {
//if v.Company.CompanyId == int(ltoken.CompanyId) {
for _, vv := range v.UserOrg {
if vv.OrgID == int(ltoken.OrgId) {
currentOrgIsOK = true
currentUserId = int64(v.UserId)
break loopUser1
}
}
//}
}
if !currentOrgIsOK {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用")
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var loginAccessRepository domain.LoginAccessRepository
if loginAccessRepository, err = factory.CreateLoginAccessRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
_, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
"account": ltoken.Account,
"platform": domain.LoginPlatformApp,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var currentAccess *domain.LoginAccess
if len(lAccess) > 0 {
currentAccess = lAccess[0]
currentAccess.UpdatedTime = time.Now()
} else {
currentAccess = &domain.LoginAccess{
UserBaseId: ltoken.UserBaseId,
UserId: ltoken.UserId,
Account: ltoken.Account,
Platform: domain.LoginPlatformApp,
CompanyId: ltoken.CompanyId,
OrganizationId: ltoken.OrgId,
AccessToken: "",
RefreshToken: "",
AccessExpired: 0,
RefreshExpired: 0,
CreatedTime: time.Now(),
UpdatedTime: time.Now(),
}
}
accessTokenStr, err := ltoken.GenerateAccessToken()
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
currentAccess.AccessToken = accessTokenStr
currentAccess.AccessExpired = ltoken.ExpiresAt
refreshTokenStr, err := ltoken.GenerateRefreshToken()
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
currentAccess.RefreshToken = refreshTokenStr
currentAccess.RefreshExpired = ltoken.ExpiresAt
//先存数据库
_, err = loginAccessRepository.Save(currentAccess)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
//后处理redis缓存
tokenCache := cache.LoginTokenCache{}
tokenCache.RemoveAccessToken(currentAccess.Account, domain.LoginPlatformApp)
tokenCache.RemoveRefreshToken(currentAccess.Account, domain.LoginPlatformApp)
tokenCache.SaveAccessToken(currentAccess)
tokenCache.SaveRefreshToken(currentAccess)
nowTime := time.Now().Unix()
token := map[string]interface{}{
"refreshToken": accessTokenStr,
"accessToken": refreshTokenStr,
"expiresIn": currentAccess.AccessExpired - nowTime,
}
return map[string]interface{}{
"token": token,
"userId": currentUserId,
}, nil
}
//GetCompanyOrgsByUser 获取登录用户的公司组织列表
func (srv AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUserQuery) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(queryParam.Operator)
result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
Phone: queryParam.Phone,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var (
companys []dto.CompanyItem
orgs []dto.OrgItem
)
for _, v := range result.Users {
companys = append(companys, dto.CompanyItem{
CompanyId: v.Company.CompanyId,
CompanyName: v.Company.CompanyName,
})
for _, vv := range v.UserOrg {
orgs = append(orgs, dto.OrgItem{
OrganizationId: vv.OrgID,
OrganizationName: vv.OrgName,
CompanyId: v.Company.CompanyId,
})
}
}
data := map[string]interface{}{
"companys": companys,
"organizations": orgs,
}
return data, nil
}
//GetQrcode 获取扫码登录需要的二维码
func (srv AuthService) GetQrcode() (interface{}, error) {
qrmsg := domain.QrcodeMessage{}
_, err := qrmsg.GenerateImageBase64() //imgBase64
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
qrCache := cache.LoginQrcodeCache{}
err = qrCache.Save(qrmsg)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
data := map[string]interface{}{
//"image": imgBase64,
"key": qrmsg.Token,
}
return data, nil
}
//QrcodeLoginStatus 询问扫码登录状态
func (srv AuthService) QrcodeLoginStatus(queryParam *query.QrcodeLoginStatusQuery) (interface{}, error) {
qrmsg := domain.QrcodeMessage{}
err := qrmsg.ParseToken(queryParam.Key)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
qrCache := cache.LoginQrcodeCache{}
qrmsgCache, err := qrCache.Get(qrmsg.Id)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
loginToken := domain.LoginToken{
UserId: qrmsgCache.UserId,
UserBaseId: qrmsgCache.UserBaseId,
Account: qrmsgCache.Account,
Platform: domain.LoginPlatformWeb,
CompanyId: qrmsgCache.CompanyId,
OrgId: qrmsgCache.OrgId,
}
accessToken, err := loginToken.GenerateAccessToken()
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
_ = accessToken
//TODO 填充token数据
data := map[string]interface{}{
"isLogin": qrmsgCache.IsLogin,
"access": "",
}
return data, nil
}
//CheckSmsCode 验证手机短信验证码
func (srv AuthService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) (interface{}, error) {
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
err := smsServeGateway.CheckSmsCode(smsCodeCommand.Phone, smsCodeCommand.SmsCode)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
uid := uuid.New()
pcc := cache.PhoneCheckCache{}
if err := pcc.Add(uid.String(), cache.PhoneCheckItem{
Phone: smsCodeCommand.Phone,
SmsCodeIdentity: uid.String(),
Action: smsCodeCommand.Action,
}); err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.BUSINESS_ERROR, "系统错误")
}
return map[string]interface{}{
"smsCodeIdentity": uid.String(),
}, nil
}