From 5ad6640c995b7ae2ad87cc497aaa526d921020ae Mon Sep 17 00:00:00 2001 From: chenpeng <18485757865@163.com> Date: Sat, 22 Jun 2024 14:18:04 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84=E4=B8=9A=E5=8A=A1=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/verifyCode/verifyCode.pb.go | 293 ++++++++++++++++++ .../api/verifyCode/verifyCode.proto | 29 ++ .../api/verifyCode/verifyCode_grpc.pb.go | 110 +++++++ backend/verifyCode/cmd/verifyCode/main.go | 4 +- backend/verifyCode/cmd/verifyCode/wire_gen.go | 3 +- backend/verifyCode/cs.go | 9 + backend/verifyCode/go.mod | 18 +- backend/verifyCode/go.sum | 46 ++- backend/verifyCode/internal/server/grpc.go | 4 +- .../verifyCode/internal/service/service.go | 2 +- .../verifyCode/internal/service/verifycode.go | 86 +++++ backend/verifyCode/openapi.yaml | 35 +-- 12 files changed, 568 insertions(+), 71 deletions(-) create mode 100644 backend/verifyCode/api/verifyCode/verifyCode.pb.go create mode 100644 backend/verifyCode/api/verifyCode/verifyCode.proto create mode 100644 backend/verifyCode/api/verifyCode/verifyCode_grpc.pb.go create mode 100644 backend/verifyCode/cs.go create mode 100644 backend/verifyCode/internal/service/verifycode.go diff --git a/backend/verifyCode/api/verifyCode/verifyCode.pb.go b/backend/verifyCode/api/verifyCode/verifyCode.pb.go new file mode 100644 index 0000000..ac0f733 --- /dev/null +++ b/backend/verifyCode/api/verifyCode/verifyCode.pb.go @@ -0,0 +1,293 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.34.2 +// protoc v5.26.1 +// source: api/verifyCode/verifyCode.proto + +package verifyCode + +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 TYPE int32 + +const ( + TYPE_DEFAULT TYPE = 0 + TYPE_DIGIT TYPE = 1 + TYPE_LETTER TYPE = 2 + TYPE_MIXED TYPE = 3 +) + +// Enum value maps for TYPE. +var ( + TYPE_name = map[int32]string{ + 0: "DEFAULT", + 1: "DIGIT", + 2: "LETTER", + 3: "MIXED", + } + TYPE_value = map[string]int32{ + "DEFAULT": 0, + "DIGIT": 1, + "LETTER": 2, + "MIXED": 3, + } +) + +func (x TYPE) Enum() *TYPE { + p := new(TYPE) + *p = x + return p +} + +func (x TYPE) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (TYPE) Descriptor() protoreflect.EnumDescriptor { + return file_api_verifyCode_verifyCode_proto_enumTypes[0].Descriptor() +} + +func (TYPE) Type() protoreflect.EnumType { + return &file_api_verifyCode_verifyCode_proto_enumTypes[0] +} + +func (x TYPE) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use TYPE.Descriptor instead. +func (TYPE) EnumDescriptor() ([]byte, []int) { + return file_api_verifyCode_verifyCode_proto_rawDescGZIP(), []int{0} +} + +// 定义 GetVerifyCodeRequest 消息 +type GetVerifyCodeRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // 验证码长度 + Length uint32 `protobuf:"varint,1,opt,name=length,proto3" json:"length,omitempty"` + // 验证码类型 + Type TYPE `protobuf:"varint,2,opt,name=type,proto3,enum=api.verifyCode.TYPE" json:"type,omitempty"` +} + +func (x *GetVerifyCodeRequest) Reset() { + *x = GetVerifyCodeRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_api_verifyCode_verifyCode_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetVerifyCodeRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetVerifyCodeRequest) ProtoMessage() {} + +func (x *GetVerifyCodeRequest) ProtoReflect() protoreflect.Message { + mi := &file_api_verifyCode_verifyCode_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 GetVerifyCodeRequest.ProtoReflect.Descriptor instead. +func (*GetVerifyCodeRequest) Descriptor() ([]byte, []int) { + return file_api_verifyCode_verifyCode_proto_rawDescGZIP(), []int{0} +} + +func (x *GetVerifyCodeRequest) GetLength() uint32 { + if x != nil { + return x.Length + } + return 0 +} + +func (x *GetVerifyCodeRequest) GetType() TYPE { + if x != nil { + return x.Type + } + return TYPE_DEFAULT +} + +// 定义 GetVerifyCodeReply 消息 +type GetVerifyCodeReply struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // 生成的验证码 + Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"` +} + +func (x *GetVerifyCodeReply) Reset() { + *x = GetVerifyCodeReply{} + if protoimpl.UnsafeEnabled { + mi := &file_api_verifyCode_verifyCode_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetVerifyCodeReply) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetVerifyCodeReply) ProtoMessage() {} + +func (x *GetVerifyCodeReply) ProtoReflect() protoreflect.Message { + mi := &file_api_verifyCode_verifyCode_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 GetVerifyCodeReply.ProtoReflect.Descriptor instead. +func (*GetVerifyCodeReply) Descriptor() ([]byte, []int) { + return file_api_verifyCode_verifyCode_proto_rawDescGZIP(), []int{1} +} + +func (x *GetVerifyCodeReply) GetCode() string { + if x != nil { + return x.Code + } + return "" +} + +var File_api_verifyCode_verifyCode_proto protoreflect.FileDescriptor + +var file_api_verifyCode_verifyCode_proto_rawDesc = []byte{ + 0x0a, 0x1f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, + 0x2f, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x12, 0x0e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, + 0x65, 0x22, 0x58, 0x0a, 0x14, 0x47, 0x65, 0x74, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, + 0x64, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6c, 0x65, 0x6e, + 0x67, 0x74, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x6c, 0x65, 0x6e, 0x67, 0x74, + 0x68, 0x12, 0x28, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, + 0x14, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, + 0x2e, 0x54, 0x59, 0x50, 0x45, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x22, 0x28, 0x0a, 0x12, 0x47, + 0x65, 0x74, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x52, 0x65, 0x70, 0x6c, + 0x79, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0x35, 0x0a, 0x04, 0x54, 0x59, 0x50, 0x45, 0x12, 0x0b, 0x0a, + 0x07, 0x44, 0x45, 0x46, 0x41, 0x55, 0x4c, 0x54, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x44, 0x49, + 0x47, 0x49, 0x54, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x4c, 0x45, 0x54, 0x54, 0x45, 0x52, 0x10, + 0x02, 0x12, 0x09, 0x0a, 0x05, 0x4d, 0x49, 0x58, 0x45, 0x44, 0x10, 0x03, 0x32, 0x67, 0x0a, 0x0a, + 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x59, 0x0a, 0x0d, 0x47, 0x65, + 0x74, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x24, 0x2e, 0x61, 0x70, + 0x69, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x2e, 0x47, 0x65, 0x74, + 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x1a, 0x22, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, + 0x64, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, + 0x52, 0x65, 0x70, 0x6c, 0x79, 0x42, 0x26, 0x5a, 0x24, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, + 0x6f, 0x64, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, + 0x64, 0x65, 0x3b, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_api_verifyCode_verifyCode_proto_rawDescOnce sync.Once + file_api_verifyCode_verifyCode_proto_rawDescData = file_api_verifyCode_verifyCode_proto_rawDesc +) + +func file_api_verifyCode_verifyCode_proto_rawDescGZIP() []byte { + file_api_verifyCode_verifyCode_proto_rawDescOnce.Do(func() { + file_api_verifyCode_verifyCode_proto_rawDescData = protoimpl.X.CompressGZIP(file_api_verifyCode_verifyCode_proto_rawDescData) + }) + return file_api_verifyCode_verifyCode_proto_rawDescData +} + +var file_api_verifyCode_verifyCode_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_api_verifyCode_verifyCode_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_api_verifyCode_verifyCode_proto_goTypes = []any{ + (TYPE)(0), // 0: api.verifyCode.TYPE + (*GetVerifyCodeRequest)(nil), // 1: api.verifyCode.GetVerifyCodeRequest + (*GetVerifyCodeReply)(nil), // 2: api.verifyCode.GetVerifyCodeReply +} +var file_api_verifyCode_verifyCode_proto_depIdxs = []int32{ + 0, // 0: api.verifyCode.GetVerifyCodeRequest.type:type_name -> api.verifyCode.TYPE + 1, // 1: api.verifyCode.VerifyCode.GetVerifyCode:input_type -> api.verifyCode.GetVerifyCodeRequest + 2, // 2: api.verifyCode.VerifyCode.GetVerifyCode:output_type -> api.verifyCode.GetVerifyCodeReply + 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_api_verifyCode_verifyCode_proto_init() } +func file_api_verifyCode_verifyCode_proto_init() { + if File_api_verifyCode_verifyCode_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_api_verifyCode_verifyCode_proto_msgTypes[0].Exporter = func(v any, i int) any { + switch v := v.(*GetVerifyCodeRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_verifyCode_verifyCode_proto_msgTypes[1].Exporter = func(v any, i int) any { + switch v := v.(*GetVerifyCodeReply); 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_api_verifyCode_verifyCode_proto_rawDesc, + NumEnums: 1, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_api_verifyCode_verifyCode_proto_goTypes, + DependencyIndexes: file_api_verifyCode_verifyCode_proto_depIdxs, + EnumInfos: file_api_verifyCode_verifyCode_proto_enumTypes, + MessageInfos: file_api_verifyCode_verifyCode_proto_msgTypes, + }.Build() + File_api_verifyCode_verifyCode_proto = out.File + file_api_verifyCode_verifyCode_proto_rawDesc = nil + file_api_verifyCode_verifyCode_proto_goTypes = nil + file_api_verifyCode_verifyCode_proto_depIdxs = nil +} diff --git a/backend/verifyCode/api/verifyCode/verifyCode.proto b/backend/verifyCode/api/verifyCode/verifyCode.proto new file mode 100644 index 0000000..61b27de --- /dev/null +++ b/backend/verifyCode/api/verifyCode/verifyCode.proto @@ -0,0 +1,29 @@ +syntax = "proto3"; + +package api.verifyCode; +option go_package = "verifyCode/api/verifyCode;verifyCode"; + +// 类型常量 +enum TYPE { + DEFAULT = 0; + DIGIT = 1; + LETTER = 2; + MIXED = 3; +}; +// 定义 GetVerifyCodeRequest 消息 +message GetVerifyCodeRequest { + // 验证码长度 + uint32 length = 1; + // 验证码类型 + TYPE type = 2; + +} +// 定义 GetVerifyCodeReply 消息 +message GetVerifyCodeReply { + // 生成的验证码 + string code = 1; +} + +service VerifyCode { + rpc GetVerifyCode (GetVerifyCodeRequest) returns (GetVerifyCodeReply); +} \ No newline at end of file diff --git a/backend/verifyCode/api/verifyCode/verifyCode_grpc.pb.go b/backend/verifyCode/api/verifyCode/verifyCode_grpc.pb.go new file mode 100644 index 0000000..e863f77 --- /dev/null +++ b/backend/verifyCode/api/verifyCode/verifyCode_grpc.pb.go @@ -0,0 +1,110 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.4.0 +// - protoc v5.26.1 +// source: api/verifyCode/verifyCode.proto + +package verifyCode + +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.62.0 or later. +const _ = grpc.SupportPackageIsVersion8 + +const ( + VerifyCode_GetVerifyCode_FullMethodName = "/api.verifyCode.VerifyCode/GetVerifyCode" +) + +// VerifyCodeClient is the client API for VerifyCode 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 VerifyCodeClient interface { + GetVerifyCode(ctx context.Context, in *GetVerifyCodeRequest, opts ...grpc.CallOption) (*GetVerifyCodeReply, error) +} + +type verifyCodeClient struct { + cc grpc.ClientConnInterface +} + +func NewVerifyCodeClient(cc grpc.ClientConnInterface) VerifyCodeClient { + return &verifyCodeClient{cc} +} + +func (c *verifyCodeClient) GetVerifyCode(ctx context.Context, in *GetVerifyCodeRequest, opts ...grpc.CallOption) (*GetVerifyCodeReply, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(GetVerifyCodeReply) + err := c.cc.Invoke(ctx, VerifyCode_GetVerifyCode_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// VerifyCodeServer is the server API for VerifyCode service. +// All implementations must embed UnimplementedVerifyCodeServer +// for forward compatibility +type VerifyCodeServer interface { + GetVerifyCode(context.Context, *GetVerifyCodeRequest) (*GetVerifyCodeReply, error) + mustEmbedUnimplementedVerifyCodeServer() +} + +// UnimplementedVerifyCodeServer must be embedded to have forward compatible implementations. +type UnimplementedVerifyCodeServer struct { +} + +func (UnimplementedVerifyCodeServer) GetVerifyCode(context.Context, *GetVerifyCodeRequest) (*GetVerifyCodeReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetVerifyCode not implemented") +} +func (UnimplementedVerifyCodeServer) mustEmbedUnimplementedVerifyCodeServer() {} + +// UnsafeVerifyCodeServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to VerifyCodeServer will +// result in compilation errors. +type UnsafeVerifyCodeServer interface { + mustEmbedUnimplementedVerifyCodeServer() +} + +func RegisterVerifyCodeServer(s grpc.ServiceRegistrar, srv VerifyCodeServer) { + s.RegisterService(&VerifyCode_ServiceDesc, srv) +} + +func _VerifyCode_GetVerifyCode_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetVerifyCodeRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(VerifyCodeServer).GetVerifyCode(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: VerifyCode_GetVerifyCode_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(VerifyCodeServer).GetVerifyCode(ctx, req.(*GetVerifyCodeRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// VerifyCode_ServiceDesc is the grpc.ServiceDesc for VerifyCode service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var VerifyCode_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "api.verifyCode.VerifyCode", + HandlerType: (*VerifyCodeServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetVerifyCode", + Handler: _VerifyCode_GetVerifyCode_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "api/verifyCode/verifyCode.proto", +} diff --git a/backend/verifyCode/cmd/verifyCode/main.go b/backend/verifyCode/cmd/verifyCode/main.go index 4830be6..9dce354 100644 --- a/backend/verifyCode/cmd/verifyCode/main.go +++ b/backend/verifyCode/cmd/verifyCode/main.go @@ -2,7 +2,9 @@ package main import ( "flag" + "math/rand" "os" + "time" "verifyCode/internal/conf" @@ -79,7 +81,7 @@ func main() { panic(err) } defer cleanup() - + rand.NewSource(time.Now().UnixNano()) // start and wait for stop signal if err := app.Run(); err != nil { panic(err) diff --git a/backend/verifyCode/cmd/verifyCode/wire_gen.go b/backend/verifyCode/cmd/verifyCode/wire_gen.go index a6c75d4..eda4387 100644 --- a/backend/verifyCode/cmd/verifyCode/wire_gen.go +++ b/backend/verifyCode/cmd/verifyCode/wire_gen.go @@ -31,7 +31,8 @@ func wireApp(confServer *conf.Server, confData *conf.Data, logger log.Logger) (* greeterRepo := data.NewGreeterRepo(dataData, logger) greeterUsecase := biz.NewGreeterUsecase(greeterRepo, logger) greeterService := service.NewGreeterService(greeterUsecase) - grpcServer := server.NewGRPCServer(confServer, greeterService, logger) + verifyCodeService := service.NewVerifyCodeService() + grpcServer := server.NewGRPCServer(confServer, greeterService, verifyCodeService, logger) httpServer := server.NewHTTPServer(confServer, greeterService, logger) app := newApp(logger, grpcServer, httpServer) return app, func() { diff --git a/backend/verifyCode/cs.go b/backend/verifyCode/cs.go new file mode 100644 index 0000000..c21c462 --- /dev/null +++ b/backend/verifyCode/cs.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Printf("%E", 121414.4353553535) +} diff --git a/backend/verifyCode/go.mod b/backend/verifyCode/go.mod index 2462bc6..6e0db78 100644 --- a/backend/verifyCode/go.mod +++ b/backend/verifyCode/go.mod @@ -6,9 +6,9 @@ require ( github.com/go-kratos/kratos/v2 v2.7.2 github.com/google/wire v0.6.0 go.uber.org/automaxprocs v1.5.1 - google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529 - google.golang.org/grpc v1.56.3 - google.golang.org/protobuf v1.32.0 + google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 + google.golang.org/grpc v1.64.0 + google.golang.org/protobuf v1.34.2 ) require ( @@ -17,7 +17,6 @@ require ( github.com/go-logr/logr v1.2.4 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-playground/form/v4 v4.2.1 // indirect - github.com/golang/protobuf v1.5.3 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gorilla/mux v1.8.0 // indirect github.com/imdario/mergo v0.3.16 // indirect @@ -25,11 +24,10 @@ require ( go.opentelemetry.io/otel v1.16.0 // indirect go.opentelemetry.io/otel/metric v1.16.0 // indirect go.opentelemetry.io/otel/trace v1.16.0 // indirect - golang.org/x/net v0.20.0 // indirect - golang.org/x/sync v0.6.0 // indirect - golang.org/x/sys v0.16.0 // indirect - golang.org/x/text v0.14.0 // indirect - google.golang.org/genproto v0.0.0-20230629202037-9506855d4529 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230629202037-9506855d4529 // indirect + golang.org/x/net v0.26.0 // indirect + golang.org/x/sync v0.7.0 // indirect + golang.org/x/sys v0.21.0 // indirect + golang.org/x/text v0.16.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/backend/verifyCode/go.sum b/backend/verifyCode/go.sum index 62172ee..c4713db 100644 --- a/backend/verifyCode/go.sum +++ b/backend/verifyCode/go.sum @@ -1,9 +1,9 @@ github.com/census-instrumentation/opencensus-proto v0.4.1 h1:iKLQ0xPNFxR/2hzXZMrBo8f1j86j5WHzznCCQxV/b8g= -github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+gqO04wryn5h75LSazbRlnya1k= +github.com/cncf/xds/go v0.0.0-20240318125728-8a4994d93e50 h1:DBmgJDC9dTfkVyGgipamEh2BpGYxScCH1TOF1LL1cXc= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/envoyproxy/go-control-plane v0.11.2-0.20230627204322-7d0032219fcb h1:kxNVXsNro/lpR5WD+P1FI/yUHn2G03Glber3k8cQL2Y= -github.com/envoyproxy/protoc-gen-validate v0.10.1 h1:c0g45+xCJhdgFGw7a5QAfdS4byAbud7miNWJ1WwEVf8= +github.com/envoyproxy/go-control-plane v0.12.0 h1:4X+VP1GHd1Mhj6IB5mMeGbLCleqxjletLK6K0rbxyZI= +github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= github.com/go-kratos/aegis v0.2.0 h1:dObzCDWn3XVjUkgxyBp6ZeWtx/do0DPZ7LY3yNSJLUQ= @@ -19,12 +19,9 @@ github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBY github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/form/v4 v4.2.1 h1:HjdRDKO0fftVMU5epjPW2SOREcZ6/wLUzEobqUGJuPw= github.com/go-playground/form/v4 v4.2.1/go.mod h1:q1a2BY+AQUUzhl6xA/6hBetay6dEIhMHjgvJiGo6K7U= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= -github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -66,14 +63,16 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= -golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= +golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= +golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -83,8 +82,9 @@ golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -97,8 +97,9 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= @@ -106,19 +107,14 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto v0.0.0-20230629202037-9506855d4529 h1:9JucMWR7sPvCxUFd6UsOUNmA5kCcWOfORaT3tpAsKQs= -google.golang.org/genproto v0.0.0-20230629202037-9506855d4529/go.mod h1:xZnkP7mREFX5MORlOPEzLMr+90PPZQ2QWzrVTWfAq64= -google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529 h1:s5YSX+ZH5b5vS9rnpGymvIyMpLRJizowqDlOuyjXnTk= -google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529/go.mod h1:vHYtlOoi6TsQ3Uk2yxR7NI5z8uoV+3pZtR4jmHIkRig= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230629202037-9506855d4529 h1:DEH99RbiLZhMxrpEJCZ0A+wdTe0EOgou/poSLx9vWf4= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230629202037-9506855d4529/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= -google.golang.org/grpc v1.56.3 h1:8I4C0Yq1EjstUzUJzpcRVbuYA2mODtEmpWiQoN/b2nc= -google.golang.org/grpc v1.56.3/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= -google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 h1:RFiFrvy37/mpSpdySBDrUdipW/dHwsRwh3J3+A9VgT4= +google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237/go.mod h1:Z5Iiy3jtmioajWHDGFk7CeugTyHtPvMHA4UTmUkyalE= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 h1:Di6ANFilr+S60a4S61ZM00vLdw0IrQOSMS2/6mrnOU0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= +google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY= +google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/backend/verifyCode/internal/server/grpc.go b/backend/verifyCode/internal/server/grpc.go index 5849968..5290fc3 100644 --- a/backend/verifyCode/internal/server/grpc.go +++ b/backend/verifyCode/internal/server/grpc.go @@ -2,6 +2,7 @@ package server import ( v1 "verifyCode/api/helloworld/v1" + "verifyCode/api/verifyCode" "verifyCode/internal/conf" "verifyCode/internal/service" @@ -11,7 +12,7 @@ import ( ) // NewGRPCServer new a gRPC server. -func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *grpc.Server { +func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, VerifyCodeService *service.VerifyCodeService, logger log.Logger) *grpc.Server { var opts = []grpc.ServerOption{ grpc.Middleware( recovery.Recovery(), @@ -28,5 +29,6 @@ func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, logger log.L } srv := grpc.NewServer(opts...) v1.RegisterGreeterServer(srv, greeter) + verifyCode.RegisterVerifyCodeServer(srv, VerifyCodeService) return srv } diff --git a/backend/verifyCode/internal/service/service.go b/backend/verifyCode/internal/service/service.go index 5e32840..7ae1096 100644 --- a/backend/verifyCode/internal/service/service.go +++ b/backend/verifyCode/internal/service/service.go @@ -3,4 +3,4 @@ package service import "github.com/google/wire" // ProviderSet is service providers. -var ProviderSet = wire.NewSet(NewGreeterService) +var ProviderSet = wire.NewSet(NewGreeterService, NewVerifyCodeService) diff --git a/backend/verifyCode/internal/service/verifycode.go b/backend/verifyCode/internal/service/verifycode.go new file mode 100644 index 0000000..aaf8ce6 --- /dev/null +++ b/backend/verifyCode/internal/service/verifycode.go @@ -0,0 +1,86 @@ +package service + +import ( + "context" + "math/rand" + + pb "verifyCode/api/verifyCode" +) + +type VerifyCodeService struct { + pb.UnimplementedVerifyCodeServer +} + +func NewVerifyCodeService() *VerifyCodeService { + return &VerifyCodeService{} +} + +func (s *VerifyCodeService) GetVerifyCode(ctx context.Context, req *pb.GetVerifyCodeRequest) (*pb.GetVerifyCodeReply, error) { + //log.Info("current verifyCode service Run") + return &pb.GetVerifyCodeReply{ + Code: RandCode(int(req.Length), req.Type), + }, nil +} + +// RandCode 开放的被调用的方法,用于区分类型 +func RandCode(l int, t pb.TYPE) string { + switch t { + case pb.TYPE_DEFAULT: + fallthrough + case pb.TYPE_DIGIT: + // idxBits表示使用4位二进制数就可以表示完chars的索引了 + return randCode("0123456789", l, 4) + case pb.TYPE_LETTER: + return randCode("abcdefghijklmnopqrstuvwxyz", l, 5) + case pb.TYPE_MIXED: + return randCode("0123456789abcdefghijklmnopqrstuvwxyz", l, 6) + default: + } + return "" +} + +// 随机数的核心方法(优化实现) +// 一次随机多个随机位,分部分多次使用, +// idxBits表示使用4位二进制数就可以表示完chars的索引了 +func randCode(chars string, l, idxBits int) string { + // 计算有效的二进制数位,基于 chars 的长度 + // 推荐写死,因为chars固定,对应的位数长度也固定 + + // 形成掩码,mask + // 例如,使用低idxBits位:00000000000111111 + idxMask := 1<>= idxBits + remain-- + } + return string(result) +} + +// 随机的核心方法(简单的实现) +//func randCode(chars string, l int) string { +// charsLen := len(chars) +// // 结果 +// result := make([]byte, l) +// // 根据目标长度,进行循环 +// for i := 0; i < l; i++ { +// // 核心函数 rand.Intn() 生成[0, n)的整型随机数 +// randIndex := rand.Intn(charsLen) +// // 字符串的单个字符是uint8类型,即byte类型,因此可以赋值 +// result[i] = chars[randIndex] +// } +// return string(result) +//} diff --git a/backend/verifyCode/openapi.yaml b/backend/verifyCode/openapi.yaml index 6867057..f8f3735 100644 --- a/backend/verifyCode/openapi.yaml +++ b/backend/verifyCode/openapi.yaml @@ -3,37 +3,8 @@ openapi: 3.0.3 info: - title: Greeter API - description: The greeting service definition. + title: "" version: 0.0.1 -paths: - /helloworld/{name}: - get: - tags: - - Greeter - - subgroup - description: Sends a greeting - operationId: Greeter_SayHello - parameters: - - name: name - in: path - required: true - schema: - type: string - responses: - "200": - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/helloworld.v1.HelloReply' +paths: {} components: - schemas: - helloworld.v1.HelloReply: - type: object - properties: - message: - type: string - description: The response message containing the greetings -tags: - - name: Greeter + schemas: {}