service.go 4.3 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 struct{}{}, nil
}

//ChangePassword 修改密码
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.OldPhone,
		NewPhone: resetPhoneCommand.NewPhone,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return struct{}{}, nil
}

//UpdateUserInfo 更新用户信息
func (srv UserService) UpdateUserInfo(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, "用户不存在")
	}
	_, err = creationUserGateway.UserUpdateBaseInfo(allied_creation_user.ReqUserUpdateBaseInfo{
		UserId:   int64(user.UserId),
		UserName: updateUserInfoCommand.UserName,
		Avatar:   updateUserInfoCommand.Avatar,
		Phone:    user.UserInfo.Phone,
		Email:    user.UserInfo.Email,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return struct{}{}, nil
}