获取用户信息
parent
c6359ecb42
commit
faa692a001
|
|
@ -177,6 +177,28 @@ namespace Infrastructure.Cache
|
|||
{
|
||||
return server.Keys(pattern: new RedisValue(pattern));
|
||||
}
|
||||
public async Task<List<RedisKey>> ScanKeysAsync(string pattern)
|
||||
{
|
||||
var keys = new List<RedisKey>();
|
||||
|
||||
foreach (var endpoint in _conn.GetEndPoints())
|
||||
{
|
||||
var server = _conn.GetServer(endpoint);
|
||||
|
||||
// 必须跳过从节点
|
||||
if (server.IsSlave || server.IsReplica)
|
||||
continue;
|
||||
|
||||
var scan = server.Keys(
|
||||
database: iDatabase.Database,
|
||||
pattern: pattern,
|
||||
pageSize: 1000);
|
||||
|
||||
keys.AddRange(scan);
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
/// <summary>
|
||||
/// HSET - 异步设置哈希表中的多个字段。
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -1,24 +1,25 @@
|
|||
using DocumentFormat.OpenXml.EMMA;
|
||||
using DocumentFormat.OpenXml.Math;
|
||||
using DocumentFormat.OpenXml.Spreadsheet;
|
||||
using HidSharp;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Cache;
|
||||
using Infrastructure.CloudSdk.minio;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Minio;
|
||||
using MQTTnet;
|
||||
using OpenAuth.App.ServiceApp;
|
||||
using OpenAuth.App.ServiceApp.Response;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Formats.Jpeg;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using StackExchange.Redis;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using SixLabors.ImageSharp.Formats.Jpeg;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using Minio;
|
||||
using Infrastructure.CloudSdk.minio;
|
||||
|
||||
namespace OpenAuth.WebApi.Controllers.ServiceControllers
|
||||
{
|
||||
|
|
@ -580,7 +581,7 @@ namespace OpenAuth.WebApi.Controllers.ServiceControllers
|
|||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<Response<MqttClientResp>> GetRedisUser(string id)
|
||||
public async Task<Response<MqttClientResp>> GetRedisUserold(string id)
|
||||
{
|
||||
var result = new Response<MqttClientResp>();
|
||||
try
|
||||
|
|
@ -595,6 +596,61 @@ namespace OpenAuth.WebApi.Controllers.ServiceControllers
|
|||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取当前用户mqtt客户端信息
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public async Task<Response<MqttClientResp>> GetRedisUser(string id)
|
||||
{
|
||||
var result = new Response<MqttClientResp>();
|
||||
try
|
||||
{
|
||||
var keys = await _cache.ScanKeysAsync("client:*");
|
||||
foreach (var key in keys)
|
||||
{
|
||||
// 2. 获取 hash 全部字段
|
||||
var hashEntries = await _cache.HashGetAllAsync(key);
|
||||
|
||||
if (hashEntries == null || hashEntries.Length == 0)
|
||||
continue;
|
||||
|
||||
// 3. 取 UserId 字段
|
||||
var userIdEntry = hashEntries.FirstOrDefault(x => x.Name == "UserId");
|
||||
|
||||
if (userIdEntry.Value == RedisValue.Null)
|
||||
continue;
|
||||
|
||||
// 4. 判断是否等于目标 UserId
|
||||
if (userIdEntry.Value.ToString() == id)
|
||||
{
|
||||
// 将 HashEntry[] 转成字典
|
||||
var dict = hashEntries.ToDictionary(
|
||||
x => x.Name.ToString(),
|
||||
x => x.Value.ToString()
|
||||
);
|
||||
MqttClientResp mqttClientResp = new MqttClientResp();
|
||||
|
||||
mqttClientResp.ClientId = dict["ClientId"];
|
||||
mqttClientResp.UserId = dict["UserId"];
|
||||
mqttClientResp.UserName = dict["UserName"];
|
||||
mqttClientResp.ConnectTime = DateTime.Parse(dict["ConnectTime"]);
|
||||
mqttClientResp.DeviceSn = dict["DeviceSn"];
|
||||
mqttClientResp.IsLock = bool.Parse(dict["IsLock"]);
|
||||
result.Result = mqttClientResp;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.Code = 500;
|
||||
result.Message = ex.Message;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private MqttClientResp ParseClient(HashEntry[] entries)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -344,7 +344,7 @@ namespace OpenAuth.WebApi
|
|||
#endregion
|
||||
|
||||
#region Quartz
|
||||
services.AddHostedService<QuartzService>();
|
||||
//services.AddHostedService<QuartzService>();
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue