add: some user api and all api desc
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ForgotPasswordLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 忘记密码-发送验证码
|
||||
func NewForgotPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ForgotPasswordLogic {
|
||||
return &ForgotPasswordLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ForgotPasswordLogic) ForgotPassword(req *types.ForgotPasswordReq) (resp *types.EmptyResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
+8
-9
@@ -1,15 +1,15 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -20,7 +20,7 @@ type LoginLogic struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 用户登录接口
|
||||
// 用户登录
|
||||
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
|
||||
return &LoginLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
@@ -30,6 +30,7 @@ func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic
|
||||
}
|
||||
|
||||
func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
if len(req.Username) < 3 || len(req.Password) < 8 || len(req.Password) > 20 {
|
||||
return nil, errors.New("the information is illegal")
|
||||
}
|
||||
@@ -50,10 +51,8 @@ func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err erro
|
||||
}
|
||||
|
||||
return &types.LoginResp{
|
||||
UserId: res.Id,
|
||||
Username: res.Username,
|
||||
Email: res.Email,
|
||||
Token: res.Token,
|
||||
Expires: int64((7 * 24 * time.Hour).Seconds()),
|
||||
AccessToken: "",
|
||||
RefreshToken: res.Token,
|
||||
User: types.User{},
|
||||
}, nil
|
||||
}
|
||||
+13
-8
@@ -1,15 +1,16 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -20,7 +21,7 @@ type LogoutLogic struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 用户登出
|
||||
// 退出登录
|
||||
func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogic {
|
||||
return &LogoutLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
@@ -29,15 +30,19 @@ func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogi
|
||||
}
|
||||
}
|
||||
|
||||
func (l *LogoutLogic) Logout(req *types.LogoutReq) (resp *types.LogoutResp, err error) {
|
||||
if req.UserId <= 0 {
|
||||
func (l *LogoutLogic) Logout(_ *types.LogoutReq) (resp *types.EmptyResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
userId, err := contextx.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.New("illegal id")
|
||||
}
|
||||
if userId <= 0 {
|
||||
return nil, errors.New("invalid userId")
|
||||
}
|
||||
|
||||
_, err = l.svcCtx.UserRpc.Logout(l.ctx, &usercenter.LogoutReq{UserId: req.UserId})
|
||||
_, err = l.svcCtx.UserRpc.Logout(l.ctx, &usercenter.LogoutReq{UserId: userId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.LogoutResp{Message: "logout success"}, nil
|
||||
return &types.EmptyResp{}, nil
|
||||
}
|
||||
+11
-12
@@ -1,19 +1,19 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/api/internal/contextx"
|
||||
"juwan-backend/app/users/rpc/pb"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
"juwan-backend/common/utils/pwdUtils"
|
||||
"regexp"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
"juwan-backend/app/users/rpc/pb"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/utils"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -24,7 +24,7 @@ type RegisterLogic struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 用户注册接口
|
||||
// 用户注册
|
||||
func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
|
||||
return &RegisterLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
@@ -52,7 +52,7 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
|
||||
return nil, errors.New("user already exists")
|
||||
}
|
||||
|
||||
hashedPassword, err := utils.HashPassword(req.Password)
|
||||
hashedPassword, err := pwdUtils.HashPassword(req.Password)
|
||||
if err != nil {
|
||||
return nil, errors.New("hash password failed")
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
|
||||
return nil, errors.New("contextx.RequestIdFrom failed")
|
||||
}
|
||||
|
||||
_, err = l.svcCtx.UserRpc.Register(l.ctx, &usercenter.RegisterReq{
|
||||
res, err := l.svcCtx.UserRpc.Register(l.ctx, &usercenter.RegisterReq{
|
||||
Username: req.Username,
|
||||
Passwd: hashedPassword,
|
||||
Phone: req.Username,
|
||||
@@ -78,9 +78,8 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
|
||||
|
||||
// 返回响应
|
||||
return &types.RegisterResp{
|
||||
UserId: 0,
|
||||
Username: req.Username,
|
||||
Email: req.Email,
|
||||
Message: "register success",
|
||||
AccessToken: "",
|
||||
RefreshToken: res.Res,
|
||||
User: types.User{},
|
||||
}, nil
|
||||
}
|
||||
+9
-10
@@ -1,14 +1,14 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/api/internal/contextx"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/utils"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
"juwan-backend/common/utils/pwdUtils"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
@@ -16,29 +16,28 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdatePasswordByVcodeLogic struct {
|
||||
type ResetPasswordLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 修改密码-使用验证码
|
||||
func NewUpdatePasswordByVcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePasswordByVcodeLogic {
|
||||
return &UpdatePasswordByVcodeLogic{
|
||||
// 重置密码
|
||||
func NewResetPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResetPasswordLogic {
|
||||
return &ResetPasswordLogic{
|
||||
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
|
||||
func (l *ResetPasswordLogic) ResetPassword(req *types.ResetPasswordReq) (resp *types.EmptyResp, err error) {
|
||||
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)
|
||||
hashedPassword, err := pwdUtils.HashPassword(req.NewPassword)
|
||||
if err != nil {
|
||||
logx.Errorf("hash password failed, err:%v.", err)
|
||||
return nil, errors.New("bad password")
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type FollowUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 关注用户
|
||||
func NewFollowUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FollowUserLogic {
|
||||
return &FollowUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *FollowUserLogic) FollowUser(req *types.FollowUserReq) (resp *types.EmptyResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -6,9 +6,10 @@ package user
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/api/internal/contextx"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/converter"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
@@ -31,7 +32,7 @@ func NewGetMeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMeLogic
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetMeLogic) GetMe() (resp *types.UserInfo, err error) {
|
||||
func (l *GetMeLogic) GetMe() (resp *types.User, err error) {
|
||||
userId, err := contextx.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.New("illegal id")
|
||||
@@ -43,6 +44,8 @@ func (l *GetMeLogic) GetMe() (resp *types.UserInfo, err error) {
|
||||
return nil, errors.New("get user by id error")
|
||||
}
|
||||
err = converter.StructToStruct(user, &resp)
|
||||
createAt := time.Unix(user.Users.CreatedAt, 0)
|
||||
resp.CreatedAt = createAt.Format(time.DateTime)
|
||||
if err != nil {
|
||||
return nil, errors.New("to struct error")
|
||||
}
|
||||
|
||||
@@ -6,12 +6,11 @@ package user
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/converter"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -30,21 +29,21 @@ func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUs
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetUserInfoLogic) GetUserInfo(req *types.GetUserInfoReq) (resp types.UserInfo, err error) {
|
||||
|
||||
func (l *GetUserInfoLogic) GetUserInfo(req *types.GetUserReq) (resp types.User, err error) {
|
||||
|
||||
pbUser, err := l.svcCtx.UserRpc.GetUsersById(l.ctx, &usercenter.GetUsersByIdReq{
|
||||
Id: req.UserId,
|
||||
Id: req.Id,
|
||||
})
|
||||
if err != nil {
|
||||
return types.UserInfo{}, errors.New("failed to get user info by userid")
|
||||
}
|
||||
user := types.UserInfo{}
|
||||
err = converter.StructToStruct(&pbUser.Users, &user)
|
||||
if err != nil {
|
||||
logx.Errorf("struct to user info failed, err:%v.", err)
|
||||
return types.UserInfo{}, errors.New("failed to get user info by userid")
|
||||
return types.User{}, errors.New("failed to get user info by userid")
|
||||
}
|
||||
|
||||
user := types.User{}
|
||||
err = copier.Copy(&user, &pbUser.Users)
|
||||
if err != nil {
|
||||
logx.Errorf("struct to user info failed, err:%v.", err)
|
||||
return types.User{}, errors.New("failed to get user info by userid")
|
||||
}
|
||||
|
||||
//req.UserId
|
||||
return user, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type SwitchRoleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 切换当前激活角色
|
||||
func NewSwitchRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SwitchRoleLogic {
|
||||
return &SwitchRoleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SwitchRoleLogic) SwitchRole(req *types.SwitchRoleReq) (resp *types.EmptyResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
id, err := contextx.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("get user id from context: %v", err)
|
||||
return nil, errors.New("illegal id")
|
||||
}
|
||||
_, err = l.svcCtx.UserRpc.UpdateUsers(l.ctx, &usercenter.UpdateUsersReq{
|
||||
Id: id,
|
||||
CurrentRole: proto.String(req.Role),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logx.Errorf("update user info by id: %v", err)
|
||||
return nil, errors.New("update user info by userid")
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UnfollowUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 取消关注用户
|
||||
func NewUnfollowUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UnfollowUserLogic {
|
||||
return &UnfollowUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UnfollowUserLogic) UnfollowUser(req *types.UnfollowUserReq) (resp *types.EmptyResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -6,9 +6,10 @@ package user
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/users/api/internal/contextx"
|
||||
"juwan-backend/app/users/rpc/usercenter"
|
||||
"juwan-backend/common/converter"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
"strings"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
@@ -31,16 +32,16 @@ func NewUpdateMeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateMe
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateMeLogic) UpdateMe(req *types.UpdateUserInfoReq) (resp *types.UserInfo, err error) {
|
||||
func (l *UpdateMeLogic) UpdateMe(req *types.UpdateUserProfileReq) (resp *types.UpdateUserProfileReq, 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,
|
||||
Nickname: proto_string(req.Nickname),
|
||||
Avatar: proto_string(req.Avatar),
|
||||
Bio: proto_string(req.Bio),
|
||||
VerifiedRoles: nil,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -53,3 +54,10 @@ func (l *UpdateMeLogic) UpdateMe(req *types.UpdateUserInfoReq) (resp *types.User
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func proto_string(s string) *string {
|
||||
if len(s) == 0 || strings.Contains(s, " ") {
|
||||
return nil
|
||||
}
|
||||
return &s
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateNotificationSettingsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 更新通知偏好
|
||||
func NewUpdateNotificationSettingsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateNotificationSettingsLogic {
|
||||
return &UpdateNotificationSettingsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateNotificationSettingsLogic) UpdateNotificationSettings(req *types.UpdateNotifySettingsReq) (resp *types.EmptyResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateThemeSettingsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 更新主题偏好
|
||||
func NewUpdateThemeSettingsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateThemeSettingsLogic {
|
||||
return &UpdateThemeSettingsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateThemeSettingsLogic) UpdateThemeSettings(req *types.UpdateThemeSettingsReq) (resp *types.EmptyResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
// 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/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateUserInfoLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 修改用户信息
|
||||
func NewUpdateUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserInfoLogic {
|
||||
return &UpdateUserInfoLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateUserInfoLogic) UpdateUserInfo(req *types.UpdateUserInfoReq) (resp *types.UpdateUserInfoResp, err error) {
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package verification_admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/user_verifications/rpc/pb"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ApproveVerificationLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 管理员通过申请
|
||||
func NewApproveVerificationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApproveVerificationLogic {
|
||||
return &ApproveVerificationLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
APPROVE = "approved"
|
||||
)
|
||||
|
||||
func (l *ApproveVerificationLogic) ApproveVerification(req *types.VerificationIdReq) (resp *types.VerificationEmptyResp, err error) {
|
||||
adminId, err := contextx.AdminIdFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = l.svcCtx.UserVerificationsRpc.UpdateUserVerifications(l.ctx, &pb.UpdateUserVerificationsReq{
|
||||
Id: req.Id,
|
||||
Status: &APPROVE,
|
||||
ReviewedBy: adminId,
|
||||
ReviewedAt: time.Now().Unix(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package verification_admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/user_verifications/rpc/pb"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetVerificationsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 管理员获取认证申请列表 (分页)
|
||||
func NewGetVerificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetVerificationsLogic {
|
||||
return &GetVerificationsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetVerificationsLogic) GetVerifications(req *types.GetPendingListReq) (resp *types.GetPendingListResp, err error) {
|
||||
_, err = contextx.AdminIdFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
verifications, err := l.svcCtx.UserVerificationsRpc.SearchUserVerifications(l.ctx, &pb.SearchUserVerificationsReq{
|
||||
Page: req.Page,
|
||||
Limit: req.Size,
|
||||
Role: req.Role,
|
||||
Status: req.Status,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var searchResults []types.VerificationItem
|
||||
for _, v := range verifications.UserVerifications {
|
||||
temp := types.VerificationItem{}
|
||||
err = copier.Copy(&temp, v)
|
||||
if err != nil {
|
||||
logx.Errorf("copy verification item err: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
temp.CreatedAt = time.Unix(v.CreatedAt, 0).Format(time.DateTime)
|
||||
temp.ReviewedAt = time.Unix(v.ReviewedAt, 0).Format(time.DateTime)
|
||||
searchResults = append(searchResults, temp)
|
||||
}
|
||||
resp = &types.GetPendingListResp{
|
||||
List: searchResults,
|
||||
Total: 0,
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package verification_admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/user_verifications/rpc/pb"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RejectVerificationLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 管理员驳回申请
|
||||
func NewRejectVerificationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RejectVerificationLogic {
|
||||
return &RejectVerificationLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
var REJECTED = "rejected"
|
||||
|
||||
func (l *RejectVerificationLogic) RejectVerification(req *types.RejectVerificationReq) (resp *types.VerificationEmptyResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
adminId, err := contextx.AdminIdFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = l.svcCtx.UserVerificationsRpc.UpdateUserVerifications(l.ctx, &pb.UpdateUserVerificationsReq{
|
||||
Id: req.Id,
|
||||
Status: &REJECTED,
|
||||
RejectReason: &req.Reason,
|
||||
ReviewedBy: adminId,
|
||||
ReviewedAt: time.Now().Unix(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package verification_user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"juwan-backend/app/user_verifications/rpc/pb"
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ApplyVerificationLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 提交或修改角色认证申请 (支持幂等更新)
|
||||
func NewApplyVerificationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApplyVerificationLogic {
|
||||
return &ApplyVerificationLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ApplyVerificationLogic) ApplyVerification(req *types.ApplyVerificationReq) (resp *types.VerificationEmptyResp, err error) {
|
||||
userId, err := contextx.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("get user id from context: %v", err)
|
||||
return nil, contextx.ERRILLEGALUSER
|
||||
}
|
||||
verifications, err := l.svcCtx.UserVerificationsRpc.SearchUserVerifications(l.ctx, &pb.SearchUserVerificationsReq{
|
||||
UserId: userId,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("search user verifications: %v", err)
|
||||
return nil, errors.New("search user verifications failed")
|
||||
}
|
||||
|
||||
materials, err := json.Marshal(req.Materials)
|
||||
if err != nil {
|
||||
logx.Errorf("marshal materials: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if verifications == nil || len(verifications.UserVerifications) == 0 {
|
||||
// 如果没有则增加
|
||||
_, err = l.svcCtx.UserVerificationsRpc.AddUserVerifications(l.ctx, &pb.AddUserVerificationsReq{
|
||||
Role: req.Role,
|
||||
Materials: string(materials),
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("add user verifications: %v", err)
|
||||
return nil, errors.New("add user verifications failed")
|
||||
}
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
return &types.VerificationEmptyResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package verification_user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/user_verifications/rpc/pb"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetMyVerificationsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 获取我的所有认证状态
|
||||
func NewGetMyVerificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMyVerificationsLogic {
|
||||
return &GetMyVerificationsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetMyVerificationsLogic) GetMyVerifications() (resp *types.GetMyVerificationsResp, 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: %v", err)
|
||||
return nil, contextx.ERRILLEGALUSER
|
||||
}
|
||||
|
||||
verifications, err := l.svcCtx.UserVerificationsRpc.SearchUserVerifications(l.ctx, &pb.SearchUserVerificationsReq{
|
||||
UserId: userId,
|
||||
Page: 1,
|
||||
Limit: 100, // assuming a user won't have more than 100 verification records, adjust as needed
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var searchResults []types.VerificationItem
|
||||
for _, v := range verifications.UserVerifications {
|
||||
temp := types.VerificationItem{}
|
||||
err = copier.Copy(&temp, v)
|
||||
if err != nil {
|
||||
logx.Errorf("copy verification item err: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
temp.CreatedAt = time.Unix(v.CreatedAt, 0).Format(time.DateTime)
|
||||
temp.ReviewedAt = time.Unix(v.ReviewedAt, 0).Format(time.DateTime)
|
||||
searchResults = append(searchResults, temp)
|
||||
}
|
||||
|
||||
resp = &types.GetMyVerificationsResp{
|
||||
List: searchResults,
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user