lanjianrong 4 rokov pred
rodič
commit
76d685b686
4 zmenil súbory, kde vykonal 48 pridanie a 57 odobranie
  1. 25 25
      proto/safe.pb.go
  2. 1 1
      proto/safe.proto
  3. 16 29
      services/safe_service.go
  4. 6 2
      web/api/safe_rpc_api.go

+ 25 - 25
proto/safe.pb.go

@@ -82,7 +82,7 @@ type SafePostRequest struct {
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
 
-	BidsectionId int32  `protobuf:"varint,1,opt,name=bidsectionId,proto3" json:"bidsectionId,omitempty"`
+	BidsectionId int32  `protobuf:"varint,1,opt,name=bidsection_id,json=bidsectionId,proto3" json:"bidsection_id,omitempty"`
 	Code         string `protobuf:"bytes,2,opt,name=code,proto3" json:"code,omitempty"`
 	Position     string `protobuf:"bytes,3,opt,name=position,proto3" json:"position,omitempty"`
 	CreateTime   string `protobuf:"bytes,4,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty"`
@@ -226,30 +226,30 @@ var file_safe_proto_rawDesc = []byte{
 	0x0a, 0x0a, 0x73, 0x61, 0x66, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, 0x72,
 	0x6f, 0x74, 0x6f, 0x22, 0x20, 0x0a, 0x0e, 0x53, 0x61, 0x66, 0x65, 0x47, 0x65, 0x74, 0x52, 0x65,
 	0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
-	0x05, 0x52, 0x02, 0x69, 0x64, 0x22, 0xa6, 0x01, 0x0a, 0x0f, 0x53, 0x61, 0x66, 0x65, 0x50, 0x6f,
-	0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x22, 0x0a, 0x0c, 0x62, 0x69, 0x64,
-	0x73, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52,
-	0x0c, 0x62, 0x69, 0x64, 0x73, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x12, 0x0a,
-	0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f, 0x64,
-	0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x03, 0x20,
-	0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1f, 0x0a,
-	0x0b, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01,
-	0x28, 0x09, 0x52, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x1e,
-	0x0a, 0x0a, 0x69, 0x6e, 0x73, 0x70, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x05, 0x20, 0x01,
-	0x28, 0x09, 0x52, 0x0a, 0x69, 0x6e, 0x73, 0x70, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x45,
-	0x0a, 0x09, 0x53, 0x61, 0x66, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x12, 0x0a, 0x04, 0x63,
-	0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12,
-	0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64,
-	0x61, 0x74, 0x61, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09,
-	0x52, 0x03, 0x6d, 0x73, 0x67, 0x32, 0x78, 0x0a, 0x04, 0x53, 0x61, 0x66, 0x65, 0x12, 0x38, 0x0a,
-	0x0b, 0x67, 0x65, 0x74, 0x53, 0x61, 0x66, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x15, 0x2e, 0x70,
-	0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x61, 0x66, 0x65, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75,
-	0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x61, 0x66, 0x65,
-	0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x36, 0x0a, 0x08, 0x70, 0x6f, 0x73, 0x74, 0x53,
-	0x61, 0x66, 0x65, 0x12, 0x16, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x61, 0x66, 0x65,
-	0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x70, 0x72,
-	0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x61, 0x66, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x62,
-	0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+	0x05, 0x52, 0x02, 0x69, 0x64, 0x22, 0xa7, 0x01, 0x0a, 0x0f, 0x53, 0x61, 0x66, 0x65, 0x50, 0x6f,
+	0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x23, 0x0a, 0x0d, 0x62, 0x69, 0x64,
+	0x73, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05,
+	0x52, 0x0c, 0x62, 0x69, 0x64, 0x73, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x12, 0x12,
+	0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x6f,
+	0x64, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x03,
+	0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1f,
+	0x0a, 0x0b, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x04, 0x20,
+	0x01, 0x28, 0x09, 0x52, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x12,
+	0x1e, 0x0a, 0x0a, 0x69, 0x6e, 0x73, 0x70, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x05, 0x20,
+	0x01, 0x28, 0x09, 0x52, 0x0a, 0x69, 0x6e, 0x73, 0x70, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x22,
+	0x45, 0x0a, 0x09, 0x53, 0x61, 0x66, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x12, 0x0a, 0x04,
+	0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65,
+	0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04,
+	0x64, 0x61, 0x74, 0x61, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x03, 0x20, 0x01, 0x28,
+	0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x32, 0x78, 0x0a, 0x04, 0x53, 0x61, 0x66, 0x65, 0x12, 0x38,
+	0x0a, 0x0b, 0x67, 0x65, 0x74, 0x53, 0x61, 0x66, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x15, 0x2e,
+	0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x61, 0x66, 0x65, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71,
+	0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x61, 0x66,
+	0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x36, 0x0a, 0x08, 0x70, 0x6f, 0x73, 0x74,
+	0x53, 0x61, 0x66, 0x65, 0x12, 0x16, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x61, 0x66,
+	0x65, 0x50, 0x6f, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x70,
+	0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x61, 0x66, 0x65, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00,
+	0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
 }
 
 var (

+ 1 - 1
proto/safe.proto

@@ -14,7 +14,7 @@ message SafeGetRequest {
     int32 id = 1;
 }
 message SafePostRequest {
-  int32 bidsectionId = 1;
+  int32 bidsection_id = 1;
   string code = 2;
   string position = 3;
   string create_time = 4;

+ 16 - 29
services/safe_service.go

@@ -19,14 +19,13 @@ import (
 )
 
 type SafeService interface {
-	Get(id int) (int32, string, []viewmodels.Safe)
-	Post(bidsectionId int, code string, position string, createTime string, inspection string) (int32, string)
+	Get(id int, RpcConnect *grpc.ClientConn) (int32, string, []viewmodels.Safe)
+	Post(bidsectionId int, code string, position string, createTime string, inspection string, RpcConnect *grpc.ClientConn) (int32, string)
 	ValidRule(ctx iris.Context) (viewmodels.Safe, error)
 }
 
 // //返回service操作类
 type safeService struct {
-	rpcClient   *grpc.ClientConn
 	valid       string
 	validCreate string
 }
@@ -34,30 +33,19 @@ type safeService struct {
 //创建项目用户service
 func NewSafeService() SafeService {
 	return &safeService{
-		rpcClient:   newGrpcClient(conf.NodeRpcHost),
 		valid:       "/api/safe",
 		validCreate: "/api/safe/create",
 	}
 }
 
-func newGrpcClient(address string) *grpc.ClientConn {
-	// 启动grpc客户端,连接grpc服务端
-	conn, err := grpc.Dial(address, grpc.WithInsecure())
-	if err != nil {
-		log.Fatalf("did not connect: %v", err)
-	}
-	// defer conn.Close()
-	return conn
-}
-
-func (s *safeService) Get(id int) (int32, string, []viewmodels.Safe) {
+func (s *safeService) Get(id int, RpcConnect *grpc.ClientConn) (int32, string, []viewmodels.Safe) {
 	// 1.结束后关闭
 	// defer s.rpcClient.Close()
 	// 使用连接,创建HelloService实例
 	safeCmList := make([]models.CmSafe, 0)
 	safeList := make([]viewmodels.Safe, 0)
 
-	rpcClient := rpc.NewSafeClient(s.rpcClient)
+	rpcClient := rpc.NewSafeClient(RpcConnect)
 
 	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
 	defer cancel()
@@ -97,8 +85,8 @@ func (s *safeService) Get(id int) (int32, string, []viewmodels.Safe) {
 	return res.GetCode(), res.GetMsg(), safeList
 }
 
-func (s *safeService) Post(bidsectionId int, code string, position string, createTime string, inspection string) (int32, string) {
-	rpcClient := rpc.NewSafeClient(s.rpcClient)
+func (s *safeService) Post(bidsectionId int, code string, position string, createTime string, inspection string, RpcConnect *grpc.ClientConn) (int32, string) {
+	rpcClient := rpc.NewSafeClient(RpcConnect)
 
 	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
 	defer cancel()
@@ -112,24 +100,23 @@ func (s *safeService) Post(bidsectionId int, code string, position string, creat
 
 func (s *safeService) ValidRule(ctx iris.Context) (viewmodels.Safe, error) {
 	safeVaild := viewmodels.Safe{}
-	err := ctx.ReadForm(&safeVaild)
-	//ReadJSON
-	if err != nil {
-		log.Println("account-ValidRule-ReadForm转换异常, error=", err)
-		return safeVaild, err
-	}
-
 	if ctx.Path() == s.valid {
+		err := ctx.ReadForm(&safeVaild)
+		if err != nil {
+			log.Println("safe-ValidRule-ReadJson转换异常, error=", err)
+			return safeVaild, err
+		}
 		err = safeVaild.ValidateList()
 	}
 
 	if ctx.Path() == s.validCreate {
+		err := ctx.ReadJSON(&safeVaild)
+		if err != nil {
+			log.Println("safe-ValidRule-ReadForm转换异常, error=", err)
+			return safeVaild, err
+		}
 		err = safeVaild.ValidateCreate()
 	}
-	if err != nil {
-		log.Println("获取安全巡检列表, error=", err)
-		return safeVaild, err
-	}
 	return safeVaild, nil
 
 }

+ 6 - 2
web/api/safe_rpc_api.go

@@ -13,6 +13,7 @@ import (
 	"github.com/kataras/iris/v12"
 	"go.mod/services"
 	"go.mod/web/utils"
+	"google.golang.org/grpc"
 )
 
 type SafeRpcApi struct {
@@ -33,6 +34,8 @@ type SafeRpcApi struct {
 // @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
 // @Router /api/safe [get]
 func (c *SafeRpcApi) Get() {
+
+	RpcConnect := c.Ctx.Values().Get("RpcConnect").(*grpc.ClientConn)
 	// 1.规则验证
 	safeData, err := c.ServiceSafe.ValidRule(c.Ctx)
 	if err != nil {
@@ -45,7 +48,7 @@ func (c *SafeRpcApi) Get() {
 		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
 		return
 	}
-	c.ServiceSafe.Get(bidsectionId)
+	c.ServiceSafe.Get(bidsectionId, RpcConnect)
 	code, msg, data := c.ServiceSafe.Get(bidsectionId)
 
 	c.Ctx.JSON(iris.Map{
@@ -82,7 +85,8 @@ func (c *SafeRpcApi) PostCreate() {
 		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
 		return
 	}
-	code, msg := c.ServiceSafe.Post(bidsectionId, safeData.Position, safeData.Code, safeData.CreateTime, safeData.Inspection)
+	RpcConnect := c.Ctx.Values().Get("RpcConnect").(*grpc.ClientConn)
+	code, msg := c.ServiceSafe.Post(bidsectionId, safeData.Position, safeData.Code, safeData.CreateTime, safeData.Inspection, RpcConnect)
 
 	c.Ctx.JSON(iris.Map{
 		"code": code,