Browse Source

merge

master
listen 3 years ago
parent
commit
8816569a30
16 changed files with 521 additions and 36 deletions
  1. +54
    -0
      address.client.go
  2. +16
    -0
      address.client_test.go
  3. +48
    -0
      business.client.go
  4. +16
    -0
      business.client_test.go
  5. +20
    -3
      common.go
  6. +106
    -0
      data.go
  7. +2
    -1
      go.mod
  8. +2
    -0
      go.sum
  9. +43
    -0
      sign.go
  10. +19
    -11
      third.client_test.go
  11. +56
    -0
      third_v2.client.go
  12. +13
    -0
      third_v2.client_test.go
  13. +54
    -0
      user.client.go
  14. +21
    -21
      user.client_test.go
  15. +42
    -0
      user.pb.go
  16. +9
    -0
      user.proto

+ 54
- 0
address.client.go View File

@ -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
}

+ 16
- 0
address.client_test.go View File

@ -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)
}

+ 48
- 0
business.client.go View File

@ -100,3 +100,51 @@ func HandleGetBusinessRes(res *Response) (*Business, error) {
return &res_arr, nil 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
}

+ 16
- 0
business.client_test.go View File

@ -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)
}

+ 20
- 3
common.go View File

@ -1,5 +1,9 @@
package userrpc package userrpc
import (
"git.tetele.net/tgo/conf"
)
const DES_KEY = "usersrpc" const DES_KEY = "usersrpc"
type Req struct { type Req struct {
@ -33,6 +37,15 @@ type GetBusinessReq struct {
BusinessId string BusinessId string
} }
type GetUserReq struct {
Req
UserId string
}
type GetAddressReq struct {
Req
AddressId string
}
type Res struct { type Res struct {
Errcode int Errcode int
Errmsg string Errmsg string
@ -52,12 +65,16 @@ type Business struct {
func rpc_server_conn(url ...string) (*UserServiceClient, error) { 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] != "" { 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 { if err != nil {
return nil, err return nil, err
} }


+ 106
- 0
data.go View File

@ -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
}

+ 2
- 1
go.mod View File

@ -3,7 +3,8 @@ module git.tetele.net/tgo/userrpc
go 1.14 go 1.14
require ( 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/chai2010/protorpc v1.0.0
github.com/golang/protobuf v1.0.0 github.com/golang/protobuf v1.0.0
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect


+ 2
- 0
go.sum View File

@ -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 h1:YMQJh2Gj5Po4ZfelJUmXBKi01UbmtiSy3bmqRfnYQMo=
git.tetele.net/tgo/crypter v0.2.2/go.mod h1:vfvRLZA8+lHNgNXneOcgvVhDyuv25ZRb+C6xHOmXNx0= 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 h1:aJ45G9sl1utSKo35EqnBSTs5jqTpdJDJAuZMMYPAtFo=


+ 43
- 0
sign.go View File

@ -84,3 +84,46 @@ func HandleRes(res *Response) (*Res, error) {
return &res_arr, nil 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
}

+ 19
- 11
third.client_test.go View File

@ -2,19 +2,27 @@ package userrpc
import ( import (
"testing" "testing"
"git.tetele.net/tgo/crypter"
) )
func Test_BindThird(t *testing.T) { 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)
} }

+ 56
- 0
third_v2.client.go View File

@ -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
}

+ 13
- 0
third_v2.client_test.go View File

@ -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)
}

+ 54
- 0
user.client.go View File

@ -1,6 +1,12 @@
package userrpc package userrpc
import ( import (
"encoding/json"
"strconv"
"time"
"git.tetele.net/tgo/crypter"
"github.com/golang/protobuf/proto" "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 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)
}

+ 21
- 21
user.client_test.go View File

@ -5,31 +5,31 @@ import (
) )
func Test_GetUserByToken(t *testing.T) { 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)
t.Log(res2.GetUserId())
// t.Log(res2.GetUserId())
t.Log(err) 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)
}
// }

+ 42
- 0
user.pb.go View File

@ -11,6 +11,7 @@ It is generated from these files:
It has these top-level messages: It has these top-level messages:
UserRequest UserRequest
UserInfoByUsername UserInfoByUsername
UserInfoById
UserInfoByUsercode UserInfoByUsercode
UserResponse UserResponse
LoginRequest LoginRequest
@ -84,6 +85,31 @@ func (m *UserInfoByUsername) GetUsername() string {
return "" 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查询用户信息请求结构 // 使用usercode查询用户信息请求结构
type UserInfoByUsercode struct { type UserInfoByUsercode struct {
Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"` Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"`
@ -452,7 +478,11 @@ type UserService interface {
GetByUsercode(in *UserInfoByUsercode, out *UserResponse) error GetByUsercode(in *UserInfoByUsercode, out *UserResponse) error
BindThird(in *Request, out *Response) error BindThird(in *Request, out *Response) error
GetThird(in *Request, out *Response) error GetThird(in *Request, out *Response) error
GetUserThird(in *Request, out *Response) error
GetBusiness(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 // 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 { func (c *UserServiceClient) GetThird(in *Request, out *Response) error {
return c.Call("UserService.GetThird", in, out) 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 { func (c *UserServiceClient) GetBusiness(in *Request, out *Response) error {
return c.Call("UserService.GetBusiness", in, out) 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. // DialUserService connects to an UserService at the specified network address.
func DialUserService(network, addr string) (*UserServiceClient, *rpc.Client, error) { func DialUserService(network, addr string) (*UserServiceClient, *rpc.Client, error) {


+ 9
- 0
user.proto View File

@ -11,6 +11,11 @@ message UserInfoByUsername {
string dbname = 1; string dbname = 1;
string username = 2; string username = 2;
} }
// 使userid查询用户信息请求结构
message UserInfoById {
string dbname = 1;
string userid = 2;
}
// 使usercode查询用户信息请求结构 // 使usercode查询用户信息请求结构
message UserInfoByUsercode { message UserInfoByUsercode {
@ -87,7 +92,11 @@ service UserService {
rpc getByUsercode (UserInfoByUsercode) returns (UserResponse); // 使usercode查询用户 rpc getByUsercode (UserInfoByUsercode) returns (UserResponse); // 使usercode查询用户
rpc bindThird (Request) returns (Response); // rpc bindThird (Request) returns (Response); //
rpc getThird(Request) returns (Response); // rpc getThird(Request) returns (Response); //
rpc getUserThird(Request) returns (Response); // v2
rpc getBusiness(Request) returns (Response); // 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 getUserScore(Request) returns (Response); //
rpc getUserRedEnvelope(Request) returns (Response); // rpc getUserRedEnvelope(Request) returns (Response); //
} }

Loading…
Cancel
Save