mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
ReceiveMessageOpt
This commit is contained in:
parent
52498bf05e
commit
9dbad9ce53
@ -6,6 +6,7 @@ import (
|
|||||||
"Open_IM/pkg/common/token_verify"
|
"Open_IM/pkg/common/token_verify"
|
||||||
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
||||||
"Open_IM/pkg/proto/user"
|
"Open_IM/pkg/proto/user"
|
||||||
|
"Open_IM/pkg/utils"
|
||||||
"context"
|
"context"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -18,10 +19,15 @@ type paramsSetReceiveMessageOpt struct {
|
|||||||
ConversationIdList []string `json:"conversationIdList" binding:"required"`
|
ConversationIdList []string `json:"conversationIdList" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OptResult struct {
|
||||||
|
ConversationId string `json:"conversationId" binding:"required"`
|
||||||
|
Result int32 `json:"result" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
type SetReceiveMessageOptResp struct {
|
type SetReceiveMessageOptResp struct {
|
||||||
ErrCode int32 `json:"errCode"`
|
ErrCode int32 `json:"errCode"`
|
||||||
ErrMsg string `json:"errMsg"`
|
ErrMsg string `json:"errMsg"`
|
||||||
Data []*user.OptResult `json:"data"`
|
Data []OptResult `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type paramGetReceiveMessageOpt struct {
|
type paramGetReceiveMessageOpt struct {
|
||||||
@ -41,6 +47,8 @@ type GetAllConversationMessageOptResp struct {
|
|||||||
SetReceiveMessageOptResp
|
SetReceiveMessageOptResp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//CopyStructFields
|
||||||
|
|
||||||
func GetAllConversationMessageOpt(c *gin.Context) {
|
func GetAllConversationMessageOpt(c *gin.Context) {
|
||||||
params := paramGetAllConversationMessageOpt{}
|
params := paramGetAllConversationMessageOpt{}
|
||||||
if err := c.BindJSON(¶ms); err != nil {
|
if err := c.BindJSON(¶ms); err != nil {
|
||||||
@ -72,7 +80,15 @@ func GetAllConversationMessageOpt(c *gin.Context) {
|
|||||||
var ginResp GetAllConversationMessageOptResp
|
var ginResp GetAllConversationMessageOptResp
|
||||||
ginResp.ErrCode = resp.ErrCode
|
ginResp.ErrCode = resp.ErrCode
|
||||||
ginResp.ErrMsg = resp.ErrMsg
|
ginResp.ErrMsg = resp.ErrMsg
|
||||||
ginResp.Data = resp.ConversationOptResult
|
for _, v := range resp.ConversationOptResult {
|
||||||
|
var opt OptResult
|
||||||
|
err := utils.CopyStructFields(&opt, v)
|
||||||
|
if err != nil {
|
||||||
|
log.NewError(req.OperationID, "CopyStructFields failed ", err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ginResp.Data = append(ginResp.Data, opt)
|
||||||
|
}
|
||||||
log.NewInfo(req.OperationID, "GetAllConversationMsgOpt resp: ", ginResp, req)
|
log.NewInfo(req.OperationID, "GetAllConversationMsgOpt resp: ", ginResp, req)
|
||||||
c.JSON(http.StatusOK, ginResp)
|
c.JSON(http.StatusOK, ginResp)
|
||||||
}
|
}
|
||||||
@ -109,7 +125,16 @@ func GetReceiveMessageOpt(c *gin.Context) {
|
|||||||
var ginResp GetReceiveMessageOptResp
|
var ginResp GetReceiveMessageOptResp
|
||||||
ginResp.ErrCode = resp.ErrCode
|
ginResp.ErrCode = resp.ErrCode
|
||||||
ginResp.ErrMsg = resp.ErrMsg
|
ginResp.ErrMsg = resp.ErrMsg
|
||||||
ginResp.Data = resp.ConversationOptResult
|
|
||||||
|
for _, v := range resp.ConversationOptResult {
|
||||||
|
var opt OptResult
|
||||||
|
err := utils.CopyStructFields(&opt, v)
|
||||||
|
if err != nil {
|
||||||
|
log.NewError(req.OperationID, "CopyStructFields failed ", err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ginResp.Data = append(ginResp.Data, opt)
|
||||||
|
}
|
||||||
log.NewInfo(req.OperationID, "GetReceiveMessageOpt resp: ", ginResp)
|
log.NewInfo(req.OperationID, "GetReceiveMessageOpt resp: ", ginResp)
|
||||||
c.JSON(http.StatusOK, ginResp)
|
c.JSON(http.StatusOK, ginResp)
|
||||||
}
|
}
|
||||||
@ -148,7 +173,16 @@ func SetReceiveMessageOpt(c *gin.Context) {
|
|||||||
ginResp := SetReceiveMessageOptResp{
|
ginResp := SetReceiveMessageOptResp{
|
||||||
ErrCode: resp.ErrCode,
|
ErrCode: resp.ErrCode,
|
||||||
ErrMsg: resp.ErrMsg,
|
ErrMsg: resp.ErrMsg,
|
||||||
Data: resp.OptResult,
|
}
|
||||||
|
|
||||||
|
for _, v := range resp.OptResult {
|
||||||
|
var opt OptResult
|
||||||
|
err := utils.CopyStructFields(&opt, v)
|
||||||
|
if err != nil {
|
||||||
|
log.NewError(req.OperationID, "CopyStructFields failed ", err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ginResp.Data = append(ginResp.Data, opt)
|
||||||
}
|
}
|
||||||
log.NewInfo(req.OperationID, "SetReceiveMessageOpt resp: ", ginResp)
|
log.NewInfo(req.OperationID, "SetReceiveMessageOpt resp: ", ginResp)
|
||||||
c.JSON(http.StatusOK, ginResp)
|
c.JSON(http.StatusOK, ginResp)
|
||||||
|
55
pkg/utils/utils.go
Normal file
55
pkg/utils/utils.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
// copy a by b b->a
|
||||||
|
func CopyStructFields(a interface{}, b interface{}, fields ...string) (err error) {
|
||||||
|
at := reflect.TypeOf(a)
|
||||||
|
av := reflect.ValueOf(a)
|
||||||
|
bt := reflect.TypeOf(b)
|
||||||
|
bv := reflect.ValueOf(b)
|
||||||
|
|
||||||
|
if at.Kind() != reflect.Ptr {
|
||||||
|
err = fmt.Errorf("a must be a struct pointer")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
av = reflect.ValueOf(av.Interface())
|
||||||
|
|
||||||
|
_fields := make([]string, 0)
|
||||||
|
if len(fields) > 0 {
|
||||||
|
_fields = fields
|
||||||
|
} else {
|
||||||
|
for i := 0; i < bv.NumField(); i++ {
|
||||||
|
_fields = append(_fields, bt.Field(i).Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(_fields) == 0 {
|
||||||
|
err = fmt.Errorf("no fields to copy")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(_fields); i++ {
|
||||||
|
name := _fields[i]
|
||||||
|
f := av.Elem().FieldByName(name)
|
||||||
|
bValue := bv.FieldByName(name)
|
||||||
|
|
||||||
|
if f.IsValid() && f.Kind() == bValue.Kind() {
|
||||||
|
f.Set(bValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type S1 struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
|
||||||
|
type S2 struct {
|
||||||
|
Name string
|
||||||
|
Age int32
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user