作者 yangfu

组织切换修改

@@ -338,7 +338,7 @@ func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.Refre @@ -338,7 +338,7 @@ func (srv AuthService) RefreshAuthAccessToken(refreshTokenCommand *command.Refre
338 // "accessToken": refreshTokenStr, 338 // "accessToken": refreshTokenStr,
339 // "expiresIn": currentAccess.AccessExpired - nowTime, 339 // "expiresIn": currentAccess.AccessExpired - nowTime,
340 // }, nil 340 // }, nil
341 - return token, err 341 + return token["token"], err
342 } 342 }
343 343
344 //GetUserMenus 获取用户信息 344 //GetUserMenus 获取用户信息
@@ -396,8 +396,43 @@ func (srv AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (inter @@ -396,8 +396,43 @@ func (srv AuthService) GetUserOrg(userOrgCommand *command.UserOrgCommand) (inter
396 396
397 //OrgSwitch 组织切换 397 //OrgSwitch 组织切换
398 func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (interface{}, error) { 398 func (srv AuthService) OrgSwitch(switchOrgCommand *command.SwitchOrgCommand) (interface{}, error) {
399 -  
400 - return nil, nil 399 + if err := switchOrgCommand.ValidateCommand(); err != nil {
  400 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  401 + }
  402 + ltoken := domain.LoginToken{}
  403 + err := ltoken.ParseToken(switchOrgCommand.Operator.Token)
  404 + if err != nil {
  405 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "accessToken 不可用,"+err.Error())
  406 + }
  407 + ltoken.OrgId = switchOrgCommand.OrgId
  408 + token, err := srv.getToken(domain.Operator{}, ltoken)
  409 + if err != nil {
  410 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  411 + }
  412 + var userId int64
  413 + if v, ok := token["userId"]; ok {
  414 + if userId, ok = v.(int64); !ok {
  415 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "用户不存在")
  416 + }
  417 + }
  418 + user, err := srv.getUserInfo(domain.Operator{UserId: userId})
  419 + if err != nil {
  420 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  421 + }
  422 + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
  423 + switchOrgCommand.Operator)
  424 + resultMenu, err := creationUserGateway.UserAccessMenus(allied_creation_user.ReqUserAccessMenus{
  425 + UserId: int(userId),
  426 + })
  427 + if err != nil {
  428 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  429 + }
  430 + var res = map[string]interface{}{
  431 + "user": user,
  432 + "accessMenus": resultMenu.Menus,
  433 + "token": token["token"],
  434 + }
  435 + return res, nil
401 } 436 }
402 437
403 // CompanySignUp 企业注册 438 // CompanySignUp 企业注册
@@ -477,7 +512,7 @@ func (srv AuthService) getUserInfo(operator domain.Operator) (interface{}, error @@ -477,7 +512,7 @@ func (srv AuthService) getUserInfo(operator domain.Operator) (interface{}, error
477 return user, nil 512 return user, nil
478 } 513 }
479 514
480 -func (srv AuthService) getToken(operator domain.Operator, ltoken domain.LoginToken) (interface{}, error) { 515 +func (srv AuthService) getToken(operator domain.Operator, ltoken domain.LoginToken) (map[string]interface{}, error) {
481 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) 516 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
482 userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{ 517 userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
483 Phone: ltoken.Account, 518 Phone: ltoken.Account,
@@ -487,16 +522,18 @@ func (srv AuthService) getToken(operator domain.Operator, ltoken domain.LoginTok @@ -487,16 +522,18 @@ func (srv AuthService) getToken(operator domain.Operator, ltoken domain.LoginTok
487 } 522 }
488 //判定当前凭证的companyId,OrganizationId 是否在用户列表中 523 //判定当前凭证的companyId,OrganizationId 是否在用户列表中
489 var currentOrgIsOK bool 524 var currentOrgIsOK bool
  525 + var currentUserId int64
490 loopUser1: 526 loopUser1:
491 for _, v := range userSearchResult.Users { 527 for _, v := range userSearchResult.Users {
492 - if v.Company.CompanyId == int(ltoken.CompanyId) { 528 + //if v.Company.CompanyId == int(ltoken.CompanyId) {
493 for _, vv := range v.UserOrg { 529 for _, vv := range v.UserOrg {
494 - if vv.OrgID == int(ltoken.OrgId) || vv.OrgID == int(operator.OrgId) { 530 + if vv.OrgID == int(ltoken.OrgId) {
495 currentOrgIsOK = true 531 currentOrgIsOK = true
  532 + currentUserId = int64(v.UserId)
496 break loopUser1 533 break loopUser1
497 } 534 }
498 } 535 }
499 - } 536 + //}
500 } 537 }
501 if !currentOrgIsOK { 538 if !currentOrgIsOK {
502 return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用") 539 return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用")
@@ -572,9 +609,13 @@ loopUser1: @@ -572,9 +609,13 @@ loopUser1:
572 tokenCache.SaveAccessToken(currentAccess) 609 tokenCache.SaveAccessToken(currentAccess)
573 tokenCache.SaveRefreshToken(currentAccess) 610 tokenCache.SaveRefreshToken(currentAccess)
574 nowTime := time.Now().Unix() 611 nowTime := time.Now().Unix()
575 - return map[string]interface{}{ 612 + token := map[string]interface{}{
576 "refreshToken": accessTokenStr, 613 "refreshToken": accessTokenStr,
577 "accessToken": refreshTokenStr, 614 "accessToken": refreshTokenStr,
578 "expiresIn": currentAccess.AccessExpired - nowTime, 615 "expiresIn": currentAccess.AccessExpired - nowTime,
  616 + }
  617 + return map[string]interface{}{
  618 + "token": token,
  619 + "userId": currentUserId,
579 }, nil 620 }, nil
580 } 621 }
@@ -7,4 +7,5 @@ type Operator struct { @@ -7,4 +7,5 @@ type Operator struct {
7 OrgId int64 `json:"orgId"` 7 OrgId int64 `json:"orgId"`
8 UserBaseId int64 `json:"userBaseId"` 8 UserBaseId int64 `json:"userBaseId"`
9 Phone string `json:"phone"` 9 Phone string `json:"phone"`
  10 + Token string `json:"token"`
10 } 11 }
@@ -33,7 +33,7 @@ func init() { @@ -33,7 +33,7 @@ func init() {
33 web.InsertFilter("/*", web.BeforeRouter, filters.AllowCors()) 33 web.InsertFilter("/*", web.BeforeRouter, filters.AllowCors())
34 web.InsertFilter("/*", web.BeforeExec, filters.CreateRequstLogFilter(log.Logger)) 34 web.InsertFilter("/*", web.BeforeExec, filters.CreateRequstLogFilter(log.Logger))
35 web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(log.Logger), web.WithReturnOnOutput(false)) 35 web.InsertFilter("/*", web.AfterExec, filters.CreateResponseLogFilter(log.Logger), web.WithReturnOnOutput(false))
36 - web.InsertFilter("/v1/app/*", web.BeforeExec, filters.SecureHandler( 36 + web.InsertFilter("/v1/app1/*", web.BeforeExec, filters.SecureHandler(
37 filters.WithEnableCheckTimestamp(false), 37 filters.WithEnableCheckTimestamp(false),
38 filters.WithOnInvalidRequest(func(ctx *context.Context) { 38 filters.WithOnInvalidRequest(func(ctx *context.Context) {
39 headerData, _ := json.Marshal(ctx.Input.Context.Request.Header) 39 headerData, _ := json.Marshal(ctx.Input.Context.Request.Header)
@@ -109,3 +109,16 @@ func (controller *AuthController) ResetPassword() { @@ -109,3 +109,16 @@ func (controller *AuthController) ResetPassword() {
109 data, err := authService.ResetPassword(userOrgCommand) 109 data, err := authService.ResetPassword(userOrgCommand)
110 controller.Response(data, err) 110 controller.Response(data, err)
111 } 111 }
  112 +
  113 +func (controller *AuthController) OrgSwitch() {
  114 + authService := service.AuthService{}
  115 + cmd := &command.SwitchOrgCommand{}
  116 + err := controller.Unmarshal(cmd)
  117 + if err != nil {
  118 + controller.Response(nil, err)
  119 + return
  120 + }
  121 + cmd.Operator = controller.GetOperator()
  122 + data, err := authService.OrgSwitch(cmd)
  123 + controller.Response(data, err)
  124 +}
@@ -46,5 +46,6 @@ func (controller *baseController) GetOperator() domain.Operator { @@ -46,5 +46,6 @@ func (controller *baseController) GetOperator() domain.Operator {
46 OrgId: loginToken.OrgId, 46 OrgId: loginToken.OrgId,
47 UserBaseId: loginToken.UserBaseId, 47 UserBaseId: loginToken.UserBaseId,
48 Phone: loginToken.Account, 48 Phone: loginToken.Account,
  49 + Token: token,
49 } 50 }
50 } 51 }
@@ -18,4 +18,5 @@ func init() { @@ -18,4 +18,5 @@ func init() {
18 18
19 web.Router("/v1/app/auth/company-sign-up", &mobile_client.AuthController{}, "Post:CompanySignUp") 19 web.Router("/v1/app/auth/company-sign-up", &mobile_client.AuthController{}, "Post:CompanySignUp")
20 web.Router("/v1/app/auth/reset-password", &mobile_client.AuthController{}, "Post:ResetPassword") 20 web.Router("/v1/app/auth/reset-password", &mobile_client.AuthController{}, "Post:ResetPassword")
  21 + web.Router("/v1/app/auth/org-switch", &mobile_client.AuthController{}, "Post:OrgSwitch")
21 } 22 }