From 2d24c944de010767b8786b26028264a29029b41d Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 6 Apr 2021 17:02:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=95=86=E6=88=B7=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business.client.go | 102 +++++++++++++++++++++++++++++++++++++++++++++ common.go | 18 ++++++++ 2 files changed, 120 insertions(+) create mode 100644 business.client.go diff --git a/business.client.go b/business.client.go new file mode 100644 index 0000000..9dd3cdf --- /dev/null +++ b/business.client.go @@ -0,0 +1,102 @@ +package userrpc + +import ( + "encoding/json" + "errors" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + "github.com/golang/protobuf/proto" +) + +/** + * 查找商户信息 + * 2021/04/06 + * gz + */ +func GetBusiness(site_id, dbname, business_id string, url ...string) (*Business, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + data := GetBusinessReq{} + data.SiteId = site_id + data.Dbname = dbname + data.BusinessId = business_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.GetBusiness(req, res) + + if err != nil { + return nil, err + } + + return HandleGetBusinessRes(res) +} + +/** + * 处理返回结果 + */ +func HandleGetBusinessRes(res *Response) (*Business, 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, DES_KEY) + + var res_arr Business + + 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 index 413547a..255fc12 100644 --- a/common.go +++ b/common.go @@ -27,11 +27,29 @@ type GetThirdReq struct { Userid string Platform string } + +type GetBusinessReq struct { + Req + BusinessId string +} + type Res struct { Errcode int Errmsg string } +type Business struct { + Type string + BusinessId string + CustomerId string + Name string + CompanyId string + Contact string + Mobile string + HandingFee string + IsOpen string +} + func rpc_server_conn(url ...string) (*UserServiceClient, error) { var user_rpc_url string = "127.0.0.1:7976"