test-errcode
Gordon 2 years ago
parent 33a09acdee
commit 71156eca21

@ -2,7 +2,6 @@ package api
import (
"context"
"fmt"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp"
"github.com/OpenIMSDK/Open-IM-Server/pkg/apistruct"
@ -16,7 +15,6 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"github.com/golang/protobuf/proto"
"github.com/mitchellh/mapstructure"
@ -150,38 +148,14 @@ func (m *Message) AddMessageReactionExtensions(c *gin.Context) {
func (m *Message) DeleteMessageReactionExtensions(c *gin.Context) {
a2r.Call(msg.MsgClient.DeleteMessageReactionExtensions, m.client, c)
}
func RequiredIf(fl validator.FieldLevel) bool {
sessionType := fl.Parent().FieldByName("SessionType").Int()
switch sessionType {
case 1, 4:
fmt.Println("1", sessionType)
if fl.StructFieldName() == "RecvID" {
fmt.Println("2", sessionType)
return fl.Field().String() != ""
}
case 2, 3:
fmt.Println("3", sessionType)
if fl.StructFieldName() == "GroupID" {
fmt.Println("4", sessionType)
return fl.Field().String() != ""
}
default:
return true
}
return true
}
func (m *Message) SendMessage(c *gin.Context) {
params := apistruct.ManagementSendMsgReq{}
if err := c.BindJSON(&params); err != nil {
apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap())
return
}
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
_ = v.RegisterValidation("required_if", RequiredIf)
}
var data interface{}
switch params.ContentType {
case constant.Text:

@ -1,12 +1,15 @@
package api
import (
"fmt"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"github.com/go-redis/redis/v8"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
@ -19,6 +22,9 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, rdb redis.Universal
//gin.DefaultWriter = io.MultiWriter(f)
//gin.SetMode(gin.DebugMode)
r := gin.New()
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
_ = v.RegisterValidation("required_if", RequiredIf)
}
log.Info("load config: ", config.Config)
r.Use(gin.Recovery(), mw.CorsHandler(), mw.GinParseOperationID())
if config.Config.Prometheus.Enable {
@ -156,3 +162,27 @@ func NewGinRouter(zk discoveryregistry.SvcDiscoveryRegistry, rdb redis.Universal
}
return r
}
func RequiredIf(fl validator.FieldLevel) bool {
sessionType := fl.Parent().FieldByName("SessionType").Int()
switch sessionType {
case 1, 4:
fmt.Println("1", sessionType)
if fl.FieldName() == "RecvID" {
fmt.Println("2", sessionType)
return fl.Field().String() != ""
}
case 2, 3:
fmt.Println("3", sessionType)
if fl.FieldName() == "GroupID" {
fmt.Println("4", sessionType)
return fl.Field().String() != ""
}
default:
return true
}
return true
}

Loading…
Cancel
Save