作者 yangfu

平台登录重构

@@ -7,12 +7,13 @@ import ( @@ -7,12 +7,13 @@ import (
7 ) 7 )
8 8
9 type LoginCommand struct { 9 type LoginCommand struct {
10 - Phone string `json:"phone" valid:"Required"`  
11 - GrantType string `json:"grantType" valid:"Required"` //登录方式(signInPassword 密码登录、signInCaptcha 验证码登录)  
12 - Password string `json:"password"`  
13 - Captcha string `json:"captcha"`  
14 - SessionMode int `json:"sessionMode"`  
15 - DeviceType string `json:"-"` 10 + Phone string `json:"phone" valid:"Required"`
  11 + GrantType string `json:"grantType" valid:"Required"` //登录方式(signInPassword 密码登录、signInCaptcha 验证码登录)
  12 + Password string `json:"password"`
  13 + Captcha string `json:"captcha"`
  14 + SessionMode int `json:"sessionMode"`
  15 + DeviceType string `json:"-"`
  16 + LoginPlatform string `json:"-"` //登录平台
16 } 17 }
17 18
18 func (orgAddCommand *LoginCommand) Valid(validation *validation.Validation) { 19 func (orgAddCommand *LoginCommand) Valid(validation *validation.Validation) {
@@ -40,6 +40,9 @@ func (svr AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{ @@ -40,6 +40,9 @@ func (svr AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{
40 if loginCommand.DeviceType == domain.DeviceTypeWeb { 40 if loginCommand.DeviceType == domain.DeviceTypeWeb {
41 loginPlatform = domain.LoginPlatformWeb 41 loginPlatform = domain.LoginPlatformWeb
42 } 42 }
  43 + if len(loginCommand.LoginPlatform) != 0 {
  44 + loginPlatform = loginCommand.LoginPlatform
  45 + }
43 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{}) 46 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
44 _, err = creationUserGateway.AuthRefreshIM(allied_creation_user.ReqAuthRefreshIM{ 47 _, err = creationUserGateway.AuthRefreshIM(allied_creation_user.ReqAuthRefreshIM{
45 Phone: loginCommand.Phone, 48 Phone: loginCommand.Phone,
@@ -553,7 +556,7 @@ func (svr AuthService) getUserInfo(operator domain.Operator) (interface{}, error @@ -553,7 +556,7 @@ func (svr AuthService) getUserInfo(operator domain.Operator) (interface{}, error
553 556
554 func (svr AuthService) getToken(loginToken domain.LoginToken) (map[string]interface{}, error) { 557 func (svr AuthService) getToken(loginToken domain.LoginToken) (map[string]interface{}, error) {
555 // 1.匹配账号对应的用户 558 // 1.匹配账号对应的用户
556 - currentUser, err := svr.matchUser(&loginToken) 559 + currentUser, err := svr.matchUserV2(&loginToken)
557 if err != nil { 560 if err != nil {
558 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error()) 561 return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
559 } 562 }
@@ -693,6 +696,99 @@ loopUser1: @@ -693,6 +696,99 @@ loopUser1:
693 return &currentUser, nil 696 return &currentUser, nil
694 } 697 }
695 698
  699 +func (svr AuthService) matchUserV2(loginToken *domain.LoginToken) (*allied_creation_user.UserDetail, error) {
  700 + var users []allied_creation_user.UserDetail
  701 + var currentUser *allied_creation_user.UserDetail
  702 + var defaultUser *allied_creation_user.UserDetail
  703 + var ok bool
  704 + var mapOrgUser = make(map[int]*allied_creation_user.UserDetail)
  705 + creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
  706 + switch loginToken.Platform {
  707 + case domain.LoginPlatformWeb:
  708 + userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
  709 + Phone: loginToken.Account,
  710 + UserType: domain.UserTypeEmployee,
  711 + EnableStatus: domain.UserStatusEnable,
  712 + })
  713 + if err != nil {
  714 + return nil, application.ThrowError(application.BUSINESS_ERROR, "用户信息获取失败,"+err.Error())
  715 + }
  716 + users = userSearchResult.Users
  717 + case domain.LoginPlatformApp:
  718 + userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
  719 + Phone: loginToken.Account,
  720 + UserType: domain.UserTypeEmployee | domain.UserTypeCooperation | domain.UserTypeVisitor,
  721 + EnableStatus: domain.UserStatusEnable,
  722 + })
  723 + if err != nil {
  724 + return nil, application.ThrowError(application.BUSINESS_ERROR, "用户信息获取失败,"+err.Error())
  725 + }
  726 + users = userSearchResult.Users
  727 + case domain.LoginPlatformOperatorWeb:
  728 + userSearchResult, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
  729 + Phone: loginToken.Account,
  730 + UserType: domain.UserTypeOperationAdmin,
  731 + EnableStatus: domain.UserStatusEnable,
  732 + })
  733 + if err != nil {
  734 + return nil, application.ThrowError(application.BUSINESS_ERROR, "用户信息获取失败,"+err.Error())
  735 + }
  736 + users = userSearchResult.Users
  737 + }
  738 + if len(users) == 0 {
  739 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "账号不存在")
  740 + }
  741 +
  742 + for index, user := range users {
  743 + if user.Company != nil && user.Company.Status != domain.CompanyAuthenticated {
  744 + continue
  745 + }
  746 + for _, userOrg := range user.UserOrg {
  747 + mapOrgUser[userOrg.OrgID] = &users[index]
  748 + if defaultUser == nil {
  749 + defaultUser = &users[index]
  750 + }
  751 + }
  752 + }
  753 + if defaultUser == nil {
  754 + defaultUser = &users[0]
  755 + }
  756 + //切换组织
  757 + if loginToken.OrgId != 0 {
  758 + if currentUser, ok = mapOrgUser[int(loginToken.OrgId)]; !ok { //&& loginToken.Platform == domain.LoginPlatformWeb
  759 + return nil, application.ThrowError(application.TRANSACTION_ERROR, "登录的公司组织不可用")
  760 + }
  761 + }
  762 + // 使用默认账号
  763 + if currentUser == nil {
  764 + currentUser = defaultUser
  765 + }
  766 +
  767 + SetLoginToken(loginToken, currentUser)
  768 +
  769 + return currentUser, nil
  770 +}
  771 +
  772 +func SetLoginToken(loginToken *domain.LoginToken, currentUser *allied_creation_user.UserDetail) {
  773 + // 当前登录的公司用户信息
  774 + loginToken.UserId = int64(currentUser.UserId)
  775 + loginToken.UserBaseId = int64(currentUser.UserBaseId)
  776 + if currentUser.Company != nil {
  777 + loginToken.CompanyId = int64(currentUser.Company.CompanyId)
  778 + }
  779 +
  780 + // 关联的组织
  781 + var orgIds []int64
  782 + for i := range currentUser.UserOrg {
  783 + orgIds = append(orgIds, int64(currentUser.UserOrg[i].OrgID))
  784 + // 默认组织
  785 + if loginToken.OrgId == 0 {
  786 + loginToken.OrgId = int64(currentUser.UserOrg[i].OrgID)
  787 + }
  788 + }
  789 + loginToken.OrgIds = orgIds
  790 +}
  791 +
696 //GetCompanyOrgsByUser 获取登录用户的公司组织列表 792 //GetCompanyOrgsByUser 获取登录用户的公司组织列表
697 func (svr AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUserQuery) (interface{}, error) { 793 func (svr AuthService) GetCompanyOrgsByUser(queryParam *query.GetCompanyOrgsByUserQuery) (interface{}, error) {
698 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(queryParam.Operator) 794 creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(queryParam.Operator)
@@ -804,36 +900,49 @@ func (svr *AuthService) CaptchaInit(request *query.CaptchaInitRequest) (interfac @@ -804,36 +900,49 @@ func (svr *AuthService) CaptchaInit(request *query.CaptchaInitRequest) (interfac
804 /*******************运营登录**********************/ 900 /*******************运营登录**********************/
805 //AuthLogin 用户登录 901 //AuthLogin 用户登录
806 func (svr AuthService) AuthAdminLogin(loginCommand *command.LoginPwdCommand) (interface{}, error) { 902 func (svr AuthService) AuthAdminLogin(loginCommand *command.LoginPwdCommand) (interface{}, error) {
807 - if err := loginCommand.ValidateCommand(); err != nil {  
808 - return nil, application.ThrowError(application.ARG_ERROR, err.Error())  
809 - }  
810 - if len(loginCommand.Username) == 0 {  
811 - return nil, application.ThrowError(application.BUSINESS_ERROR, "账号不存在")  
812 - }  
813 - creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})  
814 - users, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{  
815 - UserType: domain.UserTypeOperationAdmin,  
816 - Phone: loginCommand.Username,  
817 - })  
818 - if err != nil {  
819 - return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())  
820 - }  
821 - if len(users.Users) == 0 {  
822 - return nil, application.ThrowError(application.BUSINESS_ERROR, "账号不存在")  
823 - }  
824 login := &command.LoginCommand{ 903 login := &command.LoginCommand{
825 - GrantType: "signInPassword",  
826 - Phone: loginCommand.Username,  
827 - Password: loginCommand.Password,  
828 - SessionMode: loginCommand.SessionMode,  
829 - DeviceType: loginCommand.DeviceType,  
830 - }  
831 - if len(loginCommand.CaptchaChallenge) > 0 {  
832 - geetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)  
833 - validateResult := geetest.SuccessValidate(loginCommand.CaptchaChallenge, loginCommand.CaptchaValidate, loginCommand.CaptchaSeccode, "", "")  
834 - if !validateResult {  
835 - log.Logger.Error("validate captcha fail")  
836 - } 904 + GrantType: "signInPassword",
  905 + Phone: loginCommand.Username,
  906 + Password: loginCommand.Password,
  907 + SessionMode: loginCommand.SessionMode,
  908 + DeviceType: loginCommand.DeviceType,
  909 + LoginPlatform: domain.LoginPlatformOperatorWeb,
837 } 910 }
838 return svr.AuthLogin(login) 911 return svr.AuthLogin(login)
839 } 912 }
  913 +
  914 +//AuthLogin 用户登录
  915 +//func (svr AuthService) AuthAdminLoginBak(loginCommand *command.LoginPwdCommand) (interface{}, error) {
  916 +// if err := loginCommand.ValidateCommand(); err != nil {
  917 +// return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  918 +// }
  919 +// if len(loginCommand.Username) == 0 {
  920 +// return nil, application.ThrowError(application.BUSINESS_ERROR, "账号不存在")
  921 +// }
  922 +// creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
  923 +// users, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
  924 +// UserType: domain.UserTypeOperationAdmin,
  925 +// Phone: loginCommand.Username,
  926 +// })
  927 +// if err != nil {
  928 +// return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  929 +// }
  930 +// if len(users.Users) == 0 {
  931 +// return nil, application.ThrowError(application.BUSINESS_ERROR, "账号不存在")
  932 +// }
  933 +// login := &command.LoginCommand{
  934 +// GrantType: "signInPassword",
  935 +// Phone: loginCommand.Username,
  936 +// Password: loginCommand.Password,
  937 +// SessionMode: loginCommand.SessionMode,
  938 +// DeviceType: loginCommand.DeviceType,
  939 +// }
  940 +// if len(loginCommand.CaptchaChallenge) > 0 {
  941 +// geetest := geetest.NewGeetestLib(captchaID, privateKey, 2*time.Second)
  942 +// validateResult := geetest.SuccessValidate(loginCommand.CaptchaChallenge, loginCommand.CaptchaValidate, loginCommand.CaptchaSeccode, "", "")
  943 +// if !validateResult {
  944 +// log.Logger.Error("validate captcha fail")
  945 +// }
  946 +// }
  947 +// return svr.AuthLogin(login)
  948 +//}
@@ -7,8 +7,9 @@ import ( @@ -7,8 +7,9 @@ import (
7 7
8 //登录的平台 8 //登录的平台
9 const ( 9 const (
10 - LoginPlatformApp string = "app"  
11 - LoginPlatformWeb string = "web" 10 + LoginPlatformApp string = "app" //天联共创app
  11 + LoginPlatformWeb string = "web" //天联共创web
  12 + LoginPlatformOperatorWeb string = "operator-web" //天联共创运营-web
12 ) 13 )
13 14
14 const ( 15 const (
@@ -97,7 +97,7 @@ func (controller *BaseController) GetOperator() domain.Operator { @@ -97,7 +97,7 @@ func (controller *BaseController) GetOperator() domain.Operator {
97 } 97 }
98 98
99 // 打印测试日志 99 // 打印测试日志
100 - // log.Logger.Debug("operator " + json.MarshalToString(op)) 100 + log.Logger.Debug("operator " + json.MarshalToString(op))
101 return op 101 return op
102 } 102 }
103 103