Refactor code

pull/131/head
wenxu12345 3 years ago
parent 3737e889ea
commit b30cb5254e

@ -21,7 +21,7 @@ func AddBlacklist(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
req := &rpc.AddBlacklistReq{} req := &rpc.AddBlacklistReq{CommID: &rpc.CommID{}}
utils.CopyStructFields(req.CommID, &params) utils.CopyStructFields(req.CommID, &params)
var ok bool var ok bool
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
@ -84,8 +84,8 @@ func AddFriend(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
req := &rpc.AddFriendReq{} req := &rpc.AddFriendReq{CommID: &rpc.CommID{}}
utils.CopyStructFields(req.CommID, params) utils.CopyStructFields(req.CommID, &params)
var ok bool var ok bool
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
if !ok { if !ok {
@ -116,7 +116,7 @@ func AddFriendResponse(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
req := &rpc.AddFriendResponseReq{} req := &rpc.AddFriendResponseReq{CommID: &rpc.CommID{}}
utils.CopyStructFields(req.CommID, &params) utils.CopyStructFields(req.CommID, &params)
var ok bool var ok bool
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
@ -125,7 +125,7 @@ func AddFriendResponse(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"}) c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"})
return return
} }
utils.CopyStructFields(&req, &params) utils.CopyStructFields(req, &params)
log.NewInfo(req.CommID.OperationID, "AddFriendResponse args ", req.String()) log.NewInfo(req.CommID.OperationID, "AddFriendResponse args ", req.String())
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImFriendName) etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImFriendName)
@ -149,7 +149,7 @@ func DeleteFriend(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
req := &rpc.DeleteFriendReq{} req := &rpc.DeleteFriendReq{CommID: &rpc.CommID{}}
utils.CopyStructFields(req.CommID, &params) utils.CopyStructFields(req.CommID, &params)
var ok bool var ok bool
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
@ -181,7 +181,7 @@ func GetBlacklist(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
req := &rpc.GetBlacklistReq{} req := &rpc.GetBlacklistReq{CommID: &rpc.CommID{}}
utils.CopyStructFields(req.CommID, &params) utils.CopyStructFields(req.CommID, &params)
var ok bool var ok bool
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
@ -218,8 +218,8 @@ func SetFriendComment(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
req := &rpc.SetFriendCommentReq{} req := &rpc.SetFriendCommentReq{CommID: &rpc.CommID{}}
utils.CopyStructFields(req.CommID, params) utils.CopyStructFields(req.CommID, &params)
req.Remark = params.Remark req.Remark = params.Remark
var ok bool var ok bool
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
@ -251,8 +251,8 @@ func RemoveBlacklist(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
req := &rpc.RemoveBlacklistReq{} req := &rpc.RemoveBlacklistReq{CommID: &rpc.CommID{}}
utils.CopyStructFields(req.CommID, params) utils.CopyStructFields(req.CommID, &params)
var ok bool var ok bool
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
if !ok { if !ok {
@ -282,8 +282,8 @@ func IsFriend(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
req := &rpc.IsFriendReq{} req := &rpc.IsFriendReq{CommID: &rpc.CommID{}}
utils.CopyStructFields(req.CommID, params) utils.CopyStructFields(req.CommID, &params)
var ok bool var ok bool
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
if !ok { if !ok {
@ -347,8 +347,8 @@ func GetFriendList(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
req := &rpc.GetFriendListReq{} req := &rpc.GetFriendListReq{CommID: &rpc.CommID{}}
utils.CopyStructFields(req.CommID, params) utils.CopyStructFields(req.CommID, &params)
var ok bool var ok bool
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
if !ok { if !ok {
@ -367,9 +367,9 @@ func GetFriendList(c *gin.Context) {
return return
} }
resp := api.GetFriendListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}} resp := api.GetFriendListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}, FriendInfoList: RpcResp.FriendInfoList}
utils.CopyStructFields(&resp, RpcResp)
log.NewInfo(req.CommID.OperationID, "GetFriendList api return ", resp) log.NewInfo(req.CommID.OperationID, "GetFriendList api return ", resp)
c.JSON(http.StatusOK, resp)
} }
func GetFriendApplyList(c *gin.Context) { func GetFriendApplyList(c *gin.Context) {
@ -379,8 +379,8 @@ func GetFriendApplyList(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
req := &rpc.GetFriendApplyListReq{} req := &rpc.GetFriendApplyListReq{CommID: &rpc.CommID{}}
utils.CopyStructFields(req.CommID, params) utils.CopyStructFields(req.CommID, &params)
var ok bool var ok bool
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
if !ok { if !ok {
@ -400,8 +400,7 @@ func GetFriendApplyList(c *gin.Context) {
return return
} }
resp := api.GetFriendApplyListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}} resp := api.GetFriendApplyListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}, FriendRequestList: RpcResp.FriendRequestList}
utils.CopyStructFields(&resp, RpcResp)
log.NewInfo(req.CommID.OperationID, "GetFriendApplyList api return ", resp) log.NewInfo(req.CommID.OperationID, "GetFriendApplyList api return ", resp)
} }
@ -413,8 +412,8 @@ func GetSelfApplyList(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
req := &rpc.GetSelfApplyListReq{} req := &rpc.GetSelfApplyListReq{CommID: &rpc.CommID{}}
utils.CopyStructFields(req.CommID, params) utils.CopyStructFields(req.CommID, &params)
var ok bool var ok bool
ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) ok, req.CommID.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
if !ok { if !ok {
@ -433,9 +432,7 @@ func GetSelfApplyList(c *gin.Context) {
return return
} }
resp := api.GetSelfApplyListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}} resp := api.GetSelfApplyListResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}, FriendRequestList: RpcResp.FriendRequestList}
utils.CopyStructFields(resp, RpcResp)
c.JSON(http.StatusOK, resp)
log.NewInfo(req.CommID.OperationID, "GetSelfApplyList api return ", resp) log.NewInfo(req.CommID.OperationID, "GetSelfApplyList api return ", resp)
c.JSON(http.StatusOK, resp)
} }

@ -112,7 +112,7 @@ type GetFriendApplyListReq struct {
} }
type GetFriendApplyListResp struct { type GetFriendApplyListResp struct {
CommResp CommResp
FriendRequestList open_im_sdk.FriendRequest `json:"data"` FriendRequestList []*open_im_sdk.FriendRequest `json:"data"`
} }
type GetSelfApplyListReq struct { type GetSelfApplyListReq struct {
@ -120,5 +120,5 @@ type GetSelfApplyListReq struct {
} }
type GetSelfApplyListResp struct { type GetSelfApplyListResp struct {
CommResp CommResp
FriendRequestList open_im_sdk.FriendRequest `json:"data"` FriendRequestList []*open_im_sdk.FriendRequest `json:"data"`
} }

@ -1,49 +1,10 @@
package utils package utils
import ( import (
"fmt"
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
"reflect"
) )
// copy a by b b->a // copy a by b b->a
func CopyStructFields(a interface{}, b interface{}, fields ...string) (err error) { func CopyStructFields(a interface{}, b interface{}, fields ...string) (err error) {
return copier.Copy(a, b) return copier.Copy(a, b)
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
} }

Loading…
Cancel
Save