Files
lone-services/services/user/internal/logic/rebindMobileLogic.go
T
zzw 3517713853
CI / changes (push) Successful in 1m57s
CI / ad (push) Successful in 1s
CI / admin (push) Successful in 0s
CI / bff (push) Successful in 26s
CI / chore (push) Successful in 26s
CI / equipment (push) Successful in 0s
CI / express (push) Successful in 0s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 0s
CI / order (push) Successful in 1s
CI / payment (push) Successful in 0s
CI / percutaneous (push) Successful in 1s
CI / product (push) Successful in 0s
CI / record (push) Successful in 1s
CI / sale (push) Successful in 0s
CI / support (push) Successful in 0s
CI / task (push) Successful in 0s
CI / user (push) Successful in 28s
CI / water (push) Successful in 0s
CI / wecom (push) Successful in 0s
feat: rebind mobile
2026-10-21 14:21:19 +08:00

94 lines
2.6 KiB
Go

package logic
import (
"context"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
user "lone-services/rpc/user/pb"
"lone-services/services/user/internal/dao"
"lone-services/services/user/internal/svc"
"lone-services/services/user/validator"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type RebindMobileLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewRebindMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RebindMobileLogic {
return &RebindMobileLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *RebindMobileLogic) RebindMobile(in *user.RebindMobileReq) (*user.Response, error) {
var req validator.RebindMobileValidator
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
return outResponse(utils.ErrorParams, msg), nil
}
if req.OldMobile == req.NewMobile {
return outResponse(utils.ErrorParams, "新手机号不能与原手机号相同"), nil
}
loginUser := utils.GetUserFromCtx(l.ctx)
if loginUser.ID < utils.NumberOne {
return failResponse(utils.ErrorNoLoginInfo), nil
}
row, err := loadEnabledUser(loginUser.ID)
if err != nil {
l.Errorf("rebind mobile load user: %v", err)
return failResponse(utils.ErrorNotFund), nil
}
if row.Mobile == utils.StringEmpty {
return outResponse(utils.ErrorParams, "用户未绑定手机号"), nil
}
oldEnc, encErr := utils.EncryptPhone(req.OldMobile)
if encErr != nil {
l.Errorf("rebind mobile encrypt old: %v", encErr)
return failResponse(utils.ErrorEncryptAesError), nil
}
if oldEnc != row.Mobile {
return outResponse(utils.ErrorParams, "原手机号不正确"), nil
}
ok, verifyErr := verifyAndConsumeSmsCode(l.ctx, dao.SmsSceneRebindMobile, req.NewMobile, req.SmsCode)
if verifyErr != nil {
l.Errorf("rebind mobile verify sms: %v", verifyErr)
return failResponse(utils.Fail), nil
}
if !ok {
return outResponse(utils.ErrorParams, "验证码错误或已过期"), nil
}
_, updateErr := NewInternalUpdateMobileLogic(l.ctx, l.svcCtx).InternalUpdateMobile(&user.InternalUpdateMobileReq{
UserId: loginUser.ID,
Mobile: req.NewMobile,
})
if updateErr != nil {
l.Errorf("rebind mobile update: %v", updateErr)
if st, ok := status.FromError(updateErr); ok {
switch st.Code() {
case codes.InvalidArgument:
return outResponse(utils.ErrorParams, st.Message()), nil
case codes.AlreadyExists:
return failResponse(utils.ErrorDataIsExist), nil
case codes.NotFound:
return failResponse(utils.ErrorNotFund), nil
}
}
return failResponse(utils.Fail), nil
}
return okResponse(nil), nil
}