From 263e03d427b10ed255f786821f198f03eaa98275 Mon Sep 17 00:00:00 2001 From: yinyong <3360238657@qq.com> Date: Mon, 26 May 2025 21:24:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=A2=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/GOHCache.xml | 364 ++++++++++++++++-- api-getaway/basic/config/global.go | 8 +- api-getaway/basic/init/init.go | 10 + api-getaway/basic/proto/user/order.pb.go | 270 +++++++++++++ api-getaway/basic/proto/user/order.proto | 23 ++ api-getaway/basic/proto/user/order_grpc.pb.go | 110 ++++++ api-getaway/basic/proto/user/user.pb.go | 50 +-- api-getaway/basic/proto/user/user_grpc.pb.go | 18 +- api-getaway/go.mod | 4 +- api-getaway/go.sum | 4 + api-getaway/handler/api/order.go | 43 +++ api-getaway/handler/request/order.go | 10 + api-getaway/router/order.go | 16 + api-getaway/router/router.go | 1 + order-server/basic/cmd/main.go | 30 ++ order-server/basic/config/config.go | 26 ++ order-server/basic/config/global.go | 11 + order-server/basic/init/init.go | 53 +++ order-server/basic/proto/order/order.pb.go | 270 +++++++++++++ order-server/basic/proto/order/order.proto | 23 ++ .../basic/proto/order/order_grpc.pb.go | 110 ++++++ order-server/handler/dao/common.go | 120 ++++++ order-server/handler/models/goods.go | 16 + order-server/handler/service/order.go | 31 ++ order-server/pkg/sendsms.go | 48 +++ order-server/user.yaml | 15 + user-server/basic/proto/user/user.pb.go | 50 +-- user-server/basic/proto/user/user_grpc.pb.go | 18 +- 28 files changed, 1655 insertions(+), 97 deletions(-) create mode 100644 api-getaway/basic/proto/user/order.pb.go create mode 100644 api-getaway/basic/proto/user/order.proto create mode 100644 api-getaway/basic/proto/user/order_grpc.pb.go create mode 100644 api-getaway/handler/api/order.go create mode 100644 api-getaway/handler/request/order.go create mode 100644 api-getaway/router/order.go create mode 100644 order-server/basic/cmd/main.go create mode 100644 order-server/basic/config/config.go create mode 100644 order-server/basic/config/global.go create mode 100644 order-server/basic/init/init.go create mode 100644 order-server/basic/proto/order/order.pb.go create mode 100644 order-server/basic/proto/order/order.proto create mode 100644 order-server/basic/proto/order/order_grpc.pb.go create mode 100644 order-server/handler/dao/common.go create mode 100644 order-server/handler/models/goods.go create mode 100644 order-server/handler/service/order.go create mode 100644 order-server/pkg/sendsms.go create mode 100644 order-server/user.yaml diff --git a/.idea/GOHCache.xml b/.idea/GOHCache.xml index 2d70da1..39907b1 100644 --- a/.idea/GOHCache.xml +++ b/.idea/GOHCache.xml @@ -3,9 +3,17 @@ \ No newline at end of file diff --git a/api-getaway/basic/config/global.go b/api-getaway/basic/config/global.go index 1ae0917..4988293 100644 --- a/api-getaway/basic/config/global.go +++ b/api-getaway/basic/config/global.go @@ -1,7 +1,11 @@ package config -import __ "zg4/jd/api-getaway/basic/proto/user" +import ( + __ "zg4/jd/api-getaway/basic/proto/user" +) var ( - UserClient __.UserClient + UserClient __.UserClient + OrderClient __.OrderClient ) +var JwtKey string = "2212" diff --git a/api-getaway/basic/init/init.go b/api-getaway/basic/init/init.go index f0782c4..3dff91a 100644 --- a/api-getaway/basic/init/init.go +++ b/api-getaway/basic/init/init.go @@ -11,6 +11,7 @@ import ( func init() { UserGrpcInit() + OrderGrpcInit() } func UserGrpcInit() { @@ -22,3 +23,12 @@ func UserGrpcInit() { } config.UserClient = __.NewUserClient(conn) } +func OrderGrpcInit() { + flag.Parse() + // Set up a connection to the server. + conn, err := grpc.NewClient("127.0.0.1:50053", grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + log.Fatalf("did not connect: %v", err) + } + config.OrderClient = __.NewOrderClient(conn) +} diff --git a/api-getaway/basic/proto/user/order.pb.go b/api-getaway/basic/proto/user/order.pb.go new file mode 100644 index 0000000..951039a --- /dev/null +++ b/api-getaway/basic/proto/user/order.pb.go @@ -0,0 +1,270 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.34.2 +// protoc v3.19.4 +// source: order.proto + +package __ + +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 AddOrderReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + UserId int64 `protobuf:"varint,1,opt,name=userId,proto3" json:"userId,omitempty"` + ProductName string `protobuf:"bytes,2,opt,name=productName,proto3" json:"productName,omitempty"` + Price uint64 `protobuf:"fixed64,3,opt,name=price,proto3" json:"price,omitempty"` + ShippingAddress string `protobuf:"bytes,4,opt,name=shippingAddress,proto3" json:"shippingAddress,omitempty"` + OrderNotes string `protobuf:"bytes,5,opt,name=orderNotes,proto3" json:"orderNotes,omitempty"` + PaymentMethod string `protobuf:"bytes,6,opt,name=paymentMethod,proto3" json:"paymentMethod,omitempty"` + OrderStatus string `protobuf:"bytes,7,opt,name=orderStatus,proto3" json:"orderStatus,omitempty"` +} + +func (x *AddOrderReq) Reset() { + *x = AddOrderReq{} + if protoimpl.UnsafeEnabled { + mi := &file_order_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *AddOrderReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AddOrderReq) ProtoMessage() {} + +func (x *AddOrderReq) ProtoReflect() protoreflect.Message { + mi := &file_order_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 AddOrderReq.ProtoReflect.Descriptor instead. +func (*AddOrderReq) Descriptor() ([]byte, []int) { + return file_order_proto_rawDescGZIP(), []int{0} +} + +func (x *AddOrderReq) GetUserId() int64 { + if x != nil { + return x.UserId + } + return 0 +} + +func (x *AddOrderReq) GetProductName() string { + if x != nil { + return x.ProductName + } + return "" +} + +func (x *AddOrderReq) GetPrice() uint64 { + if x != nil { + return x.Price + } + return 0 +} + +func (x *AddOrderReq) GetShippingAddress() string { + if x != nil { + return x.ShippingAddress + } + return "" +} + +func (x *AddOrderReq) GetOrderNotes() string { + if x != nil { + return x.OrderNotes + } + return "" +} + +func (x *AddOrderReq) GetPaymentMethod() string { + if x != nil { + return x.PaymentMethod + } + return "" +} + +func (x *AddOrderReq) GetOrderStatus() string { + if x != nil { + return x.OrderStatus + } + return "" +} + +type AddOrderResp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + OrderId int64 `protobuf:"varint,1,opt,name=orderId,proto3" json:"orderId,omitempty"` +} + +func (x *AddOrderResp) Reset() { + *x = AddOrderResp{} + if protoimpl.UnsafeEnabled { + mi := &file_order_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *AddOrderResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AddOrderResp) ProtoMessage() {} + +func (x *AddOrderResp) ProtoReflect() protoreflect.Message { + mi := &file_order_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 AddOrderResp.ProtoReflect.Descriptor instead. +func (*AddOrderResp) Descriptor() ([]byte, []int) { + return file_order_proto_rawDescGZIP(), []int{1} +} + +func (x *AddOrderResp) GetOrderId() int64 { + if x != nil { + return x.OrderId + } + return 0 +} + +var File_order_proto protoreflect.FileDescriptor + +var file_order_proto_rawDesc = []byte{ + 0x0a, 0x0b, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xef, 0x01, 0x0a, 0x0b, 0x41, 0x64, 0x64, 0x4f, 0x72, 0x64, 0x65, + 0x72, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x20, 0x0a, 0x0b, + 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x0b, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, + 0x0a, 0x05, 0x70, 0x72, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x06, 0x52, 0x05, 0x70, + 0x72, 0x69, 0x63, 0x65, 0x12, 0x28, 0x0a, 0x0f, 0x73, 0x68, 0x69, 0x70, 0x70, 0x69, 0x6e, 0x67, + 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0f, 0x73, + 0x68, 0x69, 0x70, 0x70, 0x69, 0x6e, 0x67, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x1e, + 0x0a, 0x0a, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x4e, 0x6f, 0x74, 0x65, 0x73, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x0a, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x4e, 0x6f, 0x74, 0x65, 0x73, 0x12, 0x24, + 0x0a, 0x0d, 0x70, 0x61, 0x79, 0x6d, 0x65, 0x6e, 0x74, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, + 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x70, 0x61, 0x79, 0x6d, 0x65, 0x6e, 0x74, 0x4d, 0x65, + 0x74, 0x68, 0x6f, 0x64, 0x12, 0x20, 0x0a, 0x0b, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x53, 0x74, 0x61, + 0x74, 0x75, 0x73, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x6f, 0x72, 0x64, 0x65, 0x72, + 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x28, 0x0a, 0x0c, 0x41, 0x64, 0x64, 0x4f, 0x72, 0x64, + 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x49, + 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x49, 0x64, + 0x32, 0x3c, 0x0a, 0x05, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x12, 0x33, 0x0a, 0x08, 0x41, 0x64, 0x64, + 0x4f, 0x72, 0x64, 0x65, 0x72, 0x12, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x41, 0x64, + 0x64, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x52, 0x65, 0x71, 0x1a, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x2e, 0x41, 0x64, 0x64, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x42, 0x03, + 0x5a, 0x01, 0x2f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_order_proto_rawDescOnce sync.Once + file_order_proto_rawDescData = file_order_proto_rawDesc +) + +func file_order_proto_rawDescGZIP() []byte { + file_order_proto_rawDescOnce.Do(func() { + file_order_proto_rawDescData = protoimpl.X.CompressGZIP(file_order_proto_rawDescData) + }) + return file_order_proto_rawDescData +} + +var file_order_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_order_proto_goTypes = []any{ + (*AddOrderReq)(nil), // 0: proto.AddOrderReq + (*AddOrderResp)(nil), // 1: proto.AddOrderResp +} +var file_order_proto_depIdxs = []int32{ + 0, // 0: proto.Order.AddOrder:input_type -> proto.AddOrderReq + 1, // 1: proto.Order.AddOrder:output_type -> proto.AddOrderResp + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_order_proto_init() } +func file_order_proto_init() { + if File_order_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_order_proto_msgTypes[0].Exporter = func(v any, i int) any { + switch v := v.(*AddOrderReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_order_proto_msgTypes[1].Exporter = func(v any, i int) any { + switch v := v.(*AddOrderResp); 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_order_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_order_proto_goTypes, + DependencyIndexes: file_order_proto_depIdxs, + MessageInfos: file_order_proto_msgTypes, + }.Build() + File_order_proto = out.File + file_order_proto_rawDesc = nil + file_order_proto_goTypes = nil + file_order_proto_depIdxs = nil +} diff --git a/api-getaway/basic/proto/user/order.proto b/api-getaway/basic/proto/user/order.proto new file mode 100644 index 0000000..5209f27 --- /dev/null +++ b/api-getaway/basic/proto/user/order.proto @@ -0,0 +1,23 @@ +syntax = "proto3"; + +package proto; + +option go_package = "/"; + +message AddOrderReq { + int64 userId =1; + string productName =2; + fixed64 price =3; + string shippingAddress =4; + string orderNotes =5; + string paymentMethod =6; + string orderStatus =7; +} + +message AddOrderResp { + int64 orderId =1; +} + +service Order { + rpc AddOrder(AddOrderReq) returns (AddOrderResp); +} \ No newline at end of file diff --git a/api-getaway/basic/proto/user/order_grpc.pb.go b/api-getaway/basic/proto/user/order_grpc.pb.go new file mode 100644 index 0000000..9a19e0a --- /dev/null +++ b/api-getaway/basic/proto/user/order_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 v3.19.4 +// source: order.proto + +package __ + +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 ( + Order_AddOrder_FullMethodName = "/proto.Order/AddOrder" +) + +// OrderClient is the client API for Order 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 OrderClient interface { + AddOrder(ctx context.Context, in *AddOrderReq, opts ...grpc.CallOption) (*AddOrderResp, error) +} + +type orderClient struct { + cc grpc.ClientConnInterface +} + +func NewOrderClient(cc grpc.ClientConnInterface) OrderClient { + return &orderClient{cc} +} + +func (c *orderClient) AddOrder(ctx context.Context, in *AddOrderReq, opts ...grpc.CallOption) (*AddOrderResp, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(AddOrderResp) + err := c.cc.Invoke(ctx, Order_AddOrder_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// OrderServer is the server API for Order service. +// All implementations must embed UnimplementedOrderServer +// for forward compatibility +type OrderServer interface { + AddOrder(context.Context, *AddOrderReq) (*AddOrderResp, error) + mustEmbedUnimplementedOrderServer() +} + +// UnimplementedOrderServer must be embedded to have forward compatible implementations. +type UnimplementedOrderServer struct { +} + +func (UnimplementedOrderServer) AddOrder(context.Context, *AddOrderReq) (*AddOrderResp, error) { + return nil, status.Errorf(codes.Unimplemented, "method AddOrder not implemented") +} +func (UnimplementedOrderServer) mustEmbedUnimplementedOrderServer() {} + +// UnsafeOrderServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to OrderServer will +// result in compilation errors. +type UnsafeOrderServer interface { + mustEmbedUnimplementedOrderServer() +} + +func RegisterOrderServer(s grpc.ServiceRegistrar, srv OrderServer) { + s.RegisterService(&Order_ServiceDesc, srv) +} + +func _Order_AddOrder_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(AddOrderReq) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(OrderServer).AddOrder(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Order_AddOrder_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(OrderServer).AddOrder(ctx, req.(*AddOrderReq)) + } + return interceptor(ctx, in, info, handler) +} + +// Order_ServiceDesc is the grpc.ServiceDesc for Order service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Order_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "proto.Order", + HandlerType: (*OrderServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "AddOrder", + Handler: _Order_AddOrder_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "order.proto", +} diff --git a/api-getaway/basic/proto/user/user.pb.go b/api-getaway/basic/proto/user/user.pb.go index 5c416f0..9d2c926 100644 --- a/api-getaway/basic/proto/user/user.pb.go +++ b/api-getaway/basic/proto/user/user.pb.go @@ -2,7 +2,7 @@ // versions: // protoc-gen-go v1.36.5 // protoc v3.19.4 -// source: user.proto +// source: order.proto package __ @@ -695,32 +695,32 @@ func file_user_proto_rawDescGZIP() []byte { var file_user_proto_msgTypes = make([]protoimpl.MessageInfo, 12) var file_user_proto_goTypes = []any{ - (*RegisterReq)(nil), // 0: user.RegisterReq - (*RegisterRes)(nil), // 1: user.RegisterRes - (*CheckMobileReq)(nil), // 2: user.CheckMobileReq - (*CheckMobileRes)(nil), // 3: user.CheckMobileRes - (*LoginReq)(nil), // 4: user.LoginReq - (*LoginRes)(nil), // 5: user.LoginRes - (*SendSmsReq)(nil), // 6: user.SendSmsReq - (*SendSmsRes)(nil), // 7: user.SendSmsRes - (*UpdateUserAvatarReq)(nil), // 8: user.UpdateUserAvatarReq - (*UpdateUserAvatarRes)(nil), // 9: user.UpdateUserAvatarRes - (*ExistUserReq)(nil), // 10: user.ExistUserReq - (*ExistUserRes)(nil), // 11: user.ExistUserRes + (*RegisterReq)(nil), // 0: order.RegisterReq + (*RegisterRes)(nil), // 1: order.RegisterRes + (*CheckMobileReq)(nil), // 2: order.CheckMobileReq + (*CheckMobileRes)(nil), // 3: order.CheckMobileRes + (*LoginReq)(nil), // 4: order.LoginReq + (*LoginRes)(nil), // 5: order.LoginRes + (*SendSmsReq)(nil), // 6: order.SendSmsReq + (*SendSmsRes)(nil), // 7: order.SendSmsRes + (*UpdateUserAvatarReq)(nil), // 8: order.UpdateUserAvatarReq + (*UpdateUserAvatarRes)(nil), // 9: order.UpdateUserAvatarRes + (*ExistUserReq)(nil), // 10: order.ExistUserReq + (*ExistUserRes)(nil), // 11: order.ExistUserRes } var file_user_proto_depIdxs = []int32{ - 0, // 0: user.user.Register:input_type -> user.RegisterReq - 2, // 1: user.user.CheckMobile:input_type -> user.CheckMobileReq - 4, // 2: user.user.Login:input_type -> user.LoginReq - 6, // 3: user.user.SendSms:input_type -> user.SendSmsReq - 8, // 4: user.user.UpdateUserAvatar:input_type -> user.UpdateUserAvatarReq - 10, // 5: user.user.ExistUser:input_type -> user.ExistUserReq - 1, // 6: user.user.Register:output_type -> user.RegisterRes - 3, // 7: user.user.CheckMobile:output_type -> user.CheckMobileRes - 5, // 8: user.user.Login:output_type -> user.LoginRes - 7, // 9: user.user.SendSms:output_type -> user.SendSmsRes - 9, // 10: user.user.UpdateUserAvatar:output_type -> user.UpdateUserAvatarRes - 11, // 11: user.user.ExistUser:output_type -> user.ExistUserRes + 0, // 0: order.order.Register:input_type -> order.RegisterReq + 2, // 1: order.order.CheckMobile:input_type -> order.CheckMobileReq + 4, // 2: order.order.Login:input_type -> order.LoginReq + 6, // 3: order.order.SendSms:input_type -> order.SendSmsReq + 8, // 4: order.order.UpdateUserAvatar:input_type -> order.UpdateUserAvatarReq + 10, // 5: order.order.ExistUser:input_type -> order.ExistUserReq + 1, // 6: order.order.Register:output_type -> order.RegisterRes + 3, // 7: order.order.CheckMobile:output_type -> order.CheckMobileRes + 5, // 8: order.order.Login:output_type -> order.LoginRes + 7, // 9: order.order.SendSms:output_type -> order.SendSmsRes + 9, // 10: order.order.UpdateUserAvatar:output_type -> order.UpdateUserAvatarRes + 11, // 11: order.order.ExistUser:output_type -> order.ExistUserRes 6, // [6:12] is the sub-list for method output_type 0, // [0:6] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name diff --git a/api-getaway/basic/proto/user/user_grpc.pb.go b/api-getaway/basic/proto/user/user_grpc.pb.go index 4d3865b..33409c1 100644 --- a/api-getaway/basic/proto/user/user_grpc.pb.go +++ b/api-getaway/basic/proto/user/user_grpc.pb.go @@ -2,7 +2,7 @@ // versions: // - protoc-gen-go-grpc v1.4.0 // - protoc v3.19.4 -// source: user.proto +// source: order.proto package __ @@ -19,12 +19,12 @@ import ( const _ = grpc.SupportPackageIsVersion8 const ( - User_Register_FullMethodName = "/user.user/Register" - User_CheckMobile_FullMethodName = "/user.user/CheckMobile" - User_Login_FullMethodName = "/user.user/Login" - User_SendSms_FullMethodName = "/user.user/SendSms" - User_UpdateUserAvatar_FullMethodName = "/user.user/UpdateUserAvatar" - User_ExistUser_FullMethodName = "/user.user/ExistUser" + User_Register_FullMethodName = "/order.order/Register" + User_CheckMobile_FullMethodName = "/order.order/CheckMobile" + User_Login_FullMethodName = "/order.order/Login" + User_SendSms_FullMethodName = "/order.order/SendSms" + User_UpdateUserAvatar_FullMethodName = "/order.order/UpdateUserAvatar" + User_ExistUser_FullMethodName = "/order.order/ExistUser" ) // UserClient is the client API for User service. @@ -267,7 +267,7 @@ func _User_ExistUser_Handler(srv interface{}, ctx context.Context, dec func(inte // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) var User_ServiceDesc = grpc.ServiceDesc{ - ServiceName: "user.user", + ServiceName: "order.order", HandlerType: (*UserServer)(nil), Methods: []grpc.MethodDesc{ { @@ -296,5 +296,5 @@ var User_ServiceDesc = grpc.ServiceDesc{ }, }, Streams: []grpc.StreamDesc{}, - Metadata: "user.proto", + Metadata: "order.proto", } diff --git a/api-getaway/go.mod b/api-getaway/go.mod index 7cf3130..e557c4a 100644 --- a/api-getaway/go.mod +++ b/api-getaway/go.mod @@ -4,7 +4,9 @@ go 1.23 require ( github.com/gin-gonic/gin v1.10.1 + github.com/golangblogs/gojwt v0.0.0-20231007025427-9a57ce3b7b94 google.golang.org/grpc v1.72.1 + google.golang.org/protobuf v1.36.5 ) require ( @@ -12,6 +14,7 @@ require ( github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect + github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -33,6 +36,5 @@ require ( golang.org/x/sys v0.30.0 // indirect golang.org/x/text v0.22.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a // indirect - google.golang.org/protobuf v1.36.5 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/api-getaway/go.sum b/api-getaway/go.sum index 8187a23..ecafbbc 100644 --- a/api-getaway/go.sum +++ b/api-getaway/go.sum @@ -9,6 +9,8 @@ github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -31,6 +33,8 @@ github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/golangblogs/gojwt v0.0.0-20231007025427-9a57ce3b7b94 h1:CLWIFZILOjqoS/H1e/zVvKO6/gErJ6d+j1no1crr2dk= +github.com/golangblogs/gojwt v0.0.0-20231007025427-9a57ce3b7b94/go.mod h1:1LIkO76AhSy14JeSYw0mh4KM6tOp5RjLK/CF59HKu+Y= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= diff --git a/api-getaway/handler/api/order.go b/api-getaway/handler/api/order.go new file mode 100644 index 0000000..7347385 --- /dev/null +++ b/api-getaway/handler/api/order.go @@ -0,0 +1,43 @@ +package api + +import ( + "github.com/gin-gonic/gin" + "net/http" + "zg4/jd/api-getaway/basic/config" + __ "zg4/jd/api-getaway/basic/proto/user" + "zg4/jd/api-getaway/handler/request" +) + +func AddOrder(c *gin.Context) { + var req request.AddOrderReq + if err := c.ShouldBind(&req); err != nil { + c.JSON(http.StatusOK, gin.H{ + "code": 100001, + "msg": "参数验证失败", + "data": nil, + }) + return + } + order, err := config.OrderClient.AddOrder(c, &__.AddOrderReq{ + UserId: int64(c.GetUint("userId")), + ProductName: req.ProductName, + Price: uint64(req.Price), + ShippingAddress: req.ShippingAddress, + OrderNotes: req.OrderNotes, + PaymentMethod: req.PaymentMethod, + OrderStatus: req.OrderStatus, + }) + if err != nil { + c.JSON(http.StatusOK, gin.H{ + "code": 100001, + "msg": "订单添加失败", + "data": nil, + }) + return + } + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "msg": "订单添加成功", + "data": order.OrderId, + }) +} diff --git a/api-getaway/handler/request/order.go b/api-getaway/handler/request/order.go new file mode 100644 index 0000000..7e1233f --- /dev/null +++ b/api-getaway/handler/request/order.go @@ -0,0 +1,10 @@ +package request + +type AddOrderReq struct { + ProductName string `json:"product_name" form:"product_name" binding:"required"` // 商品名称 + Price float64 `json:"price" form:"price" binding:"required"` // 订单金额 + ShippingAddress string `json:"shipping_address" form:"shipping_address" binding:"required"` // 收货地址 + OrderNotes string `json:"order_notes" form:"order_notes" binding:"required"` // 订单备注信息 + PaymentMethod string `json:"payment_method" form:"payment_method" binding:"required"` // 支付方式 + OrderStatus string `json:"order_status" form:"order_status" binding:"required"` // 订单状态 +} diff --git a/api-getaway/router/order.go b/api-getaway/router/order.go new file mode 100644 index 0000000..95dd92d --- /dev/null +++ b/api-getaway/router/order.go @@ -0,0 +1,16 @@ +package router + +import ( + "github.com/gin-gonic/gin" + jwt "github.com/golangblogs/gojwt" + "zg4/jd/api-getaway/basic/config" + "zg4/jd/api-getaway/handler/api" +) + +func OrderRouter(apiGroup *gin.RouterGroup) { + order := apiGroup.Group("order") + { + order.Use(jwt.JWTAuth(config.JwtKey)) + order.POST("addOrder", api.AddOrder) + } +} diff --git a/api-getaway/router/router.go b/api-getaway/router/router.go index 4b2dfc3..3a3df78 100644 --- a/api-getaway/router/router.go +++ b/api-getaway/router/router.go @@ -6,5 +6,6 @@ func Router(r *gin.Engine) { apiGroup := r.Group("api") { UserRouter(apiGroup) + OrderRouter(apiGroup) } } diff --git a/order-server/basic/cmd/main.go b/order-server/basic/cmd/main.go new file mode 100644 index 0000000..484d652 --- /dev/null +++ b/order-server/basic/cmd/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "flag" + "fmt" + "google.golang.org/grpc" + "log" + "net" + _ "zg4/jd/order_server/basic/init" + __ "zg4/jd/order_server/basic/proto/order" + "zg4/jd/order_server/handler/service" +) + +var ( + port = flag.Int("port", 50053, "The server port") +) + +func main() { + flag.Parse() + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + s := grpc.NewServer() + __.RegisterOrderServer(s, &service.Server{}) + log.Printf("server listening at %v", lis.Addr()) + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } +} diff --git a/order-server/basic/config/config.go b/order-server/basic/config/config.go new file mode 100644 index 0000000..3f2941f --- /dev/null +++ b/order-server/basic/config/config.go @@ -0,0 +1,26 @@ +package config + +type Mysql struct { + Host string + Port int + User string + Password string + DB string +} +type Redis struct { + Addr string + Password string + DB int +} +type SendSms struct { + AK string + SK string +} + +type AppConfig struct { + Mysql + Redis + SendSms +} + +var AppConf AppConfig diff --git a/order-server/basic/config/global.go b/order-server/basic/config/global.go new file mode 100644 index 0000000..bb8d634 --- /dev/null +++ b/order-server/basic/config/global.go @@ -0,0 +1,11 @@ +package config + +import ( + "context" + "github.com/go-redis/redis/v8" + "gorm.io/gorm" +) + +var DB *gorm.DB +var RDB *redis.Client +var Ctx = context.Background() diff --git a/order-server/basic/init/init.go b/order-server/basic/init/init.go new file mode 100644 index 0000000..a68fd6b --- /dev/null +++ b/order-server/basic/init/init.go @@ -0,0 +1,53 @@ +package init + +import ( + "fmt" + "github.com/go-redis/redis/v8" + "github.com/spf13/viper" + "gorm.io/driver/mysql" + "gorm.io/gorm" + "gorm.io/gorm/logger" + "log" + "zg4/jd/order_server/basic/config" +) + +func init() { + ConfigInit() + MysqlInit() + RedisInit() +} +func ConfigInit() { + viper.SetConfigFile("./user.yaml") + viper.ReadInConfig() + viper.Unmarshal(&config.AppConf) + log.Println(config.AppConf) +} + +func MysqlInit() { + var err error + c := config.AppConf.Mysql + dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", c.User, c.Password, c.Host, c.Port, c.DB) + config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ + Logger: logger.Default.LogMode(logger.Info), + }) + + if err != nil { + panic("数据库连接失败") + } + log.Println("数据库连接成功") +} + +func RedisInit() { + conf := config.AppConf.Redis + config.RDB = redis.NewClient(&redis.Options{ + Addr: conf.Addr, + Password: conf.Password, // no password set + DB: conf.DB, // use default DB + }) + + err := config.RDB.Set(config.Ctx, "key", "value", 0).Err() + if err != nil { + panic(err) + } + log.Println("init redis success") +} diff --git a/order-server/basic/proto/order/order.pb.go b/order-server/basic/proto/order/order.pb.go new file mode 100644 index 0000000..951039a --- /dev/null +++ b/order-server/basic/proto/order/order.pb.go @@ -0,0 +1,270 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.34.2 +// protoc v3.19.4 +// source: order.proto + +package __ + +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 AddOrderReq struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + UserId int64 `protobuf:"varint,1,opt,name=userId,proto3" json:"userId,omitempty"` + ProductName string `protobuf:"bytes,2,opt,name=productName,proto3" json:"productName,omitempty"` + Price uint64 `protobuf:"fixed64,3,opt,name=price,proto3" json:"price,omitempty"` + ShippingAddress string `protobuf:"bytes,4,opt,name=shippingAddress,proto3" json:"shippingAddress,omitempty"` + OrderNotes string `protobuf:"bytes,5,opt,name=orderNotes,proto3" json:"orderNotes,omitempty"` + PaymentMethod string `protobuf:"bytes,6,opt,name=paymentMethod,proto3" json:"paymentMethod,omitempty"` + OrderStatus string `protobuf:"bytes,7,opt,name=orderStatus,proto3" json:"orderStatus,omitempty"` +} + +func (x *AddOrderReq) Reset() { + *x = AddOrderReq{} + if protoimpl.UnsafeEnabled { + mi := &file_order_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *AddOrderReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AddOrderReq) ProtoMessage() {} + +func (x *AddOrderReq) ProtoReflect() protoreflect.Message { + mi := &file_order_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 AddOrderReq.ProtoReflect.Descriptor instead. +func (*AddOrderReq) Descriptor() ([]byte, []int) { + return file_order_proto_rawDescGZIP(), []int{0} +} + +func (x *AddOrderReq) GetUserId() int64 { + if x != nil { + return x.UserId + } + return 0 +} + +func (x *AddOrderReq) GetProductName() string { + if x != nil { + return x.ProductName + } + return "" +} + +func (x *AddOrderReq) GetPrice() uint64 { + if x != nil { + return x.Price + } + return 0 +} + +func (x *AddOrderReq) GetShippingAddress() string { + if x != nil { + return x.ShippingAddress + } + return "" +} + +func (x *AddOrderReq) GetOrderNotes() string { + if x != nil { + return x.OrderNotes + } + return "" +} + +func (x *AddOrderReq) GetPaymentMethod() string { + if x != nil { + return x.PaymentMethod + } + return "" +} + +func (x *AddOrderReq) GetOrderStatus() string { + if x != nil { + return x.OrderStatus + } + return "" +} + +type AddOrderResp struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + OrderId int64 `protobuf:"varint,1,opt,name=orderId,proto3" json:"orderId,omitempty"` +} + +func (x *AddOrderResp) Reset() { + *x = AddOrderResp{} + if protoimpl.UnsafeEnabled { + mi := &file_order_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *AddOrderResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AddOrderResp) ProtoMessage() {} + +func (x *AddOrderResp) ProtoReflect() protoreflect.Message { + mi := &file_order_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 AddOrderResp.ProtoReflect.Descriptor instead. +func (*AddOrderResp) Descriptor() ([]byte, []int) { + return file_order_proto_rawDescGZIP(), []int{1} +} + +func (x *AddOrderResp) GetOrderId() int64 { + if x != nil { + return x.OrderId + } + return 0 +} + +var File_order_proto protoreflect.FileDescriptor + +var file_order_proto_rawDesc = []byte{ + 0x0a, 0x0b, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xef, 0x01, 0x0a, 0x0b, 0x41, 0x64, 0x64, 0x4f, 0x72, 0x64, 0x65, + 0x72, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x20, 0x0a, 0x0b, + 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x0b, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, + 0x0a, 0x05, 0x70, 0x72, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x06, 0x52, 0x05, 0x70, + 0x72, 0x69, 0x63, 0x65, 0x12, 0x28, 0x0a, 0x0f, 0x73, 0x68, 0x69, 0x70, 0x70, 0x69, 0x6e, 0x67, + 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0f, 0x73, + 0x68, 0x69, 0x70, 0x70, 0x69, 0x6e, 0x67, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x1e, + 0x0a, 0x0a, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x4e, 0x6f, 0x74, 0x65, 0x73, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x0a, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x4e, 0x6f, 0x74, 0x65, 0x73, 0x12, 0x24, + 0x0a, 0x0d, 0x70, 0x61, 0x79, 0x6d, 0x65, 0x6e, 0x74, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x18, + 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x70, 0x61, 0x79, 0x6d, 0x65, 0x6e, 0x74, 0x4d, 0x65, + 0x74, 0x68, 0x6f, 0x64, 0x12, 0x20, 0x0a, 0x0b, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x53, 0x74, 0x61, + 0x74, 0x75, 0x73, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x6f, 0x72, 0x64, 0x65, 0x72, + 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x28, 0x0a, 0x0c, 0x41, 0x64, 0x64, 0x4f, 0x72, 0x64, + 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x49, + 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x49, 0x64, + 0x32, 0x3c, 0x0a, 0x05, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x12, 0x33, 0x0a, 0x08, 0x41, 0x64, 0x64, + 0x4f, 0x72, 0x64, 0x65, 0x72, 0x12, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x41, 0x64, + 0x64, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x52, 0x65, 0x71, 0x1a, 0x13, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x2e, 0x41, 0x64, 0x64, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x42, 0x03, + 0x5a, 0x01, 0x2f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_order_proto_rawDescOnce sync.Once + file_order_proto_rawDescData = file_order_proto_rawDesc +) + +func file_order_proto_rawDescGZIP() []byte { + file_order_proto_rawDescOnce.Do(func() { + file_order_proto_rawDescData = protoimpl.X.CompressGZIP(file_order_proto_rawDescData) + }) + return file_order_proto_rawDescData +} + +var file_order_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_order_proto_goTypes = []any{ + (*AddOrderReq)(nil), // 0: proto.AddOrderReq + (*AddOrderResp)(nil), // 1: proto.AddOrderResp +} +var file_order_proto_depIdxs = []int32{ + 0, // 0: proto.Order.AddOrder:input_type -> proto.AddOrderReq + 1, // 1: proto.Order.AddOrder:output_type -> proto.AddOrderResp + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_order_proto_init() } +func file_order_proto_init() { + if File_order_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_order_proto_msgTypes[0].Exporter = func(v any, i int) any { + switch v := v.(*AddOrderReq); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_order_proto_msgTypes[1].Exporter = func(v any, i int) any { + switch v := v.(*AddOrderResp); 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_order_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_order_proto_goTypes, + DependencyIndexes: file_order_proto_depIdxs, + MessageInfos: file_order_proto_msgTypes, + }.Build() + File_order_proto = out.File + file_order_proto_rawDesc = nil + file_order_proto_goTypes = nil + file_order_proto_depIdxs = nil +} diff --git a/order-server/basic/proto/order/order.proto b/order-server/basic/proto/order/order.proto new file mode 100644 index 0000000..7383b6a --- /dev/null +++ b/order-server/basic/proto/order/order.proto @@ -0,0 +1,23 @@ +syntax = "proto3"; + +package proto; + +option go_package = "/"; + +message AddOrderReq { + int64 userId =1; + string productName =2; + fixed64 price =3; + string shippingAddress =4; + string orderNotes =5; + string paymentMethod =6; + string orderStatus =7; +} + +message AddOrderResp { + int64 orderId =1; +} + +service Order { + rpc AddOrder(AddOrderReq) returns (AddOrderResp); +} \ No newline at end of file diff --git a/order-server/basic/proto/order/order_grpc.pb.go b/order-server/basic/proto/order/order_grpc.pb.go new file mode 100644 index 0000000..9a19e0a --- /dev/null +++ b/order-server/basic/proto/order/order_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 v3.19.4 +// source: order.proto + +package __ + +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 ( + Order_AddOrder_FullMethodName = "/proto.Order/AddOrder" +) + +// OrderClient is the client API for Order 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 OrderClient interface { + AddOrder(ctx context.Context, in *AddOrderReq, opts ...grpc.CallOption) (*AddOrderResp, error) +} + +type orderClient struct { + cc grpc.ClientConnInterface +} + +func NewOrderClient(cc grpc.ClientConnInterface) OrderClient { + return &orderClient{cc} +} + +func (c *orderClient) AddOrder(ctx context.Context, in *AddOrderReq, opts ...grpc.CallOption) (*AddOrderResp, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(AddOrderResp) + err := c.cc.Invoke(ctx, Order_AddOrder_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// OrderServer is the server API for Order service. +// All implementations must embed UnimplementedOrderServer +// for forward compatibility +type OrderServer interface { + AddOrder(context.Context, *AddOrderReq) (*AddOrderResp, error) + mustEmbedUnimplementedOrderServer() +} + +// UnimplementedOrderServer must be embedded to have forward compatible implementations. +type UnimplementedOrderServer struct { +} + +func (UnimplementedOrderServer) AddOrder(context.Context, *AddOrderReq) (*AddOrderResp, error) { + return nil, status.Errorf(codes.Unimplemented, "method AddOrder not implemented") +} +func (UnimplementedOrderServer) mustEmbedUnimplementedOrderServer() {} + +// UnsafeOrderServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to OrderServer will +// result in compilation errors. +type UnsafeOrderServer interface { + mustEmbedUnimplementedOrderServer() +} + +func RegisterOrderServer(s grpc.ServiceRegistrar, srv OrderServer) { + s.RegisterService(&Order_ServiceDesc, srv) +} + +func _Order_AddOrder_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(AddOrderReq) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(OrderServer).AddOrder(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Order_AddOrder_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(OrderServer).AddOrder(ctx, req.(*AddOrderReq)) + } + return interceptor(ctx, in, info, handler) +} + +// Order_ServiceDesc is the grpc.ServiceDesc for Order service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Order_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "proto.Order", + HandlerType: (*OrderServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "AddOrder", + Handler: _Order_AddOrder_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "order.proto", +} diff --git a/order-server/handler/dao/common.go b/order-server/handler/dao/common.go new file mode 100644 index 0000000..5ecc36d --- /dev/null +++ b/order-server/handler/dao/common.go @@ -0,0 +1,120 @@ +package dao + +import ( + "zg4/jd/order_server/basic/config" +) + +func Create[T any](data *T) bool { + + if err := config.DB.Create(data).Error; err != nil { + return false + } + + return true +} + +func Delete[T any](data *T) bool { + if err := config.DB.Delete(data).Error; err != nil { + return false + } + return true +} + +func BatchDelete[T any](data *T) bool { //批量删除 + if err := config.DB.Where(data).Delete(data).Error; err != nil { + return false + } + return true +} + +// save 结构体全量更新 +// updates 有值的更新,不更新 + +func Update[T any](data *T) bool { + if err := config.DB.Updates(data).Error; err != nil { + return false + } + return true +} + +func BatchUpdate[T any](where *T, data *T) bool { //批量更新 + if err := config.DB.Model(data).Where(where).Updates(data).Error; err != nil { + return false + } + return true +} + +func GetOneById[T any](id uint, data *T) bool { + + if err := config.DB.Where("id=?", id).Find(data).Limit(1).Error; err != nil { + return false + } + return true +} + +func GetOneByFields[T any](where *T, data *T) bool { + + if err := config.DB.Where(where).Find(data).Limit(1).Error; err != nil { + return false + } + return true + +} + +func GetListByFields[T any](where *T, data *[]T) bool { + + if err := config.DB.Where(where).Find(data).Error; err != nil { + return false + } + return true + +} + +type PageDto struct { + Page uint `json:"page" form:"page"` + PageSize uint `json:"page_size" form:"page_size"` + OrderBy string `json:"order_by" form:"order_by"` // e.g. "created_at desc" +} + +type PageResult[T any] struct { + List []T `json:"list"` + Total uint `json:"total"` + Page uint `json:"page"` + PageSize uint `json:"page_size"` +} + +func Paginate[T any](where *T, req PageDto, out *[]T) (PageResult[T], error) { + if req.Page <= 0 { + req.Page = 1 + } + if req.PageSize <= 0 { + req.PageSize = 10 + } + + offset := (req.Page - 1) * req.PageSize + + query := config.DB.Model(out).Where(where) + + // 统计总数 + var total int64 + if err := query.Count(&total).Error; err != nil { + return PageResult[T]{}, err + } + + // 排序 + if req.OrderBy != "" { + query = query.Order(req.OrderBy) + } + + // 查询数据 + if err := query.Offset(int(offset)).Limit(int(req.PageSize)).Find(out).Error; err != nil { + return PageResult[T]{}, err + } + + return PageResult[T]{ + List: *out, + Total: uint(total), + Page: req.Page, + PageSize: req.PageSize, + }, nil +} diff --git a/order-server/handler/models/goods.go b/order-server/handler/models/goods.go new file mode 100644 index 0000000..2398364 --- /dev/null +++ b/order-server/handler/models/goods.go @@ -0,0 +1,16 @@ +package models + +import "time" + +type Orders struct { + OrderId uint32 `gorm:"column:order_id;type:INT UNSIGNED;comment:订单唯一ID;primaryKey;" json:"order_id"` // 订单唯一ID + UserId uint32 `gorm:"column:user_id;type:INT UNSIGNED;comment:下单用户ID;not null;" json:"user_id"` // 下单用户ID + ProductName string `gorm:"column:product_name;type:VARCHAR(255);comment:商品名称;not null;" json:"product_name"` // 商品名称 + Price float64 `gorm:"column:price;type:DECIMAL(10, 2);comment:订单金额;not null;" json:"price"` // 订单金额 + ShippingAddress string `gorm:"column:shipping_address;type:VARCHAR(500);comment:收货地址;not null;" json:"shipping_address"` // 收货地址 + OrderNotes string `gorm:"column:order_notes;type:TEXT;comment:订单备注信息;" json:"order_notes"` // 订单备注信息 + PaymentMethod string `gorm:"column:payment_method;type:ENUM('支付宝', '微信支付', '银行卡', '线下支付');comment:支付方式;not null;" json:"payment_method"` // 支付方式 + OrderStatus string `gorm:"column:order_status;type:ENUM('待付款', '已付款', '已发货', '已完成', '已取消');comment:订单状态;not null;default:待付款;" json:"order_status"` // 订单状态 + CreatedAt time.Time `gorm:"column:created_at;type:TIMESTAMP;comment:订单创建时间;default:CURRENT_TIMESTAMP;" json:"created_at"` // 订单创建时间 + UpdatedAt time.Time `gorm:"column:updated_at;type:TIMESTAMP;comment:最后更新时间;default:CURRENT_TIMESTAMP;" json:"updated_at"` // 最后更新时间 +} diff --git a/order-server/handler/service/order.go b/order-server/handler/service/order.go new file mode 100644 index 0000000..33de61b --- /dev/null +++ b/order-server/handler/service/order.go @@ -0,0 +1,31 @@ +package service + +import ( + "context" + __ "zg4/jd/order_server/basic/proto/order" + "zg4/jd/order_server/handler/dao" + models2 "zg4/jd/order_server/handler/models" +) + +type Server struct { + __.UnimplementedOrderServer +} + +func (s *Server) AddOrder(_ context.Context, in *__.AddOrderReq) (*__.AddOrderResp, error) { + order := models2.Orders{ + UserId: uint32(in.UserId), + ProductName: in.ProductName, + Price: float64(in.Price), + ShippingAddress: in.ShippingAddress, + OrderNotes: in.OrderNotes, + PaymentMethod: in.PaymentMethod, + OrderStatus: in.OrderStatus, + } + if !dao.Create(&order) { + return &__.AddOrderResp{}, nil + } + return &__.AddOrderResp{ + OrderId: int64(order.OrderId), + }, nil + +} diff --git a/order-server/pkg/sendsms.go b/order-server/pkg/sendsms.go new file mode 100644 index 0000000..bc9e2d2 --- /dev/null +++ b/order-server/pkg/sendsms.go @@ -0,0 +1,48 @@ +package pkg + +import ( + openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" + dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v5/client" + util "github.com/alibabacloud-go/tea-utils/v2/service" + "github.com/alibabacloud-go/tea/tea" + credential "github.com/aliyun/credentials-go/credentials" + "log" + config2 "zg4/jd/order_server/basic/config" +) + +func CreateClient() (_result *dysmsapi20170525.Client, _err error) { + // 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/378661.html。 + credential, _err := credential.NewCredential(nil) + if _err != nil { + return _result, _err + } + conf := config2.AppConf.SendSms + config := &openapi.Config{ + Credential: credential, + AccessKeyId: tea.String(conf.AK), + AccessKeySecret: tea.String(conf.SK), + } + // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi + config.Endpoint = tea.String("dysmsapi.aliyuncs.com") + _result = &dysmsapi20170525.Client{} + _result, _err = dysmsapi20170525.NewClient(config) + return _result, _err +} + +func SendSms(tel, code string) bool { + client, _err := CreateClient() + if _err != nil { + return false + } + + sendSmsRequest := &dysmsapi20170525.SendSmsRequest{ + SignName: tea.String("阿里云短信测试"), + TemplateCode: tea.String("SMS_154950909"), + PhoneNumbers: tea.String(tel), + TemplateParam: tea.String("{\"code\":\"" + code + "\"}"), + } + runtime := &util.RuntimeOptions{} + res, _ := client.SendSmsWithOptions(sendSmsRequest, runtime) + log.Println(res) + return true +} diff --git a/order-server/user.yaml b/order-server/user.yaml new file mode 100644 index 0000000..df4a5fe --- /dev/null +++ b/order-server/user.yaml @@ -0,0 +1,15 @@ +Mysql: + Host: "127.0.0.1" + Port: 3306 + User: "root" + Password: "root" + DB: "rk" + +Redis: + Addr: "43.142.79.30:6379" + Password: "CDD8249CAF64AAD5EB4AA66429867003" + DB: 2 + +SendSms: + AK: "LTAI5tJNte4BtyFWSqHn1H7B" + SK: "WENlZQR7cU3S2nUMYFukQjEDiFs7An" \ No newline at end of file diff --git a/user-server/basic/proto/user/user.pb.go b/user-server/basic/proto/user/user.pb.go index 5c416f0..9d2c926 100644 --- a/user-server/basic/proto/user/user.pb.go +++ b/user-server/basic/proto/user/user.pb.go @@ -2,7 +2,7 @@ // versions: // protoc-gen-go v1.36.5 // protoc v3.19.4 -// source: user.proto +// source: order.proto package __ @@ -695,32 +695,32 @@ func file_user_proto_rawDescGZIP() []byte { var file_user_proto_msgTypes = make([]protoimpl.MessageInfo, 12) var file_user_proto_goTypes = []any{ - (*RegisterReq)(nil), // 0: user.RegisterReq - (*RegisterRes)(nil), // 1: user.RegisterRes - (*CheckMobileReq)(nil), // 2: user.CheckMobileReq - (*CheckMobileRes)(nil), // 3: user.CheckMobileRes - (*LoginReq)(nil), // 4: user.LoginReq - (*LoginRes)(nil), // 5: user.LoginRes - (*SendSmsReq)(nil), // 6: user.SendSmsReq - (*SendSmsRes)(nil), // 7: user.SendSmsRes - (*UpdateUserAvatarReq)(nil), // 8: user.UpdateUserAvatarReq - (*UpdateUserAvatarRes)(nil), // 9: user.UpdateUserAvatarRes - (*ExistUserReq)(nil), // 10: user.ExistUserReq - (*ExistUserRes)(nil), // 11: user.ExistUserRes + (*RegisterReq)(nil), // 0: order.RegisterReq + (*RegisterRes)(nil), // 1: order.RegisterRes + (*CheckMobileReq)(nil), // 2: order.CheckMobileReq + (*CheckMobileRes)(nil), // 3: order.CheckMobileRes + (*LoginReq)(nil), // 4: order.LoginReq + (*LoginRes)(nil), // 5: order.LoginRes + (*SendSmsReq)(nil), // 6: order.SendSmsReq + (*SendSmsRes)(nil), // 7: order.SendSmsRes + (*UpdateUserAvatarReq)(nil), // 8: order.UpdateUserAvatarReq + (*UpdateUserAvatarRes)(nil), // 9: order.UpdateUserAvatarRes + (*ExistUserReq)(nil), // 10: order.ExistUserReq + (*ExistUserRes)(nil), // 11: order.ExistUserRes } var file_user_proto_depIdxs = []int32{ - 0, // 0: user.user.Register:input_type -> user.RegisterReq - 2, // 1: user.user.CheckMobile:input_type -> user.CheckMobileReq - 4, // 2: user.user.Login:input_type -> user.LoginReq - 6, // 3: user.user.SendSms:input_type -> user.SendSmsReq - 8, // 4: user.user.UpdateUserAvatar:input_type -> user.UpdateUserAvatarReq - 10, // 5: user.user.ExistUser:input_type -> user.ExistUserReq - 1, // 6: user.user.Register:output_type -> user.RegisterRes - 3, // 7: user.user.CheckMobile:output_type -> user.CheckMobileRes - 5, // 8: user.user.Login:output_type -> user.LoginRes - 7, // 9: user.user.SendSms:output_type -> user.SendSmsRes - 9, // 10: user.user.UpdateUserAvatar:output_type -> user.UpdateUserAvatarRes - 11, // 11: user.user.ExistUser:output_type -> user.ExistUserRes + 0, // 0: order.order.Register:input_type -> order.RegisterReq + 2, // 1: order.order.CheckMobile:input_type -> order.CheckMobileReq + 4, // 2: order.order.Login:input_type -> order.LoginReq + 6, // 3: order.order.SendSms:input_type -> order.SendSmsReq + 8, // 4: order.order.UpdateUserAvatar:input_type -> order.UpdateUserAvatarReq + 10, // 5: order.order.ExistUser:input_type -> order.ExistUserReq + 1, // 6: order.order.Register:output_type -> order.RegisterRes + 3, // 7: order.order.CheckMobile:output_type -> order.CheckMobileRes + 5, // 8: order.order.Login:output_type -> order.LoginRes + 7, // 9: order.order.SendSms:output_type -> order.SendSmsRes + 9, // 10: order.order.UpdateUserAvatar:output_type -> order.UpdateUserAvatarRes + 11, // 11: order.order.ExistUser:output_type -> order.ExistUserRes 6, // [6:12] is the sub-list for method output_type 0, // [0:6] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name diff --git a/user-server/basic/proto/user/user_grpc.pb.go b/user-server/basic/proto/user/user_grpc.pb.go index 4d3865b..33409c1 100644 --- a/user-server/basic/proto/user/user_grpc.pb.go +++ b/user-server/basic/proto/user/user_grpc.pb.go @@ -2,7 +2,7 @@ // versions: // - protoc-gen-go-grpc v1.4.0 // - protoc v3.19.4 -// source: user.proto +// source: order.proto package __ @@ -19,12 +19,12 @@ import ( const _ = grpc.SupportPackageIsVersion8 const ( - User_Register_FullMethodName = "/user.user/Register" - User_CheckMobile_FullMethodName = "/user.user/CheckMobile" - User_Login_FullMethodName = "/user.user/Login" - User_SendSms_FullMethodName = "/user.user/SendSms" - User_UpdateUserAvatar_FullMethodName = "/user.user/UpdateUserAvatar" - User_ExistUser_FullMethodName = "/user.user/ExistUser" + User_Register_FullMethodName = "/order.order/Register" + User_CheckMobile_FullMethodName = "/order.order/CheckMobile" + User_Login_FullMethodName = "/order.order/Login" + User_SendSms_FullMethodName = "/order.order/SendSms" + User_UpdateUserAvatar_FullMethodName = "/order.order/UpdateUserAvatar" + User_ExistUser_FullMethodName = "/order.order/ExistUser" ) // UserClient is the client API for User service. @@ -267,7 +267,7 @@ func _User_ExistUser_Handler(srv interface{}, ctx context.Context, dec func(inte // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) var User_ServiceDesc = grpc.ServiceDesc{ - ServiceName: "user.user", + ServiceName: "order.order", HandlerType: (*UserServer)(nil), Methods: []grpc.MethodDesc{ { @@ -296,5 +296,5 @@ var User_ServiceDesc = grpc.ServiceDesc{ }, }, Streams: []grpc.StreamDesc{}, - Metadata: "user.proto", + Metadata: "order.proto", } -- Gitee