pg_manager_auth_services.go
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
}