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 }