| @ -1,3 +1,9 @@ | |||
| # orderrpc | |||
| 订单rpc | |||
| 订单rpc | |||
| ## 自动生成PB文件 | |||
| ~~~ | |||
| protoc --go_out=./ order.proto | |||
| ~~~ | |||
| @ -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 | |||
| } | |||
| @ -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 | |||
| } | |||