add: user accomplished
This commit is contained in:
@@ -2,6 +2,7 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/users/rpc/internal/models/users"
|
||||
|
||||
"juwan-backend/app/users/rpc/internal/svc"
|
||||
"juwan-backend/app/users/rpc/pb"
|
||||
@@ -25,9 +26,10 @@ func NewGetUserByUsernameLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
||||
}
|
||||
|
||||
func (l *GetUserByUsernameLogic) GetUserByUsername(in *pb.GetUserByUsernameReq) (*pb.GetUserByUsernameResp, error) {
|
||||
user, err := l.svcCtx.UsersModelRO.FindOneByUsername(l.ctx, in.Username)
|
||||
pbUsers := &pb.Users{}
|
||||
converter.StructToStruct(user, pbUsers)
|
||||
|
||||
//user, err := l.svcCtx.UsersModelRO.FindOneByUsername(l.ctx, in.Username)
|
||||
user, err := l.svcCtx.UsersModelRO.Query().Where(users.UsernameEQ(in.Username)).First(l.ctx)
|
||||
if err == nil || user != nil {
|
||||
return &pb.GetUserByUsernameResp{
|
||||
Users: pbUsers,
|
||||
@@ -36,5 +38,10 @@ func (l *GetUserByUsernameLogic) GetUserByUsername(in *pb.GetUserByUsernameReq)
|
||||
if err.Error() != "not found" {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = converter.StructToStruct(user, pbUsers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.GetUserByUsernameResp{}, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/users/rpc/internal/models/users"
|
||||
|
||||
"juwan-backend/app/users/rpc/internal/svc"
|
||||
"juwan-backend/app/users/rpc/pb"
|
||||
@@ -26,12 +27,16 @@ func NewGetUsersByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetU
|
||||
|
||||
func (l *GetUsersByIdLogic) GetUsersById(in *pb.GetUsersByIdReq) (*pb.GetUsersByIdResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
user, err := l.svcCtx.UsersModelRO.FindOne(l.ctx, in.Id)
|
||||
//user, err := l.svcCtx.UsersModelRO.FindOne(l.ctx, in.Id)
|
||||
user, err := l.svcCtx.UsersModelRO.Query().Where(users.IDEQ(in.Id)).All(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pbUser := &pb.Users{}
|
||||
converter.StructToStruct(&user, &pbUser)
|
||||
err = converter.StructToStruct(&user, &pbUser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.GetUsersByIdResp{Users: pbUser}, nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package logic
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/rpc/internal/models/users"
|
||||
"juwan-backend/app/users/rpc/internal/svc"
|
||||
utils2 "juwan-backend/app/users/rpc/internal/utils"
|
||||
"juwan-backend/app/users/rpc/pb"
|
||||
@@ -11,6 +12,8 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
//var UserNotFound = errors.New("user not Found")
|
||||
|
||||
type LoginLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
@@ -26,23 +29,24 @@ func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic
|
||||
}
|
||||
|
||||
func (l *LoginLogic) Login(in *pb.LoginReq) (*pb.LoginResp, error) {
|
||||
user, err := l.svcCtx.UsersModelRO.FindOneByUsername(l.ctx, in.Username)
|
||||
//user, err := l.svcCtx.UsersModelRO.FindOneByUsername(l.ctx, in.Username)
|
||||
user, err := l.svcCtx.UsersModelRO.Query().Where(users.NicknameEQ(in.Username)).First(l.ctx)
|
||||
if err != nil {
|
||||
logx.WithContext(l.ctx).Errorf("LoginLogic.Login error:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
logx.Infof("user:%v", user)
|
||||
if !utils.VerifyPassword(user.Passwd, in.Passwd) {
|
||||
if !utils.VerifyPassword(user.PasswordHash, in.Passwd) {
|
||||
logx.WithContext(l.ctx).Errorf("User %s Login failed", user.Username)
|
||||
return nil, errors.New("incorrect password")
|
||||
}
|
||||
|
||||
token, err := l.svcCtx.JwtManager.New(l.ctx, &utils2.TokenPayload{
|
||||
UserId: user.UserId,
|
||||
UserId: user.ID,
|
||||
IsAdmin: false,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("LoginLogic.Login gen jwt for user %v error:%v", user.UserId, err)
|
||||
logx.Errorf("LoginLogic.Login gen jwt for user %v error:%v", user.ID, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -50,6 +54,6 @@ func (l *LoginLogic) Login(in *pb.LoginReq) (*pb.LoginResp, error) {
|
||||
Token: token,
|
||||
Username: user.Username,
|
||||
Email: user.Email,
|
||||
Id: user.UserId,
|
||||
Id: user.ID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -6,13 +6,12 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
"juwan-backend/app/users/rpc/internal/models"
|
||||
"juwan-backend/app/users/rpc/internal/svc"
|
||||
"juwan-backend/app/users/rpc/pb"
|
||||
"juwan-backend/common/redisx"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -44,13 +43,12 @@ func mustNewRandomNickname() string {
|
||||
}
|
||||
|
||||
func (l *RegisterLogic) Register(in *pb.RegisterReq) (*pb.RegisterResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
if in.Phone == "" || in.Username == "" || in.Passwd == "" {
|
||||
if in.Username == "" || in.Passwd == "" {
|
||||
logx.Error("invalid input")
|
||||
return nil, errors.New("invalid input")
|
||||
}
|
||||
|
||||
redisKey := fmt.Sprintf("vcode:%s:%s:%s", in.RequestId, "register", in.Email)
|
||||
redisKey := fmt.Sprintf(redisx.VCODE_KEY_PREFIX, in.RequestId, redisx.SCENE_REG, in.Email)
|
||||
vcode, err := l.svcCtx.RedisCluster.Get(l.ctx, redisKey).Result()
|
||||
logx.Infof("vcode:%s, err:%v", vcode, err)
|
||||
if err != nil {
|
||||
@@ -69,21 +67,17 @@ func (l *RegisterLogic) Register(in *pb.RegisterReq) (*pb.RegisterResp, error) {
|
||||
return nil, errors.New("generate user ID failed")
|
||||
}
|
||||
|
||||
user := models.Users{
|
||||
UserId: resp.Id,
|
||||
Username: in.Username,
|
||||
Nickname: mustNewRandomNickname(),
|
||||
Passwd: in.Passwd,
|
||||
Phone: in.Phone,
|
||||
Email: in.Email,
|
||||
RoleType: 0,
|
||||
IsVerified: false,
|
||||
}
|
||||
|
||||
_, err = l.svcCtx.UsersModelRW.Insert(l.ctx, &user)
|
||||
_, err = l.svcCtx.UsersModelRW.Create().
|
||||
SetID(resp.Id).
|
||||
SetUsername(in.Username).
|
||||
SetPasswordHash(in.Passwd).
|
||||
SetPhone(in.Phone).
|
||||
SetEmail(in.Email).
|
||||
SetNickname(mustNewRandomNickname()).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
logx.Error("failed to create user: ", err)
|
||||
return nil, errors.New("failed to create user")
|
||||
logx.Errorf("create user err:%v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.RegisterResp{
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"juwan-backend/app/users/rpc/internal/models/users"
|
||||
"juwan-backend/common/redisx"
|
||||
|
||||
"juwan-backend/app/users/rpc/internal/svc"
|
||||
"juwan-backend/app/users/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ResetPasswordLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewResetPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResetPasswordLogic {
|
||||
return &ResetPasswordLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ResetPasswordLogic) ResetPassword(in *pb.ResetPasswordReq) (*pb.ResetPasswordResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
redisKey := fmt.Sprintf(redisx.VCODE_KEY_PREFIX, in.RequestId, redisx.SCENE_REG, in.Email)
|
||||
vcode, err := l.svcCtx.RedisCluster.Get(l.ctx, redisKey).Result()
|
||||
if err != nil {
|
||||
logx.Errorf("get reset password vcode from redis failed, err:%v.", err)
|
||||
return nil, err
|
||||
}
|
||||
if vcode != in.Vcode {
|
||||
return nil, errors.New(fmt.Sprintf("user %v reset password failed, invalid vcode.", in.Email))
|
||||
}
|
||||
err = l.svcCtx.UsersModelRW.Update().Where(users.EmailEQ(in.Email)).
|
||||
SetPasswordHash(in.NewPassword).
|
||||
Exec(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("reset password failed, err:%v.", err)
|
||||
return nil, errors.New("reset password failed")
|
||||
}
|
||||
return &pb.ResetPasswordResp{}, nil
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/users/rpc/internal/svc"
|
||||
"juwan-backend/app/users/rpc/pb"
|
||||
|
||||
@@ -24,7 +23,18 @@ func NewUpdateUsersLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Updat
|
||||
}
|
||||
|
||||
func (l *UpdateUsersLogic) UpdateUsers(in *pb.UpdateUsersReq) (*pb.UpdateUsersResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
updater := l.svcCtx.UsersModelRW.UpdateOneID(in.Id).
|
||||
SetNillableNickname(in.Nickname).
|
||||
SetNillableAvatar(in.Avatar).
|
||||
SetNillableBio(in.Bio).
|
||||
SetNillableCurrentRole(in.CurrentRole).
|
||||
SetNillablePasswordHash(in.PasswordHash)
|
||||
if len(in.VerifiedRoles) > 0 {
|
||||
updater.SetVerifiedRoles(in.VerifiedRoles)
|
||||
}
|
||||
err := updater.Exec(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.UpdateUsersResp{}, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/users/rpc/internal/models/users"
|
||||
|
||||
"juwan-backend/app/users/rpc/internal/svc"
|
||||
"juwan-backend/app/users/rpc/pb"
|
||||
@@ -31,7 +32,8 @@ func (l *ValidateTokenLogic) ValidateToken(in *pb.ValidateTokenReq) (*pb.Validat
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
users, err := l.svcCtx.UsersModelRO.FindOne(l.ctx, in.UserId)
|
||||
//users, err := l.svcCtx.UsersModelRO.FindOne(l.ctx, in.UserId)
|
||||
user, err := l.svcCtx.UsersModelRO.Query().Where(users.IDEQ(in.UserId)).First(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -40,6 +42,6 @@ func (l *ValidateTokenLogic) ValidateToken(in *pb.ValidateTokenReq) (*pb.Validat
|
||||
Valid: true,
|
||||
Message: "OK",
|
||||
UserId: in.UserId,
|
||||
RoleType: users.RoleType,
|
||||
RoleType: user.CurrentRole,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user