service.go 5.2 KB
package service

import (
	"github.com/google/uuid"
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/user/command"
	"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"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
)

type UserService struct {
}

//SendSmsCaptcha 发送验证码短信
func (srv UserService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand) error {
	smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
	err := smsServeGateway.SendSms(smsCodeCommand.Phone)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil
}

//CheckSmsCode 验证手机短信验证码
func (srv UserService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) (interface{}, error) {
	smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
	err := smsServeGateway.CheckSmsCode(smsCodeCommand.Phone, smsCodeCommand.SmsCode)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	uid := uuid.New()
	pcc := cache.PhoneCheckCache{}
	if err := pcc.Add(uid.String(), cache.PhoneCheckItem{
		Phone:           smsCodeCommand.Phone,
		SmsCodeIdentity: uid.String(),
		Action:          smsCodeCommand.Action,
	}); err != nil {
		log.Logger.Error(err.Error())
		return nil, application.ThrowError(application.BUSINESS_ERROR, "系统错误")
	}
	return map[string]interface{}{
		"smsCodeIdentity": uid.String(),
	}, nil
}

//ChangePassword 修改密码
func (srv UserService) ChangePassword(changePasswordCommand *command.ChangePasswordCommand) (interface{}, error) {
	smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
	err := smsServeGateway.CheckSmsCode(changePasswordCommand.Phone, changePasswordCommand.SmsCode)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	// 2.重置密码
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	_, err = creationUserGateway.AuthResetPassword(allied_creation_user.ReqAuthResetPassword{
		Phone:    changePasswordCommand.Phone,
		Password: changePasswordCommand.NewPassword,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return changePasswordCommand, nil
}

//ChangePhone 修改手机号
func (srv UserService) ChangePhone(resetPhoneCommand *command.ResetPhoneCommand) (interface{}, error) {
	smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
	err := smsServeGateway.CheckSmsCode(resetPhoneCommand.NewPhone, resetPhoneCommand.SmsCode)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	// 2.重置手机号
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	_, err = creationUserGateway.AuthResetPhone(allied_creation_user.ReqAuthResetPhone{
		UserId:   resetPhoneCommand.Operator.UserId,
		OldPhone: resetPhoneCommand.Operator.Phone,
		NewPhone: resetPhoneCommand.NewPhone,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return resetPhoneCommand, nil
}

//UpdateUserBaseInfo 更新用户信息
func (srv UserService) UpdateUserBaseInfo(updateUserInfoCommand *command.UpdateUserInfoCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	user, err := creationUserGateway.UserGet(allied_creation_user.ReqGetUser{
		UserId: int(updateUserInfoCommand.Operator.UserId),
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "用户不存在")
	}
	var (
		userName string
		avatar   string
	)
	if len(updateUserInfoCommand.Avatar) > 0 {
		avatar = updateUserInfoCommand.Avatar
	} else {
		avatar = user.UserInfo.Avatar
	}
	if len(updateUserInfoCommand.UserName) > 0 {
		userName = updateUserInfoCommand.UserName
	} else {
		userName = user.UserInfo.UserName
	}
	_, err = creationUserGateway.UserUpdateBaseInfo(allied_creation_user.ReqUserUpdateBaseInfo{
		UserId:   int64(user.UserId),
		UserName: userName,
		Avatar:   avatar,
		Phone:    user.UserInfo.Phone,
		Email:    user.UserInfo.Email,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return map[string]interface{}{
		"avatar":   avatar,
		"userName": userName,
	}, nil
}

// CompanySignUp 企业注册
func (srv UserService) DestroyAccount(destroyAccountCommand *command.DestroyAccountCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	result, err := creationUserGateway.AuthDestroyAccount(allied_creation_user.ReqAuthDestroyAccount{
		UserId: destroyAccountCommand.Operator.UserId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return result, err
}