审查视图

pkg/application/auth/service/auth.go 5.8 KB
庄敏学 authored
1 2
package service
庄敏学 authored
3 4 5 6
import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/auth/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
7
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
庄敏学 authored
8 9 10
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
庄敏学 authored
11 12
type AuthService struct {
}
庄敏学 authored
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

// Login PC端登录
func (service *AuthService) Login(loginCommand *command.LoginCommand) (interface{}, error) {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if errStart := transactionContext.StartTransaction(); errStart != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	//统一用户中心登录
	authCodeReply, err := factory.UCenterApi().AuthCode(loginCommand.Code)
	if err != nil || !authCodeReply.IsOk() {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统一用户中心认证失败")
	}
	//用户权限校验
	userAuthReply, err := factory.BusinessAdminApi().GetUserAuth(authCodeReply.Data.MUid, loginCommand.PlatformId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户鉴权失败")
	}
	if !userAuthReply.IsOk() {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, userAuthReply.Message())
	}
	//获取公司数据
	companyRepository := factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	company, err := companyRepository.FindOne(map[string]interface{}{
		"id": authCodeReply.Data.CompanyId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司数据失败")
	}
	userRepository := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	user, err := userRepository.FindOne(map[string]interface{}{
		"id":        authCodeReply.Data.MUid,
		"companyId": company.Id,
	})
	if err != nil {
57
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户数据失败")
庄敏学 authored
58 59 60 61 62
	}
	if user.Status != domain.UserStatusEnable {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户被禁用")
	}
	userAuth := &domain.UserAuth{
63 64 65 66 67 68 69
		UserId:      user.Id,
		CompanyId:   user.CompanyId,
		CompanyName: company.Name,
		Phone:       user.Account,
		PlatformId:  loginCommand.PlatformId,
		Name:        user.Name,
		AdminType:   user.AdminType,
庄敏学 authored
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	}
	accessToken, err := userAuth.CreateAccessToken()
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return map[string]interface{}{
		"access": map[string]interface{}{
			"accessToken": accessToken,
			"expiresIn":   domain.JWTExpiresSecond,
		},
	}, nil
}
tangxvhui authored
85
86 87
// 员工绩效 手机端登录,来源于能力展示app
func (service *AuthService) MobileLogin(param *command.MobileLoginCommand) (map[string]interface{}, error) {
tangxvhui authored
88 89 90 91 92 93 94 95 96 97
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if errStart := transactionContext.StartTransaction(); errStart != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, errStart.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
98 99 100 101 102 103 104
	// 统一用户中心登录
	authCodeReply, err := factory.UCenterApi().AppAuthCode(param.Credentials, param.Cuid, param.Cid)
	if err != nil || !authCodeReply.IsOk() {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统一用户中心认证失败")
	}
	// 用户权限校验
	// 登录平台ID,28-绩效管理后台  29-员工绩效
105
	userAuthReply, err := factory.BusinessAdminApi().GetUserAuth(int64(param.Muid), constant.IdPlatformUser)
106 107 108 109 110 111 112 113 114 115 116
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户鉴权失败")
	}
	if !userAuthReply.IsOk() {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, userAuthReply.Message())
	}
	//获取公司数据
	companyRepository := factory.CreateCompanyRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	company, err := companyRepository.FindOne(map[string]interface{}{
117
		"id": param.Cid,
118 119 120 121 122 123 124 125
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司数据失败")
	}
	userRepository := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	user, err := userRepository.FindOne(map[string]interface{}{
126
		"id":        param.Muid,
127 128 129 130 131 132 133 134 135
		"companyId": company.Id,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户数据失败")
	}
	if user.Status != domain.UserStatusEnable {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "用户被禁用")
	}
	userAuth := &domain.UserAuth{
136 137 138 139 140 141 142
		UserId:      user.Id,
		CompanyId:   user.CompanyId,
		CompanyName: company.Name,
		Phone:       user.Account,
		PlatformId:  constant.IdPlatformUser,
		Name:        user.Name,
		AdminType:   user.AdminType,
143 144 145 146 147
	}
	accessToken, err := userAuth.CreateAccessToken()
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
tangxvhui authored
148 149 150 151 152
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	result := map[string]interface{}{
		"access": map[string]interface{}{
153
			"accessToken": accessToken,
tangxvhui authored
154 155 156 157 158
			"expiresIn":   domain.JWTExpiresSecond,
		},
	}
	return result, nil
}