diff --git a/internal/api/friend/friend1.go b/internal/api/friend/friend1.go index 88e5c56ea..0eb2ca79a 100644 --- a/internal/api/friend/friend1.go +++ b/internal/api/friend/friend1.go @@ -4,7 +4,6 @@ import ( common "Open_IM/internal/api_to_rpc" api "Open_IM/pkg/base_info" "Open_IM/pkg/common/config" - "Open_IM/pkg/common/token_verify" rpc "Open_IM/pkg/proto/friend" "Open_IM/pkg/utils" "github.com/gin-gonic/gin" @@ -13,20 +12,20 @@ import ( // 不一致 func AddBlacklist(c *gin.Context) { common.ApiToRpc(c, &api.AddBlacklistReq{}, &api.AddBlacklistResp{}, - config.Config.RpcRegisterName.OpenImFriendName, rpc.NewFriendClient, utils.GetSelfFuncName(), token_verify.ParseUserIDFromToken) + config.Config.RpcRegisterName.OpenImFriendName, rpc.NewFriendClient, utils.GetSelfFuncName()) } func ImportFriend1(c *gin.Context) { common.ApiToRpc(c, &api.ImportFriendReq{}, &api.ImportFriendResp{}, - config.Config.RpcRegisterName.OpenImFriendName, rpc.NewFriendClient, utils.GetSelfFuncName(), token_verify.ParseUserIDFromToken) + config.Config.RpcRegisterName.OpenImFriendName, rpc.NewFriendClient, utils.GetSelfFuncName()) } func AddFriend1(c *gin.Context) { common.ApiToRpc(c, &api.AddFriendReq{}, &api.AddFriendResp{}, - config.Config.RpcRegisterName.OpenImFriendName, rpc.NewFriendClient, utils.GetSelfFuncName(), token_verify.ParseUserIDFromToken) + config.Config.RpcRegisterName.OpenImFriendName, rpc.NewFriendClient, utils.GetSelfFuncName()) } func AddFriendResponse1(c *gin.Context) { common.ApiToRpc(c, &api.AddFriendResponseReq{}, &api.AddFriendResponseResp{}, - config.Config.RpcRegisterName.OpenImFriendName, rpc.NewFriendClient, utils.GetSelfFuncName(), token_verify.ParseUserIDFromToken) + config.Config.RpcRegisterName.OpenImFriendName, rpc.NewFriendClient, utils.GetSelfFuncName()) } diff --git a/internal/api/group/group.go b/internal/api/group/group.go index 263400eef..0fd3cf184 100644 --- a/internal/api/group/group.go +++ b/internal/api/group/group.go @@ -445,7 +445,7 @@ func InviteUserToGroup(c *gin.Context) { func NewCreateGroup(c *gin.Context) { common.ApiToRpc(c, &api.CreateGroupReq{}, &api.CreateGroupResp{}, - config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, "CreateGroup", token_verify.ParseUserIDFromToken) + config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, "CreateGroup") } // @Summary 获取用户收到的加群信息列表 @@ -515,7 +515,7 @@ func GetRecvGroupApplicationList(c *gin.Context) { // @Router /group/get_user_req_group_applicationList [post] func GetUserReqGroupApplicationList(c *gin.Context) { common.ApiToRpc(c, &api.GetUserReqGroupApplicationListReq{}, &api.GetUserRespGroupApplicationResp{}, - config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, "GetGroupApplicationList", token_verify.ParseUserIDFromToken) + config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, "GetGroupApplicationList") } // @Summary 通过群ID列表获取群信息 @@ -803,7 +803,7 @@ func QuitGroup(c *gin.Context) { func NewSetGroupInfo(c *gin.Context) { common.ApiToRpc(c, &api.SetGroupInfoReq{}, &api.SetGroupInfoResp{}, - config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, "SetGroupInfo", token_verify.ParseUserIDFromToken) + config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, "SetGroupInfo") } func argsHandle(params *api.SetGroupInfoReq, req *rpc.SetGroupInfoReq) { @@ -1263,7 +1263,7 @@ func GetGroupAbstractInfo(c *gin.Context) { GroupMemberListHash uint64 `json:"groupMemberListHash"` } //common.ApiToRpc(c, &api.GetGroupAbstractInfoReq{}, &GetGroupAbstractInfoResp{}, config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, utils.GetSelfFuncName(), token_verify.ParseUserIDFromToken) - common.ApiToRpc(c, &api.GetGroupAbstractInfoReq{}, &GetGroupAbstractInfoResp{}, config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, utils.GetSelfFuncName(), nil) + common.ApiToRpc(c, &api.GetGroupAbstractInfoReq{}, &GetGroupAbstractInfoResp{}, config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, utils.GetSelfFuncName()) //var ( //req api.GetGroupAbstractInfoReq diff --git a/internal/api_to_rpc/api.go b/internal/api_to_rpc/api.go index c7b4d1238..fc58a01fe 100644 --- a/internal/api_to_rpc/api.go +++ b/internal/api_to_rpc/api.go @@ -12,7 +12,7 @@ import ( "reflect" ) -func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcClientFunc interface{}, rpcFuncName string, tokenFunc func(token string, operationID string) (string, error)) { +func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcClientFunc interface{}, rpcFuncName string) { logFuncName := fmt.Sprintf("[ApiToRpc: %s]%s", utils2.GetFuncName(1), rpcFuncName) operationID := c.GetHeader("operationID") nCtx := trace_log.NewCtx1(c, rpcFuncName, operationID) @@ -31,23 +31,12 @@ func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcCl reflect.ValueOf(etcdConn), })[0].MethodByName(rpcFuncName) // rpc func rpcReqPtr := reflect.New(rpc.Type().In(1).Elem()) // *req参数 - var opUserID string - if tokenFunc != nil { - var err error - opUserID, err = tokenFunc(c.GetHeader("token"), operationID) - if err != nil { - trace_log.WriteErrorResponse(nCtx, "TokenFunc", err) - return - } - } if err := utils.CopyStructFields(rpcReqPtr.Interface(), apiReq); err != nil { trace_log.WriteErrorResponse(nCtx, "CopyStructFields_RpcReq", err) return } - trace_log.SetCtxInfo(nCtx, logFuncName, nil, "opUserID", opUserID, "callRpcReq", rpcString(rpcReqPtr.Elem().Interface())) - //md := metadata.Pairs("operationID", operationID, "opUserID", opUserID) + trace_log.SetCtxInfo(nCtx, logFuncName, nil, "opUserID", c.GetString("opUserID"), "callRpcReq", rpcString(rpcReqPtr.Elem().Interface())) respArr := rpc.Call([]reflect.Value{ - //reflect.ValueOf(metadata.NewOutgoingContext(c, md)), // context.Context reflect.ValueOf(context.Context(c)), // context.Context rpcReqPtr, // rpc apiReq }) // respArr => (apiResp, error) diff --git a/pkg/common/middleware/rpc.go b/pkg/common/middleware/rpc.go index a87d54a0b..cda1c8f00 100644 --- a/pkg/common/middleware/rpc.go +++ b/pkg/common/middleware/rpc.go @@ -4,7 +4,6 @@ import ( "Open_IM/pkg/common/constant" "Open_IM/pkg/common/log" "Open_IM/pkg/common/trace_log" - "Open_IM/pkg/utils" "context" "fmt" "google.golang.org/grpc" @@ -60,25 +59,26 @@ func RpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary return } -func RpcClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) { - //if cc == nil { - // return utils.Wrap(constant.ErrRpcConn, "") - //} - operationID, ok := ctx.Value("operationID").(string) - if !ok { - return utils.Wrap(constant.ErrArgs, "ctx missing operationID") - } - opUserID, ok := ctx.Value("opUserID").(string) - if !ok { - return utils.Wrap(constant.ErrArgs, "ctx missing opUserID") - } - md := metadata.Pairs("operationID", operationID, "opUserID", opUserID) - return invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...) -} - func rpcString(v interface{}) string { if s, ok := v.(interface{ String() string }); ok { return s.String() } return fmt.Sprintf("%+v", v) } + +//func RpcClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) { +// //if cc == nil { +// // return utils.Wrap(constant.ErrRpcConn, "") +// //} +// operationID, ok := ctx.Value("operationID").(string) +// if !ok { +// return utils.Wrap(constant.ErrArgs, "ctx missing operationID") +// } +// opUserID, ok := ctx.Value("opUserID").(string) +// if !ok { +// return utils.Wrap(constant.ErrArgs, "ctx missing opUserID") +// } +// md := metadata.Pairs("operationID", operationID, "opUserID", opUserID) +// return invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...) +//} +// diff --git a/pkg/common/trace_log/ctx.go b/pkg/common/trace_log/ctx.go index 60f2dbac3..e7e6ffe9c 100644 --- a/pkg/common/trace_log/ctx.go +++ b/pkg/common/trace_log/ctx.go @@ -73,7 +73,7 @@ func WriteErrorResponse(ctx context.Context, funcName string, err error, args .. ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": t.ErrCode, "errMsg": t.ErrMsg, "errDtl": t.DetailErrMsg}) return default: - s, ok := status.FromError(err) + s, ok := status.FromError(e) if !ok { ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": constant.ErrDefaultOther.ErrCode, "errMsg": err.Error(), "errDtl": fmt.Sprintf("%+v", err)}) return