jiangshuai
2023-11-23 18c42494735992c42d63c7858f5fdb31069d0342
获取客户列表服务端
4个文件已添加
2个文件已修改
436 ■■■■■ 已修改文件
main.go 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/client.go 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/client.proto 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/client/client.pb.go 275 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/client/client_grpc.pb.go 109 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/client/server.go 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main.go
@@ -8,6 +8,7 @@
    "aps_crm/model"
    "aps_crm/model/grpc_init"
    "aps_crm/pkg/logx"
    "aps_crm/proto/client"
    "aps_crm/proto/crm_aps"
    "aps_crm/proto/product_inventory"
    "aps_crm/router"
@@ -65,6 +66,7 @@
        //todo 添加具体服务
        product_inventory.RegisterProductInventoryServiceServer(s, &product_inventory.Server{})
        crm_aps.RegisterCrmAndApsGrpcServiceServer(s, &crm_aps.Server{})
        client.RegisterClientServiceServer(s, &client.Server{})
        err = s.Serve(ln)
        if err != nil {
            logx.Errorf("grpc server init error: %v", err.Error())
model/client.go
@@ -241,3 +241,11 @@
    slf.Orm = slf.Orm.Where("id in (?)", ids)
    return slf
}
func (slf *ClientSearch) FindAll() ([]*Client, error) {
    var db = slf.build()
    var records = make([]*Client, 0)
    err := db.Find(&records).Error
    return records, err
}
proto/client.proto
New file
@@ -0,0 +1,20 @@
syntax = "proto3";
option go_package = "./client";
service ClientService {
  rpc GetClientList(ClientListRequest) returns (ClientListResponse);
}
message ClientListRequest {
}
message ClientInfo {
  int32 ClientId =1;
  string ClientName =2;
}
message ClientListResponse {
  repeated ClientInfo List =1;
}
proto/client/client.pb.go
New file
@@ -0,0 +1,275 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
//     protoc-gen-go v1.30.0
//     protoc        v3.8.0
// source: client.proto
package client
import (
    protoreflect "google.golang.org/protobuf/reflect/protoreflect"
    protoimpl "google.golang.org/protobuf/runtime/protoimpl"
    reflect "reflect"
    sync "sync"
)
const (
    // Verify that this generated code is sufficiently up-to-date.
    _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
    // Verify that runtime/protoimpl is sufficiently up-to-date.
    _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type ClientListRequest struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields
}
func (x *ClientListRequest) Reset() {
    *x = ClientListRequest{}
    if protoimpl.UnsafeEnabled {
        mi := &file_client_proto_msgTypes[0]
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        ms.StoreMessageInfo(mi)
    }
}
func (x *ClientListRequest) String() string {
    return protoimpl.X.MessageStringOf(x)
}
func (*ClientListRequest) ProtoMessage() {}
func (x *ClientListRequest) ProtoReflect() protoreflect.Message {
    mi := &file_client_proto_msgTypes[0]
    if protoimpl.UnsafeEnabled && x != nil {
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        if ms.LoadMessageInfo() == nil {
            ms.StoreMessageInfo(mi)
        }
        return ms
    }
    return mi.MessageOf(x)
}
// Deprecated: Use ClientListRequest.ProtoReflect.Descriptor instead.
func (*ClientListRequest) Descriptor() ([]byte, []int) {
    return file_client_proto_rawDescGZIP(), []int{0}
}
type ClientInfo struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields
    ClientId   int32  `protobuf:"varint,1,opt,name=ClientId,proto3" json:"ClientId,omitempty"`
    ClientName string `protobuf:"bytes,2,opt,name=ClientName,proto3" json:"ClientName,omitempty"`
}
func (x *ClientInfo) Reset() {
    *x = ClientInfo{}
    if protoimpl.UnsafeEnabled {
        mi := &file_client_proto_msgTypes[1]
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        ms.StoreMessageInfo(mi)
    }
}
func (x *ClientInfo) String() string {
    return protoimpl.X.MessageStringOf(x)
}
func (*ClientInfo) ProtoMessage() {}
func (x *ClientInfo) ProtoReflect() protoreflect.Message {
    mi := &file_client_proto_msgTypes[1]
    if protoimpl.UnsafeEnabled && x != nil {
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        if ms.LoadMessageInfo() == nil {
            ms.StoreMessageInfo(mi)
        }
        return ms
    }
    return mi.MessageOf(x)
}
// Deprecated: Use ClientInfo.ProtoReflect.Descriptor instead.
func (*ClientInfo) Descriptor() ([]byte, []int) {
    return file_client_proto_rawDescGZIP(), []int{1}
}
func (x *ClientInfo) GetClientId() int32 {
    if x != nil {
        return x.ClientId
    }
    return 0
}
func (x *ClientInfo) GetClientName() string {
    if x != nil {
        return x.ClientName
    }
    return ""
}
type ClientListResponse struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields
    List []*ClientInfo `protobuf:"bytes,1,rep,name=List,proto3" json:"List,omitempty"`
}
func (x *ClientListResponse) Reset() {
    *x = ClientListResponse{}
    if protoimpl.UnsafeEnabled {
        mi := &file_client_proto_msgTypes[2]
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        ms.StoreMessageInfo(mi)
    }
}
func (x *ClientListResponse) String() string {
    return protoimpl.X.MessageStringOf(x)
}
func (*ClientListResponse) ProtoMessage() {}
func (x *ClientListResponse) ProtoReflect() protoreflect.Message {
    mi := &file_client_proto_msgTypes[2]
    if protoimpl.UnsafeEnabled && x != nil {
        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
        if ms.LoadMessageInfo() == nil {
            ms.StoreMessageInfo(mi)
        }
        return ms
    }
    return mi.MessageOf(x)
}
// Deprecated: Use ClientListResponse.ProtoReflect.Descriptor instead.
func (*ClientListResponse) Descriptor() ([]byte, []int) {
    return file_client_proto_rawDescGZIP(), []int{2}
}
func (x *ClientListResponse) GetList() []*ClientInfo {
    if x != nil {
        return x.List
    }
    return nil
}
var File_client_proto protoreflect.FileDescriptor
var file_client_proto_rawDesc = []byte{
    0x0a, 0x0c, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x13,
    0x0a, 0x11, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75,
    0x65, 0x73, 0x74, 0x22, 0x48, 0x0a, 0x0a, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x6e, 0x66,
    0x6f, 0x12, 0x1a, 0x0a, 0x08, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x18, 0x01, 0x20,
    0x01, 0x28, 0x05, 0x52, 0x08, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x1e, 0x0a,
    0x0a, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28,
    0x09, 0x52, 0x0a, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x22, 0x35, 0x0a,
    0x12, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f,
    0x6e, 0x73, 0x65, 0x12, 0x1f, 0x0a, 0x04, 0x4c, 0x69, 0x73, 0x74, 0x18, 0x01, 0x20, 0x03, 0x28,
    0x0b, 0x32, 0x0b, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x6e, 0x66, 0x6f, 0x52, 0x04,
    0x4c, 0x69, 0x73, 0x74, 0x32, 0x49, 0x0a, 0x0d, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x53, 0x65,
    0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x38, 0x0a, 0x0d, 0x47, 0x65, 0x74, 0x43, 0x6c, 0x69, 0x65,
    0x6e, 0x74, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x12, 0x2e, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x4c,
    0x69, 0x73, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x43, 0x6c, 0x69,
    0x65, 0x6e, 0x74, 0x4c, 0x69, 0x73, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42,
    0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f,
    0x74, 0x6f, 0x33,
}
var (
    file_client_proto_rawDescOnce sync.Once
    file_client_proto_rawDescData = file_client_proto_rawDesc
)
func file_client_proto_rawDescGZIP() []byte {
    file_client_proto_rawDescOnce.Do(func() {
        file_client_proto_rawDescData = protoimpl.X.CompressGZIP(file_client_proto_rawDescData)
    })
    return file_client_proto_rawDescData
}
var file_client_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_client_proto_goTypes = []interface{}{
    (*ClientListRequest)(nil),  // 0: ClientListRequest
    (*ClientInfo)(nil),         // 1: ClientInfo
    (*ClientListResponse)(nil), // 2: ClientListResponse
}
var file_client_proto_depIdxs = []int32{
    1, // 0: ClientListResponse.List:type_name -> ClientInfo
    0, // 1: ClientService.GetClientList:input_type -> ClientListRequest
    2, // 2: ClientService.GetClientList:output_type -> ClientListResponse
    2, // [2:3] is the sub-list for method output_type
    1, // [1:2] is the sub-list for method input_type
    1, // [1:1] is the sub-list for extension type_name
    1, // [1:1] is the sub-list for extension extendee
    0, // [0:1] is the sub-list for field type_name
}
func init() { file_client_proto_init() }
func file_client_proto_init() {
    if File_client_proto != nil {
        return
    }
    if !protoimpl.UnsafeEnabled {
        file_client_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
            switch v := v.(*ClientListRequest); i {
            case 0:
                return &v.state
            case 1:
                return &v.sizeCache
            case 2:
                return &v.unknownFields
            default:
                return nil
            }
        }
        file_client_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
            switch v := v.(*ClientInfo); i {
            case 0:
                return &v.state
            case 1:
                return &v.sizeCache
            case 2:
                return &v.unknownFields
            default:
                return nil
            }
        }
        file_client_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
            switch v := v.(*ClientListResponse); i {
            case 0:
                return &v.state
            case 1:
                return &v.sizeCache
            case 2:
                return &v.unknownFields
            default:
                return nil
            }
        }
    }
    type x struct{}
    out := protoimpl.TypeBuilder{
        File: protoimpl.DescBuilder{
            GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
            RawDescriptor: file_client_proto_rawDesc,
            NumEnums:      0,
            NumMessages:   3,
            NumExtensions: 0,
            NumServices:   1,
        },
        GoTypes:           file_client_proto_goTypes,
        DependencyIndexes: file_client_proto_depIdxs,
        MessageInfos:      file_client_proto_msgTypes,
    }.Build()
    File_client_proto = out.File
    file_client_proto_rawDesc = nil
    file_client_proto_goTypes = nil
    file_client_proto_depIdxs = nil
}
proto/client/client_grpc.pb.go
New file
@@ -0,0 +1,109 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.3.0
// - protoc             v3.8.0
// source: client.proto
package client
import (
    context "context"
    grpc "google.golang.org/grpc"
    codes "google.golang.org/grpc/codes"
    status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
const (
    ClientService_GetClientList_FullMethodName = "/ClientService/GetClientList"
)
// ClientServiceClient is the client API for ClientService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type ClientServiceClient interface {
    GetClientList(ctx context.Context, in *ClientListRequest, opts ...grpc.CallOption) (*ClientListResponse, error)
}
type clientServiceClient struct {
    cc grpc.ClientConnInterface
}
func NewClientServiceClient(cc grpc.ClientConnInterface) ClientServiceClient {
    return &clientServiceClient{cc}
}
func (c *clientServiceClient) GetClientList(ctx context.Context, in *ClientListRequest, opts ...grpc.CallOption) (*ClientListResponse, error) {
    out := new(ClientListResponse)
    err := c.cc.Invoke(ctx, ClientService_GetClientList_FullMethodName, in, out, opts...)
    if err != nil {
        return nil, err
    }
    return out, nil
}
// ClientServiceServer is the server API for ClientService service.
// All implementations must embed UnimplementedClientServiceServer
// for forward compatibility
type ClientServiceServer interface {
    GetClientList(context.Context, *ClientListRequest) (*ClientListResponse, error)
    mustEmbedUnimplementedClientServiceServer()
}
// UnimplementedClientServiceServer must be embedded to have forward compatible implementations.
type UnimplementedClientServiceServer struct {
}
func (UnimplementedClientServiceServer) GetClientList(context.Context, *ClientListRequest) (*ClientListResponse, error) {
    return nil, status.Errorf(codes.Unimplemented, "method GetClientList not implemented")
}
func (UnimplementedClientServiceServer) mustEmbedUnimplementedClientServiceServer() {}
// UnsafeClientServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to ClientServiceServer will
// result in compilation errors.
type UnsafeClientServiceServer interface {
    mustEmbedUnimplementedClientServiceServer()
}
func RegisterClientServiceServer(s grpc.ServiceRegistrar, srv ClientServiceServer) {
    s.RegisterService(&ClientService_ServiceDesc, srv)
}
func _ClientService_GetClientList_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
    in := new(ClientListRequest)
    if err := dec(in); err != nil {
        return nil, err
    }
    if interceptor == nil {
        return srv.(ClientServiceServer).GetClientList(ctx, in)
    }
    info := &grpc.UnaryServerInfo{
        Server:     srv,
        FullMethod: ClientService_GetClientList_FullMethodName,
    }
    handler := func(ctx context.Context, req interface{}) (interface{}, error) {
        return srv.(ClientServiceServer).GetClientList(ctx, req.(*ClientListRequest))
    }
    return interceptor(ctx, in, info, handler)
}
// ClientService_ServiceDesc is the grpc.ServiceDesc for ClientService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var ClientService_ServiceDesc = grpc.ServiceDesc{
    ServiceName: "ClientService",
    HandlerType: (*ClientServiceServer)(nil),
    Methods: []grpc.MethodDesc{
        {
            MethodName: "GetClientList",
            Handler:    _ClientService_GetClientList_Handler,
        },
    },
    Streams:  []grpc.StreamDesc{},
    Metadata: "client.proto",
}
proto/client/server.go
New file
@@ -0,0 +1,22 @@
package client
import (
    "aps_crm/model"
    "context"
)
type Server struct {
    UnimplementedClientServiceServer
}
func (s *Server) GetClientList(c context.Context, req *ClientListRequest) (*ClientListResponse, error) {
    resp := new(ClientListResponse)
    clientList, err := model.NewClientSearch(nil).FindAll()
    if err != nil {
        return resp, err
    }
    for _, v := range clientList {
        resp.List = append(resp.List, &ClientInfo{ClientId: int32(v.Id), ClientName: v.Name})
    }
    return resp, nil
}