ReceiveMessageOpt

This commit is contained in:
wenxu12345 2021-12-07 15:44:12 +08:00
parent 52498bf05e
commit 9dbad9ce53
2 changed files with 95 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import (
"Open_IM/pkg/common/token_verify"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
"Open_IM/pkg/proto/user"
"Open_IM/pkg/utils"
"context"
"github.com/gin-gonic/gin"
"net/http"
@ -18,10 +19,15 @@ type paramsSetReceiveMessageOpt struct {
ConversationIdList []string `json:"conversationIdList" binding:"required"`
}
type OptResult struct {
ConversationId string `json:"conversationId" binding:"required"`
Result int32 `json:"result" binding:"required"`
}
type SetReceiveMessageOptResp struct {
ErrCode int32 `json:"errCode"`
ErrMsg string `json:"errMsg"`
Data []*user.OptResult `json:"data"`
ErrCode int32 `json:"errCode"`
ErrMsg string `json:"errMsg"`
Data []OptResult `json:"data"`
}
type paramGetReceiveMessageOpt struct {
@ -41,6 +47,8 @@ type GetAllConversationMessageOptResp struct {
SetReceiveMessageOptResp
}
//CopyStructFields
func GetAllConversationMessageOpt(c *gin.Context) {
params := paramGetAllConversationMessageOpt{}
if err := c.BindJSON(&params); err != nil {
@ -72,7 +80,15 @@ func GetAllConversationMessageOpt(c *gin.Context) {
var ginResp GetAllConversationMessageOptResp
ginResp.ErrCode = resp.ErrCode
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)
c.JSON(http.StatusOK, ginResp)
}
@ -109,7 +125,16 @@ func GetReceiveMessageOpt(c *gin.Context) {
var ginResp GetReceiveMessageOptResp
ginResp.ErrCode = resp.ErrCode
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)
c.JSON(http.StatusOK, ginResp)
}
@ -148,7 +173,16 @@ func SetReceiveMessageOpt(c *gin.Context) {
ginResp := SetReceiveMessageOptResp{
ErrCode: resp.ErrCode,
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)
c.JSON(http.StatusOK, ginResp)

55
pkg/utils/utils.go Normal file
View 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
}