Refactor code

pull/131/head
wenxu12345 3 years ago
parent e964ff0b70
commit 0fce4c6177

@ -10,12 +10,13 @@ import (
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils"
"context"
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"net/http"
"strings"
jsonData "Open_IM/internal/utils"
)
func KickGroupMember(c *gin.Context) {
@ -167,56 +168,28 @@ func GetGroupAllMemberList(c *gin.Context) {
memberListResp.MemberList = []*open_im_sdk.GroupMemberFullInfo{}
}
jsm := &jsonpb.Marshaler{
OrigName: true,
EnumsAsInts: false,
EmitDefaults: true,
}
if len(memberListResp.MemberList) > 0 {
for _, v := range memberListResp.MemberList {
s, err := jsm.MarshalToString(v)
log.NewDebug(req.OperationID, "MarshalToString ", s, err)
m := ProtoToMap(memberListResp.MemberList[0], false)
log.NewDebug(req.OperationID, "mmm ", m)
memberListResp.Test = append(memberListResp.Test, m)
}
}
memberListResp.Test = JsonData(memberListResp.MemberList)
//jsm := &jsonpb.Marshaler{
// OrigName: true,
// EnumsAsInts: false,
// EmitDefaults: true,
//}
//
//if len(memberListResp.MemberList) > 0 {
// for _, v := range memberListResp.MemberList {
// s, err := jsm.MarshalToString(v)
// log.NewDebug(req.OperationID, "MarshalToString ", s, err)
// m := ProtoToMap(memberListResp.MemberList[0], false)
// log.NewDebug(req.OperationID, "mmm ", m)
// memberListResp.Test = append(memberListResp.Test, m)
// }
//
//}
memberListResp.Test = jsonData.JsonDataList(memberListResp.MemberList)
log.NewInfo(req.OperationID, "GetGroupAllMember api return ", memberListResp)
c.JSON(http.StatusOK, memberListResp)
}
func JsonData(resp interface{}) []map[string]interface{} {
var result []map[string]interface{}
for _, v := range resp.([]proto.Message) {
m := ProtoToMap(v.(proto.Message), false)
result = append(result, m)
}
return result
}
func ProtoToMap(pb proto.Message, idFix bool) map[string]interface{} {
marshaler := jsonpb.Marshaler{
OrigName: true,
EnumsAsInts: false,
EmitDefaults: true,
}
s, _ := marshaler.MarshalToString(pb)
out := make(map[string]interface{})
json.Unmarshal([]byte(s), &out)
if idFix {
if _, ok := out["id"]; ok {
out["_id"] = out["id"]
delete(out, "id")
}
}
return out
}
func GetJoinedGroupList(c *gin.Context) {
params := api.GetJoinedGroupListReq{}
if err := c.BindJSON(&params); err != nil {

@ -0,0 +1,39 @@
package utils
import (
"encoding/json"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)
func JsonDataList(resp interface{}) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
for _, v := range resp.([]proto.Message) {
m := ProtoToMap(v.(proto.Message), false)
result = append(result, m)
}
return result
}
func JsonDataOne(pb proto.Message) map[string]interface{} {
return ProtoToMap(pb, false)
}
func ProtoToMap(pb proto.Message, idFix bool) map[string]interface{} {
marshaler := jsonpb.Marshaler{
OrigName: true,
EnumsAsInts: false,
EmitDefaults: true,
}
s, _ := marshaler.MarshalToString(pb)
out := make(map[string]interface{})
json.Unmarshal([]byte(s), &out)
if idFix {
if _, ok := out["id"]; ok {
out["_id"] = out["id"]
delete(out, "id")
}
}
return out
}

@ -69,8 +69,8 @@ type GetGroupAllMemberReq struct {
}
type GetGroupAllMemberResp struct {
CommResp
MemberList []*open_im_sdk.GroupMemberFullInfo `json:"data"`
Test []map[string]interface{} `json:"data2"`
MemberList []*open_im_sdk.GroupMemberFullInfo
Test []map[string]interface{} `json:"data"`
}
type CreateGroupReq struct {

Loading…
Cancel
Save