diff --git a/backend/verifyCode/api/verifyCode/verifyCode.pb.go b/backend/verifyCode/api/verifyCode/verifyCode.pb.go new file mode 100644 index 0000000..712c79c --- /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.32.0 +// protoc v4.24.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, 0x38, 0x0a, 0x0e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x65, 0x72, + 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x50, 0x01, 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 = []interface{}{ + (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 interface{}, i int) interface{} { + 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 interface{}, i int) interface{} { + 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..210c666 --- /dev/null +++ b/backend/verifyCode/api/verifyCode/verifyCode.proto @@ -0,0 +1,31 @@ +syntax = "proto3"; + +package api.verifyCode; + +option go_package = "verifyCode/api/verifyCode;verifyCode"; +option java_multiple_files = true; +option java_package = "api.verifyCode"; + +service VerifyCode { + rpc GetVerifyCode (GetVerifyCodeRequest) returns (GetVerifyCodeReply); +} + +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; +} 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..525bec1 --- /dev/null +++ b/backend/verifyCode/api/verifyCode/verifyCode_grpc.pb.go @@ -0,0 +1,109 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.3.0 +// - protoc v4.24.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.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +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) { + out := new(GetVerifyCodeReply) + err := c.cc.Invoke(ctx, VerifyCode_GetVerifyCode_FullMethodName, in, out, opts...) + 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..c0c2072 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" @@ -80,6 +82,7 @@ func main() { } defer cleanup() + rand.Seed(time.Now().Unix()) //改变随机数种子 // 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 0391a99..349c5cd 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/internal/server/grpc.go b/backend/verifyCode/internal/server/grpc.go index 5849968..e066699 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,10 @@ 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, + verifyCoderService *service.VerifyCodeService, + logger log.Logger) *grpc.Server { var opts = []grpc.ServerOption{ grpc.Middleware( recovery.Recovery(), @@ -28,5 +32,6 @@ func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, logger log.L } srv := grpc.NewServer(opts...) v1.RegisterGreeterServer(srv, greeter) + verifyCode.RegisterVerifyCodeServer(srv, verifyCoderService) 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..865cc84 --- /dev/null +++ b/backend/verifyCode/internal/service/verifycode.go @@ -0,0 +1,80 @@ +package service + +import ( + "context" + "math/rand" + "strings" + 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) { + return &pb.GetVerifyCodeReply{ + Code: RandCode(int(req.Length), req.Type), + }, nil +} + +func RandCode(l int, t pb.TYPE) string { + switch t { + case pb.TYPE_DEFAULT: + fallthrough //跟下面那个保持一样 + case pb.TYPE_DIGIT: + return randCode("0123456789", 4, l) + case pb.TYPE_LETTER: + return randCode("abcdefghijklmnopqrstuvwxyz", 5, l) + case pb.TYPE_MIXED: + return randCode("0123456789abcdefghijklnmopqrstuvwxyz", 6, l) + default: + } + return "" +} + +//// 最简单实现版本 +//func randCode(chars string, l int) string { +// charsLen := len(chars) +// result := make([]byte, l) +// for i := 0; i < l; i++ { +// randIndex := rand.Intn(charsLen) +// result[i] = chars[randIndex] +// } +// return string(result) +//} + +func randCode(chars string, idxBits, l int) string { + // 形成掩码 + idxMask := 1<= 0; { + // 随机缓存不足,重新生成 + if remain == 0 { + cache, remain = rand.Int63(), idxMax + } + // 利用掩码生成随机索引,有效索引为小于字符集合长度 + if idx := int(cache & int64(idxMask)); idx < len(chars) { + sb.WriteByte(chars[idx]) + i-- + } + // 利用下一组随机数位 + cache >>= idxBits + remain-- + } + + return sb.String() +} 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: {}