refactor: localize protobuf definitions
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/ec/order/internal/logic/cart"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type CartServer struct {
|
||||
@@ -21,16 +21,16 @@ func (s *CartServer) Fetch(ctx context.Context, in *pb.CartGetRequest) (*pb.Cart
|
||||
}
|
||||
|
||||
// 将商品增加至购物车
|
||||
func (s *CartServer) Create(ctx context.Context, in *pb.CartAddRequest) (*pb.StatusReply, error) {
|
||||
func (s *CartServer) Create(ctx context.Context, in *pb.CartAddRequest) (*pb.OrderStatusReply, error) {
|
||||
return cart.Create(ctx, in)
|
||||
}
|
||||
|
||||
// 修改购物车中的商品数量
|
||||
func (s *CartServer) Modify(ctx context.Context, in *pb.CartSetRequest) (*pb.StatusReply, error) {
|
||||
func (s *CartServer) Modify(ctx context.Context, in *pb.CartSetRequest) (*pb.OrderStatusReply, error) {
|
||||
return cart.Modify(ctx, in)
|
||||
}
|
||||
|
||||
// 删除购物车中的商品
|
||||
func (s *CartServer) Delete(ctx context.Context, in *pb.CartDelRequest) (*pb.StatusReply, error) {
|
||||
func (s *CartServer) Delete(ctx context.Context, in *pb.CartDelRequest) (*pb.OrderStatusReply, error) {
|
||||
return cart.Delete(ctx, in)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/ec/order/internal/logic/coupon"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type CouponServer struct {
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/ec/order/internal/logic/mgt"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type MgtServer struct {
|
||||
@@ -16,17 +16,17 @@ func NewMgtServer() *MgtServer {
|
||||
}
|
||||
|
||||
// 创建订单
|
||||
func (s *MgtServer) OrderCreate(ctx context.Context, in *pb.CreateOrderRequest) (*pb.StatusReply, error) {
|
||||
func (s *MgtServer) OrderCreate(ctx context.Context, in *pb.CreateOrderRequest) (*pb.OrderStatusReply, error) {
|
||||
return mgt.OrderCreate(ctx, in)
|
||||
}
|
||||
|
||||
// 修改订单
|
||||
func (s *MgtServer) OrderModify(ctx context.Context, in *pb.OrderSummaryItem) (*pb.StatusReply, error) {
|
||||
func (s *MgtServer) OrderModify(ctx context.Context, in *pb.OrderSummaryItem) (*pb.OrderStatusReply, error) {
|
||||
return mgt.OrderModify(ctx, in)
|
||||
}
|
||||
|
||||
// 获取一个订单的详情数据
|
||||
func (s *MgtServer) OrderGet(ctx context.Context, in *pb.IdentRequest) (*pb.OrderGetReply, error) {
|
||||
func (s *MgtServer) OrderGet(ctx context.Context, in *pb.OrderIdentRequest) (*pb.OrderGetReply, error) {
|
||||
return mgt.OrderGet(ctx, in)
|
||||
}
|
||||
|
||||
@@ -36,16 +36,16 @@ func (s *MgtServer) OrderListByStore(ctx context.Context, in *pb.OrderListByStor
|
||||
}
|
||||
|
||||
// 取消订单
|
||||
func (s *MgtServer) OrderCancel(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *MgtServer) OrderCancel(ctx context.Context, in *pb.OrderIdentRequest) (*pb.OrderStatusReply, error) {
|
||||
return mgt.OrderCancel(ctx, in)
|
||||
}
|
||||
|
||||
// 退货
|
||||
func (s *MgtServer) OrderReturnable(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *MgtServer) OrderReturnable(ctx context.Context, in *pb.OrderIdentRequest) (*pb.OrderStatusReply, error) {
|
||||
return mgt.OrderReturnable(ctx, in)
|
||||
}
|
||||
|
||||
// 订单审批
|
||||
func (s *MgtServer) OrderApprove(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *MgtServer) OrderApprove(ctx context.Context, in *pb.OrderIdentRequest) (*pb.OrderStatusReply, error) {
|
||||
return mgt.OrderApprove(ctx, in)
|
||||
}
|
||||
|
||||
@@ -2,28 +2,27 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"context"
|
||||
|
||||
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Grpc *grpc.Server
|
||||
Ctx context.Context
|
||||
Mux *gwRuntime.ServeMux
|
||||
Grpc *grpc.Server
|
||||
Ctx context.Context
|
||||
Mux *gwRuntime.ServeMux
|
||||
grpcConns map[string]*grpc.ClientConn // 连接池
|
||||
}
|
||||
|
||||
func New(addr string) *Server {
|
||||
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux(
|
||||
gwRuntime.WithForwardResponseRewriter(responseEnvelope),
|
||||
)}
|
||||
srv := &Server{
|
||||
Ctx: context.Background(),
|
||||
Grpc: grpc.NewServer(),
|
||||
grpcConns: make(map[string]*grpc.ClientConn),
|
||||
}
|
||||
|
||||
// register service to grpc.Server
|
||||
pb.RegisterCartServer(srv.Grpc, NewCartServer())
|
||||
@@ -33,45 +32,5 @@ func New(addr string) *Server {
|
||||
|
||||
reflection.Register(srv.Grpc)
|
||||
|
||||
// 将服务注册到Gateway
|
||||
opts := []grpc.DialOption{grpc.WithInsecure()}
|
||||
pb.RegisterCartHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterCouponHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterMgtHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterSummaryHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
|
||||
// Register services swagger
|
||||
srv.RegisterSwagger()
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
// RegisterSwagger 注册swagger
|
||||
func (s *Server) RegisterSwagger() {
|
||||
srvKey := strings.ToLower(vars.ServiceKey)
|
||||
s.Mux.HandlePath("GET", "/"+srvKey+".swagger.json", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
bytes, err := os.ReadFile("./swagger/" + srvKey + ".swagger.json")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
w.Write(bytes)
|
||||
return
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// response envelope
|
||||
func responseEnvelope(_ context.Context, response proto.Message) (interface{}, error) {
|
||||
name := string(response.ProtoReflect().Descriptor().Name())
|
||||
if name == "Status" || name == "Error" || name == "StatusReply" {
|
||||
return response, nil
|
||||
}
|
||||
return map[string]any{
|
||||
"code": 0,
|
||||
"message": "OK",
|
||||
"result": response,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/ec/order/internal/logic/summary"
|
||||
pb "bsm/full/module/ec/order/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type SummaryServer struct {
|
||||
@@ -16,22 +16,22 @@ func NewSummaryServer() *SummaryServer {
|
||||
}
|
||||
|
||||
// 快速创建一个店铺订单
|
||||
func (s *SummaryServer) QuickCreateByProduct(ctx context.Context, in *pb.QuickCreateByProductRequest) (*pb.StatusReply, error) {
|
||||
func (s *SummaryServer) QuickCreateByProduct(ctx context.Context, in *pb.QuickCreateByProductRequest) (*pb.OrderStatusReply, error) {
|
||||
return summary.QuickCreateByProduct(ctx, in)
|
||||
}
|
||||
|
||||
// 将购物车的数据提交生成订单
|
||||
func (s *SummaryServer) Submit(ctx context.Context, in *pb.SubmitRequest) (*pb.StatusReply, error) {
|
||||
func (s *SummaryServer) Submit(ctx context.Context, in *pb.SubmitRequest) (*pb.OrderStatusReply, error) {
|
||||
return summary.Submit(ctx, in)
|
||||
}
|
||||
|
||||
// 检测是否有未确认及付款的订单
|
||||
func (s *SummaryServer) Check(ctx context.Context, in *pb.Empty) (*pb.StatusReply, error) {
|
||||
func (s *SummaryServer) Check(ctx context.Context, in *pb.Empty) (*pb.OrderStatusReply, error) {
|
||||
return summary.Check(ctx, in)
|
||||
}
|
||||
|
||||
// 获取一个订单的详情数据
|
||||
func (s *SummaryServer) Get(ctx context.Context, in *pb.IdentRequest) (*pb.SummaryGetReply, error) {
|
||||
func (s *SummaryServer) Get(ctx context.Context, in *pb.OrderIdentRequest) (*pb.SummaryGetReply, error) {
|
||||
return summary.Get(ctx, in)
|
||||
}
|
||||
|
||||
@@ -46,21 +46,21 @@ func (s *SummaryServer) Confirm(ctx context.Context, in *pb.ConfirmRequest) (*pb
|
||||
}
|
||||
|
||||
// 取消订单
|
||||
func (s *SummaryServer) Cancel(ctx context.Context, in *pb.CancelRequest) (*pb.StatusReply, error) {
|
||||
func (s *SummaryServer) Cancel(ctx context.Context, in *pb.CancelRequest) (*pb.OrderStatusReply, error) {
|
||||
return summary.Cancel(ctx, in)
|
||||
}
|
||||
|
||||
// 模拟支付
|
||||
func (s *SummaryServer) SimulatePay(ctx context.Context, in *pb.SimulatePayRequest) (*pb.StatusReply, error) {
|
||||
func (s *SummaryServer) SimulatePay(ctx context.Context, in *pb.SimulatePayRequest) (*pb.OrderStatusReply, error) {
|
||||
return summary.SimulatePay(ctx, in)
|
||||
}
|
||||
|
||||
// 模拟发货
|
||||
func (s *SummaryServer) SimulateShipments(ctx context.Context, in *pb.SimulateShipmentsRequest) (*pb.StatusReply, error) {
|
||||
func (s *SummaryServer) SimulateShipments(ctx context.Context, in *pb.SimulateShipmentsRequest) (*pb.OrderStatusReply, error) {
|
||||
return summary.SimulateShipments(ctx, in)
|
||||
}
|
||||
|
||||
// 模拟收货
|
||||
func (s *SummaryServer) SimulateReceiving(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *SummaryServer) SimulateReceiving(ctx context.Context, in *pb.OrderIdentRequest) (*pb.OrderStatusReply, error) {
|
||||
return summary.SimulateReceiving(ctx, in)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user