add: user accomplished
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/api/internal/contextx"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/converter"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetMeLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 获取当前登录用户信息
|
||||
func NewGetMeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMeLogic {
|
||||
return &GetMeLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetMeLogic) GetMe() (resp *types.UserInfo, err error) {
|
||||
userId, err := contextx.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.New("illegal id")
|
||||
}
|
||||
user, err := l.svcCtx.UserRpc.GetUsersById(l.ctx, &usercenter.GetUsersByIdReq{
|
||||
Id: userId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New("get user by id error")
|
||||
}
|
||||
err = converter.StructToStruct(user, &resp)
|
||||
if err != nil {
|
||||
return nil, errors.New("to struct error")
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -31,7 +31,7 @@ func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUs
|
||||
}
|
||||
|
||||
func (l *GetUserInfoLogic) GetUserInfo(req *types.GetUserInfoReq) (resp types.UserInfo, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
pbUser, err := l.svcCtx.UserRpc.GetUsersById(l.ctx, &usercenter.GetUsersByIdReq{
|
||||
Id: req.UserId,
|
||||
})
|
||||
|
||||
@@ -30,7 +30,7 @@ func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic
|
||||
}
|
||||
|
||||
func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
|
||||
if len(req.Username) < 3 || len(req.Password) > 20 || len(req.Password) < 8 || len(req.Password) > 20 {
|
||||
if len(req.Username) < 3 || len(req.Password) < 8 || len(req.Password) > 20 {
|
||||
return nil, errors.New("the information is illegal")
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/api/internal/contextx"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/converter"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateMeLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 更改当前登录用户信息
|
||||
func NewUpdateMeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateMeLogic {
|
||||
return &UpdateMeLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateMeLogic) UpdateMe(req *types.UpdateUserInfoReq) (resp *types.UserInfo, err error) {
|
||||
userId, err := contextx.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, err := l.svcCtx.UserRpc.UpdateUsers(l.ctx, &usercenter.UpdateUsersReq{
|
||||
Id: userId,
|
||||
Nickname: req.Nickname,
|
||||
Avatar: req.Avatar,
|
||||
Bio: req.Bio,
|
||||
VerifiedRoles: nil,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New("update info failed")
|
||||
}
|
||||
err = converter.StructToStruct(res, &resp)
|
||||
if err != nil {
|
||||
logx.Errorf("unmarshal user info failed, err:%v.", err)
|
||||
return nil, errors.New("unmarshal user info failed")
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/api/internal/contextx"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/utils"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdatePasswordByVcodeLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 修改密码-使用验证码
|
||||
func NewUpdatePasswordByVcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePasswordByVcodeLogic {
|
||||
return &UpdatePasswordByVcodeLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdatePasswordByVcodeLogic) UpdatePasswordByVcode(req *types.ResetPasswordByVcode) (resp *types.EmptyResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
requestId, err := contextx.RequestIdFrom(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("get request id from context failed, err:%v.", err)
|
||||
return nil, errors.New("bad request")
|
||||
}
|
||||
hashedPassword, err := utils.HashPassword(req.Password)
|
||||
if err != nil {
|
||||
logx.Errorf("hash password failed, err:%v.", err)
|
||||
return nil, errors.New("bad password")
|
||||
}
|
||||
|
||||
_, err = l.svcCtx.UserRpc.ResetPassword(l.ctx, &usercenter.ResetPasswordReq{
|
||||
NewPassword: hashedPassword,
|
||||
Email: req.Email,
|
||||
RequestId: requestId,
|
||||
Vcode: req.Vcode,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("reset password failed, err:%v.", err)
|
||||
return nil, errors.New("reset password failed")
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -5,6 +5,10 @@ package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/api/internal/contextx"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/utils"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
@@ -12,6 +16,8 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
var ChangeUserPassFailed = errors.New("change user pass failed")
|
||||
|
||||
type UpdatePasswordLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
@@ -29,6 +35,38 @@ func NewUpdatePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Up
|
||||
|
||||
func (l *UpdatePasswordLogic) UpdatePassword(req *types.UpdatePasswordReq) (resp *types.UpdatePasswordResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
userId, err := contextx.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("get user id from context failed, err:%v.", err)
|
||||
return nil, ChangeUserPassFailed
|
||||
}
|
||||
|
||||
user, err := l.svcCtx.UserRpc.GetUsersById(l.ctx, &usercenter.GetUsersByIdReq{
|
||||
Id: userId,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("get user info failed, err:%v.", err)
|
||||
return nil, ChangeUserPassFailed
|
||||
}
|
||||
|
||||
oldPasswd, err := utils.HashPassword(req.OldPassword)
|
||||
if err != nil {
|
||||
logx.Errorf("hash old password failed, err:%v.", err)
|
||||
return nil, ChangeUserPassFailed
|
||||
}
|
||||
|
||||
if oldPasswd != user.Users.PasswordHash {
|
||||
return nil, ChangeUserPassFailed
|
||||
}
|
||||
|
||||
_, err = l.svcCtx.UserRpc.UpdateUsers(l.ctx, &usercenter.UpdateUsersReq{
|
||||
Id: userId,
|
||||
Username: &user.Users.Username,
|
||||
PasswordHash: &req.NewPassword,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("update user password failed, err:%v.", err)
|
||||
return nil, ChangeUserPassFailed
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/api/internal/contextx"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
@@ -28,7 +31,18 @@ func NewUpdateUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Up
|
||||
}
|
||||
|
||||
func (l *UpdateUserInfoLogic) UpdateUserInfo(req *types.UpdateUserInfoReq) (resp *types.UpdateUserInfoResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
userId, err := contextx.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
_, err = l.svcCtx.UserRpc.UpdateUsers(l.ctx, &usercenter.UpdateUsersReq{
|
||||
Id: userId,
|
||||
Nickname: req.Nickname,
|
||||
Avatar: req.Avatar,
|
||||
Bio: req.Bio,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New("update user info failed")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user