zhangqian
2023-10-12 4f6d9b1e7284831d8225ec207f0fa83e36b09b9d
crm开启登录认证,增加菜单权限认证
2个文件已添加
9个文件已修改
435 ■■■■ 已修改文件
conf/aps-crm.json 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
constvar/const.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main.go 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
middleware/interceptor.go 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
middleware/jwt.go 13 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
middleware/user.go 78 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/client.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/user.proto 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/user/user.pb.go 194 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/user/user_grpc.pb.go 49 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/user.go 55 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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"
  }
}
constvar/const.go
@@ -132,3 +132,7 @@
    ServiceFeeKeywordProductName    ServiceFeeKeywordType = "产品名称"
    ServiceFeeKeywordServiceEndDate ServiceFeeKeywordType = "服务到期日"
)
const (
    SystemType = 2
)
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)
middleware/interceptor.go
New file
@@ -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
}
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存储到cookie或者本地localStorage中 不过需要跟后端协商过期时间 可以约定刷新令牌或者重新登录
        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
        }
    }
}
middleware/user.go
New file
@@ -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
        }
    }
}
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" {
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;
}
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,
        },
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",
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
        }
    }
}