Browse Source

增加预约核销

master v0.3.0
guzeng 1 year ago
parent
commit
dfbea56333
4 changed files with 205 additions and 0 deletions
  1. +36
    -0
      common.go
  2. +76
    -0
      order.pb.go
  3. +17
    -0
      order.proto
  4. +76
    -0
      verify.go

+ 36
- 0
common.go View File

@ -60,3 +60,39 @@ func HandleResponse(res *CreateResponse) (string, error) {
return res_data_de, nil 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
}

+ 76
- 0
order.pb.go View File

@ -11,6 +11,8 @@ It is generated from these files:
It has these top-level messages: It has these top-level messages:
CreateRequest CreateRequest
CreateResponse CreateResponse
Request
Response
*/ */
package orderrpc package orderrpc
@ -94,6 +96,72 @@ func (m *CreateResponse) GetSign() string {
return "" 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() { func init() {
} }
@ -101,6 +169,8 @@ type OrderService interface {
Create(in *CreateRequest, out *CreateResponse) error Create(in *CreateRequest, out *CreateResponse) error
CreateByCart(in *CreateRequest, out *CreateResponse) error CreateByCart(in *CreateRequest, out *CreateResponse) error
Cancel(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 // 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 { func (c *OrderServiceClient) Cancel(in *CreateRequest, out *CreateResponse) error {
return c.Call("OrderService.Cancel", in, out) 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. // DialOrderService connects to an OrderService at the specified network address.
func DialOrderService(network, addr string) (*OrderServiceClient, *rpc.Client, error) { func DialOrderService(network, addr string) (*OrderServiceClient, *rpc.Client, error) {


+ 17
- 0
order.proto View File

@ -15,9 +15,26 @@ message CreateResponse {
string sign = 3; 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方法 // rpc方法
service OrderService { service OrderService {
rpc create (CreateRequest) returns (CreateResponse); // rpc create (CreateRequest) returns (CreateResponse); //
rpc createByCart (CreateRequest) returns (CreateResponse); // rpc createByCart (CreateRequest) returns (CreateResponse); //
rpc cancel (CreateRequest) returns (CreateResponse); // rpc cancel (CreateRequest) returns (CreateResponse); //
rpc autoReceive(Request) returns(Response); //
rpc VerifyByBooking(Request) returns(Response); //
} }

+ 76
- 0
verify.go View File

@ -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
}

Loading…
Cancel
Save