作者 yangfu

修改:密码修改/密码重置 /修改手机号

... ... @@ -52,6 +52,9 @@ func Partners(header *protocol.RequestHeader, request *protocolx.PartnersRequest
log.Error(err)
return
}
if rsp.Partners == nil {
rsp.Partners = []struct{}{}
}
err = transactionContext.CommitTransaction()
return
}
... ...
... ... @@ -7,6 +7,8 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/service"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
domain_service_i "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain/service"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/domain_service"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/svr"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
... ... @@ -129,9 +131,7 @@ func CheckSmsCode(header *protocol.RequestHeader, request *protocol.CheckSmsCode
func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRequest) (rsp *protocol.ChangePhoneResponse, err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
PartnerInfoDao, _ = factory.CreatePartnerInfoDao(transactionContext)
PartnerInfoService, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
partnerInfo *domain.PartnerInfo
UserAuthService = CreateUserAuthService(header.AdminType, transactionContext)
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
... ... @@ -139,32 +139,10 @@ func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRe
defer func() {
transactionContext.RollbackTransaction()
}()
// 管理员不支持修改手机号
if header.AdminType == int(protocolx.AdminTypeManager) {
//if err=changeUserPhone(header.UserId,request.Phone,transactionContext);err!=nil{
// err = protocol.NewCustomMessage(1,err.Error())
//}
return
}
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
//验证新手机是否已经存在
if _, e := PartnerInfoService.FindOne(map[string]interface{}{"account": request.Phone}); e == nil {
err = protocol.NewErrWithMessage(2029, err) //账号已存在
return
}
if _, err = auth.CheckSmsCode(request.Phone, request.Captcha); err != nil {
log.Error(err)
return
}
if err = PartnerInfoDao.Update(map[string]interface{}{
"Id": partnerInfo.Id,
"Account": request.Phone,
}); err != nil {
if err = UserAuthService.ChangeUserPhone(header.UserId, request.Phone, request.OldPhone); err != nil {
return
}
//TODO:修改完手机号 ,token失效,清理缓存
err = transactionContext.CommitTransaction()
return
}
... ... @@ -172,10 +150,8 @@ func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRe
//重置密码
func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswordRequest) (rsp *protocol.ResetPasswordResponse, err error) {
var (
partnerInfo *domain.PartnerInfo
transactionContext, _ = factory.CreateTransactionContext(nil)
PartnerInfoDao, _ = factory.CreatePartnerInfoDao(transactionContext)
PartnerInfoService, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
UserAuthService = CreateUserAuthService(header.AdminType, transactionContext)
)
rsp = &protocol.ResetPasswordResponse{}
if err = transactionContext.StartTransaction(); err != nil {
... ... @@ -184,40 +160,9 @@ func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswo
defer func() {
transactionContext.RollbackTransaction()
}()
if len(request.NewPwd) < 6 {
err = protocol.NewErrWithMessage(2027)
return
}
if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
err = protocol.NewErrWithMessage(2026)
return
}
if header.AdminType == int(protocolx.AdminTypeManager) {
if err = changeUserInfo(header.UserId, fmt.Sprintf("%v", header.SimNum), request.NewPwd, transactionContext); err != nil {
err = protocol.NewCustomMessage(1, err.Error())
}
if err = UserAuthService.ResetUserPassword(header.UserId, fmt.Sprintf("%v", header.SimNum), request.NewPwd); err != nil {
return
}
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if err = PartnerInfoDao.Update(map[string]interface{}{
"Id": partnerInfo.Id,
"Password": request.ConfirmPwd,
}); err != nil {
return
}
//清除auth token
//auth := userAuth.NewRedisUserAuth(userAuth.WithUserId(header.UserId))
//if !auth.Exist() {
// return
//}
//if err = auth.RemoveAuth(); err != nil {
// log.Error(err)
// err = protocol.NewErrWithMessage(4140, err)
// return
//}
err = transactionContext.CommitTransaction()
return
}
... ... @@ -225,10 +170,8 @@ func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswo
//修改密码
func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePasswordRequest) (rsp *protocol.ChangePasswordResponse, err error) {
var (
partnerInfo *domain.PartnerInfo
transactionContext, _ = factory.CreateTransactionContext(nil)
PartnerInfoDao, _ = factory.CreatePartnerInfoDao(transactionContext)
PartnerInfoService, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
UserAuthService = CreateUserAuthService(header.AdminType, transactionContext)
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
... ... @@ -237,49 +180,10 @@ func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePass
transactionContext.RollbackTransaction()
}()
rsp = &protocol.ChangePasswordResponse{}
if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
err = protocol.NewErrWithMessage(2026)
if err = UserAuthService.ChangeUserPassword(header.UserId, request.NewPwd, request.OldPwd, fmt.Sprintf("%v", header.SimNum)); err != nil {
err = protocol.NewCustomMessage(1, err.Error())
return
}
if header.AdminType == int(protocolx.AdminTypeManager) {
if err = changeUserPassword(header.UserId, request.NewPwd, request.OldPwd, fmt.Sprintf("%v", header.SimNum), transactionContext); err != nil {
err = protocol.NewCustomMessage(1, err.Error())
}
return
}
if strings.EqualFold(request.NewPwd, request.OldPwd) {
err = protocol.NewErrWithMessage(2030)
return
}
if len(request.NewPwd) < 6 {
err = protocol.NewErrWithMessage(2027)
return
}
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if !strings.EqualFold(partnerInfo.Password, request.OldPwd) {
//密码不一致
err = protocol.NewErrWithMessage(2028, err) //账号不存在
return
}
if err = PartnerInfoDao.Update(map[string]interface{}{
"Id": partnerInfo.Id,
"Password": request.ConfirmPwd,
}); err != nil {
return
}
//清除auth token
//auth := userAuth.NewRedisUserAuth(userAuth.WithUserId(header.UserId))
//if !auth.Exist() {
// return
//}
//if err = auth.RemoveAuth(); err != nil {
// log.Error(err)
// err = protocol.NewErrWithMessage(4140, err)
// return
//}
err = transactionContext.CommitTransaction()
return
}
... ... @@ -401,10 +305,10 @@ func changeUserInfo(userId int64, phone, password string, transactionContext *tr
err = fmt.Errorf("用户不存在")
return
}
_, err = ucenterApiGateway.UpdateUserPassword(user.OpenId, phone, password)
_, err = ucenterApiGateway.UpdateUser(user.OpenId, phone, password)
return
}
func changeUserPhone(userId int64, phone string, transactionContext *transaction.TransactionContext) (err error) {
func changeUserPhone(userId int64, phone, oldPhone string, transactionContext *transaction.TransactionContext) (err error) {
var (
ucenterApiGateway = svr.NewHttplibUCenterApiServiceGateway()
UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
... ... @@ -419,10 +323,10 @@ func changeUserPhone(userId int64, phone string, transactionContext *transaction
err = protocol.NewErrWithMessage(2029, err)
return
}
_, err = ucenterApiGateway.UpdateUserPassword(user.OpenId, phone, "")
if err != nil {
_, err = transactionContext.PgTx.Exec("update users set phone=? where id=?", phone, userId)
}
_, err = ucenterApiGateway.ChangePhone(phone, oldPhone)
//if err != nil {
// _, err = transactionContext.PgTx.Exec("update users set phone=? where id=?", phone, userId)
//}
return
}
func changeUserPassword(userId int64, newPwd, oldPwd, phone string, transactionContext *transaction.TransactionContext) (err error) {
... ... @@ -439,3 +343,22 @@ func changeUserPassword(userId int64, newPwd, oldPwd, phone string, transactionC
_, err = ucenterApiGateway.ChangePassword(phone, newPwd, oldPwd)
return
}
func CreateUserAuthService(adminType int, transactionContext *transaction.TransactionContext) domain_service_i.UserAuthService {
if adminType == int(protocolx.AdminTypeManager) {
return domain_service.NewPgManagerAuthService(transactionContext)
}
return domain_service.NewPgPartnerAuthService(transactionContext)
}
//TODO:修改密码/重置密码 清除uer_auth 缓存
//清除auth token
//auth := userAuth.NewRedisUserAuth(userAuth.WithUserId(header.UserId))
//if !auth.Exist() {
// return
//}
//if err = auth.RemoveAuth(); err != nil {
// log.Error(err)
// err = protocol.NewErrWithMessage(4140, err)
// return
//}
... ...
package service
type UserAuthService interface {
ChangeUserPhone(userId int64, newPhone, oldPhone string) (err error)
ChangeUserPassword(userId int64, newPwd, oldPwd, phone string) (err error)
ResetUserPassword(userId int64, phone, password string) (err error)
}
... ...
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"
)
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{}{"id": userId}); err != nil || user == nil {
log.Error(err)
err = fmt.Errorf("用户不存在")
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{}{"id": userId}); 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{}{"id": userId}); err != nil || user == nil {
log.Error(err)
err = fmt.Errorf("用户不存在")
return
}
_, err = ucenterApiGateway.UpdateUser(user.OpenId, phone, password)
return
}
... ...
package domain_service
import (
"github.com/tiptok/gocomm/xa/eda"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/dao"
"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/protocol"
"strings"
)
type PgPartnerAuthService struct {
eda.EventCenterPublisher
transactionContext *transaction.TransactionContext
}
func NewPgPartnerAuthService(ctx *transaction.TransactionContext) *PgPartnerAuthService {
return &PgPartnerAuthService{
transactionContext: ctx,
}
}
func (s *PgPartnerAuthService) ChangeUserPhone(userId int64, newPhone, oldPhone string) (err error) {
var (
PartnerInfoDao, _ = dao.NewPartnerInfoDao(s.transactionContext)
PartnerInfoService, _ = repository.NewPartnerInfoRepository(s.transactionContext)
partnerInfo *domain.PartnerInfo
)
//验证新手机是否已经存在
if _, e := PartnerInfoService.FindOne(map[string]interface{}{"account": newPhone}); e == nil {
err = protocol.NewErrWithMessage(2029, err) //账号已存在
return
}
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": userId}); err != nil || (partnerInfo.Account != oldPhone) {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if err = PartnerInfoDao.Update(map[string]interface{}{
"Id": userId,
"Account": newPhone,
}); err != nil {
return
}
return
}
func (s *PgPartnerAuthService) ChangeUserPassword(userId int64, newPwd, oldPwd, phone string) (err error) {
var (
partnerInfo *domain.PartnerInfo
PartnerInfoDao, _ = dao.NewPartnerInfoDao(s.transactionContext)
PartnerInfoService, _ = repository.NewPartnerInfoRepository(s.transactionContext)
)
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": userId}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if !strings.EqualFold(partnerInfo.Password, oldPwd) {
//密码不一致
err = protocol.NewErrWithMessage(2028, err) //账号不存在
return
}
if err = PartnerInfoDao.Update(map[string]interface{}{
"Id": partnerInfo.Id,
"Password": newPwd,
}); err != nil {
return
}
return
}
func (s *PgPartnerAuthService) ResetUserPassword(userId int64, phone, password string) (err error) {
var (
partnerInfo *domain.PartnerInfo
PartnerInfoDao, _ = dao.NewPartnerInfoDao(s.transactionContext)
PartnerInfoService, _ = repository.NewPartnerInfoRepository(s.transactionContext)
)
if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": userId}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
if err = PartnerInfoDao.Update(map[string]interface{}{
"Id": partnerInfo.Id,
"Password": password,
}); err != nil {
return
}
return
}
... ...
... ... @@ -43,7 +43,7 @@ func (serviceGateway *HttplibUCenterApiServiceGateway) ServerLogin(phone, passwo
}
// 修改密码
func (serviceGateway *HttplibUCenterApiServiceGateway) UpdateUserPassword(uid int64, phone, password string) (int, error) {
func (serviceGateway *HttplibUCenterApiServiceGateway) UpdateUser(uid int64, phone, password string) (int, error) {
url := strings.Join([]string{serviceGateway.baseURL, "users", fmt.Sprintf("%v", uid)}, "/")
request := serviceGateway.createRequest(url, "put")
//request.Header("appKey", constant.UCENTER_APP_KEY)
... ... @@ -81,6 +81,21 @@ func (serviceGateway *HttplibUCenterApiServiceGateway) ChangePassword(phone, new
}
return serviceGateway.handlerError(response)
}
func (serviceGateway *HttplibUCenterApiServiceGateway) ChangePhone(newPhone, oldPhone string) (int, error) {
url := strings.Join([]string{serviceGateway.baseURL, "user", "change-phone"}, "/")
request := serviceGateway.createRequest(url, "post")
options := make(map[string]interface{})
options["phone"] = strings.TrimSpace(newPhone)
options["oldPhone"] = strings.TrimSpace(oldPhone)
request.JSONBody(options)
response := make(map[string]interface{})
err := request.ToJSON(&response)
if err != nil {
log.Error("Service Gateway Fail:", err)
return 0, err
}
return serviceGateway.handlerError(response)
}
func (serviceGateway *HttplibUCenterApiServiceGateway) handlerError(in map[string]interface{}) (int, error) {
var rspCode int
... ...
... ... @@ -67,19 +67,19 @@ func (this *UserController) ChangePhone() {
msg = m
return
}
//if !CacheSms.IsExist(request.OldPhone) {
// msg = protocol.NewMesage(1009) //验证码失效
// return
//}
//if v := CacheSms.Get(request.OldPhone); v != nil {
// if !strings.EqualFold(fmt.Sprintf("%v", v), strings.TrimSpace(request.CaptchaCertificate)) {
// msg = protocol.NewMesage(1012) //验证码不一致
// return
// }
//} else {
// msg = protocol.NewMesage(1009) //验证码不一致
// return
//}
if !CacheSms.IsExist(request.OldPhone) {
msg = protocol.NewMesage(1009) //验证码失效
return
}
if v := CacheSms.Get(request.OldPhone); v != nil {
if !strings.EqualFold(fmt.Sprintf("%v", v), strings.TrimSpace(request.CaptchaCertificate)) {
msg = protocol.NewMesage(1012) //验证码不一致
return
}
} else {
msg = protocol.NewMesage(1009) //验证码不一致
return
}
header := this.GetRequestHeader(this.Ctx)
data, err := user.ChangePhone(header, request)
if err == nil {
... ... @@ -105,6 +105,15 @@ func (this *UserController) ResetPassword() {
msg = m
return
}
if len(request.NewPwd) < 6 {
msg = protocol.BadRequestParam(2027)
return
}
if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
msg = protocol.BadRequestParam(2026)
return
}
if !CacheSms.IsExist(request.Phone) {
msg = protocol.NewMesage(1009) //验证码失效
return
... ... @@ -118,6 +127,7 @@ func (this *UserController) ResetPassword() {
msg = protocol.NewMesage(1013) //验证码不一致
return
}
header := this.GetRequestHeader(this.Ctx)
data, err := user.ResetPassword(header, request)
if err == nil {
... ... @@ -139,10 +149,24 @@ func (this *UserController) ChangePassword() {
msg = protocol.BadRequestParam(1)
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
if strings.EqualFold(request.NewPwd, request.OldPwd) {
msg = protocol.BadRequestParam(2030)
return
}
if len(request.NewPwd) < 6 {
msg = protocol.BadRequestParam(2027)
return
}
if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
msg = protocol.BadRequestParam(2026)
return
}
header := this.GetRequestHeader(this.Ctx)
data, err := user.ChangePassword(header, request)
if err == nil {
... ...