From dfbea563339246a0272285e27f3403c92baca693 Mon Sep 17 00:00:00 2001 From: guzeng Date: Wed, 14 Dec 2022 09:27:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=A2=84=E7=BA=A6=E6=A0=B8?= =?UTF-8?q?=E9=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 36 +++++++++++++++++++++++++ order.pb.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ order.proto | 17 ++++++++++++ verify.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 verify.go diff --git a/common.go b/common.go index 59e2e73..a425f58 100644 --- a/common.go +++ b/common.go @@ -60,3 +60,39 @@ func HandleResponse(res *CreateResponse) (string, error) { return res_data_de, nil } + +/** + * 处理返回结果 + */ +func GetOrgData(res *Response) (string, error) { + + res_data := res.GetData() + + if res_data == "" { + + return "", errors.New("未收到收据") + } + + time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64) + if err != nil { + return "", err + } + + now_int64 := time.Now().Unix() + + if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 { + //时间误差前后10秒,返回 + return "", errors.New("返回时间错误") + } + + check_sign := CheckSign(res.GetSign(), res_data, res.GetTime()) + if !check_sign { + return "", errors.New("返回数据签名错误") + } + + //解密 + res_data_de := crypter.DesDe(res_data, DES_KEY) + + return res_data_de, nil + +} diff --git a/order.pb.go b/order.pb.go index bf23c9d..e5bef4d 100644 --- a/order.pb.go +++ b/order.pb.go @@ -11,6 +11,8 @@ It is generated from these files: It has these top-level messages: CreateRequest CreateResponse + Request + Response */ package orderrpc @@ -94,6 +96,72 @@ func (m *CreateResponse) GetSign() string { return "" } +// 请求结构 +type Request 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 *Request) Reset() { *m = Request{} } +func (m *Request) String() string { return proto.CompactTextString(m) } +func (*Request) ProtoMessage() {} + +func (m *Request) GetData() string { + if m != nil && m.Data != nil { + return *m.Data + } + return "" +} + +func (m *Request) GetTime() string { + if m != nil && m.Time != nil { + return *m.Time + } + return "" +} + +func (m *Request) GetSign() string { + if m != nil && m.Sign != nil { + return *m.Sign + } + return "" +} + +// 响应结构 +type Response 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 *Response) Reset() { *m = Response{} } +func (m *Response) String() string { return proto.CompactTextString(m) } +func (*Response) ProtoMessage() {} + +func (m *Response) GetData() string { + if m != nil && m.Data != nil { + return *m.Data + } + return "" +} + +func (m *Response) GetTime() string { + if m != nil && m.Time != nil { + return *m.Time + } + return "" +} + +func (m *Response) GetSign() string { + if m != nil && m.Sign != nil { + return *m.Sign + } + return "" +} + func init() { } @@ -101,6 +169,8 @@ type OrderService interface { Create(in *CreateRequest, out *CreateResponse) error CreateByCart(in *CreateRequest, out *CreateResponse) error Cancel(in *CreateRequest, out *CreateResponse) error + AutoReceive(in *Request, out *Response) error + VerifyByBooking(in *Request, out *Response) error } // AcceptOrderServiceClient accepts connections on the listener and serves requests @@ -181,6 +251,12 @@ func (c *OrderServiceClient) CreateByCart(in *CreateRequest, out *CreateResponse func (c *OrderServiceClient) Cancel(in *CreateRequest, out *CreateResponse) error { return c.Call("OrderService.Cancel", in, out) } +func (c *OrderServiceClient) AutoReceive(in *Request, out *Response) error { + return c.Call("OrderService.AutoReceive", in, out) +} +func (c *OrderServiceClient) VerifyByBooking(in *Request, out *Response) error { + return c.Call("OrderService.VerifyByBooking", in, out) +} // DialOrderService connects to an OrderService at the specified network address. func DialOrderService(network, addr string) (*OrderServiceClient, *rpc.Client, error) { diff --git a/order.proto b/order.proto index 5dfb931..6d28290 100644 --- a/order.proto +++ b/order.proto @@ -15,9 +15,26 @@ message CreateResponse { string sign = 3; } + +// 请求结构 +message Request { + string data = 1; + string time = 2; + string sign = 3; +} + +// 响应结构 +message Response { + string data = 1; + string time = 2; + string sign = 3; +} + // rpc方法 service OrderService { rpc create (CreateRequest) returns (CreateResponse); // 创建订单 rpc createByCart (CreateRequest) returns (CreateResponse); // 创建订单 rpc cancel (CreateRequest) returns (CreateResponse); // 取消订单 + rpc autoReceive(Request) returns(Response); //自动收货 + rpc VerifyByBooking(Request) returns(Response); //核销 } \ No newline at end of file diff --git a/verify.go b/verify.go new file mode 100644 index 0000000..f93b3fc --- /dev/null +++ b/verify.go @@ -0,0 +1,76 @@ +package orderrpc + +import ( + "crypto/md5" + "encoding/hex" + "encoding/json" + "errors" + "log" + "strconv" + "strings" + "time" + + "git.tetele.net/tgo/crypter" + + "github.com/golang/protobuf/proto" +) + +type OrderVerifyRes struct { + Result bool +} + +func VerifyByBooking(dbname, site_id string, data map[string]string, url ...string) (*OrderVerifyRes, 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), DES_KEY) + + now_int64 := time.Now().Unix() + + now := strconv.FormatInt(now_int64, 10) + + sign := Sign(encryData, now) + + req := &Request{ + proto.String(encryData), + proto.String(now), + proto.String(sign), + nil} + + res := &Response{} + + err = conn.VerifyByBooking(req, res) + + if err != nil { + return nil, err + } + + res_data_de := GetOrgData(res) + + var res_arr OrderVerifyRes + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + + return &res_arr, nil + +}