作者 yangfu

平台登录重构

... ... @@ -13,6 +13,7 @@ type LoginCommand struct {
Captcha string `json:"captcha"`
SessionMode int `json:"sessionMode"`
DeviceType string `json:"-"`
LoginPlatform string `json:"-"` //登录平台
}
func (orgAddCommand *LoginCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -40,6 +40,9 @@ func (svr AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{
if loginCommand.DeviceType == domain.DeviceTypeWeb {
loginPlatform = domain.LoginPlatformWeb
}
if len(loginCommand.LoginPlatform) != 0 {
loginPlatform = loginCommand.LoginPlatform
}
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
_, err = creationUserGateway.AuthRefreshIM(allied_creation_user.ReqAuthRefreshIM{
Phone: loginCommand.Phone,
... ... @@ -553,7 +556,7 @@ 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)
currentUser, err := svr.matchUserV2(&loginToken)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
... ... @@ -693,6 +696,99 @@ loopUser1:
return &currentUser, nil
}
func (svr AuthService) matchUserV2(loginToken *domain.LoginToken) (*allied_creation_user.UserDetail, error) {
var users []allied_creation_user.UserDetail
var currentUser *allied_creation_user.UserDetail
var defaultUser *allied_creation_user.UserDetail
var ok bool
var mapOrgUser = make(map[int]*allied_creation_user.UserDetail)
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
switch loginToken.Platform {
case domain.LoginPlatformWeb:
userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
Phone: loginToken.Account,
UserType: domain.UserTypeEmployee,
EnableStatus: domain.UserStatusEnable,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "用户信息获取失败,"+err.Error())
}
users = userSearchResult.Users
case domain.LoginPlatformApp:
userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
Phone: loginToken.Account,
UserType: domain.UserTypeEmployee | domain.UserTypeCooperation | domain.UserTypeVisitor,
EnableStatus: domain.UserStatusEnable,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "用户信息获取失败,"+err.Error())
}
users = userSearchResult.Users
case domain.LoginPlatformOperatorWeb:
userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
Phone: loginToken.Account,
UserType: domain.UserTypeOperationAdmin,
EnableStatus: domain.UserStatusEnable,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "用户信息获取失败,"+err.Error())
}
users = userSearchResult.Users
}
if len(users) == 0 {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "账号不存在")
}
for index, user := range users {
if user.Company != nil && user.Company.Status != domain.CompanyAuthenticated {
continue
}
for _, userOrg := range user.UserOrg {
mapOrgUser[userOrg.OrgID] = &users[index]
if defaultUser == nil {
defaultUser = &users[index]
}
}
}
if defaultUser == nil {
defaultUser = &users[0]
}
//切换组织
if loginToken.OrgId != 0 {
if currentUser, ok = mapOrgUser[int(loginToken.OrgId)]; !ok { //&& loginToken.Platform == domain.LoginPlatformWeb
return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用")
}
}
// 使用默认账号
if currentUser == nil {
currentUser = defaultUser
}
SetLoginToken(loginToken, currentUser)
return currentUser, nil
}
func SetLoginToken(loginToken *domain.LoginToken, currentUser *allied_creation_user.UserDetail) {
// 当前登录的公司用户信息
loginToken.UserId = int64(currentUser.UserId)
loginToken.UserBaseId = int64(currentUser.UserBaseId)
if currentUser.Company != nil {
loginToken.CompanyId = int64(currentUser.Company.CompanyId)
}
// 关联的组织
var orgIds []int64
for i := range currentUser.UserOrg {
orgIds = append(orgIds, int64(currentUser.UserOrg[i].OrgID))
// 默认组织
if loginToken.OrgId == 0 {
loginToken.OrgId = int64(currentUser.UserOrg[i].OrgID)
}
}
loginToken.OrgIds = orgIds
}
//GetCompanyOrgsByUser 获取登录用户的公司组织列表
func (svr AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUserQuery) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(queryParam.Operator)
... ... @@ -804,36 +900,49 @@ func (svr *AuthService) CaptchaInit(request *query.CaptchaInitRequest) (interfac
/*******************运营登录**********************/
//AuthLogin 用户登录
func (svr AuthService) AuthAdminLogin(loginCommand *command.LoginPwdCommand) (interface{}, error) {
if err := loginCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
if len(loginCommand.Username) == 0 {
return nil, application.ThrowError(application.BUSINESS_ERROR, "账号不存在")
}
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
users, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
UserType: domain.UserTypeOperationAdmin,
Phone: loginCommand.Username,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if len(users.Users) == 0 {
return nil, application.ThrowError(application.BUSINESS_ERROR, "账号不存在")
}
login := &command.LoginCommand{
GrantType: "signInPassword",
Phone: loginCommand.Username,
Password: loginCommand.Password,
SessionMode: loginCommand.SessionMode,
DeviceType: loginCommand.DeviceType,
}
if len(loginCommand.CaptchaChallenge) > 0 {
geetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
validateResult := geetest.SuccessValidate(loginCommand.CaptchaChallenge, loginCommand.CaptchaValidate, loginCommand.CaptchaSeccode, "", "")
if !validateResult {
log.Logger.Error("validate captcha fail")
}
LoginPlatform: domain.LoginPlatformOperatorWeb,
}
return svr.AuthLogin(login)
}
//AuthLogin 用户登录
//func (svr AuthService) AuthAdminLoginBak(loginCommand *command.LoginPwdCommand) (interface{}, error) {
// if err := loginCommand.ValidateCommand(); err != nil {
// return nil, application.ThrowError(application.ARG_ERROR, err.Error())
// }
// if len(loginCommand.Username) == 0 {
// return nil, application.ThrowError(application.BUSINESS_ERROR, "账号不存在")
// }
// creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
// users, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
// UserType: domain.UserTypeOperationAdmin,
// Phone: loginCommand.Username,
// })
// if err != nil {
// return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
// }
// if len(users.Users) == 0 {
// return nil, application.ThrowError(application.BUSINESS_ERROR, "账号不存在")
// }
// login := &command.LoginCommand{
// GrantType: "signInPassword",
// Phone: loginCommand.Username,
// Password: loginCommand.Password,
// SessionMode: loginCommand.SessionMode,
// DeviceType: loginCommand.DeviceType,
// }
// if len(loginCommand.CaptchaChallenge) > 0 {
// geetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
// validateResult := geetest.SuccessValidate(loginCommand.CaptchaChallenge, loginCommand.CaptchaValidate, loginCommand.CaptchaSeccode, "", "")
// if !validateResult {
// log.Logger.Error("validate captcha fail")
// }
// }
// return svr.AuthLogin(login)
//}
... ...
... ... @@ -7,8 +7,9 @@ import (
//登录的平台
const (
LoginPlatformApp string = "app"
LoginPlatformWeb string = "web"
LoginPlatformApp string = "app" //天联共创app
LoginPlatformWeb string = "web" //天联共创web
LoginPlatformOperatorWeb string = "operator-web" //天联共创运营-web
)
const (
... ...
... ... @@ -97,7 +97,7 @@ func (controller *BaseController) GetOperator() domain.Operator {
}
// 打印测试日志
// log.Logger.Debug("operator " + json.MarshalToString(op))
log.Logger.Debug("operator " + json.MarshalToString(op))
return op
}
... ...