Files
zzw b77a1f1b95
CI / changes (push) Successful in 1m31s
CI / ad (push) Successful in 32s
CI / admin (push) Successful in 26s
CI / analysis (push) Successful in 28s
CI / bff (push) Successful in 23s
CI / chore (push) Successful in 24s
CI / equipment (push) Successful in 21s
CI / express (push) Successful in 21s
CI / factory (push) Successful in 18s
CI / message (push) Successful in 18s
CI / order (push) Successful in 21s
CI / payment (push) Successful in 20s
CI / percutaneous (push) Successful in 19s
CI / product (push) Successful in 21s
CI / record (push) Successful in 21s
CI / sale (push) Successful in 20s
CI / store (push) Successful in 25s
CI / support (push) Successful in 21s
CI / task (push) Successful in 20s
CI / user (push) Successful in 22s
CI / water (push) Successful in 20s
CI / wecom (push) Successful in 21s
CI / ws (push) Successful in 8s
feat: user improve
2026-10-27 13:01:24 +08:00

139 lines
3.8 KiB
Go

package analysisuserlogic
import (
"context"
"strconv"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
analysis "lone-services/rpc/analysis/pb"
userpb "lone-services/rpc/user/pb"
"lone-services/services/analysis/internal/logic/common"
"lone-services/services/analysis/internal/model"
"lone-services/services/analysis/internal/svc"
"lone-services/services/analysis/validator"
"github.com/zeromicro/go-zero/core/logx"
)
type UserImproveLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUserImproveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserImproveLogic {
return &UserImproveLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UserImproveLogic) UserImprove(in *analysis.UserImproveReq) (*analysis.Response, error) {
var req validator.UserImproveValidator
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
return common.OutResponse(utils.ErrorParams, msg), nil
}
bizId, loginUser, fail := common.RequireBizUserId(l.ctx)
if fail != nil {
return fail, nil
}
row, err := common.LoadAnalysisUser(bizId)
if err != nil {
l.Errorf("improve load user: %v", err)
return common.FailResponse(utils.Fail), nil
}
if row == nil {
return common.FailResponse(utils.ErrorNotFund), nil
}
if req.Name != utils.StringEmpty || req.Avatar != utils.StringEmpty ||
req.Birthday != utils.StringEmpty || req.Gender > utils.NumberZero ||
req.Mobile != utils.StringEmpty || req.Password != utils.StringEmpty {
if st := l.updatePlatformUser(loginUser, bizId, req); st.Code != utils.Ok.Code {
if st.Code == utils.ErrorParams.Code {
return common.OutResponse(utils.ErrorParams, "请先完成手机号注册"), nil
}
return common.FailResponse(st), nil
}
}
_, err = model.UserModel{}.Init().Edit(modelbase.Params{
Eq: map[string]string{"id": strconv.FormatInt(bizId, utils.NumberTen)},
}, map[string]interface{}{
"is_profile_completed": uint8(utils.StatusOk),
})
if err != nil {
l.Errorf("improve update flag: %v", err)
return common.FailResponse(utils.Fail), nil
}
return common.OkResponse(nil), nil
}
func (l *UserImproveLogic) updatePlatformUser(loginUser utils.UserInfo, bizId int64, req validator.UserImproveValidator) utils.Status {
if l.svcCtx.UserSvcName == utils.StringEmpty {
return utils.Fail
}
cli, err := svc.GetRpcClient(l.svcCtx.UserSvcName)
if err != nil {
l.Errorf("improve get user rpc: %v", err)
return utils.Fail
}
userClient := userpb.NewUserClient(cli.Conn())
info, err := userClient.InternalGetUserInfo(l.ctx, &userpb.InternalGetUserInfoReq{
UserId: loginUser.ID,
SubjectType: "user",
})
if err != nil {
l.Errorf("improve get user info: %v", err)
return utils.Fail
}
mobile := req.Mobile
if mobile == utils.StringEmpty && info.Mobile != utils.StringEmpty {
plain, decErr := utils.DecryptPhone(info.Mobile)
if decErr == nil {
mobile = plain
}
}
if mobile == utils.StringEmpty || len(mobile) != utils.NumberEleven {
return utils.ErrorParams
}
updateReq := &userpb.InternalUpdateReq{
BizId: bizId,
SubjectType: "user",
Mobile: mobile,
ClientCode: []string{loginUser.ClientCode},
}
if req.Name != utils.StringEmpty {
updateReq.Name = req.Name
}
if req.Avatar != utils.StringEmpty {
updateReq.Avatar = req.Avatar
}
if req.Gender > utils.NumberZero {
updateReq.Gender = uint32(req.Gender)
}
if req.Birthday != utils.StringEmpty {
updateReq.Birthday = req.Birthday
}
if req.Password != utils.StringEmpty {
updateReq.CredentialType = []string{"password"}
updateReq.Secret = req.Password
updateReq.Identifier = mobile
}
if _, err = userClient.InternalUpdate(l.ctx, updateReq); err != nil {
l.Errorf("improve internal update: %v", err)
return utils.Fail
}
return utils.Ok
}