作者 yangfu

组织切换修改

... ... @@ -338,7 +338,7 @@ func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.Refre
// "accessToken": refreshTokenStr,
// "expiresIn": currentAccess.AccessExpired - nowTime,
// }, nil
return token, err
return token["token"], err
}
//GetUserMenus 获取用户信息
... ... @@ -396,8 +396,43 @@ func (srv AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (inter
//OrgSwitch 组织切换
func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (interface{}, error) {
return nil, nil
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 企业注册
... ... @@ -477,7 +512,7 @@ func (srv AuthService) getUserInfo(operator domain.Operator) (interface{}, error
return user, nil
}
func (srv AuthService) getToken(operator domain.Operator, ltoken domain.LoginToken) (interface{}, error) {
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,
... ... @@ -487,16 +522,18 @@ func (srv AuthService) getToken(operator domain.Operator, ltoken domain.LoginTok
}
//判定当前凭证的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) || vv.OrgID == int(operator.OrgId) {
currentOrgIsOK = true
break loopUser1
}
//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, "登录的公司组织不可用")
... ... @@ -572,9 +609,13 @@ loopUser1:
tokenCache.SaveAccessToken(currentAccess)
tokenCache.SaveRefreshToken(currentAccess)
nowTime := time.Now().Unix()
return map[string]interface{}{
token := map[string]interface{}{
"refreshToken": accessTokenStr,
"accessToken": refreshTokenStr,
"expiresIn": currentAccess.AccessExpired - nowTime,
}
return map[string]interface{}{
"token": token,
"userId": currentUserId,
}, nil
}
... ...
... ... @@ -7,4 +7,5 @@ type Operator struct {
OrgId int64 `json:"orgId"`
UserBaseId int64 `json:"userBaseId"`
Phone string `json:"phone"`
Token string `json:"token"`
}
... ...
... ... @@ -33,7 +33,7 @@ func init() {
web.InsertFilter("/*", web.BeforeRouter, filters.AllowCors())
web.InsertFilter("/*", web.BeforeExec, filters.CreateRequstLogFilter(log.Logger))
web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(log.Logger), web.WithReturnOnOutput(false))
web.InsertFilter("/v1/app/*", web.BeforeExec, filters.SecureHandler(
web.InsertFilter("/v1/app1/*", web.BeforeExec, filters.SecureHandler(
filters.WithEnableCheckTimestamp(false),
filters.WithOnInvalidRequest(func(ctx *context.Context) {
headerData, _ := json.Marshal(ctx.Input.Context.Request.Header)
... ...
... ... @@ -109,3 +109,16 @@ func (controller *AuthController) ResetPassword() {
data, err := authService.ResetPassword(userOrgCommand)
controller.Response(data, err)
}
func (controller *AuthController) OrgSwitch() {
authService := service.AuthService{}
cmd := &command.SwitchOrgCommand{}
err := controller.Unmarshal(cmd)
if err != nil {
controller.Response(nil, err)
return
}
cmd.Operator = controller.GetOperator()
data, err := authService.OrgSwitch(cmd)
controller.Response(data, err)
}
... ...
... ... @@ -46,5 +46,6 @@ func (controller *baseController) GetOperator() domain.Operator {
OrgId: loginToken.OrgId,
UserBaseId: loginToken.UserBaseId,
Phone: loginToken.Account,
Token: token,
}
}
... ...
... ... @@ -18,4 +18,5 @@ func init() {
web.Router("/v1/app/auth/company-sign-up", &mobile_client.AuthController{}, "Post:CompanySignUp")
web.Router("/v1/app/auth/reset-password", &mobile_client.AuthController{}, "Post:ResetPassword")
web.Router("/v1/app/auth/org-switch", &mobile_client.AuthController{}, "Post:OrgSwitch")
}
... ...