diff --git a/address.client.go b/address.client.go new file mode 100644 index 0000000..0b9f2d2 --- /dev/null +++ b/address.client.go @@ -0,0 +1,54 @@ +package userrpc + +import ( + "encoding/json" + "errors" +) + +func GetAddressInfo(site_id, dbname, address_id string, url ...string) (map[string]string, error) { + + if dbname == "" || address_id == "" { + return nil, errors.New("参数错误") + } + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + data := GetAddressReq{} + data.SiteId = site_id + data.Dbname = dbname + data.AddressId = address_id + + req, err := SetReqData(data) + if err != nil { + return nil, err + } + + res := &Response{} + + err = conn.GetAddressInfo(req, res) + + if err != nil { + return nil, err + } + + res_data_de, err := GetResData(res) + if err != nil { + return nil, err + } + if res_data_de == "" { + return nil, nil + } + var res_arr map[string]string + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + return res_arr, nil + +} diff --git a/address.client_test.go b/address.client_test.go new file mode 100644 index 0000000..8930dd6 --- /dev/null +++ b/address.client_test.go @@ -0,0 +1,16 @@ +package userrpc + +import ( + "testing" +) + +func Test_GetAddressInfo(t *testing.T) { + + site_id := "" + dbname := "shop_v2" + id := "2" + + ret, err := GetAddressInfo(site_id, dbname, id) + t.Log(ret) + t.Log(err) +} diff --git a/business.client.go b/business.client.go index 9dd3cdf..be65467 100644 --- a/business.client.go +++ b/business.client.go @@ -100,3 +100,51 @@ func HandleGetBusinessRes(res *Response) (*Business, error) { return &res_arr, nil } + +func GetBusinessInfo(site_id, dbname, id string, url ...string) (map[string]string, error) { + + if dbname == "" || id == "" { + return nil, errors.New("参数错误") + } + + 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 = id + + req, err := SetReqData(data) + if err != nil { + return nil, err + } + + res := &Response{} + + err = conn.GetBusinessInfo(req, res) + + if err != nil { + return nil, err + } + + res_data_de, err := GetResData(res) + if err != nil { + return nil, err + } + if res_data_de == "" { + return nil, nil + } + var res_arr map[string]string + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + return res_arr, nil + +} diff --git a/business.client_test.go b/business.client_test.go new file mode 100644 index 0000000..0b484e9 --- /dev/null +++ b/business.client_test.go @@ -0,0 +1,16 @@ +package userrpc + +import ( + "testing" +) + +func Test_GetBusinessInfo(t *testing.T) { + + site_id := "" + dbname := "shop_v2" + id := "5" + + ret, err := GetBusinessInfo(site_id, dbname, id) + t.Log(ret) + t.Log(err) +} diff --git a/common.go b/common.go index 255fc12..6bf19bd 100644 --- a/common.go +++ b/common.go @@ -1,5 +1,9 @@ package userrpc +import ( + "git.tetele.net/tgo/conf" +) + const DES_KEY = "usersrpc" type Req struct { @@ -33,6 +37,15 @@ type GetBusinessReq struct { BusinessId string } +type GetUserReq struct { + Req + UserId string +} + +type GetAddressReq struct { + Req + AddressId string +} type Res struct { Errcode int Errmsg string @@ -52,12 +65,16 @@ type Business struct { func rpc_server_conn(url ...string) (*UserServiceClient, error) { - var user_rpc_url string = "127.0.0.1:7976" + var rpc_url string if len(url) > 0 && url[0] != "" { - user_rpc_url = url[0] + rpc_url = url[0] + } else if conf.USER_RPC_URL != "" { + rpc_url = conf.USER_RPC_URL + } else { + rpc_url = "127.0.0.1:" + conf.USER_RPC_PORT } - conn, _, err := DialUserService("tcp", user_rpc_url) + conn, _, err := DialUserService("tcp", rpc_url) if err != nil { return nil, err } diff --git a/data.go b/data.go new file mode 100644 index 0000000..65d61f6 --- /dev/null +++ b/data.go @@ -0,0 +1,106 @@ +package userrpc + +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 +} diff --git a/go.mod b/go.mod index ac4d69d..21ae39d 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module git.tetele.net/tgo/userrpc go 1.14 require ( - git.tetele.net/tgo/crypter v0.2.2 // indirect + git.tetele.net/tgo/conf v0.33.1 + git.tetele.net/tgo/crypter v0.2.2 github.com/chai2010/protorpc v1.0.0 github.com/golang/protobuf v1.0.0 golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect diff --git a/go.sum b/go.sum index c6beb24..61a4576 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.tetele.net/tgo/conf v0.33.1 h1:ZEIv3Vq35RCv5f3T3Uz97s2mkZLl7W5OlmXvzI3/sS8= +git.tetele.net/tgo/conf v0.33.1/go.mod h1:AWVIBEDE5dtotthUgR0SWaR2Qa6/f+O5WQ3s7Tj8q7A= 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= diff --git a/sign.go b/sign.go index 8929224..3c39f00 100644 --- a/sign.go +++ b/sign.go @@ -84,3 +84,46 @@ func HandleRes(res *Response) (*Res, error) { return &res_arr, nil } + +/** + * 处理返回结果 + */ +func HandleUserRes(res *Response) (map[string]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, DES_KEY) + + var res_arr map[string]string + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + + return res_arr, nil +} diff --git a/third.client_test.go b/third.client_test.go index 7ac4655..cbc3a52 100644 --- a/third.client_test.go +++ b/third.client_test.go @@ -2,19 +2,27 @@ package userrpc import ( "testing" + + "git.tetele.net/tgo/crypter" ) func Test_BindThird(t *testing.T) { - dbname := "test1_tetele_com" - site_id := "1001" - platform := "deliver_mini" - user_id := "1" - openid := "CasfoieFsdfsdfiDiodf_df" - openname := "测试中" - avatar := "https://ssss.ttt.ddd/sfsd" - mobile := "1234456456" - res, err := BindThird(site_id, dbname, platform, user_id, openid, openname, "", avatar, mobile) - t.Log(res) - t.Log(err) + // dbname := "test1_tetele_com" + // site_id := "1001" + // platform := "deliver_mini" + // user_id := "1" + // openid := "CasfoieFsdfsdfiDiodf_df" + // openname := "测试中" + // avatar := "https://ssss.ttt.ddd/sfsd" + // mobile := "1234456456" + // res, err := BindThird(site_id, dbname, platform, user_id, openid, openname, "", avatar, mobile) + // t.Log(res) + // t.Log(err) + + // data := "E5B2642BDEA1FBC85BA958C398E58DEC2E1425E26E25D6A178DBD078AD0DFC6859DF5634B0A01CE01F84E056380981A1F1433044FB197D8965FD8F3322DF2373619AD4ACB158605B45AC3947B200C8B62C2B30256B2D2D7283492F268F609944" + data := "E5B2642BDEA1FBC85BA958C398E58DEC2E1425E26E25D6A178DBD078AD0DFC6859DF5634B0A01CE01F84E056380981A1F1433044FB197D89AF51E05A9E9F5C76BE8F563F2E4D42AB3C3C649B4187A7C48E98988C734CEACD" + + org := crypter.DesDe(data, DES_KEY) + t.Log(org) } diff --git a/third_v2.client.go b/third_v2.client.go new file mode 100644 index 0000000..9f105fb --- /dev/null +++ b/third_v2.client.go @@ -0,0 +1,56 @@ +package userrpc + +import ( + "encoding/json" +) + +/** + * 查找第三方平台openid V2版 + * 2021/09/07 + * gz + */ +func GetUserThird(site_id, dbname, platform, user_id, field string, url ...string) (map[string]string, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + data := map[string]string{ + "site_id": site_id, + "dbname": dbname, + "user_id": user_id, + "platform": platform, + "field": field, + } + + req, err := SetReqData(data) + if err != nil { + return nil, err + } + res := &Response{} + + err = conn.GetUserThird(req, res) + + if err != nil { + return nil, err + } + + res_data_de, err := GetResData(res) + if err != nil { + return nil, err + } + if res_data_de == "" { + return nil, nil + } + var res_arr map[string]string + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + return res_arr, nil + +} diff --git a/third_v2.client_test.go b/third_v2.client_test.go new file mode 100644 index 0000000..5da2f2c --- /dev/null +++ b/third_v2.client_test.go @@ -0,0 +1,13 @@ +package userrpc + +import ( + "testing" +) + +func Test_GetUserThird(t *testing.T) { + + org, err := GetUserThird("1", "shop_v2", "miniapp", "2", "id,openid") + + t.Log(org) + t.Log(err) +} diff --git a/user.client.go b/user.client.go index 9e80959..04e1b07 100644 --- a/user.client.go +++ b/user.client.go @@ -1,6 +1,12 @@ package userrpc import ( + "encoding/json" + "strconv" + "time" + + "git.tetele.net/tgo/crypter" + "github.com/golang/protobuf/proto" ) @@ -151,3 +157,51 @@ func Login(dbname, account, password string, url ...string) (map[string]string, return map[string]string{}, nil } + +/** + * 查找商户信息 + * 2021/04/06 + * gz + */ +func GetById(site_id, dbname, user_id string, url ...string) (map[string]string, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + data := GetUserReq{} + data.SiteId = site_id + data.Dbname = dbname + data.UserId = user_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.GetById(req, res) + + if err != nil { + return nil, err + } + + return HandleUserRes(res) +} diff --git a/user.client_test.go b/user.client_test.go index 1b73206..bf50143 100644 --- a/user.client_test.go +++ b/user.client_test.go @@ -5,31 +5,31 @@ import ( ) func Test_GetUserByToken(t *testing.T) { - dbname := "dev_tetele_net" - token := "5536827c-36a5-4ec7-946d-49e4380c5103" - res, err := GetUserByToken(dbname, token, "111.229.34.252:7976") - t.Log(res) - t.Log(res["UserId"]) - t.Log(err) + dbname := "shop_v2" + // token := "5536827c-36a5-4ec7-946d-49e4380c5103" + // res, err := GetUserByToken(dbname, token, "111.229.34.252:7976") + // t.Log(res) + // t.Log(res["UserId"]) + // t.Log(err) - res2, err := GetUserByUsername(dbname, "tetele") + res2, err := GetById("1023", dbname, "3") t.Log(res2) - t.Log(res2.GetUserId()) + // t.Log(res2.GetUserId()) t.Log(err) - res3, err := GetUserByUsercode(dbname, "2233") - t.Log(res3) - t.Log(*res3.UserId) - t.Log(err) + // res3, err := GetUserByUsercode(dbname, "2233") + // t.Log(res3) + // t.Log(*res3.UserId) + // t.Log(err) } -func Test_Login(t *testing.T) { - dbname := "dev_tetele_net" - account := "admin" - password := "e10adc3949ba59abbe56e057f20f883e" - res, err := Login(dbname, account, password) - t.Log(res) - t.Log(res["UserId"]) - t.Log(err) +// func Test_Login(t *testing.T) { +// dbname := "dev_tetele_net" +// account := "admin" +// password := "e10adc3949ba59abbe56e057f20f883e" +// res, err := Login(dbname, account, password) +// t.Log(res) +// t.Log(res["UserId"]) +// t.Log(err) -} +// } diff --git a/user.pb.go b/user.pb.go index 2db43eb..d100c7a 100644 --- a/user.pb.go +++ b/user.pb.go @@ -11,6 +11,7 @@ It is generated from these files: It has these top-level messages: UserRequest UserInfoByUsername + UserInfoById UserInfoByUsercode UserResponse LoginRequest @@ -84,6 +85,31 @@ func (m *UserInfoByUsername) GetUsername() string { return "" } +// 使用userid查询用户信息请求结构 +type UserInfoById struct { + Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"` + Userid *string `protobuf:"bytes,2,opt,name=userid" json:"userid,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *UserInfoById) Reset() { *m = UserInfoById{} } +func (m *UserInfoById) String() string { return proto.CompactTextString(m) } +func (*UserInfoById) ProtoMessage() {} + +func (m *UserInfoById) GetDbname() string { + if m != nil && m.Dbname != nil { + return *m.Dbname + } + return "" +} + +func (m *UserInfoById) GetUserid() string { + if m != nil && m.Userid != nil { + return *m.Userid + } + return "" +} + // 使用usercode查询用户信息请求结构 type UserInfoByUsercode struct { Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"` @@ -452,7 +478,11 @@ type UserService interface { GetByUsercode(in *UserInfoByUsercode, out *UserResponse) error BindThird(in *Request, out *Response) error GetThird(in *Request, out *Response) error + GetUserThird(in *Request, out *Response) error GetBusiness(in *Request, out *Response) error + GetById(in *Request, out *Response) error + GetBusinessInfo(in *Request, out *Response) error + GetAddressInfo(in *Request, out *Response) error } // AcceptUserServiceClient accepts connections on the listener and serves requests @@ -542,9 +572,21 @@ func (c *UserServiceClient) BindThird(in *Request, out *Response) error { func (c *UserServiceClient) GetThird(in *Request, out *Response) error { return c.Call("UserService.GetThird", in, out) } +func (c *UserServiceClient) GetUserThird(in *Request, out *Response) error { + return c.Call("UserService.GetUserThird", in, out) +} func (c *UserServiceClient) GetBusiness(in *Request, out *Response) error { return c.Call("UserService.GetBusiness", in, out) } +func (c *UserServiceClient) GetById(in *Request, out *Response) error { + return c.Call("UserService.GetById", in, out) +} +func (c *UserServiceClient) GetBusinessInfo(in *Request, out *Response) error { + return c.Call("UserService.GetBusinessInfo", in, out) +} +func (c *UserServiceClient) GetAddressInfo(in *Request, out *Response) error { + return c.Call("UserService.GetAddressInfo", in, out) +} // DialUserService connects to an UserService at the specified network address. func DialUserService(network, addr string) (*UserServiceClient, *rpc.Client, error) { diff --git a/user.proto b/user.proto index 76674b3..6563b4a 100644 --- a/user.proto +++ b/user.proto @@ -11,6 +11,11 @@ message UserInfoByUsername { string dbname = 1; string username = 2; } +// 使用userid查询用户信息请求结构 +message UserInfoById { + string dbname = 1; + string userid = 2; +} // 使用usercode查询用户信息请求结构 message UserInfoByUsercode { @@ -87,7 +92,11 @@ service UserService { rpc getByUsercode (UserInfoByUsercode) returns (UserResponse); // 使用usercode查询用户 rpc bindThird (Request) returns (Response); // 绑定第三方账号 rpc getThird(Request) returns (Response); // 查询第三方账号 + rpc getUserThird(Request) returns (Response); // 查询第三方账号v2 rpc getBusiness(Request) returns (Response); // 查询商户 + rpc getById(Request) returns (Response); // 查询用户 + rpc getBusinessInfo(Request) returns (Response); // 查询分销商 + rpc getAddressInfo(Request) returns (Response); // 查询地址 rpc getUserScore(Request) returns (Response); // 查询用户积分 rpc getUserRedEnvelope(Request) returns (Response); // 查询用户红包 } \ No newline at end of file