@ -0,0 +1,63 @@ | |||
package yytrpc | |||
import ( | |||
"encoding/json" | |||
"errors" | |||
"log" | |||
) | |||
// 提交怡亚通订单 | |||
func CreateOrder(dbname, siteid, order_sn string, url ...string) (bool, error) { | |||
if dbname == "" || siteid == "" || order_sn == "" { | |||
return false, errors.New("参数错误") | |||
} | |||
conn, err := rpc_server_conn(url...) | |||
if err != nil { | |||
return false, err | |||
} | |||
defer conn.Close() | |||
data := OrderParam{} | |||
data.Dbname = dbname | |||
data.SiteId = siteid | |||
data.OrderSn = order_sn | |||
req, err := SetReqData(data) | |||
if err != nil { | |||
log.Println("CreateOrder crypter error:", err) | |||
return false, err | |||
} | |||
res := &Response{} | |||
err = conn.CreateOrder(req, res) | |||
if err != nil { | |||
log.Println("yytrpc CreateOrder error:", err) | |||
return false, err | |||
} | |||
res_data_de, err := GetResData(res) | |||
if err != nil { | |||
return false, err | |||
} | |||
if res_data_de == "" { | |||
return false, nil | |||
} | |||
var return_res bool | |||
err = json.Unmarshal([]byte(res_data_de), &return_res) | |||
if err != nil { | |||
return false, err | |||
} | |||
return return_res, nil | |||
} |
@ -0,0 +1,16 @@ | |||
package yytrpc | |||
import ( | |||
"testing" | |||
) | |||
func Test_CreateOrder(t *testing.T) { | |||
dbname := "shop" | |||
siteid := "1198881" | |||
order_sn := "864351351351" | |||
list, err := CreateOrder(dbname,siteid,order_sn) | |||
t.Log(list) | |||
t.Log(err) | |||
} |
@ -0,0 +1,35 @@ | |||
package yytrpc | |||
import ( | |||
"git.tetele.net/yueheng/conf" | |||
) | |||
type Req struct { | |||
SiteId string | |||
Dbname string | |||
} | |||
type Res struct { | |||
Errcode int | |||
Errmsg string | |||
} | |||
func rpc_server_conn(url ...string) (*OrderServiceClient, error) { | |||
var rpc_url string | |||
if len(url) > 0 && url[0] != "" { | |||
rpc_url = url[0] | |||
} else if conf.YYT_RPC_URL != "" { | |||
rpc_url = conf.YYT_RPC_URL | |||
} else { | |||
rpc_url = "127.0.0.1:" + conf.YYT_RPC_URL | |||
} | |||
conn, _, err := DialOrderService("tcp", rpc_url) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return conn, nil | |||
} |
@ -0,0 +1,106 @@ | |||
package yytrpc | |||
import ( | |||
"encoding/json" | |||
"errors" | |||
"strconv" | |||
"time" | |||
"git.tetele.net/tgo/crypter" | |||
"github.com/golang/protobuf/proto" | |||
) | |||
func SetResData(data interface{}, res *Response) { | |||
res_data_json, err := json.Marshal(data) | |||
if err == nil { | |||
encryData := crypter.DesEn(string(res_data_json), DES_KEY) | |||
now_str := strconv.FormatInt(time.Now().Unix(), 10) | |||
res_sign := Sign(encryData, now_str) | |||
res.Data = proto.String(encryData) | |||
res.Time = proto.String(now_str) | |||
res.Sign = proto.String(res_sign) | |||
} | |||
} | |||
func SetReqData(arg interface{}) (*Request, error) { | |||
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) | |||
return &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}, nil | |||
} | |||
func GetReqData(req *Request) (string, error) { | |||
res_data := req.GetData() | |||
if res_data != "" { | |||
time_int64, err := strconv.ParseInt(req.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(req.GetSign(), res_data, req.GetTime()) | |||
if !check_sign { | |||
return "", errors.New("返回数据签名错误") | |||
} | |||
//解密 | |||
return crypter.DesDe(res_data, DES_KEY), nil | |||
} | |||
return "", nil | |||
} | |||
func GetResData(res *Response) (string, error) { | |||
res_data := res.GetData() | |||
if res_data != "" { | |||
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("返回数据签名错误") | |||
} | |||
//解密 | |||
return crypter.DesDe(res_data, DES_KEY), nil | |||
} | |||
return "", nil | |||
} |
@ -0,0 +1,16 @@ | |||
module git.tetele.net/tgo/yytrpc | |||
go 1.17 | |||
require ( | |||
git.tetele.net/tgo/crypter v0.2.2 | |||
git.tetele.net/yueheng/conf v1.2.2 | |||
github.com/chai2010/protorpc v1.1.3 | |||
github.com/golang/protobuf v1.5.2 | |||
) | |||
require ( | |||
github.com/golang/snappy v0.0.3 // indirect | |||
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e // indirect | |||
google.golang.org/protobuf v1.26.0 // indirect | |||
) |
@ -0,0 +1,28 @@ | |||
git.tetele.net/tgo/crypter v0.2.2 h1:YMQJh2Gj5Po4ZfelJUmXBKi01UbmtiSy3bmqRfnYQMo= | |||
git.tetele.net/tgo/crypter v0.2.2/go.mod h1:vfvRLZA8+lHNgNXneOcgvVhDyuv25ZRb+C6xHOmXNx0= | |||
git.tetele.net/yueheng/conf v1.2.2 h1:oP19x+D/8DOchrhH1/yOil8TgVw17xF/Bin4DR6Bhnc= | |||
git.tetele.net/yueheng/conf v1.2.2/go.mod h1:qRujMTqjMByvdC05qdfscZMFRKM5XA1qbtz4rnFyStY= | |||
github.com/chai2010/protorpc v1.1.3 h1:VJK5hIoZn0XCGol0GmbxZkUG6FbTI5LP2Lam6RVd15w= | |||
github.com/chai2010/protorpc v1.1.3/go.mod h1:/wO0kiyVdu7ug8dCMrA2yDr2vLfyhsLEuzLa9J2HJ+I= | |||
github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | |||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= | |||
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= | |||
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= | |||
github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= | |||
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= | |||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= | |||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | |||
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e h1:MUP6MR3rJ7Gk9LEia0LP2ytiH6MuCfs7qYz+47jGdD8= | |||
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= | |||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | |||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | |||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | |||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | |||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | |||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | |||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | |||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= | |||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | |||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= | |||
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= | |||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= |
@ -0,0 +1,37 @@ | |||
package yytrpc | |||
import ( | |||
"crypto/md5" | |||
"encoding/hex" | |||
"strings" | |||
) | |||
/** | |||
* 签名 | |||
*/ | |||
func Sign(data string, salt string) string { | |||
var build strings.Builder | |||
build.WriteString(data) | |||
build.WriteString(salt) | |||
build.WriteString("ta334signsk98") | |||
data_str := build.String() | |||
h := md5.New() | |||
h.Write([]byte(data_str)) // 需要加密的字符串 | |||
return hex.EncodeToString(h.Sum(nil)) // 输出加密结果 | |||
} | |||
/** | |||
* 验证签名 | |||
*/ | |||
func CheckSign(sign_str, data, salt string) bool { | |||
sign := Sign(data, salt) | |||
if strings.Compare(sign_str, sign) > -1 { | |||
return true | |||
} | |||
return false | |||
} |
@ -0,0 +1,9 @@ | |||
package yytrpc | |||
var DES_KEY = "yyt%2468" | |||
type OrderParam struct { | |||
SiteId string `json:"site_id"` | |||
Dbname string `json:"database"` | |||
OrderSn string `json:"order_sn"` | |||
} |