...
|
...
|
@@ -237,10 +237,250 @@ func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.Refre |
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.TRANSACTION_ERROR, "refreshToken 不可用,"+err.Error())
|
|
|
}
|
|
|
phone := ltoken.Account
|
|
|
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, 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 res, nil
|
|
|
}
|
|
|
|
|
|
//OrgSwitch 组织切换
|
|
|
func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (interface{}, error) {
|
|
|
|
|
|
return nil, 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{})
|
|
|
userSeachResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
|
|
|
Phone: phone,
|
|
|
result, 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 result, 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) (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())
|
...
|
...
|
@@ -248,10 +488,10 @@ func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.Refre |
|
|
//判定当前凭证的companyId,OrganizationId 是否在用户列表中
|
|
|
var currentOrgIsOK bool
|
|
|
loopUser1:
|
|
|
for _, v := range userSeachResult.Users {
|
|
|
for _, v := range userSearchResult.Users {
|
|
|
if v.Company.CompanyId == int(ltoken.CompanyId) {
|
|
|
for _, vv := range v.UserOrg {
|
|
|
if vv.OrgID == int(ltoken.OrgId) {
|
|
|
if vv.OrgID == int(ltoken.OrgId) || vv.OrgID == int(operator.OrgId) {
|
|
|
currentOrgIsOK = true
|
|
|
break loopUser1
|
|
|
}
|
...
|
...
|
@@ -279,7 +519,7 @@ loopUser1: |
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
_, lAccess, err := loginAccessRepository.Find(map[string]interface{}{
|
|
|
"account": phone,
|
|
|
"account": ltoken.Account,
|
|
|
"platform": domain.LoginPlatformApp,
|
|
|
})
|
|
|
if err != nil {
|
...
|
...
|
@@ -338,125 +578,3 @@ loopUser1: |
|
|
"expiresIn": currentAccess.AccessExpired - nowTime,
|
|
|
}, nil
|
|
|
} |
|
|
|
|
|
//GetUserMenus 获取用户信息
|
|
|
func (srv AuthService) GetUserInfo(userInfoCommand *command.UserInfoCommand) (interface{}, error) {
|
|
|
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
|
|
|
userInfoCommand.Operator)
|
|
|
resultUser, err := creationUserGateway.UserGet(allied_creation_user.ReqGetUser{
|
|
|
UserId: int(userInfoCommand.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 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 res, nil
|
|
|
}
|
|
|
|
|
|
// CompanySignUp 企业注册
|
|
|
func (srv AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUpCommand) (interface{}, error) {
|
|
|
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{})
|
|
|
result, 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 result, 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
|
|
|
} |
...
|
...
|
|