fix: some api bug

This commit is contained in:
wwweww
2026-03-31 22:12:06 +08:00
parent c5ff4f0216
commit e7970ac25f
219 changed files with 16195 additions and 2126 deletions
@@ -6,6 +6,7 @@ package auth
import (
"context"
"errors"
"fmt"
"juwan-backend/app/users/rpc/pb"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/utils/contextj"
@@ -57,7 +58,8 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
return nil, errors.New("hash password failed")
}
requestId, err := contextj.RequestIdFrom(l.ctx)
requestId, err := contextj.RIdFrom(l.ctx)
logx.Infof("requestId: %s", requestId)
if err != nil {
logx.Errorf("contextj.RequestIdFrom failed: %v", err)
return nil, errors.New("contextj.RequestIdFrom failed")
@@ -73,7 +75,7 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
})
if err != nil {
logx.Error("failed to register user: ", err)
return nil, errors.New("failed to register user")
return nil, errors.New(fmt.Sprintf("failed to register user: %v", err.Error()))
}
// 返回响应
@@ -5,6 +5,9 @@ package user
import (
"context"
"errors"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/utils/contextj"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
@@ -29,6 +32,19 @@ func NewFollowUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Follow
func (l *FollowUserLogic) FollowUser(req *types.FollowUserReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
userId, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, errors.New("unauthorized")
}
return
_, err = l.svcCtx.UserRpc.AddUserFollows(l.ctx, &usercenter.AddUserFollowsReq{
FollowerId: userId,
FolloweeId: req.Id,
CreatedAt: 0,
})
if err != nil {
logx.Errorf("add user follow err: %v", err)
return nil, errors.New("failed to follow user")
}
return &types.EmptyResp{}, nil
}
+25 -10
View File
@@ -6,15 +6,15 @@ package user
import (
"context"
"errors"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/converter"
"juwan-backend/common/utils/contextj"
"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"
"k8s.io/apimachinery/pkg/util/json"
)
type GetMeLogic struct {
@@ -35,19 +35,34 @@ func NewGetMeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMeLogic
func (l *GetMeLogic) GetMe() (resp *types.User, err error) {
userId, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, errors.New("illegal id")
logx.Errorf("get user id from context: %v", err)
return nil, errors.New("illegal user")
}
user, err := l.svcCtx.UserRpc.GetUsersById(l.ctx, &usercenter.GetUsersByIdReq{
Id: userId,
})
if err != nil {
return nil, errors.New("get user by id error")
logx.Errorf("GetMeLogic.GetUsersById err: %v", err)
return nil, errors.New("get user failed")
}
err = converter.StructToStruct(user, &resp)
createAt := time.Unix(user.Users.CreatedAt, 0)
resp.CreatedAt = createAt.Format(time.DateTime)
logx.Debugf("get user resp: %+v", user)
resp = &types.User{}
err = copier.Copy(&resp, user.Users)
if err != nil {
return nil, errors.New("to struct error")
logx.Errorf("copier.Copy err: %v", err)
return nil, errors.New("copy user failed")
}
var verificationStatus map[string]string
err = json.Unmarshal([]byte(user.Users.VerificationStatus), &verificationStatus)
if err != nil {
logx.Errorf("json.Unmarshal err: %v", err)
}
resp.VerifiedRoles = user.Users.VerifiedRoles
resp.VerificationStatus = verificationStatus
resp.CreatedAt = time.Unix(user.Users.CreatedAt, 0).Format(time.DateTime)
return
}
@@ -32,7 +32,6 @@ func NewSwitchRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Switch
}
func (l *SwitchRoleLogic) SwitchRole(req *types.SwitchRoleReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
id, err := contextj.UserIDFrom(l.ctx)
if err != nil {
logx.Errorf("get user id from context: %v", err)
@@ -5,6 +5,9 @@ package user
import (
"context"
"errors"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/utils/contextj"
"juwan-backend/app/users/api/internal/svc"
"juwan-backend/app/users/api/internal/types"
@@ -29,6 +32,18 @@ func NewUnfollowUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Unfo
func (l *UnfollowUserLogic) UnfollowUser(req *types.UnfollowUserReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
userId, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, errors.New("unauthorized")
}
return
_, err = l.svcCtx.UserRpc.DelUserFollows(l.ctx, &usercenter.DelUserFollowsReq{
Id: req.Id,
UserId: userId,
})
if err != nil {
logx.Errorf("del user follow err: %v", err)
return nil, errors.New("unfollow user failed")
}
return &types.EmptyResp{}, nil
}
@@ -29,6 +29,5 @@ func NewUpdateNotificationSettingsLogic(ctx context.Context, svcCtx *svc.Service
func (l *UpdateNotificationSettingsLogic) UpdateNotificationSettings(req *types.UpdateNotifySettingsReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
return
}
@@ -33,7 +33,6 @@ func NewRejectVerificationLogic(ctx context.Context, svcCtx *svc.ServiceContext)
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 := contextj.AdminIdFrom(l.ctx)
if err != nil {
return nil, err
@@ -36,13 +36,6 @@ func (l *ApplyVerificationLogic) ApplyVerification(req *types.ApplyVerificationR
logx.Errorf("get user id from context: %v", err)
return nil, contextj.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 {
@@ -50,18 +43,14 @@ func (l *ApplyVerificationLogic) ApplyVerification(req *types.ApplyVerificationR
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 {
_, err = l.svcCtx.UserVerificationsRpc.AddOrUpdateUserVerifications(l.ctx, &pb.AddOrUpdateUserVerificationsReq{
UserId: userId,
Role: req.Role,
Material: string(materials),
})
if err != nil {
logx.Errorf("call AddOrUpdateUserVerifications: %v", err)
return nil, errors.New("apply verification failed: " + err.Error())
}
return &types.VerificationEmptyResp{}, nil
@@ -32,23 +32,21 @@ func NewGetMyVerificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}
func (l *GetMyVerificationsLogic) GetMyVerifications() (resp *types.GetMyVerificationsResp, err error) {
// todo: add your logic here and delete this line
userId, err := contextj.UserIDFrom(l.ctx)
if err != nil {
logx.Errorf("get user id from context: %v", err)
return nil, contextj.ERRILLEGALUSER
}
verifications, err := l.svcCtx.UserVerificationsRpc.SearchUserVerifications(l.ctx, &pb.SearchUserVerificationsReq{
verifications, err := l.svcCtx.UserVerificationsRpc.ListUserVerificationsByUserId(l.ctx, &pb.ListUserVerificationsByUserIdReq{
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)