pg_manager_auth_services.go 2.6 KB
package domain_service

import (
	"fmt"
	"github.com/tiptok/gocomm/xa/eda"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/repository"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/svr"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
)

var errUserNotFound = fmt.Errorf("用户不存在")

type PgManagerAuthService struct {
	eda.EventCenterPublisher
	transactionContext *transaction.TransactionContext
}

func NewPgManagerAuthService(ctx *transaction.TransactionContext) *PgManagerAuthService {
	return &PgManagerAuthService{
		transactionContext: ctx,
	}
}

func (s *PgManagerAuthService) ChangeUserPhone(userId int64, newPhone, oldPhone string) (err error) {
	var (
		ucenterApiGateway  = svr.NewHttplibUCenterApiServiceGateway()
		UsersRepository, _ = repository.NewUsersRepository(s.transactionContext)
		user               *domain.Users
	)
	if user, err = UsersRepository.FindOne(map[string]interface{}{"phone": oldPhone}); err != nil || user == nil {
		log.Error(err)
		err = errUserNotFound
		return
	}
	if newUser, e := UsersRepository.FindOne(map[string]interface{}{"phone": newPhone}); e == nil || newUser != nil {
		err = fmt.Errorf("当前手机号已存在,请重新输入")
		return
	}
	_, err = ucenterApiGateway.ChangePhone(newPhone, oldPhone)
	if err == nil {
		_, err = s.transactionContext.PgTx.Exec("update users set phone=? where phone=?", newPhone, oldPhone)
	}
	//event
	return
}
func (s *PgManagerAuthService) ChangeUserPassword(userId int64, newPwd, oldPwd, phone string) (err error) {
	var (
		ucenterApiGateway  = svr.NewHttplibUCenterApiServiceGateway()
		UsersRepository, _ = factory.CreateUsersRepository(s.transactionContext)
		user               *domain.Users
	)
	if user, err = UsersRepository.FindOne(map[string]interface{}{"phone": phone}); err != nil || user == nil {
		log.Error(err)
		err = fmt.Errorf("用户不存在")
		return
	}
	_, err = ucenterApiGateway.ChangePassword(phone, newPwd, oldPwd)
	return
}
func (s *PgManagerAuthService) ResetUserPassword(userId int64, phone, password string) (err error) {
	var (
		ucenterApiGateway  = svr.NewHttplibUCenterApiServiceGateway()
		UsersRepository, _ = factory.CreateUsersRepository(s.transactionContext)
		user               *domain.Users
	)
	if user, err = UsersRepository.FindOne(map[string]interface{}{"phone": phone}); err != nil || user == nil {
		log.Error(err)
		err = fmt.Errorf("用户不存在")
		return
	}
	_, err = ucenterApiGateway.UpdateUser(user.OpenId, phone, password)
	return
}