73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
// 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"
|
|
)
|
|
|
|
var ChangeUserPassFailed = errors.New("change user pass failed")
|
|
|
|
type UpdatePasswordLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 修改用户密码
|
|
func NewUpdatePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePasswordLogic {
|
|
return &UpdatePasswordLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|