From 9bee5c79c8189eeafe524ff16be688a6ef6584ff Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Fri, 31 Dec 2021 09:25:11 +0800 Subject: [PATCH] Refactor code --- internal/api/friend/friend.go | 16 ++++++---------- internal/api/group/group.go | 9 +++------ internal/api/user/user.go | 2 ++ pkg/base_info/friend_api_struct.go | 21 ++++++++++----------- pkg/base_info/group_api_struct.go | 7 +++---- pkg/base_info/user_api_struct.go | 3 ++- 6 files changed, 26 insertions(+), 32 deletions(-) diff --git a/internal/api/friend/friend.go b/internal/api/friend/friend.go index bbb31236c..eb755879b 100644 --- a/internal/api/friend/friend.go +++ b/internal/api/friend/friend.go @@ -1,6 +1,7 @@ package friend import ( + jsonData "Open_IM/internal/utils" api "Open_IM/pkg/base_info" "Open_IM/pkg/common/config" "Open_IM/pkg/common/log" @@ -214,10 +215,11 @@ func GetBlacklist(c *gin.Context) { resp := api.GetBlackListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}} for _, v := range RpcResp.BlackUserInfoList { - black := api.BlackUserInfo{} + black := open_im_sdk.PublicUserInfo{} utils.CopyStructFields(&black, v) resp.BlackUserInfoList = append(resp.BlackUserInfoList, &black) } + resp.Data = jsonData.JsonDataList(resp.BlackUserInfoList) log.NewInfo(req.CommID.OperationID, "GetBlacklist api return ", resp) c.JSON(http.StatusOK, resp) } @@ -381,9 +383,7 @@ func GetFriendList(c *gin.Context) { } resp := api.GetFriendListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}, FriendInfoList: RpcResp.FriendInfoList} - if len(resp.FriendInfoList) == 0 { - resp.FriendInfoList = []*open_im_sdk.FriendInfo{} - } + resp.Data = jsonData.JsonDataList(resp.FriendInfoList) log.NewInfo(req.CommID.OperationID, "GetFriendList api return ", resp) c.JSON(http.StatusOK, resp) } @@ -417,9 +417,7 @@ func GetFriendApplyList(c *gin.Context) { } resp := api.GetFriendApplyListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}, FriendRequestList: RpcResp.FriendRequestList} - if len(resp.FriendRequestList) == 0 { - resp.FriendRequestList = []*open_im_sdk.FriendRequest{} - } + resp.Data = jsonData.JsonDataList(resp.FriendRequestList) log.NewInfo(req.CommID.OperationID, "GetFriendApplyList api return ", resp) c.JSON(http.StatusOK, resp) } @@ -451,9 +449,7 @@ func GetSelfApplyList(c *gin.Context) { return } resp := api.GetSelfApplyListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}, FriendRequestList: RpcResp.FriendRequestList} - if len(resp.FriendRequestList) == 0 { - resp.FriendRequestList = []*open_im_sdk.FriendRequest{} - } + resp.Data = jsonData.JsonDataList(resp.FriendRequestList) log.NewInfo(req.CommID.OperationID, "GetSelfApplyList api return ", resp) c.JSON(http.StatusOK, resp) } diff --git a/internal/api/group/group.go b/internal/api/group/group.go index 2fa527e67..ef3c599cb 100644 --- a/internal/api/group/group.go +++ b/internal/api/group/group.go @@ -271,6 +271,7 @@ func CreateGroup(c *gin.Context) { resp := api.CreateGroupResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}} if RpcResp.ErrCode == 0 { utils.CopyStructFields(&resp.GroupInfo, RpcResp.GroupInfo) + resp.Data = jsonData.JsonDataOne(&resp.GroupInfo) } log.NewInfo(req.OperationID, "CreateGroup api return ", resp) c.JSON(http.StatusOK, resp) @@ -305,9 +306,7 @@ func GetGroupApplicationList(c *gin.Context) { } resp := api.GetGroupApplicationListResp{CommResp: api.CommResp{ErrCode: reply.ErrCode, ErrMsg: reply.ErrMsg}, GroupRequestList: reply.GroupRequestList} - if len(resp.GroupRequestList) == 0 { - resp.GroupRequestList = []*open_im_sdk.GroupRequest{} - } + resp.Data = jsonData.JsonDataList(resp.GroupRequestList) log.NewInfo(req.OperationID, "GetGroupApplicationList api return ", resp) c.JSON(http.StatusOK, resp) } @@ -340,9 +339,7 @@ func GetGroupsInfo(c *gin.Context) { } resp := api.GetGroupInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}, GroupInfoList: RpcResp.GroupInfoList} - if len(resp.GroupInfoList) == 0 { - resp.GroupInfoList = []*open_im_sdk.GroupInfo{} - } + resp.Data = jsonData.JsonDataList(resp.GroupInfoList) log.NewInfo(req.OperationID, "GetGroupsInfo api return ", resp) c.JSON(http.StatusOK, resp) } diff --git a/internal/api/user/user.go b/internal/api/user/user.go index 3a2620d51..ebc5decf7 100644 --- a/internal/api/user/user.go +++ b/internal/api/user/user.go @@ -1,6 +1,7 @@ package user import ( + jsonData "Open_IM/internal/utils" api "Open_IM/pkg/base_info" "Open_IM/pkg/common/config" "Open_IM/pkg/common/log" @@ -43,6 +44,7 @@ func GetUserInfo(c *gin.Context) { } resp := api.GetUserInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}, UserInfoList: RpcResp.UserInfoList} + resp.Data = jsonData.JsonDataList(resp.UserInfoList) log.NewInfo(req.OperationID, "GetUserInfo api return ", resp) c.JSON(http.StatusOK, resp) } diff --git a/pkg/base_info/friend_api_struct.go b/pkg/base_info/friend_api_struct.go index 653730fc0..4b99b71e3 100644 --- a/pkg/base_info/friend_api_struct.go +++ b/pkg/base_info/friend_api_struct.go @@ -39,8 +39,6 @@ type AddFriendResp struct { type AddFriendResponseReq struct { ParamsCommFriend - //binding:"oneof=0 1 2"` - //} Flag int32 `json:"flag" binding:"required,oneof=-1 0 1"` HandleMsg string `json:"handleMsg"` } @@ -61,7 +59,8 @@ type GetBlackListReq struct { } type GetBlackListResp struct { CommResp - BlackUserInfoList []*BlackUserInfo `json:"data"` + BlackUserInfoList []*open_im_sdk.PublicUserInfo + Data []map[string]interface{} `json:"data"` } //type PublicUserInfo struct { @@ -71,10 +70,6 @@ type GetBlackListResp struct { // Gender int32 `json:"gender"` //} -type BlackUserInfo struct { - open_im_sdk.PublicUserInfo -} - type SetFriendRemarkReq struct { ParamsCommFriend Remark string `json:"remark" binding:"required"` @@ -106,7 +101,8 @@ type GetFriendsInfoReq struct { } type GetFriendsInfoResp struct { CommResp - FriendInfoList []*open_im_sdk.FriendInfo `json:"data"` + FriendInfoList []*open_im_sdk.FriendInfo + Data []map[string]interface{} `json:"data"` } type GetFriendListReq struct { @@ -115,7 +111,8 @@ type GetFriendListReq struct { } type GetFriendListResp struct { CommResp - FriendInfoList []*open_im_sdk.FriendInfo `json:"data"` + FriendInfoList []*open_im_sdk.FriendInfo + Data []map[string]interface{} `json:"data"` } type GetFriendApplyListReq struct { @@ -124,7 +121,8 @@ type GetFriendApplyListReq struct { } type GetFriendApplyListResp struct { CommResp - FriendRequestList []*open_im_sdk.FriendRequest `json:"data"` + FriendRequestList []*open_im_sdk.FriendRequest + Data []map[string]interface{} `json:"data"` } type GetSelfApplyListReq struct { @@ -133,5 +131,6 @@ type GetSelfApplyListReq struct { } type GetSelfApplyListResp struct { CommResp - FriendRequestList []*open_im_sdk.FriendRequest `json:"data"` + FriendRequestList []*open_im_sdk.FriendRequest + Data []map[string]interface{} `json:"data"` } diff --git a/pkg/base_info/group_api_struct.go b/pkg/base_info/group_api_struct.go index 4817d2669..69c124561 100644 --- a/pkg/base_info/group_api_struct.go +++ b/pkg/base_info/group_api_struct.go @@ -73,8 +73,7 @@ type GetGroupAllMemberReq struct { type GetGroupAllMemberResp struct { CommResp MemberList []*open_im_sdk.GroupMemberFullInfo - - Data []map[string]interface{} `json:"data"` + Data []map[string]interface{} `json:"data"` } type CreateGroupReq struct { @@ -84,10 +83,10 @@ type CreateGroupReq struct { GroupType int32 `json:"groupType"` OperationID string `json:"operationID" binding:"required"` } - type CreateGroupResp struct { CommResp - GroupInfo open_im_sdk.GroupInfo `json:"data"` + GroupInfo open_im_sdk.GroupInfo + Data map[string]interface{} `json:"data"` } type GetGroupApplicationListReq struct { diff --git a/pkg/base_info/user_api_struct.go b/pkg/base_info/user_api_struct.go index 8b9d54b4f..d8412f069 100644 --- a/pkg/base_info/user_api_struct.go +++ b/pkg/base_info/user_api_struct.go @@ -10,7 +10,8 @@ type GetUserInfoReq struct { } type GetUserInfoResp struct { CommResp - UserInfoList []*open_im_sdk.UserInfo `json:"data"` + UserInfoList []*open_im_sdk.UserInfo + Data []map[string]interface{} `json:"data"` } type UpdateUserInfoReq struct {