| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.Json;
- namespace AI.Platform.Page.Pages.Media
- {
- public class Utils
- {
- private static Dictionary<string, string> _StateDic = new Dictionary<string, string>()
- {
- { "idle","空闲" },{ "lock","锁枪" },{ "offline","脱机" },{ "lift","提枪" },{ "authorised","授权" },{ "start","开始" },{ "fueling","加油中" }
- };
- /// <summary>
- /// 根据数据库中的油枪状态json转中文拼接
- /// </summary>
- /// <param name="jsonStr"></param>
- /// <returns></returns>
- public static string MachineStateJsonToString(string jsonStr)
- {
- List<string>? list = JsonSerializer.Deserialize<List<string>>(jsonStr);
- if (list == null || list.Count == 0) return "";
- var result = "";
- foreach(string item in list)
- {
- string? state = _StateDic.GetValueOrDefault(item);
- if (state == null || state.Length == 0) continue;
- result += state + ",";
- }
- return result.Remove(result.Length - 1);
- }
- public static string JoinDateTime(DateTime start,DateTime end)
- {
- return $"{start}-{end}";
- }
- public static string ToWebPath(string relativeFilePath)
- {
- if (string.IsNullOrEmpty(relativeFilePath))
- return string.Empty;
- // 1. 统一斜杠
- var path = relativeFilePath.Replace('\\', '/');
- // 2. 移除开头的 "wwwroot/" 或 "wwwroot\"
- if (path.StartsWith("wwwroot/", StringComparison.OrdinalIgnoreCase))
- {
- path = path.Substring("wwwroot/".Length);
- }
- else if (path.StartsWith("wwwroot", StringComparison.OrdinalIgnoreCase))
- {
- path = path.Substring("wwwroot".Length).TrimStart('/');
- }
- // 3. 对每一段进行 URL 编码(避免 / 被编码)
- var segments = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
- var encodedSegments = segments.Select(Uri.EscapeDataString);
- return "/" + string.Join("/", encodedSegments);
- }
- }
- }
|