fix: some api bug
This commit is contained in:
@@ -146,23 +146,20 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.Logger},
|
||||
[]rest.Route{
|
||||
{
|
||||
// 提交或修改角色认证申请 (支持幂等更新)
|
||||
Method: http.MethodPost,
|
||||
Path: "/me/verification",
|
||||
Handler: verification_user.ApplyVerificationHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 获取我的所有认证状态
|
||||
Method: http.MethodGet,
|
||||
Path: "/me/verification",
|
||||
Handler: verification_user.GetMyVerificationsHandler(serverCtx),
|
||||
},
|
||||
}...,
|
||||
),
|
||||
[]rest.Route{
|
||||
{
|
||||
// 提交或修改角色认证申请 (支持幂等更新)
|
||||
Method: http.MethodPost,
|
||||
Path: "/me/verification",
|
||||
Handler: verification_user.ApplyVerificationHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// 获取我的所有认证状态
|
||||
Method: http.MethodGet,
|
||||
Path: "/me/verification",
|
||||
Handler: verification_user.GetMyVerificationsHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithPrefix("/api/v1/users"),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
package types
|
||||
|
||||
type ApplyVerificationReq struct {
|
||||
Role string `json:"role"` // 申请什么角色
|
||||
Materials map[string]string `json:"materials"` // 证明材料键值对 {"idCardFront": "http...", "license": "http..."}
|
||||
Role string `json:"role"` // 申请什么角色
|
||||
Materials MaterialJson `json:"materials"` // 证明材料键值对 {"idCardFront": "http...", "license": "http..."}
|
||||
}
|
||||
|
||||
type EmptyResp struct {
|
||||
@@ -56,6 +56,13 @@ type LoginResp struct {
|
||||
type LogoutReq struct {
|
||||
}
|
||||
|
||||
type MaterialJson struct {
|
||||
IdCardFront string `json:"idCardFront"` // 身份证正面照片URL
|
||||
IdCardBack string `json:"idCardBack"` // 身份证反面照片URL
|
||||
GameScreenshots []string `json:"gameScreenshots"` // 游戏截图URL列表
|
||||
VoiceDemo string `json:"voiceDemo"` // 语音认证示例URL
|
||||
}
|
||||
|
||||
type RegisterReq struct {
|
||||
Phone string `json:"phone,omitempty,optional"`
|
||||
Email string `json:"email,omitempty,"`
|
||||
|
||||
Reference in New Issue
Block a user