diff --git a/README.md b/README.md index 85b831f..f0ec4ec 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # orderrpc -订单rpc \ No newline at end of file +订单rpc + +## 自动生成PB文件 + +~~~ +protoc --go_out=./ order.proto +~~~ diff --git a/cancel.go b/cancel.go new file mode 100644 index 0000000..d71ca31 --- /dev/null +++ b/cancel.go @@ -0,0 +1,75 @@ +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 OrderCancelRes struct { + Success bool +} + +func Cancel(dbname, site_id string, order_id string, url ...string) (*OrderCancelRes, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + data := map[string]string{ + "dbname": dbname, + "site_id": site_id, + "order_id": order_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 := &CreateRequest{ + proto.String(encryData), + proto.String(now), + proto.String(sign), + nil} + + res := &CreateResponse{} + + err = conn.Cancel(req, res) + + if err != nil { + return nil, err + } + + res_data, err := HandleResponse(res) + + var res_arr OrderCancelRes + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + + return &res_arr, nil + +} diff --git a/common.go b/common.go new file mode 100644 index 0000000..4a8eae8 --- /dev/null +++ b/common.go @@ -0,0 +1,54 @@ +package order + +const DES_KEY = "ordernew" + +func rpc_server_conn(url ...string) (*OrderServiceClient, 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 + } + + return conn, nil +} + +/** + * 处理返回结果 + */ +func HandleResponse(res *CreateResponse) (string, 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") + + return res_data_de + +} diff --git a/order.pb.go b/order.pb.go index b03b3aa..88ac4ea 100644 --- a/order.pb.go +++ b/order.pb.go @@ -100,6 +100,7 @@ func init() { type OrderService interface { Create(in *CreateRequest, out *CreateResponse) error CreateByCart(in *CreateRequest, out *CreateResponse) error + Cancel(in *CreateRequest, out *CreateResponse) error } // AcceptOrderServiceClient accepts connections on the listener and serves requests @@ -177,6 +178,9 @@ func (c *OrderServiceClient) Create(in *CreateRequest, out *CreateResponse) erro func (c *OrderServiceClient) CreateByCart(in *CreateRequest, out *CreateResponse) error { return c.Call("OrderService.CreateByCart", in, out) } +func (c *OrderServiceClient) Cancel(in *CreateRequest, out *CreateResponse) error { + return c.Call("OrderService.Cancel", 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 68f0a1d..4cf54b3 100644 --- a/order.proto +++ b/order.proto @@ -19,4 +19,5 @@ message CreateResponse { service OrderService { rpc create (CreateRequest) returns (CreateResponse); // 创建订单 rpc createByCart (CreateRequest) returns (CreateResponse); // 创建订单 + rpc cancel (CreateRequest) returns (CreateResponse); // 取消订单 } \ No newline at end of file diff --git a/sign.go b/sign.go new file mode 100644 index 0000000..e69de29