Browse Source

signIn example

zrufo 4 years ago
parent
commit
b6f3ff11d7

+ 1 - 0
README.md

@@ -0,0 +1 @@
+grpcui -port 20001 -plaintext localhost:19090

+ 31 - 2
api/grpc/base.go

@@ -1,7 +1,36 @@
 package grpc
 
-import pb "sportfitness/base/api/grpc/base"
+import (
+	"context"
+	"google.golang.org/grpc/metadata"
+	pb "sportfitness/base/api/grpc/base"
+	"sportfitness/base/assembly/base/service"
+	"strings"
+)
 
-type ApiBase struct {
+type Api struct {
 	pb.UnimplementedApiServer
 }
+
+func (Api) getRemoteIp(ctx context.Context) string {
+	if md, ok := metadata.FromIncomingContext(ctx); ok {
+		ip := md.Get("x-forwarded-for")
+		if len(ip) > 0 {
+			return ip[0]
+		}
+	}
+	return ""
+}
+func (Api) getUserAgent(ctx context.Context) string {
+	if md, ok := metadata.FromIncomingContext(ctx); ok {
+		agents := md.Get("user-agent")
+		return strings.Join(agents, "\n")
+	}
+	return ""
+}
+func (a Api) SignIn(ctx context.Context, q *pb.SignInRequest) (*pb.SignInReply, error) {
+	token := service.User{}.SignInUserCodePassword(
+		q.Name, q.Password, a.getRemoteIp(ctx), a.getUserAgent(ctx))
+
+	return &pb.SignInReply{Token: token}, nil
+}

+ 2 - 1
assembly/base/repository/postgre/init.go

@@ -4,6 +4,7 @@ import (
 	"embed"
 	lib "git.beswell.com/gframe/application/repository"
 	"sportfitness/base/assembly/base/repository"
+	"sportfitness/base/global"
 )
 
 //go:embed sql
@@ -23,7 +24,7 @@ func Init() {
 	// todo,需要从数据库取出sys_token用作系统验证,存全局变量,每次调用总后台接口需要传递sys_token
 
 	getClient().Raw("select sys_token from s_sys_config").Scan(&SysToken)
-	println(SysToken)
+	global.SysToken = SysToken
 }
 func getClient() *lib.DB {
 	return repository.Repository.GetGormClient()

+ 4 - 0
assembly/base/service/base.go

@@ -0,0 +1,4 @@
+package service
+
+type base struct {
+}

+ 0 - 13
assembly/base/service/shop.go

@@ -1,13 +0,0 @@
-/**
- * @ File:
- * @ Date: 2021/3/25 14:15
- * @ Author: JYQ
- * @ Description:
- */
-package service
-
-import "sportfitness/base/repository/grpc/bsw/im"
-
-func SingUp() {
-	im.SignUp()
-}

+ 16 - 0
assembly/base/service/user.go

@@ -0,0 +1,16 @@
+package service
+
+import "sportfitness/base/repository/grpc/bsw/im"
+
+type User struct {
+	base
+}
+
+func (User) SignInUserCodePassword(userCode, password, ip, ClientInfo string) (token string) {
+
+	token, userId := im.SignInUserCodePassword(userCode, password, ip, ClientInfo)
+
+	println(token, userId)
+
+	return token
+}

+ 6 - 0
global/vars.go

@@ -0,0 +1,6 @@
+package global
+
+var (
+	SysToken       string
+	TokenExpireSec = int64(60 * 60 * 24 * 14)
+)

+ 11 - 9
go.mod

@@ -3,26 +3,28 @@ module sportfitness/base
 go 1.16
 
 require (
-	git.beswell.com/gframe/application v0.3.9
-	github.com/ZR233/glog/v2 v2.0.1
+	git.beswell.com/gframe/application v0.4.8
+	github.com/ZR233/glog/v2 v2.0.4
 	github.com/armon/go-metrics v0.3.6 // indirect
 	github.com/fatih/color v1.10.0 // indirect
 	github.com/go-redis/redis/v8 v8.8.0
-	github.com/golang/protobuf v1.5.1
+	github.com/go-sql-driver/mysql v1.6.0 // indirect
+	github.com/golang/protobuf v1.5.2
 	github.com/golang/snappy v0.0.3 // indirect
 	github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
-	github.com/hashicorp/go-hclog v0.15.0 // indirect
+	github.com/hashicorp/go-hclog v0.16.0 // indirect
 	github.com/hashicorp/go-immutable-radix v1.3.0 // indirect
 	github.com/hashicorp/golang-lru v0.5.4 // indirect
 	github.com/jackc/pgproto3/v2 v2.0.7 // indirect
 	github.com/jinzhu/now v1.1.2 // indirect
-	github.com/klauspost/compress v1.11.12 // indirect
+	github.com/klauspost/compress v1.11.13 // indirect
 	github.com/mitchellh/mapstructure v1.4.1 // indirect
 	github.com/sirupsen/logrus v1.8.1
 	golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
-	golang.org/x/net v0.0.0-20210324051636-2c4c8ecb7826 // indirect
-	golang.org/x/sys v0.0.0-20210324051608-47abb6519492 // indirect
-	google.golang.org/genproto v0.0.0-20210323160006-e668133fea6a // indirect
-	google.golang.org/grpc v1.36.0
+	golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
+	golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
+	golang.org/x/text v0.3.6 // indirect
+	google.golang.org/genproto v0.0.0-20210406143921-e86de6bf7a46 // indirect
+	google.golang.org/grpc v1.36.1
 	google.golang.org/protobuf v1.26.0
 )

+ 1 - 1
main.go

@@ -35,7 +35,7 @@ func main() {
 	if err != nil {
 		logrus.Fatalf("failed to listen: %s", err)
 	}
-	pb.RegisterApiServer(grpcServer, &grpc.ApiBase{})
+	pb.RegisterApiServer(grpcServer, &grpc.Api{})
 
 	logrus.Infof("listen :%d", 19090)
 	err = grpcServer.Serve(lis)

+ 6 - 3
proto/client/bsw/im/im.proto

@@ -17,8 +17,8 @@ service Api {
   rpc SignUpUserCode (SignUpRequest) returns (CreateReply) {}
   rpc SignUpPhone (SignUpRequest) returns (CreateReply) {}
 
-  rpc SignInUserCode (SignInPasswordRequest) returns (TokenParam)  {}
-  rpc SignInWithPhonePassword (SignInPasswordRequest) returns (TokenParam)  {}
+  rpc SignInUserCode (SignInPasswordRequest) returns (SignInReply)  {}
+  rpc SignInWithPhonePassword (SignInPasswordRequest) returns (SignInReply)  {}
 
   // ---子系统专用---
 
@@ -98,7 +98,10 @@ message DefaultRequest{
 message TokenParam{
   string token = 1;
 }
-
+message SignInReply{
+  string token = 1;
+  int64 userId = 2;
+}
 message DefaultReply{}
 
 message CreateReply{

+ 30 - 14
repository/grpc/bsw/im/api.go

@@ -3,6 +3,8 @@ package im
 import (
 	"context"
 	"git.beswell.com/gframe/application"
+	"google.golang.org/grpc/metadata"
+	"sportfitness/base/global"
 	pb "sportfitness/base/repository/grpc/bsw/im/im"
 )
 
@@ -11,24 +13,38 @@ func getClient() pb.ApiClient {
 	if err != nil {
 		panic(err)
 	}
-	return pb.NewApiClient(conn)
+	client := pb.NewApiClient(conn)
+
+	return client
 }
 
-func SignUp() {
-	_, err := getClient().SignUpUserCode(context.Background(), &pb.SignUpRequest{
-		UserCode: "",
-		Name:     "",
-		Email:    "",
-		Phone:    "",
-		WxOpenId: "",
-		Qq:       "",
-		Password: "",
-		Question: "",
-		Answer:   "",
-		Memo:     "",
-	})
+func ctx() context.Context {
+	md := metadata.Pairs("sys_token", global.SysToken)
+	return metadata.NewOutgoingContext(context.Background(), md)
+}
 
+func SignUpUserCode(params *pb.SignUpRequest) (userId int64) {
+	r, err := getClient().SignUpUserCode(ctx(), params)
+
+	if err != nil {
+		panic(err)
+	}
+	userId = r.GetId()
+	return
+}
+func SignInUserCodePassword(userCode, password, ip, ClientInfo string) (token string, userId int) {
+	r, err := getClient().SignInUserCode(ctx(), &pb.SignInPasswordRequest{
+		Credential:    userCode,
+		Password:      password,
+		ExpirationSec: global.TokenExpireSec,
+		Ip:            ip,
+		ClientInfo:    ClientInfo,
+	})
 	if err != nil {
 		panic(err)
 	}
+	token = r.Token
+	userId = int(r.UserId)
+
+	return
 }

+ 62 - 1
repository/grpc/bsw/im/api_test.go

@@ -3,6 +3,9 @@ package im
 import (
 	"git.beswell.com/gframe/application"
 	"github.com/sirupsen/logrus"
+	"google.golang.org/grpc"
+	"sportfitness/base/global"
+	pb "sportfitness/base/repository/grpc/bsw/im/im"
 	"testing"
 )
 
@@ -13,8 +16,66 @@ func init() {
 	if err != nil {
 		panic(err)
 	}
+	global.SysToken = "3374a2b5a483ed1263f3574666e1ab5e"
 }
 
 func TestSignUp(t *testing.T) {
-	SignUp()
+	id := SignUpUserCode(&pb.SignUpRequest{
+		UserCode: "test123",
+		Name:     "测试123",
+		Email:    "",
+		Phone:    "",
+		WxOpenId: "",
+		Qq:       "",
+		Password: "123456",
+		Question: "",
+		Answer:   "",
+		Memo:     "",
+	})
+
+	println(id)
+
+	_, err := getClient().SystemAddUser(ctx(), &pb.SystemAddUserRequest{
+		UserId: id,
+		Memo:   "测试",
+	})
+
+	if err != nil {
+		panic(err)
+	}
+
+}
+
+func TestMD(t *testing.T) {
+	conn, err := grpc.Dial("localhost:19090", grpc.WithInsecure())
+	if err != nil {
+		panic(err)
+	}
+	client := pb.NewApiClient(conn)
+
+	_, err = client.SignInUserCode(ctx(), &pb.SignInPasswordRequest{
+		Credential:    "",
+		Password:      "",
+		SysId:         0,
+		ExpirationSec: 0,
+		Ip:            "123123",
+		ClientInfo:    "",
+	})
+
+	_, err = client.SystemAddUser(ctx(), &pb.SystemAddUserRequest{
+		UserId: 167676470552965120,
+		Memo:   "测试",
+	})
+
+	if err != nil {
+		panic(err)
+	}
+
+}
+
+func TestSignInUserCodePassword(t *testing.T) {
+	token, userId := SignInUserCodePassword(
+		"test123", "123456", "192.168.0.100", "web")
+
+	println(token, userId)
 }

File diff suppressed because it is too large
+ 160 - 105
repository/grpc/bsw/im/im/im.pb.go


+ 10 - 10
repository/grpc/bsw/im/im/im_grpc.pb.go

@@ -19,8 +19,8 @@ const _ = grpc.SupportPackageIsVersion7
 type ApiClient interface {
 	SignUpUserCode(ctx context.Context, in *SignUpRequest, opts ...grpc.CallOption) (*CreateReply, error)
 	SignUpPhone(ctx context.Context, in *SignUpRequest, opts ...grpc.CallOption) (*CreateReply, error)
-	SignInUserCode(ctx context.Context, in *SignInPasswordRequest, opts ...grpc.CallOption) (*TokenParam, error)
-	SignInWithPhonePassword(ctx context.Context, in *SignInPasswordRequest, opts ...grpc.CallOption) (*TokenParam, error)
+	SignInUserCode(ctx context.Context, in *SignInPasswordRequest, opts ...grpc.CallOption) (*SignInReply, error)
+	SignInWithPhonePassword(ctx context.Context, in *SignInPasswordRequest, opts ...grpc.CallOption) (*SignInReply, error)
 	// 检查token有效性
 	SubSessionCheck(ctx context.Context, in *TokenParam, opts ...grpc.CallOption) (*SessionCheckReply, error)
 	// 检查token所属用户是否拥有调用service权限
@@ -103,8 +103,8 @@ func (c *apiClient) SignUpPhone(ctx context.Context, in *SignUpRequest, opts ...
 	return out, nil
 }
 
-func (c *apiClient) SignInUserCode(ctx context.Context, in *SignInPasswordRequest, opts ...grpc.CallOption) (*TokenParam, error) {
-	out := new(TokenParam)
+func (c *apiClient) SignInUserCode(ctx context.Context, in *SignInPasswordRequest, opts ...grpc.CallOption) (*SignInReply, error) {
+	out := new(SignInReply)
 	err := c.cc.Invoke(ctx, "/im.Api/SignInUserCode", in, out, opts...)
 	if err != nil {
 		return nil, err
@@ -112,8 +112,8 @@ func (c *apiClient) SignInUserCode(ctx context.Context, in *SignInPasswordReques
 	return out, nil
 }
 
-func (c *apiClient) SignInWithPhonePassword(ctx context.Context, in *SignInPasswordRequest, opts ...grpc.CallOption) (*TokenParam, error) {
-	out := new(TokenParam)
+func (c *apiClient) SignInWithPhonePassword(ctx context.Context, in *SignInPasswordRequest, opts ...grpc.CallOption) (*SignInReply, error) {
+	out := new(SignInReply)
 	err := c.cc.Invoke(ctx, "/im.Api/SignInWithPhonePassword", in, out, opts...)
 	if err != nil {
 		return nil, err
@@ -411,8 +411,8 @@ func (x *apiIMMessageRcvClient) Recv() (*Message, error) {
 type ApiServer interface {
 	SignUpUserCode(context.Context, *SignUpRequest) (*CreateReply, error)
 	SignUpPhone(context.Context, *SignUpRequest) (*CreateReply, error)
-	SignInUserCode(context.Context, *SignInPasswordRequest) (*TokenParam, error)
-	SignInWithPhonePassword(context.Context, *SignInPasswordRequest) (*TokenParam, error)
+	SignInUserCode(context.Context, *SignInPasswordRequest) (*SignInReply, error)
+	SignInWithPhonePassword(context.Context, *SignInPasswordRequest) (*SignInReply, error)
 	// 检查token有效性
 	SubSessionCheck(context.Context, *TokenParam) (*SessionCheckReply, error)
 	// 检查token所属用户是否拥有调用service权限
@@ -480,10 +480,10 @@ func (UnimplementedApiServer) SignUpUserCode(context.Context, *SignUpRequest) (*
 func (UnimplementedApiServer) SignUpPhone(context.Context, *SignUpRequest) (*CreateReply, error) {
 	return nil, status.Errorf(codes.Unimplemented, "method SignUpPhone not implemented")
 }
-func (UnimplementedApiServer) SignInUserCode(context.Context, *SignInPasswordRequest) (*TokenParam, error) {
+func (UnimplementedApiServer) SignInUserCode(context.Context, *SignInPasswordRequest) (*SignInReply, error) {
 	return nil, status.Errorf(codes.Unimplemented, "method SignInUserCode not implemented")
 }
-func (UnimplementedApiServer) SignInWithPhonePassword(context.Context, *SignInPasswordRequest) (*TokenParam, error) {
+func (UnimplementedApiServer) SignInWithPhonePassword(context.Context, *SignInPasswordRequest) (*SignInReply, error) {
 	return nil, status.Errorf(codes.Unimplemented, "method SignInWithPhonePassword not implemented")
 }
 func (UnimplementedApiServer) SubSessionCheck(context.Context, *TokenParam) (*SessionCheckReply, error) {

Some files were not shown because too many files changed in this diff