UsAppAuthScopeHelper.cs
9.04 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System.Text.Json;
using FoodLabeling.Application.Contracts;
using FoodLabeling.Application.Contracts.Dtos.AuthScope;
using FoodLabeling.Application.Contracts.Dtos.UsAppAuth;
using FoodLabeling.Application.Services.DbModels;
using FoodLabeling.Domain.Entities;
using Microsoft.Extensions.Caching.Distributed;
using SqlSugar;
using Volo.Abp;
using Volo.Abp.Users;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace FoodLabeling.Application.Helpers;
/// <summary>
/// App 管理员 Company → Region → Location 级联选店(与 5-26 auth-scope / us-app-auth 文档一致)。
/// </summary>
public static class UsAppAuthScopeHelper
{
private const string CacheKeyPrefix = "FoodLabeling:UsAppAdminScope:";
public static void EnsureAdminAppToken(ICurrentUser currentUser)
{
if (currentUser.Id is null)
{
throw new UserFriendlyException("用户未登录");
}
var kind = currentUser.FindClaim(UsAppJwtClaims.ClientKind)?.Value;
if (!string.Equals(kind, UsAppJwtClaims.ClientKindUsApp, StringComparison.Ordinal))
{
throw new UserFriendlyException("请使用 App 登录令牌调用该接口");
}
if (!ReportsRoleHelper.IsAdminRole(currentUser))
{
throw new UserFriendlyException("仅管理员可使用公司/区域/门店筛选接口");
}
}
public static async Task<List<AuthScopeCompanyOptionDto>> ListCompaniesAsync(ISqlSugarClient db)
{
return await db.Queryable<FlPartnerDbEntity>()
.Where(x => !x.IsDeleted && x.State)
.OrderBy(x => x.PartnerName)
.Select(x => new AuthScopeCompanyOptionDto
{
Id = x.Id,
PartnerName = x.PartnerName ?? string.Empty,
State = x.State,
})
.ToListAsync();
}
public static async Task<List<AuthScopeRegionOptionDto>> ListRegionsAsync(
ISqlSugarClient db,
string partnerId)
{
var pid = partnerId.Trim();
if (string.IsNullOrEmpty(pid))
{
throw new UserFriendlyException("请选择公司");
}
return await db.Queryable<FlGroupDbEntity>()
.Where(x => !x.IsDeleted && x.State && x.PartnerId == pid)
.OrderBy(x => x.GroupName)
.Select(x => new AuthScopeRegionOptionDto
{
Id = x.Id,
GroupName = x.GroupName ?? string.Empty,
PartnerId = x.PartnerId,
State = x.State,
})
.ToListAsync();
}
public static async Task<List<AuthScopeLocationOptionDto>> ListLocationsAsync(
ISqlSugarClient db,
string partnerId,
string groupId)
{
var ids = await LocationScopeBindingHelper.ResolveFilteredLocationIdsForListAsync(
db,
partnerId,
groupId,
null);
if (ids is null || ids.Count == 0)
{
return new List<AuthScopeLocationOptionDto>();
}
var g = await db.Queryable<FlGroupDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.Id == groupId.Trim());
var partner = await db.Queryable<FlPartnerDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.Id == partnerId.Trim());
var locations = (await db.Queryable<LocationAggregateRoot>()
.Where(x => !x.IsDeleted && ids.Contains(x.Id.ToString()))
.OrderBy(x => x.OrderNum)
.ToListAsync())
.OrderBy(x => x.OrderNum)
.ThenBy(x => x.LocationName, StringComparer.OrdinalIgnoreCase)
.ToList();
return locations.Select(x => new AuthScopeLocationOptionDto
{
Id = x.Id.ToString(),
LocationCode = x.LocationCode ?? string.Empty,
LocationName = x.LocationName ?? string.Empty,
FullAddress = BuildFullAddress(x),
State = x.State,
PartnerId = partner?.Id ?? partnerId.Trim(),
GroupId = g?.Id ?? groupId.Trim(),
GroupName = g?.GroupName ?? string.Empty,
}).ToList();
}
public static async Task<AuthScopeSelectLocationOutputDto> SelectLocationAsync(
ISqlSugarClient db,
IDistributedCache cache,
Guid userId,
UsAppSelectAdminScopeLocationInputVo input)
{
var partnerId = input.PartnerId?.Trim() ?? string.Empty;
var groupId = input.GroupId?.Trim() ?? string.Empty;
var locationId = input.LocationId?.Trim() ?? string.Empty;
if (string.IsNullOrEmpty(partnerId) || string.IsNullOrEmpty(groupId) || string.IsNullOrEmpty(locationId))
{
throw new UserFriendlyException("请选择公司、区域和门店");
}
var scopedIds = await LocationScopeBindingHelper.ResolveFilteredLocationIdsForListAsync(
db,
partnerId,
groupId,
null);
if (scopedIds is null || !scopedIds.Contains(locationId))
{
throw new UserFriendlyException("门店与所选公司/区域不匹配");
}
if (!Guid.TryParse(locationId, out var locationGuid))
{
throw new UserFriendlyException("无效的门店标识");
}
var loc = await db.Queryable<LocationAggregateRoot>()
.FirstAsync(x => !x.IsDeleted && x.Id == locationGuid);
if (loc is null)
{
throw new UserFriendlyException("门店不存在或已删除");
}
var partner = await db.Queryable<FlPartnerDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.Id == partnerId);
var group = await db.Queryable<FlGroupDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.Id == groupId);
var bound = new UsAppBoundLocationDto
{
Id = loc.Id.ToString(),
LocationCode = loc.LocationCode ?? string.Empty,
LocationName = loc.LocationName ?? string.Empty,
FullAddress = BuildFullAddress(loc),
State = loc.State,
};
var cacheItem = new UsAppAdminScopeCacheItem
{
PartnerId = partnerId,
PartnerName = partner?.PartnerName?.Trim() ?? string.Empty,
GroupId = groupId,
GroupName = group?.GroupName?.Trim() ?? string.Empty,
Location = bound,
};
await SetAdminScopeCacheAsync(cache, userId, cacheItem);
return new AuthScopeSelectLocationOutputDto
{
PartnerId = cacheItem.PartnerId,
PartnerName = cacheItem.PartnerName,
GroupId = cacheItem.GroupId,
GroupName = cacheItem.GroupName,
Location = bound,
};
}
public static async Task<UsAppAdminScopeCacheItem?> GetAdminScopeCacheAsync(
IDistributedCache cache,
Guid userId)
{
var raw = await cache.GetStringAsync(CacheKeyPrefix + userId);
if (string.IsNullOrWhiteSpace(raw))
{
return null;
}
try
{
return JsonSerializer.Deserialize<UsAppAdminScopeCacheItem>(raw);
}
catch
{
return null;
}
}
public static Task RemoveAdminScopeCacheAsync(IDistributedCache cache, Guid userId) =>
cache.RemoveAsync(CacheKeyPrefix + userId);
private static async Task SetAdminScopeCacheAsync(
IDistributedCache cache,
Guid userId,
UsAppAdminScopeCacheItem item)
{
var json = JsonSerializer.Serialize(item);
await cache.SetStringAsync(
CacheKeyPrefix + userId,
json,
new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24),
});
}
private static string BuildFullAddress(LocationAggregateRoot loc)
{
var street = loc.Street?.Trim();
var city = loc.City?.Trim();
var state = loc.StateCode?.Trim();
var zip = loc.ZipCode?.Trim();
var line2Parts = new List<string>();
if (!string.IsNullOrEmpty(city))
{
line2Parts.Add(city);
}
if (!string.IsNullOrEmpty(state))
{
line2Parts.Add(state);
}
var line2 = line2Parts.Count > 0 ? string.Join(", ", line2Parts) : string.Empty;
if (!string.IsNullOrEmpty(zip))
{
line2 = string.IsNullOrEmpty(line2) ? zip : $"{line2} {zip}";
}
var segments = new List<string>();
if (!string.IsNullOrEmpty(street))
{
segments.Add(street);
}
if (!string.IsNullOrEmpty(line2))
{
segments.Add(line2);
}
return segments.Count == 0 ? "无" : string.Join(", ", segments);
}
public sealed class UsAppAdminScopeCacheItem
{
public string PartnerId { get; set; } = string.Empty;
public string PartnerName { get; set; } = string.Empty;
public string GroupId { get; set; } = string.Empty;
public string GroupName { get; set; } = string.Empty;
public UsAppBoundLocationDto Location { get; set; } = new();
}
}