service.go 5.4 KB
package service

import (
	"errors"

	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/cache"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/sms_serve"
)

type AuthService struct{}

//AuthLogin 用户登录
func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{}, error) {
	var (
		result interface{}
		err    error
	)
	switch loginCommand.GrantType {
	case "signInPassword":
		//账号密码登录
		result, err = srv.SignInPassword(loginCommand.Phone, loginCommand.Password)
	case "signInCaptcha":
		//手机验证码登录
		result, err = srv.SignInCaptcha(loginCommand.Phone, loginCommand.Captcha)
	default:
		err = errors.New("登录方式无法解析")
	}
	return result, err
}

//SignInPassword 使用账号密码校验
func (srv AuthService) SignInPassword(account string, password string) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	_, err := creationUserGateway.AuthCheckPassword(allied_creation_user.ReqAuthCheckPassword{
		Password: password,
		Phone:    account,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	ltoken := domain.LoginToken{
		UserId:    0,
		Account:   account,
		Platform:  domain.LoginPlatformApp,
		CompanyId: 0,
	}
	authcode, err := ltoken.GenerateAuthCode()
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	result := map[string]string{
		"authCode": authcode,
	}
	return result, nil
}

//SignInCaptcha 使用手机验证码登录
func (srv AuthService) SignInCaptcha(phone string, captcha string) (interface{}, error) {
	smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
	err := smsServeGateway.CheckSmsCode(phone, captcha)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	ltoken := domain.LoginToken{
		UserId:    0,
		Account:   phone,
		Platform:  domain.LoginPlatformApp,
		CompanyId: 0,
	}
	authcode, err := ltoken.GenerateAuthCode()
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	result := map[string]string{
		"authCode": authcode,
	}
	return result, nil
}

//GetCompanyOrgsByUser 获取登录用户的公司组织列表
func (srv AuthService) GetCompanyOrgsByUser(queryParam query.GetCompanyOrgsByUserQuery) (interface{}, error) {

	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(queryParam.Operator)
	result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
		Phone: queryParam.Phone,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	var (
		companys []dto.CompanyItem
		orgs     []dto.OrgItem
	)

	for _, v := range result.Users {
		companys = append(companys, dto.CompanyItem{
			CompanyId:   v.Company.CompanyId,
			CompanyName: v.Company.CompanyName,
		})
		for _, vv := range v.UserOrg {
			orgs = append(orgs, dto.OrgItem{
				OrganizationId:   vv.OrgID,
				OrganizationName: vv.OrgName,
				CompanyId:        v.Company.CompanyId,
			})
		}
	}

	data := map[string]interface{}{
		"companys":      companys,
		"organizations": orgs,
	}
	return data, nil
}

//GetQrcode 获取扫码登录需要的二维码
func (srv AuthService) GetQrcode() (interface{}, error) {
	qrmsg := domain.QrcodeMessage{}
	imgBase64, err := qrmsg.GenerateImageBase64()
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	qrCache := cache.LoginQrcodeCache{}
	err = qrCache.Save(qrmsg)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	data := map[string]interface{}{
		"image": imgBase64,
		"token": qrmsg.Token,
	}
	return data, nil
}

//QrcodeLoginStatus 询问扫码登录状态
func (srv AuthService) QrcodeLoginStatus(queryParam query.QrcodeLoginStatusQuery) (interface{}, error) {
	qrmsg := domain.QrcodeMessage{}
	err := qrmsg.ParseToken(queryParam.Token)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	qrCache := cache.LoginQrcodeCache{}
	qrmsgCache, err := qrCache.Get(qrmsg.Id)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	loginToken := domain.LoginToken{
		UserId:     qrmsgCache.UserId,
		UserBaseId: qrmsgCache.UserBaseId,
		Account:    qrmsgCache.Account,
		Platform:   domain.LoginPlatformWeb,
		CompanyId:  qrmsgCache.CompanyId,
		OrgId:      qrmsgCache.OrgId,
	}
	accessToken, err := loginToken.GenerateAccessToken()
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	_ = accessToken
	//TODO 填充token数据
	data := map[string]interface{}{
		"isLogin":     qrmsgCache.IsLogin,
		"accessToken": "",
	}
	return data, nil
}