func GetUserListHandler(c *gin.Context) { cfg := api.DefaultConfig() cfg.Address = fmt.Sprintf("%s:%d", global.AppConf.Consul.IP, global.AppConf.Consul.Port) userSrcHost := "" userSrvPort := 0 client, err := api.NewClient(cfg) if err != nil { zap.S().Errorw("[GetUserList] 查询 【用户列表失败,拉取服务失败】err:%s", err.Error()) return } data, err := client.Agent().ServicesWithFilter(fmt.Sprintf("Service==%s", global.AppConf.System.Name)) if err != nil { zap.S().Errorw("[GetUserList] 查询 【查询User服务失败】err:%s", err.Error()) return } for _, service := range data { userSrcHost = service.Address userSrvPort = service.Port }
userConn, err := grpc.Dial(fmt.Sprintf("%s:%d", userSrcHost, userSrvPort), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { println(err.Error()) zap.S().Errorw("[GetUserList] 连接 【用户服务失败】", "msg", err.Error()) } claims, _ := c.Get("claims") currentUser := claims.(*jwt_request.CustomClaims) zap.S().Infof("访问用户:%d", currentUser.ID) userClient := proto.NewUserClient(userConn) pn := c.DefaultQuery("pn", "0") pnInt, _ := strconv.Atoi(pn) pSize := c.DefaultQuery("psize", "0") pSizeInt, _ := strconv.Atoi(pSize) rsp, err := userClient.GetUserList(context.Background(), &proto.PageInfo{ Pn: uint32(pnInt), PSize: uint32(pSizeInt), }) if err != nil { zap.S().Errorw("[GetUserList] 查询 【用户列表失败】") convert.HandleGrpcError2Http(err, c) return } result := make([]response.UserResponse, 0) for _, value := range rsp.Data { result = append(result, response.UserResponse{ Id: value.Id, NickName: value.Nickname, Birthday: response.JsonTime(time.Unix(int64(value.BirthDay), 0)), Gender: value.Gender, Mobile: value.Mobile, }) } R.SuccessWithDetailed(c, "获取用户列表成功", result) }
|