diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b97bcb2 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module git.tetele.net/tgo/userrpc + +go 1.14 + +require ( + github.com/chai2010/protorpc v1.0.0 // indirect + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect +) + diff --git a/user.client.go b/user.client.go new file mode 100644 index 0000000..d65574d --- /dev/null +++ b/user.client.go @@ -0,0 +1,151 @@ +package user + +import ( + "github.com/golang/protobuf/proto" +) + +func GetUserByToken(dbname, token string, url ...string) (map[string]string, error) { + + var user_rpc_url string = "127.0.0.1:7976" + if len(url) > 0 && url[0] != "" { + user_rpc_url = url[0] + } + conn, _, err := DialUserService("tcp", user_rpc_url) + if err != nil { + return map[string]string{}, err + } + defer conn.Close() + + req := &UserRequest{proto.String(dbname), proto.String(token), nil} + + res := &UserResponse{} + + err = conn.GetByToken(req, res) + + if err != nil { + return map[string]string{}, err + } + + if res.GetUserId() != "" { + return map[string]string{ + "UserId": res.GetUserId(), + "Username": res.GetUsername(), + "Nickname": res.GetNickname(), + "Mobile": res.GetMobile(), + "Email": res.GetEmail(), + "Status": res.GetStatus(), + "BusinessId": res.GetBusinessId(), + "StoreId": res.GetStoreId(), + "FansTo": res.GetFansTo(), + "IsVip": res.GetIsVip(), + "Usercode": res.GetUsercode(), + "GroupId": res.GetGroupId(), + "Type": res.GetType(), + }, nil + } + + return map[string]string{}, nil +} + +/** + * 使用用户名查询 + */ +func GetUserByUsername(dbname, username string, url ...string) (*UserResponse, error) { + + var user_rpc_url string = "127.0.0.1:7976" + if len(url) > 0 && url[0] != "" { + user_rpc_url = url[0] + } + + conn, _, err := DialUserService("tcp", user_rpc_url) + if err != nil { + return nil, err + } + defer conn.Close() + + req := &UserInfoByUsername{proto.String(dbname), proto.String(username), nil} + + res := &UserResponse{} + + err = conn.GetByUsername(req, res) + + if err != nil { + return nil, err + } + + return res, nil +} + +/** + * 使用编号查询 + */ +func GetUserByUsercode(dbname, usercode string, url ...string) (*UserResponse, error) { + + var user_rpc_url string = "127.0.0.1:7976" + if len(url) > 0 && url[0] != "" { + user_rpc_url = url[0] + } + + conn, _, err := DialUserService("tcp", user_rpc_url) + if err != nil { + return nil, err + } + defer conn.Close() + + req := &UserInfoByUsercode{proto.String(dbname), proto.String(usercode), nil} + + res := &UserResponse{} + + err = conn.GetByUsercode(req, res) + + if err != nil { + return nil, err + } + + return res, nil +} + +func Login(dbname, account, password string, url ...string) (map[string]string, error) { + + var user_rpc_url string = "127.0.0.1:7976" + if len(url) > 0 && url[0] != "" { + user_rpc_url = url[0] + } + + conn, _, err := DialUserService("tcp", user_rpc_url) + if err != nil { + return map[string]string{}, err + } + defer conn.Close() + + req := &LoginRequest{proto.String(dbname), proto.String(account), proto.String(password), nil} + + res := &LoginResponse{} + + err = conn.Login(req, res) + + if err != nil { + return map[string]string{}, err + } + + if res.GetUserId() != "" { + return map[string]string{ + "UserId": res.GetUserId(), + "Username": res.GetUsername(), + "Nickname": res.GetNickname(), + "Mobile": res.GetMobile(), + "Email": res.GetEmail(), + "Status": res.GetStatus(), + "BusinessId": res.GetBusinessId(), + "StoreId": res.GetStoreId(), + "FansTo": res.GetFansTo(), + "IsVip": res.GetIsVip(), + "Token": res.GetToken(), + "Usercode": res.GetUsercode(), + "GroupId": res.GetGroupId(), + "Type": res.GetType(), + }, nil + } + + return map[string]string{}, nil +} diff --git a/user.client_test.go b/user.client_test.go new file mode 100644 index 0000000..7a78fa8 --- /dev/null +++ b/user.client_test.go @@ -0,0 +1,35 @@ +package user + +import ( + "testing" +) + +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) + + res, err = GetUserByUsername(dbname, "tetele") + t.Log(res) + t.Log(res["UserId"]) + t.Log(err) + + res, err = GetUserByUsercode(dbname, "2233") + 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 new file mode 100644 index 0000000..3dd7cff --- /dev/null +++ b/user.pb.go @@ -0,0 +1,478 @@ +// Code generated by protoc-gen-go. +// source: user.proto +// DO NOT EDIT! + +/* +Package user is a generated protocol buffer package. + +It is generated from these files: + user.proto + +It has these top-level messages: + UserRequest + UserInfoByUsername + UserInfoByUsercode + UserResponse + LoginRequest + LoginResponse +*/ +package user + +import proto "github.com/chai2010/protorpc/proto" +import math "math" + +import "io" +import "log" +import "net" +import "net/rpc" +import "time" +import protorpc "github.com/chai2010/protorpc" + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = math.Inf + +// 使用token查询用户信息请求结构 +type UserRequest struct { + Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"` + Token *string `protobuf:"bytes,2,opt,name=token" json:"token,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *UserRequest) Reset() { *m = UserRequest{} } +func (m *UserRequest) String() string { return proto.CompactTextString(m) } +func (*UserRequest) ProtoMessage() {} + +func (m *UserRequest) GetDbname() string { + if m != nil && m.Dbname != nil { + return *m.Dbname + } + return "" +} + +func (m *UserRequest) GetToken() string { + if m != nil && m.Token != nil { + return *m.Token + } + return "" +} + +// 使用username查询用户信息请求结构 +type UserInfoByUsername struct { + Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"` + Username *string `protobuf:"bytes,2,opt,name=username" json:"username,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *UserInfoByUsername) Reset() { *m = UserInfoByUsername{} } +func (m *UserInfoByUsername) String() string { return proto.CompactTextString(m) } +func (*UserInfoByUsername) ProtoMessage() {} + +func (m *UserInfoByUsername) GetDbname() string { + if m != nil && m.Dbname != nil { + return *m.Dbname + } + return "" +} + +func (m *UserInfoByUsername) GetUsername() string { + if m != nil && m.Username != nil { + return *m.Username + } + return "" +} + +// 使用usercode查询用户信息请求结构 +type UserInfoByUsercode struct { + Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"` + Usercode *string `protobuf:"bytes,2,opt,name=usercode" json:"usercode,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *UserInfoByUsercode) Reset() { *m = UserInfoByUsercode{} } +func (m *UserInfoByUsercode) String() string { return proto.CompactTextString(m) } +func (*UserInfoByUsercode) ProtoMessage() {} + +func (m *UserInfoByUsercode) GetDbname() string { + if m != nil && m.Dbname != nil { + return *m.Dbname + } + return "" +} + +func (m *UserInfoByUsercode) GetUsercode() string { + if m != nil && m.Usercode != nil { + return *m.Usercode + } + return "" +} + +// 使用token查询用户信息响应结构 +type UserResponse struct { + UserId *string `protobuf:"bytes,1,opt,name=user_id" json:"user_id,omitempty"` + Username *string `protobuf:"bytes,2,opt,name=username" json:"username,omitempty"` + Nickname *string `protobuf:"bytes,3,opt,name=nickname" json:"nickname,omitempty"` + Mobile *string `protobuf:"bytes,4,opt,name=mobile" json:"mobile,omitempty"` + Email *string `protobuf:"bytes,5,opt,name=email" json:"email,omitempty"` + Status *string `protobuf:"bytes,6,opt,name=status" json:"status,omitempty"` + BusinessId *string `protobuf:"bytes,7,opt,name=business_id" json:"business_id,omitempty"` + StoreId *string `protobuf:"bytes,8,opt,name=store_id" json:"store_id,omitempty"` + FansTo *string `protobuf:"bytes,9,opt,name=fans_to" json:"fans_to,omitempty"` + IsVip *string `protobuf:"bytes,10,opt,name=is_vip" json:"is_vip,omitempty"` + Usercode *string `protobuf:"bytes,11,opt,name=usercode" json:"usercode,omitempty"` + GroupId *string `protobuf:"bytes,12,opt,name=group_id" json:"group_id,omitempty"` + Type *string `protobuf:"bytes,13,opt,name=type" json:"type,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *UserResponse) Reset() { *m = UserResponse{} } +func (m *UserResponse) String() string { return proto.CompactTextString(m) } +func (*UserResponse) ProtoMessage() {} + +func (m *UserResponse) GetUserId() string { + if m != nil && m.UserId != nil { + return *m.UserId + } + return "" +} + +func (m *UserResponse) GetUsername() string { + if m != nil && m.Username != nil { + return *m.Username + } + return "" +} + +func (m *UserResponse) GetNickname() string { + if m != nil && m.Nickname != nil { + return *m.Nickname + } + return "" +} + +func (m *UserResponse) GetMobile() string { + if m != nil && m.Mobile != nil { + return *m.Mobile + } + return "" +} + +func (m *UserResponse) GetEmail() string { + if m != nil && m.Email != nil { + return *m.Email + } + return "" +} + +func (m *UserResponse) GetStatus() string { + if m != nil && m.Status != nil { + return *m.Status + } + return "" +} + +func (m *UserResponse) GetBusinessId() string { + if m != nil && m.BusinessId != nil { + return *m.BusinessId + } + return "" +} + +func (m *UserResponse) GetStoreId() string { + if m != nil && m.StoreId != nil { + return *m.StoreId + } + return "" +} + +func (m *UserResponse) GetFansTo() string { + if m != nil && m.FansTo != nil { + return *m.FansTo + } + return "" +} + +func (m *UserResponse) GetIsVip() string { + if m != nil && m.IsVip != nil { + return *m.IsVip + } + return "" +} + +func (m *UserResponse) GetUsercode() string { + if m != nil && m.Usercode != nil { + return *m.Usercode + } + return "" +} + +func (m *UserResponse) GetGroupId() string { + if m != nil && m.GroupId != nil { + return *m.GroupId + } + return "" +} + +func (m *UserResponse) GetType() string { + if m != nil && m.Type != nil { + return *m.Type + } + return "" +} + +// 用户登录请求结构 +type LoginRequest struct { + Dbname *string `protobuf:"bytes,1,opt,name=dbname" json:"dbname,omitempty"` + Account *string `protobuf:"bytes,2,opt,name=account" json:"account,omitempty"` + Password *string `protobuf:"bytes,3,opt,name=password" json:"password,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *LoginRequest) Reset() { *m = LoginRequest{} } +func (m *LoginRequest) String() string { return proto.CompactTextString(m) } +func (*LoginRequest) ProtoMessage() {} + +func (m *LoginRequest) GetDbname() string { + if m != nil && m.Dbname != nil { + return *m.Dbname + } + return "" +} + +func (m *LoginRequest) GetAccount() string { + if m != nil && m.Account != nil { + return *m.Account + } + return "" +} + +func (m *LoginRequest) GetPassword() string { + if m != nil && m.Password != nil { + return *m.Password + } + return "" +} + +// 使用token查询用户信息响应结构 +type LoginResponse struct { + UserId *string `protobuf:"bytes,1,opt,name=user_id" json:"user_id,omitempty"` + Username *string `protobuf:"bytes,2,opt,name=username" json:"username,omitempty"` + Nickname *string `protobuf:"bytes,3,opt,name=nickname" json:"nickname,omitempty"` + Mobile *string `protobuf:"bytes,4,opt,name=mobile" json:"mobile,omitempty"` + Email *string `protobuf:"bytes,5,opt,name=email" json:"email,omitempty"` + Status *string `protobuf:"bytes,6,opt,name=status" json:"status,omitempty"` + BusinessId *string `protobuf:"bytes,7,opt,name=business_id" json:"business_id,omitempty"` + StoreId *string `protobuf:"bytes,8,opt,name=store_id" json:"store_id,omitempty"` + FansTo *string `protobuf:"bytes,9,opt,name=fans_to" json:"fans_to,omitempty"` + IsVip *string `protobuf:"bytes,10,opt,name=is_vip" json:"is_vip,omitempty"` + Token *string `protobuf:"bytes,11,opt,name=token" json:"token,omitempty"` + Usercode *string `protobuf:"bytes,12,opt,name=usercode" json:"usercode,omitempty"` + GroupId *string `protobuf:"bytes,13,opt,name=group_id" json:"group_id,omitempty"` + Type *string `protobuf:"bytes,14,opt,name=type" json:"type,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *LoginResponse) Reset() { *m = LoginResponse{} } +func (m *LoginResponse) String() string { return proto.CompactTextString(m) } +func (*LoginResponse) ProtoMessage() {} + +func (m *LoginResponse) GetUserId() string { + if m != nil && m.UserId != nil { + return *m.UserId + } + return "" +} + +func (m *LoginResponse) GetUsername() string { + if m != nil && m.Username != nil { + return *m.Username + } + return "" +} + +func (m *LoginResponse) GetNickname() string { + if m != nil && m.Nickname != nil { + return *m.Nickname + } + return "" +} + +func (m *LoginResponse) GetMobile() string { + if m != nil && m.Mobile != nil { + return *m.Mobile + } + return "" +} + +func (m *LoginResponse) GetEmail() string { + if m != nil && m.Email != nil { + return *m.Email + } + return "" +} + +func (m *LoginResponse) GetStatus() string { + if m != nil && m.Status != nil { + return *m.Status + } + return "" +} + +func (m *LoginResponse) GetBusinessId() string { + if m != nil && m.BusinessId != nil { + return *m.BusinessId + } + return "" +} + +func (m *LoginResponse) GetStoreId() string { + if m != nil && m.StoreId != nil { + return *m.StoreId + } + return "" +} + +func (m *LoginResponse) GetFansTo() string { + if m != nil && m.FansTo != nil { + return *m.FansTo + } + return "" +} + +func (m *LoginResponse) GetIsVip() string { + if m != nil && m.IsVip != nil { + return *m.IsVip + } + return "" +} + +func (m *LoginResponse) GetToken() string { + if m != nil && m.Token != nil { + return *m.Token + } + return "" +} + +func (m *LoginResponse) GetUsercode() string { + if m != nil && m.Usercode != nil { + return *m.Usercode + } + return "" +} + +func (m *LoginResponse) GetGroupId() string { + if m != nil && m.GroupId != nil { + return *m.GroupId + } + return "" +} + +func (m *LoginResponse) GetType() string { + if m != nil && m.Type != nil { + return *m.Type + } + return "" +} + +func init() { +} + +type UserService interface { + GetByToken(in *UserRequest, out *UserResponse) error + Login(in *LoginRequest, out *LoginResponse) error +} + +// AcceptUserServiceClient accepts connections on the listener and serves requests +// for each incoming connection. Accept blocks; the caller typically +// invokes it in a go statement. +func AcceptUserServiceClient(lis net.Listener, x UserService) { + srv := rpc.NewServer() + if err := srv.RegisterName("UserService", x); err != nil { + log.Fatal(err) + } + + for { + conn, err := lis.Accept() + if err != nil { + log.Fatalf("lis.Accept(): %v\n", err) + } + go srv.ServeCodec(protorpc.NewServerCodec(conn)) + } +} + +// RegisterUserService publish the given UserService implementation on the server. +func RegisterUserService(srv *rpc.Server, x UserService) error { + if err := srv.RegisterName("UserService", x); err != nil { + return err + } + return nil +} + +// NewUserServiceServer returns a new UserService Server. +func NewUserServiceServer(x UserService) *rpc.Server { + srv := rpc.NewServer() + if err := srv.RegisterName("UserService", x); err != nil { + log.Fatal(err) + } + return srv +} + +// ListenAndServeUserService listen announces on the local network address laddr +// and serves the given UserService implementation. +func ListenAndServeUserService(network, addr string, x UserService) error { + lis, err := net.Listen(network, addr) + if err != nil { + return err + } + defer lis.Close() + + srv := rpc.NewServer() + if err := srv.RegisterName("UserService", x); err != nil { + return err + } + + for { + conn, err := lis.Accept() + if err != nil { + log.Fatalf("lis.Accept(): %v\n", err) + } + go srv.ServeCodec(protorpc.NewServerCodec(conn)) + } +} + +type UserServiceClient struct { + *rpc.Client +} + +// NewUserServiceClient returns a UserService rpc.Client and stub to handle +// requests to the set of UserService at the other end of the connection. +func NewUserServiceClient(conn io.ReadWriteCloser) (*UserServiceClient, *rpc.Client) { + c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn)) + return &UserServiceClient{c}, c +} + +func (c *UserServiceClient) GetByToken(in *UserRequest, out *UserResponse) error { + return c.Call("UserService.GetByToken", in, out) +} +func (c *UserServiceClient) Login(in *LoginRequest, out *LoginResponse) error { + return c.Call("UserService.Login", in, out) +} + +// DialUserService connects to an UserService at the specified network address. +func DialUserService(network, addr string) (*UserServiceClient, *rpc.Client, error) { + c, err := protorpc.Dial(network, addr) + if err != nil { + return nil, nil, err + } + return &UserServiceClient{c}, c, nil +} + +// DialUserServiceTimeout connects to an UserService at the specified network address. +func DialUserServiceTimeout(network, addr string, + timeout time.Duration) (*UserServiceClient, *rpc.Client, error) { + c, err := protorpc.DialTimeout(network, addr, timeout) + if err != nil { + return nil, nil, err + } + return &UserServiceClient{c}, c, nil +} diff --git a/user.proto b/user.proto new file mode 100644 index 0000000..f4ecc45 --- /dev/null +++ b/user.proto @@ -0,0 +1,68 @@ +syntax = "proto3"; +package user; + +// 使用token查询用户信息请求结构 +message UserRequest { + string dbname = 1; + string token = 2; +} +// 使用username查询用户信息请求结构 +message UserInfoByUsername { + string dbname = 1; + string username = 2; +} + +// 使用usercode查询用户信息请求结构 +message UserInfoByUsercode { + string dbname = 1; + string usercode = 2; +} +// 使用token查询用户信息响应结构 +message UserResponse { + string user_id = 1; //ID + string username = 2; //用户名 + string nickname = 3; //昵称 + string mobile = 4; //手机 + string email = 5; //邮箱 + string status = 6; //状态 + string business_id = 7; //所属商家 + string store_id = 8; //门店 + string fans_to = 9;//被谁锁粉 + string is_vip = 10;//是否VIP + string usercode = 11;//工号 + string group_id = 12;//分组ID + string type = 13;//类型 +} + +// 用户登录请求结构 +message LoginRequest { + string dbname = 1; + string account = 2; + string password = 3; +} + + +// 使用token查询用户信息响应结构 +message LoginResponse { + string user_id = 1; //ID + string username = 2; //用户名 + string nickname = 3; //昵称 + string mobile = 4; //手机 + string email = 5; //邮箱 + string status = 6; //状态 + string business_id = 7; //所属商家 + string store_id = 8; //门店 + string fans_to = 9;//被谁锁粉 + string is_vip = 10;//是否VIP + string token = 11;//token + string usercode = 12;//工号 + string group_id = 13;//分组ID + string type = 14;//类型 +} + + +// rpc方法 +service UserService { + rpc getByToken (UserRequest) returns (UserResponse); // 使用token查询用户 + rpc login (LoginRequest) returns (LoginResponse); // 登录 +} \ No newline at end of file