feat: user get mobile
CI / changes (push) Successful in 1m30s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 1s
CI / bff (push) Successful in 26s
CI / chore (push) Successful in 1s
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 0s
CI / payment (push) Successful in 0s
CI / percutaneous (push) Successful in 0s
CI / product (push) Successful in 1s
CI / record (push) Successful in 0s
CI / sale (push) Successful in 0s
CI / support (push) Successful in 1s
CI / task (push) Successful in 0s
CI / user (push) Successful in 28s
CI / water (push) Successful in 1s
CI / wecom (push) Successful in 0s

This commit is contained in:
zzw
2026-10-20 10:02:19 +08:00
parent d43e698bc3
commit 19059e0a69
22 changed files with 675 additions and 486 deletions
@@ -0,0 +1,131 @@
package logic
import (
"context"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
analysispb "lone-services/rpc/analysis/pb"
user "lone-services/rpc/user/pb"
userconfig "lone-services/services/user/internal/config"
"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"
)
const clientCodeUserSkin = "user_skin"
type BindMobileLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewBindMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindMobileLogic {
return &BindMobileLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *BindMobileLogic) BindMobile(in *user.BindMobileReq) (*user.Response, error) {
loginUser := utils.GetUserFromCtx(l.ctx)
if loginUser.ID < utils.NumberOne {
return failResponse(utils.ErrorNoLoginInfo), nil
}
var req validator.BindMobileValidator
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
return outResponse(utils.ErrorParams, msg), nil
}
if loginUser.ClientCode == utils.StringEmpty {
return outResponse(utils.ErrorParams, "端编码不能为空"), nil
}
if _, err := loadEnabledClient(loginUser.ClientCode); err != nil {
l.Errorf("bind mobile load client: %v", err)
return failResponse(utils.Fail), nil
}
wxCfg, cfgErr := userconfig.LoadWechatConfig(loginUser.ClientCode)
if cfgErr != nil {
l.Errorf("bind mobile wechat config: %v", cfgErr)
return outResponse(utils.Fail, "微信配置缺失"), nil
}
token, tokenErr := wxCfg.NewAuthClient().AccessToken(l.ctx)
if tokenErr != nil {
l.Errorf("bind mobile access token: %v", tokenErr)
return failResponse(utils.Fail), nil
}
phone, phoneErr := wxCfg.NewAuthClient().GetPhoneNumber(token.AccessToken, req.Code)
if phoneErr != nil {
l.Errorf("bind mobile get phone: %v", phoneErr)
return failResponse(utils.Fail), nil
}
mobile := phone.PhoneNumber
if mobile == utils.StringEmpty {
mobile = phone.PurePhoneNumber
}
if mobile == utils.StringEmpty {
return failResponse(utils.Fail), nil
}
_, updateErr := NewInternalUpdateMobileLogic(l.ctx, l.svcCtx).InternalUpdateMobile(&user.InternalUpdateMobileReq{
UserId: loginUser.ID,
Mobile: mobile,
})
if updateErr != nil {
l.Errorf("bind 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
}
if loginUser.ClientCode == clientCodeUserSkin {
if err := l.markAnalysisRegistered(loginUser.ID, loginUser.ClientCode, loginUser.BizId); err != nil {
l.Errorf("bind mobile set registered: %v", err)
return failResponse(utils.Fail), nil
}
}
return okResponse(nil), nil
}
func (l *BindMobileLogic) markAnalysisRegistered(userId int64, clientCode string, bizId int64) error {
if bizId < utils.NumberOne {
if err := ensureClientBizUser(l.ctx, l.svcCtx, userId, clientCode); err != nil {
return err
}
loaded, err := loadUserClientBizId(userId, clientCode)
if err != nil {
return err
}
bizId = loaded
}
if bizId < utils.NumberOne {
return status.Error(codes.FailedPrecondition, "业务用户不存在")
}
cli, err := svc.GetRpcClient(l.svcCtx.AnalysisSvcName)
if err != nil {
return err
}
_, err = analysispb.NewAnalysisClient(cli.Conn()).InternalSetRegistered(l.ctx, &analysispb.InternalSetRegisteredReq{
Id: bizId,
})
return err
}