diff --git a/client/client.go b/client/client.go index a9531bd..fedb3e3 100644 --- a/client/client.go +++ b/client/client.go @@ -1,6 +1,7 @@ package client import ( + "context" "errors" "fmt" "git.oa00.com/supply-chain/service/config" @@ -23,20 +24,41 @@ func init() { basePkgPath = pkgPath[:len(pkgPath)-6] } +type RpcClient struct { + baseName string + serviceName string + client client.XClient +} + +// Call @Title 调用接口 +func (r RpcClient) Call(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) (err error) { + if config.RpcConfig.BeforeHandel != nil { + ctx, err = config.RpcConfig.BeforeHandel(ctx, r.baseName, r.serviceName, serviceMethod, args, reply) + if err != nil { + return err + } + } + // 调用请求 + err = r.client.Call(ctx, serviceMethod, args, reply) + if config.RpcConfig.AfterHandel != nil { + err = config.RpcConfig.AfterHandel(ctx, r.baseName, r.serviceName, serviceMethod, args, reply, err) + } + return err +} + // GetClient @Title 获取RPC客户的 -func GetClient(s interface{}) (client.XClient, error) { +func GetClient(s interface{}) (*RpcClient, error) { path := strings.TrimPrefix(reflect.ValueOf(s).Elem().Type().PkgPath(), basePkgPath) actionName := reflect.ValueOf(s).Elem().Type().Name() key := path + "/" + actionName + split := strings.SplitN(key, "/", 2) + basePath := split[0] + servicePath := split[1] xClient, ok := mClient.Load(key) if !ok { mutex.Lock() xClient, ok = mClient.Load(key) if !ok { - split := strings.SplitN(key, "/", 2) - basePath := split[0] - servicePath := split[1] - d, err := client.NewConsulDiscovery(basePath, servicePath, config.RpcConfig.RegistryServer, nil) if err != nil { return nil, errors.New("系统异常") @@ -51,11 +73,11 @@ func GetClient(s interface{}) (client.XClient, error) { } mutex.Unlock() } - return xClient.(client.XClient), nil + return &RpcClient{client: xClient.(client.XClient), baseName: basePath, serviceName: servicePath}, nil } // GetClientName @Title 根据服务名获取客户端 -func GetClientName(base, service string) (client.XClient, error) { +func GetClientName(base, service string) (*RpcClient, error) { key := fmt.Sprintf("%s/%s", base, service) xClient, ok := mClient.Load(key) if !ok { @@ -77,5 +99,5 @@ func GetClientName(base, service string) (client.XClient, error) { } mutex.Unlock() } - return xClient.(client.XClient), nil + return &RpcClient{client: xClient.(client.XClient), baseName: base, serviceName: service}, nil } diff --git a/config/config.go b/config/config.go index 3270633..662c87c 100644 --- a/config/config.go +++ b/config/config.go @@ -1,9 +1,15 @@ package config +import ( + "context" +) + var RpcConfig = &Config{} type Config struct { RegistryServer []string + BeforeHandel func(ctx context.Context, baseName, serviceName, serviceMethod string, args interface{}, reply interface{}) (context.Context, error) + AfterHandel func(ctx context.Context, baseName, serviceName, serviceMethod string, args interface{}, reply interface{}, err error) error } // InitConfig @Title 初始化配置 diff --git a/supply/sku.go b/supply/sku.go index 12a66d7..b0dcd9b 100644 --- a/supply/sku.go +++ b/supply/sku.go @@ -188,6 +188,7 @@ type SkuInfo struct { SourceName string `json:"sourceName"` AdjustType uint `json:"adjustType"` AdjustPrice decimal.Decimal `json:"adjustPrice"` + SourceStatus uint `json:"sourceStatus"` } // Info @Title 商品信息