This commit is contained in:
withchao 2023-01-12 09:55:20 +08:00
parent 35c0e8303c
commit d142988390
2 changed files with 53 additions and 45 deletions

View File

@ -12,8 +12,6 @@ import (
rpc "Open_IM/pkg/proto/group" rpc "Open_IM/pkg/proto/group"
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
"context" "context"
"google.golang.org/grpc/metadata"
"github.com/golang/protobuf/ptypes/wrappers" "github.com/golang/protobuf/ptypes/wrappers"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -1256,48 +1254,58 @@ func SetGroupMemberInfo(c *gin.Context) {
} }
func GetGroupAbstractInfo(c *gin.Context) { func GetGroupAbstractInfo(c *gin.Context) {
var ( type GetGroupAbstractInfoReq struct {
req api.GetGroupAbstractInfoReq //OperationID string `json:"operationID"`
resp api.GetGroupAbstractInfoResp GroupID string `json:"groupID"`
)
nCtx := trace_log.NewCtx(c, utils.GetSelfFuncName())
if err := c.BindJSON(&req); err != nil {
log.NewError("0", "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
} }
ok, opUserID, errInfo := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID) type GetGroupAbstractInfoResp struct {
if !ok { GroupMemberNumber int32 `json:"groupMemberNumber"`
errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token") GroupMemberListHash uint64 `json:"groupMemberListHash"`
log.NewError(req.OperationID, errMsg)
c.JSON(http.StatusBadRequest, gin.H{"errCode": 500, "errMsg": errMsg})
return
} }
common.ApiToRpc(c, &api.GetGroupAbstractInfoReq{}, &GetGroupAbstractInfoResp{}, config.Config.RpcRegisterName.OpenImGroupName, rpc.NewGroupClient, utils.GetSelfFuncName(), token_verify.ParseUserIDFromToken)
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, req.OperationID) //var (
if etcdConn == nil { //req api.GetGroupAbstractInfoReq
errMsg := req.OperationID + "getcdv3.GetDefaultConn == nil" //resp api.GetGroupAbstractInfoResp
log.NewError(req.OperationID, errMsg) //)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg}) //nCtx := trace_log.NewCtx(c, utils.GetSelfFuncName())
return //if err := c.BindJSON(&req); err != nil {
} // log.NewError("0", "BindJSON failed ", err.Error())
client := rpc.NewGroupClient(etcdConn) // c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
md := metadata.Pairs("operationID", req.OperationID, "opUserID", opUserID) // return
respPb, err := client.GetGroupAbstractInfo(metadata.NewOutgoingContext(c, md), &rpc.GetGroupAbstractInfoReq{ //}
GroupID: req.GroupID, //ok, opUserID, errInfo := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
OpUserID: opUserID, //if !ok {
OperationID: req.OperationID, // errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token")
}) // log.NewError(req.OperationID, errMsg)
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " api args ", respPb.String()) // c.JSON(http.StatusBadRequest, gin.H{"errCode": 500, "errMsg": errMsg})
if err != nil { // return
//log.NewError(req.OperationID, utils.GetSelfFuncName(), " failed ", err.Error()) //}
//c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()}) //
trace_log.WriteErrorResponse(nCtx, "GetGroupAbstractInfo", utils.Wrap(err, "")) //etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, req.OperationID)
return //if etcdConn == nil {
} // errMsg := req.OperationID + "getcdv3.GetDefaultConn == nil"
resp.GroupMemberNumber = respPb.GroupMemberNumber // log.NewError(req.OperationID, errMsg)
resp.GroupMemberListHash = respPb.GroupMemberListHash // c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " api return ", resp) // return
c.JSON(http.StatusOK, resp) //}
return //client := rpc.NewGroupClient(etcdConn)
//md := metadata.Pairs("operationID", req.OperationID, "opUserID", opUserID)
//respPb, err := client.GetGroupAbstractInfo(metadata.NewOutgoingContext(c, md), &rpc.GetGroupAbstractInfoReq{
// GroupID: req.GroupID,
// OpUserID: opUserID,
// OperationID: req.OperationID,
//})
//log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " api args ", respPb.String())
//if err != nil {
// //log.NewError(req.OperationID, utils.GetSelfFuncName(), " failed ", err.Error())
// //c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
// trace_log.WriteErrorResponse(nCtx, "GetGroupAbstractInfo", utils.Wrap(err, ""))
// return
//}
//resp.GroupMemberNumber = respPb.GroupMemberNumber
//resp.GroupMemberListHash = respPb.GroupMemberListHash
//log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " api return ", resp)
//c.JSON(http.StatusOK, resp)
//return
} }

View File

@ -9,7 +9,7 @@ import (
"reflect" "reflect"
) )
func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, fn 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, tokenFunc func(token string, operationID string) (string, error)) {
operationID := c.GetHeader("operationID") operationID := c.GetHeader("operationID")
nCtx := trace_log.NewCtx(c, rpcFuncName) nCtx := trace_log.NewCtx(c, rpcFuncName)
defer trace_log.ShowLog(nCtx) defer trace_log.ShowLog(nCtx)
@ -24,7 +24,7 @@ func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, fn in
trace_log.WriteErrorResponse(nCtx, "GetDefaultConn", err) trace_log.WriteErrorResponse(nCtx, "GetDefaultConn", err)
return return
} }
rpc := reflect.ValueOf(fn).Call([]reflect.Value{ rpc := reflect.ValueOf(rpcClientFunc).Call([]reflect.Value{
reflect.ValueOf(etcdConn), reflect.ValueOf(etcdConn),
})[0].MethodByName(rpcFuncName) // rpc func })[0].MethodByName(rpcFuncName) // rpc func
rpcReqPtr := reflect.New(rpc.Type().In(1).Elem()) // *req参数 rpcReqPtr := reflect.New(rpc.Type().In(1).Elem()) // *req参数