-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDefaultRegionService.cs
More file actions
178 lines (157 loc) · 5.32 KB
/
DefaultRegionService.cs
File metadata and controls
178 lines (157 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using System.Text.Json;
namespace BootstrapBlazor.Components;
class DefaultRegionService : IRegionService
{
private static readonly ConcurrentDictionary<string, HashSet<string>> CitiesCache = new();
private static readonly ConcurrentDictionary<string, HashSet<CountyItem>> CountiesCache = new();
private static readonly ConcurrentDictionary<string, HashSet<string>> DetailCache = new();
private static bool _initialized;
#if NET9_0_OR_GREATER
private static readonly Lock Lock = new();
#else
private static readonly object Lock = new();
#endif
public HashSet<string> GetProvinces() => Provinces;
public HashSet<string> GetCities(string province)
{
LoadCityData();
return CitiesCache.TryGetValue(province, out var cities) ? cities : [];
}
public HashSet<CountyItem> GetCounties(string city)
{
LoadCityData();
return CountiesCache.TryGetValue(city, out var counties) ? counties : [];
}
public HashSet<string> GetDetails(string countyCode)
{
return DetailCache.GetOrAdd(countyCode, LoadDetailData);
}
private static HashSet<string> LoadDetailData(string countyCode)
{
var details = new HashSet<string>();
using var data = typeof(DefaultRegionService).Assembly.GetManifestResourceStream($"BootstrapBlazor.Components.Data.town.{countyCode}.json");
if (data != null)
{
var document = JsonDocument.Parse(data);
var token = document.RootElement.EnumerateObject();
foreach (var t in token)
{
var v = t.Value.GetString();
if (!string.IsNullOrEmpty(v))
{
details.Add(v);
}
}
}
return details;
}
private static void LoadCityData()
{
if (!_initialized)
{
lock (Lock)
{
if (!_initialized)
{
using var data = typeof(DefaultRegionService).Assembly.GetManifestResourceStream("BootstrapBlazor.Components.Data.data.json");
if (data != null)
{
LoadDataCore(data);
}
_initialized = true;
}
}
}
}
private static void LoadDataCore(Stream data)
{
var city = "";
HashSet<string> cities = [];
HashSet<CountyItem>? counties = null;
using var stream = new StreamReader(data);
while (!stream.EndOfStream)
{
var content = stream.ReadLine();
if (!string.IsNullOrEmpty(content))
{
var index = content.IndexOf(':');
if (index == -1)
{
continue;
}
var mem = content.AsMemory();
var code = Trim(mem[0..index].ToString());
var value = Trim(mem[(index + 1)..^1].ToString());
if (code[2..] == "0000")
{
city = value;
cities = [];
if (value == "北京市" || value == "天津市" || value == "上海市" || value == "重庆市" || value == "香港特别行政区" || value == "澳门特别行政区")
{
cities.Add(value);
}
CitiesCache.TryAdd(value, cities);
counties = null;
continue;
}
if (code[4..] == "00")
{
cities.Add(value);
counties = [];
CountiesCache.TryAdd(value, counties);
continue;
}
if (counties == null)
{
counties = [];
CountiesCache.TryAdd(city, counties);
}
counties.Add(new CountyItem() { Name = value, Code = code });
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string Trim(string segment) => segment.Replace("\"", string.Empty).Trim();
private static readonly HashSet<string> Provinces =
[
"北京市",
"天津市",
"上海市",
"重庆市",
"河北省",
"山西省",
"辽宁省",
"吉林省",
"黑龙江省",
"江苏省",
"浙江省",
"安徽省",
"福建省",
"江西省",
"山东省",
"河南省",
"湖北省",
"湖南省",
"广东省",
"海南省",
"四川省",
"贵州省",
"云南省",
"陕西省",
"甘肃省",
"青海省",
"内蒙古自治区",
"广西壮族自治区",
"西藏自治区",
"宁夏回族自治区",
"新疆维吾尔自治区",
"香港特别行政区",
"澳门特别行政区",
"台湾省"
];
}