Utils.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.Json;
  5. namespace AI.Platform.Page.Pages.Media
  6. {
  7. public class Utils
  8. {
  9. private static Dictionary<string, string> _StateDic = new Dictionary<string, string>()
  10. {
  11. { "idle","空闲" },{ "lock","锁枪" },{ "offline","脱机" },{ "lift","提枪" },{ "authorised","授权" },{ "start","开始" },{ "fueling","加油中" }
  12. };
  13. /// <summary>
  14. /// 根据数据库中的油枪状态json转中文拼接
  15. /// </summary>
  16. /// <param name="jsonStr"></param>
  17. /// <returns></returns>
  18. public static string MachineStateJsonToString(string jsonStr)
  19. {
  20. List<string>? list = JsonSerializer.Deserialize<List<string>>(jsonStr);
  21. if (list == null || list.Count == 0) return "";
  22. var result = "";
  23. foreach(string item in list)
  24. {
  25. string? state = _StateDic.GetValueOrDefault(item);
  26. if (state == null || state.Length == 0) continue;
  27. result += state + ",";
  28. }
  29. return result.Remove(result.Length - 1);
  30. }
  31. public static string JoinDateTime(DateTime start,DateTime end)
  32. {
  33. return $"{start}-{end}";
  34. }
  35. public static string ToWebPath(string relativeFilePath)
  36. {
  37. if (string.IsNullOrEmpty(relativeFilePath))
  38. return string.Empty;
  39. // 1. 统一斜杠
  40. var path = relativeFilePath.Replace('\\', '/');
  41. // 2. 移除开头的 "wwwroot/" 或 "wwwroot\"
  42. if (path.StartsWith("wwwroot/", StringComparison.OrdinalIgnoreCase))
  43. {
  44. path = path.Substring("wwwroot/".Length);
  45. }
  46. else if (path.StartsWith("wwwroot", StringComparison.OrdinalIgnoreCase))
  47. {
  48. path = path.Substring("wwwroot".Length).TrimStart('/');
  49. }
  50. // 3. 对每一段进行 URL 编码(避免 / 被编码)
  51. var segments = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
  52. var encodedSegments = segments.Select(Uri.EscapeDataString);
  53. return "/" + string.Join("/", encodedSegments);
  54. }
  55. }
  56. }