作者 yangfu

登录优化

... ... @@ -516,6 +516,73 @@ func (svr AuthService) getUserInfo(operator domain.Operator) (interface{}, error
}
func (svr AuthService) getToken(loginToken domain.LoginToken) (map[string]interface{}, error) {
// 1.匹配账号对应的用户
currentUser, err := svr.matchUser(&loginToken)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 2. 更新LoginAccess
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.BUSINESS_ERROR, err.Error())
}
_, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
"account": loginToken.Account,
"platform": loginToken.Platform,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var currentAccess = &domain.LoginAccess{CreatedTime: time.Now()}
if len(lAccess) > 0 {
currentAccess = lAccess[0]
}
if _, err = currentAccess.ResetLoginAccess(loginToken); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
//存数据库
if _, err = loginAccessRepository.Save(currentAccess); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
//redis缓存
tokenCache := cache.LoginTokenCache{}
if err = tokenCache.SaveAccessToken(currentAccess); err != nil {
return nil, err
}
if err = tokenCache.SaveRefreshToken(currentAccess); err != nil {
return nil, err
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
nowTime := time.Now().Unix()
token := map[string]interface{}{
"refreshToken": currentAccess.RefreshToken,
"accessToken": currentAccess.AccessToken,
"expiresIn": currentAccess.AccessExpired - nowTime,
}
return map[string]interface{}{
"token": token,
"userId": currentUser.UserId,
}, nil
}
func (svr AuthService) matchUser(loginToken *domain.LoginToken) (*allied_creation_user.UserDetail, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
Phone: loginToken.Account,
... ... @@ -572,87 +639,21 @@ loopUser1:
return nil, application.ThrowError(application.TRANSACTION_ERROR, "账号不存在")
}
loginToken.UserBaseId = int64(userBase.UserBaseID)
}
// 2. 更新currentAccess信息
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.BUSINESS_ERROR, err.Error())
}
_, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
"account": loginToken.Account,
"platform": loginToken.Platform,
if userBase.UserBaseID > 0 {
cooperationUsers, _ := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
UserBaseId: int64(userBase.UserBaseID),
UserType: domain.UserTypeCooperation,
EnableStatus: domain.UserStatusEnable,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
if len(cooperationUsers.Users) > 0 {
loginToken.CompanyId = int64(cooperationUsers.Users[0].Company.CompanyId)
loginToken.UserId = int64(cooperationUsers.Users[0].UserId)
loginToken.OrgId = int64(cooperationUsers.Users[0].Org.OrgId)
currentUser = cooperationUsers.Users[0]
}
var currentAccess = &domain.LoginAccess{CreatedTime: time.Now()}
if len(lAccess) > 0 {
currentAccess = lAccess[0]
}
currentAccess.UserId = int64(loginToken.UserId)
currentAccess.UserBaseId = int64(loginToken.UserBaseId)
currentAccess.Account = loginToken.Account
currentAccess.Platform = loginToken.Platform
currentAccess.CompanyId = int64(loginToken.CompanyId)
currentAccess.OrganizationId = loginToken.OrgId
currentAccess.OrgIds = loginToken.OrgIds
currentAccess.UpdatedTime = time.Now()
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.BUSINESS_ERROR, err.Error())
}
currentAccess.RefreshToken = refreshTokenStr
currentAccess.RefreshExpired = 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.BUSINESS_ERROR, err.Error())
}
//redis缓存
tokenCache := cache.LoginTokenCache{}
//todo:error handler
if err = tokenCache.SaveAccessToken(currentAccess); err != nil {
return nil, err
}
if err = tokenCache.SaveRefreshToken(currentAccess); err != nil {
return nil, err
}
nowTime := time.Now().Unix()
token := map[string]interface{}{
"refreshToken": refreshTokenStr,
"accessToken": accessTokenStr,
"expiresIn": currentAccess.AccessExpired - nowTime,
}
return map[string]interface{}{
"token": token,
"userId": currentUser.UserId,
}, nil
return &currentUser, nil
}
//GetCompanyOrgsByUser 获取登录用户的公司组织列表
... ...
... ... @@ -128,6 +128,31 @@ func ParseCodeMsg(code int) string {
return ""
}
func (loginAccess *LoginAccess) ResetLoginAccess(loginToken LoginToken) (interface{}, error) {
loginAccess.UserId = int64(loginToken.UserId)
loginAccess.UserBaseId = int64(loginToken.UserBaseId)
loginAccess.Account = loginToken.Account
loginAccess.Platform = loginToken.Platform
loginAccess.CompanyId = int64(loginToken.CompanyId)
loginAccess.OrganizationId = loginToken.OrgId
loginAccess.OrgIds = loginToken.OrgIds
loginAccess.UpdatedTime = time.Now()
accessTokenStr, err := loginToken.GenerateAccessToken()
if err != nil {
return nil, err
}
loginAccess.AccessToken = accessTokenStr
loginAccess.AccessExpired = loginToken.ExpiresAt
refreshTokenStr, err := loginToken.GenerateRefreshToken()
if err != nil {
return nil, err
}
loginAccess.RefreshToken = refreshTokenStr
loginAccess.RefreshExpired = loginToken.ExpiresAt
return nil, nil
}
func NewApplicationError(code int) error {
return &application.ServiceError{
Code: code,
... ...