接收移动端坐标上报
This commit is contained in:
		
							parent
							
								
									bca6baef65
								
							
						
					
					
						commit
						a32912a460
					
				| 
						 | 
					@ -56,5 +56,13 @@
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            return $"white_list{sn}";
 | 
					            return $"white_list{sn}";
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// 所有用户位置
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <returns></returns>
 | 
				
			||||||
 | 
					        public static string  UserLocation(long userId)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return $"user_locationbyid_{userId}";
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,6 +10,7 @@ namespace LY.App.Common.Redis
 | 
				
			||||||
    public class RedisService
 | 
					    public class RedisService
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        private readonly IDatabase _db;
 | 
					        private readonly IDatabase _db;
 | 
				
			||||||
 | 
					        private readonly IServer _redis;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// 构造函数
 | 
					        /// 构造函数
 | 
				
			||||||
| 
						 | 
					@ -38,7 +39,45 @@ namespace LY.App.Common.Redis
 | 
				
			||||||
            string jsonData = await _db.StringGetAsync(key);
 | 
					            string jsonData = await _db.StringGetAsync(key);
 | 
				
			||||||
            return jsonData is not null ? JsonSerializer.Deserialize<T>(jsonData) : default;
 | 
					            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>
 | 
					            /// <summary>
 | 
				
			||||||
            /// 删除 Key
 | 
					            /// 删除 Key
 | 
				
			||||||
            /// </summary>
 | 
					            /// </summary>
 | 
				
			||||||
| 
						 | 
					@ -78,4 +117,4 @@ namespace LY.App.Common.Redis
 | 
				
			||||||
                return value;
 | 
					                return value;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
}
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,6 +4,8 @@ using LY.App.Model;
 | 
				
			||||||
using LY.App.Service;
 | 
					using LY.App.Service;
 | 
				
			||||||
using Microsoft.AspNetCore.Http;
 | 
					using Microsoft.AspNetCore.Http;
 | 
				
			||||||
using Microsoft.AspNetCore.Mvc;
 | 
					using Microsoft.AspNetCore.Mvc;
 | 
				
			||||||
 | 
					using StackExchange.Redis;
 | 
				
			||||||
 | 
					using System.Reactive.Joins;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace LY.App.Controllers
 | 
					namespace LY.App.Controllers
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -52,13 +54,43 @@ namespace LY.App.Controllers
 | 
				
			||||||
            var weather = await _redisService.GetOrSetAsync(RedisKeyList.Index_Weather,
 | 
					            var weather = await _redisService.GetOrSetAsync(RedisKeyList.Index_Weather,
 | 
				
			||||||
                   async () => await _weatherService.GetWeather(),
 | 
					                   async () => await _weatherService.GetWeather(),
 | 
				
			||||||
            TimeSpan.FromHours(3));
 | 
					            TimeSpan.FromHours(3));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var data = await GetUserLocation();
 | 
				
			||||||
            result.data = new
 | 
					            result.data = new
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                positions,
 | 
					                positions,
 | 
				
			||||||
                alarmCount,
 | 
					                alarmCount,
 | 
				
			||||||
                weather
 | 
					                weather,
 | 
				
			||||||
 | 
					                userLocation = data
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
            return Ok(result);
 | 
					            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());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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; }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue