diff --git a/client.go b/client.go new file mode 100644 index 0000000..5ddca90 --- /dev/null +++ b/client.go @@ -0,0 +1,71 @@ +package msgrpc + +import ( + "encoding/json" + "log" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + + "github.com/golang/protobuf/proto" +) + +func SendMsg(site_id, dbname, business_id, domainname, msg_type, order_sn, memo string, data interface{}, url ...string) (*MsgSend, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + arg_data, err := json.Marshal(data) + if err != nil { + log.Println("json marshal error:", err) + } + + arg := MsgBody{} + arg.SiteId = site_id + arg.Dbname = dbname + arg.Platform = "1" + arg.BusinessId = business_id + arg.Domainname = domainname + arg.Type = msg_type + arg.Data = string(arg_data) + arg.OrderSn = order_sn + arg.Memo = memo + + data_json, err := json.Marshal(arg) + if err != nil { + return nil, err + } + now_int64 := time.Now().Unix() + + encryData := crypter.DesEn(string(data_json), DES_KEY) + + 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.SendMsg(req, res) + + if err != nil { + return nil, err + } + + res_data, err := GetOrgData(res) + + var res_arr MsgSend + + err = json.Unmarshal([]byte(res_data), &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..bf7e73b --- /dev/null +++ b/client_test.go @@ -0,0 +1,22 @@ +package msgrpc + +import ( + "testing" +) + +func Test_SendMsg(t *testing.T) { + + site_id := "100064" + dbname := "dev_tetele_net" + business_id := "12" + domainname := "http://dev6.tetele.net" + msg_type := "order_create" + data := map[string]string{"vv": "dd", "cc": "ss"} + order_sn := "" + memo := "testing" + + ret, err := SendMsg(site_id, dbname, business_id, domainname, msg_type, order_sn, memo, data) + + t.Log(ret) + t.Log(err) +} diff --git a/common.go b/common.go index 27072f2..8e7a31f 100644 --- a/common.go +++ b/common.go @@ -1,9 +1,5 @@ package msgrpc -import ( - "git.tetele.net/tgo/conf" -) - const DES_KEY = "msgsirpc" type Req struct { @@ -28,7 +24,7 @@ type MsgSend struct { func rpc_server_conn(url ...string) (*MsgRpcServiceClient, error) { - var wx_rpc_url string = "127.0.0.1:" + conf.MSG_RPC_PORT + var wx_rpc_url string = "127.0.0.1:7954" if len(url) > 0 && url[0] != "" { wx_rpc_url = url[0] } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..868a186 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module git.tetele.net/tgo/msgrpc + +go 1.14 + +require ( + git.tetele.net/tgo/crypter v0.2.2 // indirect + github.com/chai2010/protorpc v1.0.0 // indirect + golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1c4b4d9 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +git.tetele.net/tgo/crypter v0.2.2 h1:YMQJh2Gj5Po4ZfelJUmXBKi01UbmtiSy3bmqRfnYQMo= +git.tetele.net/tgo/crypter v0.2.2/go.mod h1:vfvRLZA8+lHNgNXneOcgvVhDyuv25ZRb+C6xHOmXNx0= +github.com/chai2010/protorpc v1.0.0 h1:aJ45G9sl1utSKo35EqnBSTs5jqTpdJDJAuZMMYPAtFo= +github.com/chai2010/protorpc v1.0.0/go.mod h1:woR3WwjaQDqFjlzdVsFEKiK5Ur12QL8mYxVPjfr5z54= +github.com/golang/protobuf v1.0.0 h1:lsek0oXi8iFE9L+EXARyHIjU5rlWIhhTkjDz3vHhWWQ= +github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/snappy v0.0.0-20170215233205-553a64147049 h1:K9KHZbXKpGydfDN0aZrsoHpLJlZsBrGMFWbgLDGnPZk= +github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a h1:kr2P4QFmQr29mSLA43kwrOcgcReGTfbE9N577tCTuBc= +golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=