add: user accomplished
This commit is contained in:
@@ -2,10 +2,14 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"juwan-backend/app/users/rpc/internal/models"
|
||||
"juwan-backend/app/users/rpc/internal/models/users"
|
||||
"juwan-backend/app/users/rpc/internal/svc"
|
||||
"juwan-backend/app/users/rpc/pb"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -23,8 +27,58 @@ func NewSearchUsersLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Searc
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SearchUsersLogic) SearchUsers(in *pb.SearchUsersReq) (*pb.SearchUsersResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
var SearUsersErr = errors.New("search users failed")
|
||||
|
||||
return &pb.SearchUsersResp{}, nil
|
||||
func (l *SearchUsersLogic) SearchUsers(in *pb.SearchUsersReq) (out *pb.SearchUsersResp, err error) {
|
||||
user, err := l.svcCtx.UsersModelRO.Query().
|
||||
Where(users.Or(
|
||||
users.UsernameContainsFold(*in.Username),
|
||||
users.NicknameContainsFold(*in.Username),
|
||||
users.EmailContainsFold(*in.Username),
|
||||
users.CurrentRole(*in.CurrentRole),
|
||||
)).
|
||||
All(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("search users failed, err:%v.", err)
|
||||
return nil, SearUsersErr
|
||||
}
|
||||
out = &pb.SearchUsersResp{
|
||||
Users: ConvertEntUsersToProto(user),
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ConvertEntUserToProto(entUser *models.Users) *pb.Users {
|
||||
if entUser == nil {
|
||||
return nil
|
||||
}
|
||||
out := &pb.Users{}
|
||||
err := copier.Copy(out, entUser)
|
||||
if err != nil {
|
||||
return out
|
||||
}
|
||||
out.CreatedAt = entUser.CreatedAt.Unix()
|
||||
out.UpdatedAt = entUser.UpdatedAt.Unix()
|
||||
if !entUser.DeletedAt.IsZero() {
|
||||
out.DeletedAt = entUser.DeletedAt.Unix()
|
||||
}
|
||||
|
||||
verificationStatus, err := json.Marshal(entUser.VerificationStatus)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
out.VerificationStatus = string(verificationStatus)
|
||||
out.VerifiedRoles = entUser.VerifiedRoles
|
||||
|
||||
out.PasswordHash = "" // 手动清空敏感字段
|
||||
return out
|
||||
}
|
||||
func ConvertEntUsersToProto(users []*models.Users) []*pb.Users {
|
||||
list := make([]*pb.Users, 0, len(users))
|
||||
for _, u := range users {
|
||||
list = append(list, ConvertEntUserToProto(u))
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user