Skip to content

Commit 19a798e

Browse files
committed
feat: 实现明细逻辑
1 parent 5e06328 commit 19a798e

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

src/components/BootstrapBlazor.Region/Services/DefaultRegionService.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
using System.Collections.Concurrent;
1010
using System.Runtime.CompilerServices;
11+
using System.Text.Json;
1112

1213
namespace BootstrapBlazor.Components;
1314

1415
class DefaultRegionService : IRegionService
1516
{
1617
private static readonly ConcurrentDictionary<string, IReadOnlySet<string>> _citiesCache = new();
1718
private static readonly ConcurrentDictionary<string, IReadOnlySet<CountyItem>> _countiesCache = new();
19+
private static readonly ConcurrentDictionary<string, IReadOnlySet<string>> _detailCache = new();
1820

1921
private static bool _initialized = false;
2022

@@ -38,9 +40,36 @@ public IReadOnlySet<CountyItem> GetCounties(string city)
3840
return _countiesCache.TryGetValue(city, out var counties) ? counties : new HashSet<CountyItem>();
3941
}
4042

41-
public List<string> GetDetails(string county)
43+
public IReadOnlySet<string> GetDetails(string countyCode)
4244
{
43-
throw new NotImplementedException();
45+
LoadDetailData(countyCode);
46+
return _detailCache.TryGetValue(countyCode, out var details) ? details : new HashSet<string>();
47+
}
48+
49+
private static IReadOnlySet<string> LoadDetailData(string countyCode)
50+
{
51+
if (_detailCache.TryGetValue(countyCode, out var detail))
52+
{
53+
return detail;
54+
}
55+
56+
var details = new HashSet<string>();
57+
var data = typeof(DefaultRegionService).Assembly.GetManifestResourceStream($"BootstrapBlazor.Components.Data.town.{countyCode}.json");
58+
if (data != null)
59+
{
60+
var document = JsonDocument.Parse(data);
61+
var token = document.RootElement.EnumerateObject();
62+
foreach (var t in token)
63+
{
64+
var v = t.Value.GetString();
65+
if (!string.IsNullOrEmpty(v))
66+
{
67+
details.Add(v);
68+
}
69+
}
70+
}
71+
_detailCache.TryAdd(countyCode, details);
72+
return details;
4473
}
4574

4675
private static void LoadCityData()

src/components/BootstrapBlazor.Region/Services/IRegionService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface IRegionService
3232
/// <summary>
3333
/// 获得指定区县的街道地址数据
3434
/// </summary>
35-
/// <param name="county"></param>
35+
/// <param name="countyCode"></param>
3636
/// <returns></returns>
37-
List<string> GetDetails(string county);
37+
IReadOnlySet<string> GetDetails(string countyCode);
3838
}

0 commit comments

Comments
 (0)