From ffff0e3f9805c8c13356f4d34cc6d21379877b07 Mon Sep 17 00:00:00 2001 From: guzeng Date: Sat, 13 Mar 2021 11:20:11 +0800 Subject: [PATCH] init --- client.go | 240 +++++++++++++++++++++++++++++++++++++++++++++++++ client_test.go | 31 +++++++ order.pb.go | 198 ++++++++++++++++++++++++++++++++++++++++ order.proto | 22 +++++ 4 files changed, 491 insertions(+) create mode 100644 client.go create mode 100644 client_test.go create mode 100644 order.pb.go create mode 100644 order.proto diff --git a/client.go b/client.go new file mode 100644 index 0000000..539c9ef --- /dev/null +++ b/client.go @@ -0,0 +1,240 @@ +package order + +import ( + "crypto/md5" + "encoding/hex" + "encoding/json" + "errors" + "log" + "strconv" + "strings" + "time" + + "git.tetele.net/tgo/crypter" + + "github.com/golang/protobuf/proto" +) + +type OrderCreateRes struct { + OrderSn string +} + +func Create(dbname, site_id string, data map[string]string, url ...string) (*OrderCreateRes, error) { + + var order_rpc_url string = "127.0.0.1:7973" + if len(url) > 0 && url[0] != "" { + order_rpc_url = url[0] + } + conn, _, err := DialOrderService("tcp", order_rpc_url) + if err != nil { + return nil, err + } + defer conn.Close() + + data["dbname"] = dbname + data["site_id"] = site_id + + data_json, err := json.Marshal(data) + if err != nil { + return nil, err + } + + encryData := crypter.DesEn(string(data_json), "ordernew") + + now_int64 := time.Now().Unix() + + now := strconv.FormatInt(now_int64, 10) + + sign := Sign(encryData, now) + + req := &CreateRequest{ + proto.String(encryData), + proto.String(now), + proto.String(sign), + nil} + + res := &CreateResponse{} + + err = conn.Create(req, res) + + if err != nil { + return nil, err + } + + return HandleRes(res) + + // return map[string]interface{}{ + // "code": res.GetCode(), + // "msg": res.GetMsg(), + // "order_sn": res.GetOrderSn()}, err +} + +func CreateByChannel(dbname, site_id string, data map[string]string, url ...string) (*OrderCreateRes, error) { + + var order_rpc_url string = "127.0.0.1:7973" + if len(url) > 0 && url[0] != "" { + order_rpc_url = url[0] + } + conn, _, err := DialOrderService("tcp", order_rpc_url) + if err != nil { + return nil, err + } + defer conn.Close() + + data["dbname"] = dbname + data["site_id"] = site_id + data["source"] = "channel" + + data_json, err := json.Marshal(data) + if err != nil { + return nil, err + } + + encryData := crypter.DesEn(string(data_json), "ordernew") + + now := strconv.FormatInt(time.Now().Unix(), 10) + + sign := Sign(encryData, now) + + req := &CreateRequest{ + proto.String(encryData), + proto.String(now), + proto.String(sign), + nil} + + res := &CreateResponse{} + + err = conn.Create(req, res) + + if err != nil { + return nil, err + } + + return HandleRes(res) + // return map[string]interface{}{ + // "code": res.GetCode(), + // "msg": res.GetMsg(), + // "order_sn": res.GetOrderSn()}, err +} + +func CreateByCart(dbname, site_id string, data map[string]interface{}, url ...string) (*OrderCreateRes, error) { + + var order_rpc_url string = "127.0.0.1:7973" + if len(url) > 0 && url[0] != "" { + order_rpc_url = url[0] + } + conn, _, err := DialOrderService("tcp", order_rpc_url) + if err != nil { + return nil, err + } + defer conn.Close() + + data["dbname"] = dbname + data["site_id"] = site_id + + data_json, err := json.Marshal(data) + if err != nil { + return nil, err + } + + log.Println(string(data_json)) + + encryData := crypter.DesEn(string(data_json), "cartsnew") + + now := strconv.FormatInt(time.Now().Unix(), 10) + + sign := Sign(encryData, now) + + req := &CreateRequest{ + proto.String(encryData), + proto.String(now), + proto.String(sign), + nil} + + res := &CreateResponse{} + + err = conn.CreateByCart(req, res) + + if err != nil { + return nil, err + } + + return HandleRes(res) + // return map[string]interface{}{ + // "code": res.GetCode(), + // "msg": res.GetMsg(), + // "order_sn": res.GetOrderSn()}, err +} + +/** + * 签名 + */ +func Sign(data string, salt string) string { + + var build strings.Builder + + build.WriteString(data) + build.WriteString(salt) + build.WriteString("neworder") + + data_str := build.String() + + h := md5.New() + h.Write([]byte(data_str)) // 需要加密的字符串 + return hex.EncodeToString(h.Sum(nil)) // 输出加密结果 + +} + +/** + * 验证签名 + */ +func CheckSign(sign_str, data, salt string) bool { + sign := Sign(data, salt) + if strings.Compare(sign_str, sign) > -1 { + return true + } + return false +} + +/** + * 处理返回结果 + */ +func HandleRes(res *CreateResponse) (*OrderCreateRes, error) { + + res_data := res.GetData() + + if res_data == "" { + + return nil, errors.New("未收到收据") + } + + time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64) + if err != nil { + return nil, err + } + + now_int64 := time.Now().Unix() + + if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { + //时间误差前后10秒,返回 + return nil, errors.New("返回时间错误") + } + + check_sign := CheckSign(res.GetSign(), res_data, res.GetTime()) + if !check_sign { + return nil, errors.New("返回数据签名错误") + } + + //解密 + res_data_de := crypter.DesDe(res_data, "ordernew") + + var res_arr OrderCreateRes + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + + return &res_arr, nil +} diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..aa57085 --- /dev/null +++ b/client_test.go @@ -0,0 +1,31 @@ +package order + +import ( + "testing" +) + +func Test_CreateByCart(t *testing.T) { + + products := []map[string]string{ + map[string]string{ + "product_id": "18", + "sku_id": "6", + "quantity": "3", + }, + map[string]string{ + "product_id": "6", + "sku_id": "7", + "quantity": "2", + }, + } + data := map[string]interface{}{ + "product": products, + "name": "5BC02AB31C", + "mobile": "326598744", + } + + ret, err := CreateByCart("test1_tetele_com", "100064", data) + + t.Log(ret) + t.Log(err) +} diff --git a/order.pb.go b/order.pb.go new file mode 100644 index 0000000..b03b3aa --- /dev/null +++ b/order.pb.go @@ -0,0 +1,198 @@ +// Code generated by protoc-gen-go. +// source: order.proto +// DO NOT EDIT! + +/* +Package order is a generated protocol buffer package. + +It is generated from these files: + order.proto + +It has these top-level messages: + CreateRequest + CreateResponse +*/ +package order + +import proto "github.com/chai2010/protorpc/proto" +import math "math" + +import "io" +import "log" +import "net" +import "net/rpc" +import "time" +import protorpc "github.com/chai2010/protorpc" + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = math.Inf + +// 下单请求结构 +type CreateRequest struct { + Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` + Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` + Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *CreateRequest) Reset() { *m = CreateRequest{} } +func (m *CreateRequest) String() string { return proto.CompactTextString(m) } +func (*CreateRequest) ProtoMessage() {} + +func (m *CreateRequest) GetData() string { + if m != nil && m.Data != nil { + return *m.Data + } + return "" +} + +func (m *CreateRequest) GetTime() string { + if m != nil && m.Time != nil { + return *m.Time + } + return "" +} + +func (m *CreateRequest) GetSign() string { + if m != nil && m.Sign != nil { + return *m.Sign + } + return "" +} + +// 下单响应结构 +type CreateResponse struct { + Data *string `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"` + Time *string `protobuf:"bytes,2,opt,name=time" json:"time,omitempty"` + Sign *string `protobuf:"bytes,3,opt,name=sign" json:"sign,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *CreateResponse) Reset() { *m = CreateResponse{} } +func (m *CreateResponse) String() string { return proto.CompactTextString(m) } +func (*CreateResponse) ProtoMessage() {} + +func (m *CreateResponse) GetData() string { + if m != nil && m.Data != nil { + return *m.Data + } + return "" +} + +func (m *CreateResponse) GetTime() string { + if m != nil && m.Time != nil { + return *m.Time + } + return "" +} + +func (m *CreateResponse) GetSign() string { + if m != nil && m.Sign != nil { + return *m.Sign + } + return "" +} + +func init() { +} + +type OrderService interface { + Create(in *CreateRequest, out *CreateResponse) error + CreateByCart(in *CreateRequest, out *CreateResponse) error +} + +// AcceptOrderServiceClient accepts connections on the listener and serves requests +// for each incoming connection. Accept blocks; the caller typically +// invokes it in a go statement. +func AcceptOrderServiceClient(lis net.Listener, x OrderService) { + srv := rpc.NewServer() + if err := srv.RegisterName("OrderService", x); err != nil { + log.Fatal(err) + } + + for { + conn, err := lis.Accept() + if err != nil { + log.Fatalf("lis.Accept(): %v\n", err) + } + go srv.ServeCodec(protorpc.NewServerCodec(conn)) + } +} + +// RegisterOrderService publish the given OrderService implementation on the server. +func RegisterOrderService(srv *rpc.Server, x OrderService) error { + if err := srv.RegisterName("OrderService", x); err != nil { + return err + } + return nil +} + +// NewOrderServiceServer returns a new OrderService Server. +func NewOrderServiceServer(x OrderService) *rpc.Server { + srv := rpc.NewServer() + if err := srv.RegisterName("OrderService", x); err != nil { + log.Fatal(err) + } + return srv +} + +// ListenAndServeOrderService listen announces on the local network address laddr +// and serves the given OrderService implementation. +func ListenAndServeOrderService(network, addr string, x OrderService) error { + lis, err := net.Listen(network, addr) + if err != nil { + return err + } + defer lis.Close() + + srv := rpc.NewServer() + if err := srv.RegisterName("OrderService", x); err != nil { + return err + } + + for { + conn, err := lis.Accept() + if err != nil { + log.Fatalf("lis.Accept(): %v\n", err) + } + go srv.ServeCodec(protorpc.NewServerCodec(conn)) + } +} + +type OrderServiceClient struct { + *rpc.Client +} + +// NewOrderServiceClient returns a OrderService rpc.Client and stub to handle +// requests to the set of OrderService at the other end of the connection. +func NewOrderServiceClient(conn io.ReadWriteCloser) (*OrderServiceClient, *rpc.Client) { + c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn)) + return &OrderServiceClient{c}, c +} + +func (c *OrderServiceClient) Create(in *CreateRequest, out *CreateResponse) error { + return c.Call("OrderService.Create", in, out) +} +func (c *OrderServiceClient) CreateByCart(in *CreateRequest, out *CreateResponse) error { + return c.Call("OrderService.CreateByCart", in, out) +} + +// DialOrderService connects to an OrderService at the specified network address. +func DialOrderService(network, addr string) (*OrderServiceClient, *rpc.Client, error) { + c, err := protorpc.Dial(network, addr) + if err != nil { + return nil, nil, err + } + return &OrderServiceClient{c}, c, nil +} + +// DialOrderServiceTimeout connects to an OrderService at the specified network address. +func DialOrderServiceTimeout(network, addr string, + timeout time.Duration) (*OrderServiceClient, *rpc.Client, error) { + c, err := protorpc.DialTimeout(network, addr, timeout) + if err != nil { + return nil, nil, err + } + return &OrderServiceClient{c}, c, nil +} diff --git a/order.proto b/order.proto new file mode 100644 index 0000000..68f0a1d --- /dev/null +++ b/order.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; +package order; + +// 下单请求结构 +message CreateRequest { + string data = 1; + string time = 2; + string sign = 3; +} + +// 下单响应结构 +message CreateResponse { + string data = 1; //订单号 + string time = 2; + string sign = 3; +} + +// rpc方法 +service OrderService { + rpc create (CreateRequest) returns (CreateResponse); // 创建订单 + rpc createByCart (CreateRequest) returns (CreateResponse); // 创建订单 +} \ No newline at end of file