add: user accomplished
This commit is contained in:
@@ -28,3 +28,27 @@ func TokenFrom(c context.Context) (string, error) {
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func WithUserID(c context.Context, id int64) context.Context {
|
||||
return context.WithValue(c, "user_id", id)
|
||||
}
|
||||
|
||||
func UserIDFrom(c context.Context) (int64, error) {
|
||||
if userID, ok := c.Value("user_id").(int64); !ok {
|
||||
return 0, errors.New("user_id not found in context")
|
||||
} else {
|
||||
return userID, nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithRequestID(c context.Context, requestID string) context.Context {
|
||||
return context.WithValue(c, "request_id", requestID)
|
||||
}
|
||||
|
||||
func RequestIDFrom(c context.Context) (string, error) {
|
||||
if requestID, ok := c.Value("request_id").(string); !ok {
|
||||
return "", errors.New("request_id not found in context")
|
||||
} else {
|
||||
return requestID, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,18 +17,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.Logger},
|
||||
[]rest.Route{
|
||||
{
|
||||
// 获取用户信息
|
||||
Method: http.MethodGet,
|
||||
Path: "/:userId",
|
||||
Handler: user.GetUserInfoHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 修改用户信息
|
||||
Method: http.MethodPut,
|
||||
Path: "/:userId",
|
||||
Handler: user.UpdateUserInfoHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 用户登出
|
||||
Method: http.MethodPost,
|
||||
@@ -41,6 +29,12 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/:userId/password",
|
||||
Handler: user.UpdatePasswordHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 修改密码-使用验证码
|
||||
Method: http.MethodPut,
|
||||
Path: "/forgot-password/reset",
|
||||
Handler: user.UpdatePasswordByVcodeHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 用户登录接口
|
||||
Method: http.MethodPost,
|
||||
@@ -55,6 +49,39 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
},
|
||||
}...,
|
||||
),
|
||||
rest.WithPrefix("/api/users"),
|
||||
rest.WithPrefix("/api/v1/auth"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.Logger},
|
||||
[]rest.Route{
|
||||
{
|
||||
// 获取用户信息
|
||||
Method: http.MethodGet,
|
||||
Path: "/:userId",
|
||||
Handler: user.GetUserInfoHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 修改用户信息
|
||||
Method: http.MethodPut,
|
||||
Path: "/:userId",
|
||||
Handler: user.UpdateUserInfoHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 获取当前登录用户信息
|
||||
Method: http.MethodGet,
|
||||
Path: "/me",
|
||||
Handler: user.GetMeHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 更改当前登录用户信息
|
||||
Method: http.MethodPut,
|
||||
Path: "/me",
|
||||
Handler: user.UpdateMeHandler(serverCtx),
|
||||
},
|
||||
}...,
|
||||
),
|
||||
rest.WithPrefix("/api/v1/user"),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"juwan-backend/app/users/api/internal/logic/user"
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
// 获取当前登录用户信息
|
||||
func GetMeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
l := user.NewGetMeLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetMe()
|
||||
logx.Infof("req header: %v", r.Header)
|
||||
logx.Infof("cookies: %v", r.Cookies())
|
||||
//r.Cookie()
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"juwan-backend/app/users/api/internal/logic/user"
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
"juwan-backend/common/utils"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
@@ -25,7 +26,7 @@ func LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
resp, err := l.Login(&req)
|
||||
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
httpx.ErrorCtx(r.Context(), w, utils.NewErrorResp(400, err))
|
||||
} else {
|
||||
token := resp.Token
|
||||
resp.Token = ""
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"juwan-backend/app/users/api/internal/logic/user"
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
)
|
||||
|
||||
// 更改当前登录用户信息
|
||||
func UpdateMeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UpdateUserInfoReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := user.NewUpdateMeLogic(r.Context(), svcCtx)
|
||||
resp, err := l.UpdateMe(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"juwan-backend/app/users/api/internal/logic/user"
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
)
|
||||
|
||||
// 修改密码-使用验证码
|
||||
func UpdatePasswordByVcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.ResetPasswordByVcode
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := user.NewUpdatePasswordByVcodeLogic(r.Context(), svcCtx)
|
||||
resp, err := l.UpdatePasswordByVcode(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
|
||||
package types
|
||||
|
||||
type EmptyResp struct {
|
||||
}
|
||||
|
||||
type ErrorResp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
@@ -49,6 +52,12 @@ type RegisterResp struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type ResetPasswordByVcode struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Vcode string `json:"vcode"`
|
||||
}
|
||||
|
||||
type UpdatePasswordReq struct {
|
||||
UserId int64 `path:"userId" binding:"required,gt=0"`
|
||||
OldPassword string `json:"oldPassword" binding:"required"`
|
||||
@@ -60,10 +69,9 @@ type UpdatePasswordResp struct {
|
||||
}
|
||||
|
||||
type UpdateUserInfoReq struct {
|
||||
UserId int64 `path:"userId" binding:"required,gt=0"`
|
||||
Email string `json:"email" binding:"omitempty,email"`
|
||||
Phone string `json:"phone" binding:"omitempty,len=11"`
|
||||
Avatar string `json:"avatar" binding:"omitempty,url"`
|
||||
Nickname *string `json:"nickname,omitempty"`
|
||||
Avatar *string `json:"avatar,omitempty"`
|
||||
Bio *string `json:"bio,omitempty"`
|
||||
}
|
||||
|
||||
type UpdateUserInfoResp struct {
|
||||
|
||||
Reference in New Issue
Block a user