接收移动端坐标上报

This commit is contained in:
yanghongwei 2025-06-16 17:09:38 +08:00
parent bca6baef65
commit a32912a460
4 changed files with 134 additions and 35 deletions

View File

@ -56,5 +56,13 @@
{
return $"white_list{sn}";
}
/// <summary>
/// 所有用户位置
/// </summary>
/// <returns></returns>
public static string UserLocation(long userId)
{
return $"user_locationbyid_{userId}";
}
}
}

View File

@ -10,6 +10,7 @@ namespace LY.App.Common.Redis
public class RedisService
{
private readonly IDatabase _db;
private readonly IServer _redis;
/// <summary>
/// 构造函数
@ -38,7 +39,45 @@ namespace LY.App.Common.Redis
string jsonData = await _db.StringGetAsync(key);
return jsonData is not null ? JsonSerializer.Deserialize<T>(jsonData) : default;
}
/// <summary>
/// 模糊 查询所有 Key
/// </summary>
/// <param name="pattern"></param>
/// <returns></returns>
public async Task<List<RedisKey>> GetAllKeysAsync(string pattern)
{
var redis = _db.Multiplexer;
var keys = new List<RedisKey>();
foreach (var endPoint in redis.GetEndPoints())
{
var server = redis.GetServer(endPoint);
if (!server.IsConnected)
continue;
// 使用 SCAN 获取匹配的 key避免 KEYS 阻塞
var scanKeys = server.Keys(pattern: pattern, pageSize: 1000);
keys.AddRange(scanKeys);
}
return keys;
//var result = new Dictionary<string, string>();
//if (keys.Count > 0)
//{
// // 一次批量获取 value
// var values = await _db.StringGetAsync(keys.ToArray());
// for (int i = 0; i < keys.Count; i++)
// {
// if (values[i].HasValue)
// {
// result[keys[i]] = values[i];
// }
// }
//}
//return result;
}
/// <summary>
/// 删除 Key
/// </summary>

View File

@ -4,6 +4,8 @@ using LY.App.Model;
using LY.App.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;
using System.Reactive.Joins;
namespace LY.App.Controllers
{
@ -52,13 +54,43 @@ namespace LY.App.Controllers
var weather = await _redisService.GetOrSetAsync(RedisKeyList.Index_Weather,
async () => await _weatherService.GetWeather(),
TimeSpan.FromHours(3));
var data = await GetUserLocation();
result.data = new
{
positions,
alarmCount,
weather
weather,
userLocation = data
};
return Ok(result);
}
private async Task<List<SyncLocation>> GetUserLocation()
{
var keys = await _redisService.GetAllKeysAsync("user_locationbyid_*");
List<SyncLocation> result = new List<SyncLocation>();
if (keys.Count > 0)
{
foreach (var item in keys)
{
result.Add(await _redisService.GetAsync<SyncLocation>(item));
}
}
return result;
}
/// <summary>
/// 同步移动端位置信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("syncLocation")]
public async Task<IActionResult> SyncLocation(SyncLocation input)
{
string key = RedisKeyList.UserLocation(input.userId);
await _redisService.SetAsync(key, new SyncLocation() { userId = input.userId, lat = input.lat, lon = input.lon }, TimeSpan.FromMinutes(1));
return Ok(new ApiResult());
}
}
}

20
Model/SyncLocation.cs Normal file
View File

@ -0,0 +1,20 @@
namespace LY.App.Model
{
/// <summary>
/// 移动端同步坐标信息
/// </summary>
public class SyncLocation
{
public long userId { get; set; }
public double lon { get; set; }
public double lat { get; set; }
}
/// <summary>
/// web 端同步坐标信息
/// </summary>
public class Location
{
public double lon { get; set; }
public double lat { get; set; }
}
}