From 4f6d9b1e7284831d8225ec207f0fa83e36b09b9d Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 12 十月 2023 14:28:42 +0800
Subject: [PATCH] crm开启登录认证,增加菜单权限认证
---
conf/aps-crm.json | 2
constvar/const.go | 4
service/user.go | 55 ------
proto/user.proto | 12 +
proto/user/user.pb.go | 194 ++++++++++++++++++++++--
middleware/user.go | 78 +++++++++
middleware/jwt.go | 13 +
model/client.go | 1
main.go | 6
proto/user/user_grpc.pb.go | 49 +++++
middleware/interceptor.go | 21 ++
11 files changed, 354 insertions(+), 81 deletions(-)
diff --git a/conf/aps-crm.json b/conf/aps-crm.json
index 1277fbd..d8f9bfe 100644
--- a/conf/aps-crm.json
+++ b/conf/aps-crm.json
@@ -49,7 +49,7 @@
},
"GrpcServiceAddr": {
"Aps": "192.168.20.120:9091",
- "Admin": "192.168.20.119:50051"
+ "Admin": "192.168.20.120:50051"
}
}
diff --git a/constvar/const.go b/constvar/const.go
index b70189e..c3a6587 100644
--- a/constvar/const.go
+++ b/constvar/const.go
@@ -132,3 +132,7 @@
ServiceFeeKeywordProductName ServiceFeeKeywordType = "浜у搧鍚嶇О"
ServiceFeeKeywordServiceEndDate ServiceFeeKeywordType = "鏈嶅姟鍒版湡鏃�"
)
+
+const (
+ SystemType = 2
+)
diff --git a/main.go b/main.go
index 4fc671d..7507480 100644
--- a/main.go
+++ b/main.go
@@ -4,10 +4,10 @@
v1 "aps_crm/api/v1"
"aps_crm/conf"
"aps_crm/initialize"
+ "aps_crm/middleware"
"aps_crm/model"
"aps_crm/pkg/logx"
"aps_crm/router"
- "aps_crm/service"
"fmt"
"net/http"
"os"
@@ -47,7 +47,7 @@
}
go v1.InitProductServiceConn()
- go service.InitUserConn()
+ go middleware.InitUserConn()
//c := cron.New()
//c.AddFunc("@every 15s", service.SyncUserInfo) // 姣�15绉掑悓姝ヤ竴娆�
@@ -62,7 +62,7 @@
<-quit
v1.CloseProductServiceConn()
- service.CloseUserConn()
+ middleware.CloseUserConn()
logx.Infof("aps-crm exited...")
os.Exit(0)
diff --git a/middleware/interceptor.go b/middleware/interceptor.go
new file mode 100644
index 0000000..ca3a2fb
--- /dev/null
+++ b/middleware/interceptor.go
@@ -0,0 +1,21 @@
+package middleware
+
+import (
+ "context"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/metadata"
+)
+
+// HeaderInterceptor 鎷︽埅鍣ㄥ嚱鏁�
+func HeaderInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
+ token := ctx.Value("token")
+ if token != nil {
+ // 鍒涘缓涓�涓嚜瀹氫箟鐨� metadata
+ md := metadata.Pairs("token", token.(string))
+ // 灏� metadata 娣诲姞鍒� context 涓�
+ ctx = metadata.NewOutgoingContext(ctx, md)
+ }
+ // 璋冪敤瀹為檯鐨� gRPC 鏂规硶
+ err := invoker(ctx, method, req, reply, cc, opts...)
+ return err
+}
diff --git a/middleware/jwt.go b/middleware/jwt.go
index 673087c..35509b7 100644
--- a/middleware/jwt.go
+++ b/middleware/jwt.go
@@ -82,10 +82,12 @@
func JWTAuth2() gin.HandlerFunc {
return func(c *gin.Context) {
+ ctx := new(contextx.Context).SetCtx(c)
// 鎴戜滑杩欓噷jwt閴存潈鍙栧ご閮ㄤ俊鎭� Authorization 鐧诲綍鏃跺洖杩斿洖token淇℃伅 杩欓噷鍓嶇闇�瑕佹妸token瀛樺偍鍒癱ookie鎴栬�呮湰鍦發ocalStorage涓� 涓嶈繃闇�瑕佽窡鍚庣鍗忓晢杩囨湡鏃堕棿 鍙互绾﹀畾鍒锋柊浠ょ墝鎴栬�呴噸鏂扮櫥褰�
token := c.Request.Header.Get("Authorization")
if token == "" {
- c.Next()
+ ctx.Fail(ecode.JWTEmpty)
+ c.Abort()
return
}
slices := strings.Split(token, " ")
@@ -105,6 +107,13 @@
}
c.Set("claims", claims)
- c.Next()
+
+ if CheckAuth(c.Request.RequestURI, token) {
+ c.Next()
+ } else {
+ ctx.Fail(ecode.JWTDisabled)
+ c.Abort()
+ return
+ }
}
}
diff --git a/middleware/user.go b/middleware/user.go
new file mode 100644
index 0000000..c6df3f9
--- /dev/null
+++ b/middleware/user.go
@@ -0,0 +1,78 @@
+package middleware
+
+import (
+ "aps_crm/conf"
+ "aps_crm/constvar"
+ "aps_crm/model"
+ "aps_crm/pkg/logx"
+ "aps_crm/proto/user"
+ "context"
+ "fmt"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/credentials/insecure"
+ "time"
+)
+
+var (
+ userConn *grpc.ClientConn
+)
+
+func InitUserConn() {
+ var err error
+ userConn, err = grpc.Dial(conf.Conf.GrpcServiceAddr.Admin, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithUnaryInterceptor(HeaderInterceptor))
+ if err != nil {
+ logx.Errorf("grpc dial user service error: %v", err.Error())
+ return
+ }
+}
+
+func CloseUserConn() {
+ if userConn != nil {
+ userConn.Close()
+ }
+}
+
+func CheckAuth(apiPath string, token string) bool {
+ cli := user.NewUserServiceClient(userConn)
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)
+ defer cancel()
+ ctx = context.WithValue(ctx, "token", token)
+ rsp, err := cli.UserMenuCheck(ctx, &user.CheckRequest{
+ ApiPath: apiPath,
+ SystemType: constvar.SystemType,
+ })
+ if err != nil {
+ logx.Errorf("check auth err: %v", err)
+ return false
+ }
+ return rsp.Result
+}
+
+func SyncUserInfo() {
+ cli := user.NewUserServiceClient(userConn)
+
+ var users []*user.User
+
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)
+ defer cancel()
+ r, err := cli.SyncUser(ctx, &user.UserRequest{Users: users})
+ if err != nil {
+ logx.Errorf("could not sync users: %v", err)
+ return
+ }
+
+ fmt.Printf("Synced: %v, Message: %s", r.List, r.Message)
+
+ for _, member := range r.List {
+ err = model.NewUserSearch(nil).FirstOrCreate(model.User{
+ UUID: member.Uuid,
+ Username: member.Username,
+ UserType: constvar.UserType(member.Usertype),
+ NickName: member.Nickname,
+ })
+ if err != nil {
+ logx.Errorf("sync user error: %v", err.Error())
+ continue
+ }
+ }
+}
diff --git a/model/client.go b/model/client.go
index f425230..a29ca36 100644
--- a/model/client.go
+++ b/model/client.go
@@ -113,6 +113,7 @@
}
case int:
+ case uint:
case int64:
case float64:
if key == "id" || key == "client_type_id" || key == "client_status_id" || key == "member_id" {
diff --git a/proto/user.proto b/proto/user.proto
index 42d1243..50e32f3 100644
--- a/proto/user.proto
+++ b/proto/user.proto
@@ -6,6 +6,7 @@
service UserService {
rpc SyncUser(UserRequest) returns (UserResponse);
+ rpc UserMenuCheck(CheckRequest) returns (CheckResponse);
}
message User {
@@ -13,7 +14,6 @@
string username = 2;
int32 usertype = 3;
string nickname = 4;
-
// ... other fields
}
@@ -26,5 +26,15 @@
string message = 2;
repeated User List = 3;
int64 total = 4;
+}
+message CheckRequest {
+ string apiPath = 1; //鎺ュ彛璺緞
+ int32 SystemType = 2; //绯荤粺鍚嶇О
+}
+
+message CheckResponse {
+ int32 code = 1;
+ string message = 2;
+ bool result = 3;
}
diff --git a/proto/user/user.pb.go b/proto/user/user.pb.go
index 91bbfbf..399d3aa 100644
--- a/proto/user/user.pb.go
+++ b/proto/user/user.pb.go
@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
-// protoc-gen-go v1.26.0
-// protoc v4.24.0
+// protoc-gen-go v1.31.0
+// protoc v3.19.0
// source: user.proto
package user
@@ -28,7 +28,7 @@
Uuid string `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"`
Username string `protobuf:"bytes,2,opt,name=username,proto3" json:"username,omitempty"`
Usertype int32 `protobuf:"varint,3,opt,name=usertype,proto3" json:"usertype,omitempty"`
- Nickname string `protobuf:"bytes,4,opt,name=nickname,proto3" json:"nickname,omitempty"`
+ Nickname string `protobuf:"bytes,4,opt,name=nickname,proto3" json:"nickname,omitempty"` // ... other fields
}
func (x *User) Reset() {
@@ -209,6 +209,124 @@
return 0
}
+type CheckRequest struct {
+ state protoimpl.MessageState
+ sizeCache protoimpl.SizeCache
+ unknownFields protoimpl.UnknownFields
+
+ ApiPath string `protobuf:"bytes,1,opt,name=apiPath,proto3" json:"apiPath,omitempty"` //鎺ュ彛璺緞
+ SystemType int32 `protobuf:"varint,2,opt,name=SystemType,proto3" json:"SystemType,omitempty"` //绯荤粺鍚嶇О
+}
+
+func (x *CheckRequest) Reset() {
+ *x = CheckRequest{}
+ if protoimpl.UnsafeEnabled {
+ mi := &file_user_proto_msgTypes[3]
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ ms.StoreMessageInfo(mi)
+ }
+}
+
+func (x *CheckRequest) String() string {
+ return protoimpl.X.MessageStringOf(x)
+}
+
+func (*CheckRequest) ProtoMessage() {}
+
+func (x *CheckRequest) ProtoReflect() protoreflect.Message {
+ mi := &file_user_proto_msgTypes[3]
+ if protoimpl.UnsafeEnabled && x != nil {
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ if ms.LoadMessageInfo() == nil {
+ ms.StoreMessageInfo(mi)
+ }
+ return ms
+ }
+ return mi.MessageOf(x)
+}
+
+// Deprecated: Use CheckRequest.ProtoReflect.Descriptor instead.
+func (*CheckRequest) Descriptor() ([]byte, []int) {
+ return file_user_proto_rawDescGZIP(), []int{3}
+}
+
+func (x *CheckRequest) GetApiPath() string {
+ if x != nil {
+ return x.ApiPath
+ }
+ return ""
+}
+
+func (x *CheckRequest) GetSystemType() int32 {
+ if x != nil {
+ return x.SystemType
+ }
+ return 0
+}
+
+type CheckResponse struct {
+ state protoimpl.MessageState
+ sizeCache protoimpl.SizeCache
+ unknownFields protoimpl.UnknownFields
+
+ Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
+ Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"`
+ Result bool `protobuf:"varint,3,opt,name=result,proto3" json:"result,omitempty"`
+}
+
+func (x *CheckResponse) Reset() {
+ *x = CheckResponse{}
+ if protoimpl.UnsafeEnabled {
+ mi := &file_user_proto_msgTypes[4]
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ ms.StoreMessageInfo(mi)
+ }
+}
+
+func (x *CheckResponse) String() string {
+ return protoimpl.X.MessageStringOf(x)
+}
+
+func (*CheckResponse) ProtoMessage() {}
+
+func (x *CheckResponse) ProtoReflect() protoreflect.Message {
+ mi := &file_user_proto_msgTypes[4]
+ if protoimpl.UnsafeEnabled && x != nil {
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ if ms.LoadMessageInfo() == nil {
+ ms.StoreMessageInfo(mi)
+ }
+ return ms
+ }
+ return mi.MessageOf(x)
+}
+
+// Deprecated: Use CheckResponse.ProtoReflect.Descriptor instead.
+func (*CheckResponse) Descriptor() ([]byte, []int) {
+ return file_user_proto_rawDescGZIP(), []int{4}
+}
+
+func (x *CheckResponse) GetCode() int32 {
+ if x != nil {
+ return x.Code
+ }
+ return 0
+}
+
+func (x *CheckResponse) GetMessage() string {
+ if x != nil {
+ return x.Message
+ }
+ return ""
+}
+
+func (x *CheckResponse) GetResult() bool {
+ if x != nil {
+ return x.Result
+ }
+ return false
+}
+
var File_user_proto protoreflect.FileDescriptor
var file_user_proto_rawDesc = []byte{
@@ -230,12 +348,26 @@
0x65, 0x12, 0x1e, 0x0a, 0x04, 0x4c, 0x69, 0x73, 0x74, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32,
0x0a, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52, 0x04, 0x4c, 0x69, 0x73,
0x74, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03,
- 0x52, 0x05, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x32, 0x40, 0x0a, 0x0b, 0x55, 0x73, 0x65, 0x72, 0x53,
- 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x31, 0x0a, 0x08, 0x53, 0x79, 0x6e, 0x63, 0x55, 0x73,
- 0x65, 0x72, 0x12, 0x11, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65,
- 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x55, 0x73, 0x65,
- 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x2f, 0x75,
- 0x73, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
+ 0x52, 0x05, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x22, 0x48, 0x0a, 0x0c, 0x43, 0x68, 0x65, 0x63, 0x6b,
+ 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x70, 0x69, 0x50, 0x61,
+ 0x74, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x70, 0x69, 0x50, 0x61, 0x74,
+ 0x68, 0x12, 0x1e, 0x0a, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x18,
+ 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x54, 0x79, 0x70,
+ 0x65, 0x22, 0x55, 0x0a, 0x0d, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
+ 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05,
+ 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67,
+ 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
+ 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08,
+ 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x32, 0x7a, 0x0a, 0x0b, 0x55, 0x73, 0x65, 0x72,
+ 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x31, 0x0a, 0x08, 0x53, 0x79, 0x6e, 0x63, 0x55,
+ 0x73, 0x65, 0x72, 0x12, 0x11, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x52,
+ 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x55, 0x73,
+ 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x38, 0x0a, 0x0d, 0x55, 0x73,
+ 0x65, 0x72, 0x4d, 0x65, 0x6e, 0x75, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x12, 0x12, 0x2e, 0x75, 0x73,
+ 0x65, 0x72, 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
+ 0x13, 0x2e, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x52, 0x65, 0x73, 0x70,
+ 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x62, 0x06,
+ 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@@ -250,19 +382,23 @@
return file_user_proto_rawDescData
}
-var file_user_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
+var file_user_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
var file_user_proto_goTypes = []interface{}{
- (*User)(nil), // 0: user.User
- (*UserRequest)(nil), // 1: user.UserRequest
- (*UserResponse)(nil), // 2: user.UserResponse
+ (*User)(nil), // 0: user.User
+ (*UserRequest)(nil), // 1: user.UserRequest
+ (*UserResponse)(nil), // 2: user.UserResponse
+ (*CheckRequest)(nil), // 3: user.CheckRequest
+ (*CheckResponse)(nil), // 4: user.CheckResponse
}
var file_user_proto_depIdxs = []int32{
0, // 0: user.UserRequest.users:type_name -> user.User
0, // 1: user.UserResponse.List:type_name -> user.User
1, // 2: user.UserService.SyncUser:input_type -> user.UserRequest
- 2, // 3: user.UserService.SyncUser:output_type -> user.UserResponse
- 3, // [3:4] is the sub-list for method output_type
- 2, // [2:3] is the sub-list for method input_type
+ 3, // 3: user.UserService.UserMenuCheck:input_type -> user.CheckRequest
+ 2, // 4: user.UserService.SyncUser:output_type -> user.UserResponse
+ 4, // 5: user.UserService.UserMenuCheck:output_type -> user.CheckResponse
+ 4, // [4:6] is the sub-list for method output_type
+ 2, // [2:4] is the sub-list for method input_type
2, // [2:2] is the sub-list for extension type_name
2, // [2:2] is the sub-list for extension extendee
0, // [0:2] is the sub-list for field type_name
@@ -310,6 +446,30 @@
return nil
}
}
+ file_user_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
+ switch v := v.(*CheckRequest); i {
+ case 0:
+ return &v.state
+ case 1:
+ return &v.sizeCache
+ case 2:
+ return &v.unknownFields
+ default:
+ return nil
+ }
+ }
+ file_user_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
+ switch v := v.(*CheckResponse); i {
+ case 0:
+ return &v.state
+ case 1:
+ return &v.sizeCache
+ case 2:
+ return &v.unknownFields
+ default:
+ return nil
+ }
+ }
}
type x struct{}
out := protoimpl.TypeBuilder{
@@ -317,7 +477,7 @@
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_user_proto_rawDesc,
NumEnums: 0,
- NumMessages: 3,
+ NumMessages: 5,
NumExtensions: 0,
NumServices: 1,
},
diff --git a/proto/user/user_grpc.pb.go b/proto/user/user_grpc.pb.go
index 2825eb1..789f1b7 100644
--- a/proto/user/user_grpc.pb.go
+++ b/proto/user/user_grpc.pb.go
@@ -1,4 +1,8 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
+// versions:
+// - protoc-gen-go-grpc v1.3.0
+// - protoc v3.19.0
+// source: user.proto
package user
@@ -14,11 +18,17 @@
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
+const (
+ UserService_SyncUser_FullMethodName = "/user.UserService/SyncUser"
+ UserService_UserMenuCheck_FullMethodName = "/user.UserService/UserMenuCheck"
+)
+
// UserServiceClient is the client API for UserService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type UserServiceClient interface {
SyncUser(ctx context.Context, in *UserRequest, opts ...grpc.CallOption) (*UserResponse, error)
+ UserMenuCheck(ctx context.Context, in *CheckRequest, opts ...grpc.CallOption) (*CheckResponse, error)
}
type userServiceClient struct {
@@ -31,7 +41,16 @@
func (c *userServiceClient) SyncUser(ctx context.Context, in *UserRequest, opts ...grpc.CallOption) (*UserResponse, error) {
out := new(UserResponse)
- err := c.cc.Invoke(ctx, "/user.UserService/SyncUser", in, out, opts...)
+ err := c.cc.Invoke(ctx, UserService_SyncUser_FullMethodName, in, out, opts...)
+ if err != nil {
+ return nil, err
+ }
+ return out, nil
+}
+
+func (c *userServiceClient) UserMenuCheck(ctx context.Context, in *CheckRequest, opts ...grpc.CallOption) (*CheckResponse, error) {
+ out := new(CheckResponse)
+ err := c.cc.Invoke(ctx, UserService_UserMenuCheck_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
@@ -43,6 +62,7 @@
// for forward compatibility
type UserServiceServer interface {
SyncUser(context.Context, *UserRequest) (*UserResponse, error)
+ UserMenuCheck(context.Context, *CheckRequest) (*CheckResponse, error)
mustEmbedUnimplementedUserServiceServer()
}
@@ -52,6 +72,9 @@
func (UnimplementedUserServiceServer) SyncUser(context.Context, *UserRequest) (*UserResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method SyncUser not implemented")
+}
+func (UnimplementedUserServiceServer) UserMenuCheck(context.Context, *CheckRequest) (*CheckResponse, error) {
+ return nil, status.Errorf(codes.Unimplemented, "method UserMenuCheck not implemented")
}
func (UnimplementedUserServiceServer) mustEmbedUnimplementedUserServiceServer() {}
@@ -76,10 +99,28 @@
}
info := &grpc.UnaryServerInfo{
Server: srv,
- FullMethod: "/user.UserService/SyncUser",
+ FullMethod: UserService_SyncUser_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(UserServiceServer).SyncUser(ctx, req.(*UserRequest))
+ }
+ return interceptor(ctx, in, info, handler)
+}
+
+func _UserService_UserMenuCheck_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
+ in := new(CheckRequest)
+ if err := dec(in); err != nil {
+ return nil, err
+ }
+ if interceptor == nil {
+ return srv.(UserServiceServer).UserMenuCheck(ctx, in)
+ }
+ info := &grpc.UnaryServerInfo{
+ Server: srv,
+ FullMethod: UserService_UserMenuCheck_FullMethodName,
+ }
+ handler := func(ctx context.Context, req interface{}) (interface{}, error) {
+ return srv.(UserServiceServer).UserMenuCheck(ctx, req.(*CheckRequest))
}
return interceptor(ctx, in, info, handler)
}
@@ -95,6 +136,10 @@
MethodName: "SyncUser",
Handler: _UserService_SyncUser_Handler,
},
+ {
+ MethodName: "UserMenuCheck",
+ Handler: _UserService_UserMenuCheck_Handler,
+ },
},
Streams: []grpc.StreamDesc{},
Metadata: "user.proto",
diff --git a/service/user.go b/service/user.go
index c5a1c8e..3f40330 100644
--- a/service/user.go
+++ b/service/user.go
@@ -1,20 +1,12 @@
package service
import (
- "aps_crm/conf"
"aps_crm/constvar"
"aps_crm/model"
"aps_crm/pkg/ecode"
"aps_crm/pkg/encrypt"
- "aps_crm/pkg/logx"
- "aps_crm/proto/user"
- "context"
"errors"
- "fmt"
- "google.golang.org/grpc"
- "google.golang.org/grpc/credentials/insecure"
"gorm.io/gorm"
- "time"
)
type UserService struct{}
@@ -100,51 +92,4 @@
func (userService *UserService) GetUserList() (userList []*model.User, err error) {
return model.NewUserSearch(nil).FindAll()
-}
-
-var (
- userConn *grpc.ClientConn
-)
-
-func InitUserConn() {
- var err error
- userConn, err = grpc.Dial(conf.Conf.GrpcServiceAddr.Admin, grpc.WithTransportCredentials(insecure.NewCredentials()))
- if err != nil {
- logx.Errorf("grpc dial user service error: %v", err.Error())
- return
- }
-}
-
-func CloseUserConn() {
- if userConn != nil {
- userConn.Close()
- }
-}
-
-func SyncUserInfo() {
- cli := user.NewUserServiceClient(userConn)
-
- var users []*user.User
-
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- defer cancel()
- r, err := cli.SyncUser(ctx, &user.UserRequest{Users: users})
- if err != nil {
- logx.Fatalf("could not sync users: %v", err)
- }
-
- fmt.Printf("Synced: %v, Message: %s", r.List, r.Message)
-
- for _, member := range r.List {
- err = model.NewUserSearch(nil).FirstOrCreate(model.User{
- UUID: member.Uuid,
- Username: member.Username,
- UserType: constvar.UserType(member.Usertype),
- NickName: member.Nickname,
- })
- if err != nil {
- logx.Errorf("sync user error: %v", err.Error())
- continue
- }
- }
}
--
Gitblit v1.8.0