✨
This commit is contained in:
174
StopShopping.Services/Implementions/AccessTokenService.cs
Normal file
174
StopShopping.Services/Implementions/AccessTokenService.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using StopShopping.EF;
|
||||
using StopShopping.EF.Models;
|
||||
using StopShopping.Services.Models;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class AccessTokenService : IAccessTokenService
|
||||
{
|
||||
public AccessTokenService(IOptions<JwtOptions> jwtOptions,
|
||||
IDistributedCache cache,
|
||||
StopShoppingContext dbContext,
|
||||
ILogger<AccessTokenService> logger,
|
||||
IClaimsService claimsService)
|
||||
{
|
||||
_jwtOptions = jwtOptions.Value;
|
||||
_cache = cache;
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
_claimsService = claimsService;
|
||||
}
|
||||
|
||||
private readonly JwtOptions _jwtOptions;
|
||||
private readonly IDistributedCache _cache;
|
||||
private readonly StopShoppingContext _dbContext;
|
||||
private readonly ILogger<AccessTokenService> _logger;
|
||||
private readonly IClaimsService _claimsService;
|
||||
|
||||
public AccessToken GenerateAccessToken(ClaimsIdentity claims)
|
||||
{
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Audience = _jwtOptions.ValidAudience,
|
||||
Issuer = _jwtOptions.ValidIssuer,
|
||||
Subject = claims,
|
||||
IssuedAt = DateTime.UtcNow,
|
||||
NotBefore = DateTime.UtcNow,
|
||||
Expires = DateTime.UtcNow.AddSeconds(_jwtOptions.AccessTokenExpiresIn),
|
||||
SigningCredentials = new SigningCredentials(
|
||||
new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOptions.SigningKey!)),
|
||||
SecurityAlgorithms.HmacSha256
|
||||
)
|
||||
};
|
||||
|
||||
var token = new JsonWebTokenHandler()
|
||||
.CreateToken(tokenDescriptor);
|
||||
|
||||
return new AccessToken
|
||||
{
|
||||
Token = token,
|
||||
ExpiresIn = _jwtOptions.AccessTokenExpiresIn
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<bool> AddAccessTokenBlacklistAsync(string accessToken)
|
||||
{
|
||||
JsonWebTokenHandler jwtHandler = new JsonWebTokenHandler();
|
||||
var jwtToken = jwtHandler.ReadJsonWebToken(accessToken);
|
||||
var expClaim = jwtToken.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Exp);
|
||||
if (null == expClaim)
|
||||
{
|
||||
_logger.LogError("access_token:{Token}中无法找到exp", accessToken);
|
||||
return false;
|
||||
}
|
||||
|
||||
var expTime = DateTimeOffset.FromUnixTimeSeconds(long.Parse(expClaim.Value)).UtcDateTime;
|
||||
if (DateTime.UtcNow > expTime)
|
||||
{
|
||||
_logger.LogError("access_token:{Token}已过期", accessToken);
|
||||
return false;
|
||||
}
|
||||
|
||||
await _cache.SetStringAsync(Consts.CacheKeys.AccessTokenBlacklist(accessToken),
|
||||
"quited user",
|
||||
new DistributedCacheEntryOptions
|
||||
{
|
||||
AbsoluteExpiration = expTime
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> IsAccessTokenBlacklistAsync(string accessToken)
|
||||
{
|
||||
var blacklist = await _cache.GetStringAsync(Consts.CacheKeys.AccessTokenBlacklist(accessToken));
|
||||
|
||||
return !string.IsNullOrWhiteSpace(blacklist);
|
||||
}
|
||||
|
||||
public async Task<AccessToken> SetRefreshTokenAsync(int userId, SystemRoles systemRole)
|
||||
{
|
||||
var refreshToken = Guid.NewGuid().ToString("N");
|
||||
var now = DateTime.Now;
|
||||
char role = (char)systemRole;
|
||||
|
||||
// var qry = await _dbContext.RefreshTokens
|
||||
// .Where(r => r.UserId == userId && r.SystemRole == role)
|
||||
// .ExecuteDeleteAsync();
|
||||
|
||||
var model = new RefreshToken
|
||||
{
|
||||
CreateTime = now,
|
||||
ExpiresAt = now.AddSeconds(_jwtOptions.RefreshTokenExpiresIn),
|
||||
SystemRole = role,
|
||||
Token = refreshToken,
|
||||
UserId = userId
|
||||
};
|
||||
await _dbContext.RefreshTokens.AddAsync(model);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return new AccessToken
|
||||
{
|
||||
Token = refreshToken,
|
||||
ExpiresIn = _jwtOptions.RefreshTokenExpiresIn
|
||||
};
|
||||
}
|
||||
|
||||
public async Task RevokeRefreshTokenAsync(string refreshToken)
|
||||
{
|
||||
await _dbContext.RefreshTokens
|
||||
.Where(r => r.Token == refreshToken)
|
||||
.ExecuteDeleteAsync();
|
||||
}
|
||||
|
||||
public async Task<AccessToken?> GenerateAccessTokenAsync(string refreshToken)
|
||||
{
|
||||
var rt = await _dbContext.RefreshTokens
|
||||
.AsNoTracking()
|
||||
.Where(r => r.Token == refreshToken && r.ExpiresAt > DateTime.Now)
|
||||
.FirstOrDefaultAsync();
|
||||
if (null == rt)
|
||||
return null;
|
||||
|
||||
ClaimsIdentity? claimsIdentity = null;
|
||||
|
||||
switch (rt.SystemRole)
|
||||
{
|
||||
case (char)SystemRoles.Admin:
|
||||
{
|
||||
var admin = await _dbContext.Administrators
|
||||
.AsNoTracking()
|
||||
.Where(a => a.Id == rt.UserId)
|
||||
.FirstOrDefaultAsync();
|
||||
if (null != admin)
|
||||
claimsIdentity = _claimsService.BuildAdminIdentity(admin);
|
||||
}
|
||||
break;
|
||||
case (char)SystemRoles.User:
|
||||
{
|
||||
var user = await _dbContext.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.Id == rt.UserId)
|
||||
.FirstOrDefaultAsync();
|
||||
if (null != user)
|
||||
claimsIdentity = _claimsService.BuildIdentity(user);
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
if (null == claimsIdentity)
|
||||
return null;
|
||||
|
||||
return GenerateAccessToken(claimsIdentity);
|
||||
}
|
||||
}
|
||||
202
StopShopping.Services/Implementions/CategoryService.cs
Normal file
202
StopShopping.Services/Implementions/CategoryService.cs
Normal file
@@ -0,0 +1,202 @@
|
||||
using System.Data.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StopShopping.EF;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class CategoryService : ICategoryService
|
||||
{
|
||||
public CategoryService(
|
||||
IFileService fileService,
|
||||
StopShoppingContext dbContext,
|
||||
ILogger<CategoryService> logger
|
||||
)
|
||||
{
|
||||
_fileService = fileService;
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private readonly IFileService _fileService;
|
||||
private readonly StopShoppingContext _dbContext;
|
||||
private readonly ILogger<CategoryService> _logger;
|
||||
|
||||
public async Task<ApiResponse> DeleteCategoryAsync(CategoryIdParams model)
|
||||
{
|
||||
var category = await _dbContext.Categories
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(c => c.Id == model.CategoryId && !c.Deleted);
|
||||
if (null == category)
|
||||
return ApiResponse.Failed("此分类已不存在,请刷新重试");
|
||||
|
||||
var anyProduct = await _dbContext.Products
|
||||
.AsNoTracking()
|
||||
.AnyAsync(p =>
|
||||
_dbContext.Categories
|
||||
.AsNoTracking()
|
||||
.Where(c => c.Path.StartsWith(category.Path) && !c.Deleted)
|
||||
.Select(c => c.Id)
|
||||
.Contains(p.CategoryId));
|
||||
if (anyProduct)
|
||||
return ApiResponse.Failed("分类下已有商品,不允许删除");
|
||||
|
||||
await _dbContext.Categories
|
||||
.Where(c => c.Path.StartsWith(category.Path) && !c.Deleted)
|
||||
.ExecuteDeleteAsync();
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<Category>> EditCategoryAsync(EditCategoryParams model)
|
||||
{
|
||||
EF.Models.Category category;
|
||||
|
||||
using var trans = await _dbContext.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
short level = 1, order = 1;
|
||||
string path = string.Empty;
|
||||
|
||||
var parent = await _dbContext.Categories
|
||||
.FirstOrDefaultAsync(c => c.Id == model.ParentId);
|
||||
if (null != parent)
|
||||
level = (short)(parent.Level + 1);
|
||||
|
||||
if (model.Id > 0)
|
||||
{
|
||||
category = await _dbContext.Categories
|
||||
.FirstAsync(c => c.Id == model.Id && !c.Deleted);
|
||||
if (null == category)
|
||||
return new ApiResponse<Category>().Failed("此分类已不存在");
|
||||
|
||||
path = category.Path;
|
||||
order = category.Order;
|
||||
}
|
||||
else
|
||||
{
|
||||
category = new EF.Models.Category();
|
||||
await _dbContext.Categories.AddAsync(category);
|
||||
|
||||
order = await _dbContext.Categories
|
||||
.Where(c => c.ParentId == model.ParentId)
|
||||
.Select(c => c.Order)
|
||||
.DefaultIfEmpty()
|
||||
.MaxAsync();
|
||||
}
|
||||
|
||||
category.Level = level;
|
||||
if (!string.IsNullOrWhiteSpace(model.Logo))
|
||||
category.Logo = model.Logo;
|
||||
category.Name = model.Name;
|
||||
category.Order = order;
|
||||
category.ParentId = model.ParentId;
|
||||
category.Path = path;
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
if (!(model.Id > 0))
|
||||
{
|
||||
category.Path = BuildPath(parent?.Path ?? "", category.Id);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
await trans.CommitAsync();
|
||||
|
||||
var categoryResult = Cast(category);
|
||||
|
||||
return new ApiResponse<Category>(categoryResult);
|
||||
}
|
||||
catch (DbException e)
|
||||
{
|
||||
await trans.RollbackAsync();
|
||||
_logger.LogError(e, "新增/修改分类失败");
|
||||
return new ApiResponse<Category>().Failed("数据库操作失败,请刷新重试");
|
||||
}
|
||||
}
|
||||
|
||||
public ApiResponse<List<Category>> GetCategoriesTree()
|
||||
{
|
||||
var qry = _dbContext.Categories
|
||||
.AsNoTracking()
|
||||
.Where(c => !c.Deleted)
|
||||
.OrderBy(c => c.ParentId)
|
||||
.ThenBy(c => c.Order)
|
||||
.AsEnumerable();
|
||||
|
||||
var result = new ApiResponse<List<Category>>(ToTree(qry));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> ResortCategoryAsync(ResortCategoryParams model)
|
||||
{
|
||||
var curr = await _dbContext.Categories
|
||||
.FirstOrDefaultAsync(c => c.Id == model.Id && !c.Deleted);
|
||||
if (null == curr)
|
||||
return ApiResponse.Failed("此分类已不存在,请刷新重试");
|
||||
|
||||
var target = await _dbContext.Categories
|
||||
.FirstOrDefaultAsync(c => c.Order == model.TargetOrder && !c.Deleted);
|
||||
if (null == target)
|
||||
return ApiResponse.Failed("目标位置分类已不存在,请刷新重试");
|
||||
|
||||
target.Order = curr.Order;
|
||||
curr.Order = model.TargetOrder;
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
|
||||
#region private methods
|
||||
|
||||
private List<Category> ToTree(IEnumerable<EF.Models.Category> models)
|
||||
{
|
||||
Dictionary<int, Category> idDicts = [];
|
||||
List<Category> categories = [];
|
||||
|
||||
foreach (var model in models)
|
||||
{
|
||||
Category node = Cast(model);
|
||||
node.Children = [];
|
||||
idDicts.Add(node.Id, node);
|
||||
}
|
||||
|
||||
foreach (var d in idDicts)
|
||||
{
|
||||
if (d.Value.ParentId > 0)
|
||||
idDicts[d.Value.ParentId].Children.Add(d.Value);
|
||||
else
|
||||
categories.Add(d.Value);
|
||||
}
|
||||
|
||||
return categories;
|
||||
}
|
||||
|
||||
private Category Cast(EF.Models.Category model)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Id = model.Id,
|
||||
LogoUrl = string.IsNullOrWhiteSpace(model.Logo)
|
||||
? ""
|
||||
: _fileService.GetFileUrl(Models.UploadScences.Category, model.Logo),
|
||||
Name = model.Name,
|
||||
Order = model.Order,
|
||||
ParentId = model.ParentId,
|
||||
Children = []
|
||||
};
|
||||
}
|
||||
|
||||
private string BuildPath(string prefix, params int[] ids)
|
||||
{
|
||||
return string.Format("{0}/{1}/",
|
||||
prefix.TrimEnd('/')
|
||||
, string.Join('/', ids));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
18
StopShopping.Services/Implementions/CipherService.cs
Normal file
18
StopShopping.Services/Implementions/CipherService.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class CipherService : ICipherService
|
||||
{
|
||||
public string EncryptUserPassword(string input)
|
||||
{
|
||||
string hmacKey = "stopshopping";
|
||||
|
||||
using HMACSHA256 sha256 = new(Encoding.UTF8.GetBytes(hmacKey));
|
||||
|
||||
var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
|
||||
return Convert.ToHexStringLower(hash);
|
||||
}
|
||||
}
|
||||
54
StopShopping.Services/Implementions/ClaimsService.cs
Normal file
54
StopShopping.Services/Implementions/ClaimsService.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using StopShopping.EF.Models;
|
||||
using StopShopping.Services.Models;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class ClaimsService : IClaimsService
|
||||
{
|
||||
public ClaimsService(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public ClaimsIdentity BuildIdentity(User user)
|
||||
{
|
||||
var claimsIdentity = new ClaimsIdentity(
|
||||
[
|
||||
new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Name, user.Account),
|
||||
new Claim(JwtRegisteredClaimNames.Nickname, user.NickName),
|
||||
new Claim(ClaimTypes.Role, SystemRoles.User.ToString()),
|
||||
]);
|
||||
|
||||
return claimsIdentity;
|
||||
}
|
||||
|
||||
public ClaimsIdentity BuildAdminIdentity(Administrator admin)
|
||||
{
|
||||
var claimsIdentity = new ClaimsIdentity(
|
||||
[
|
||||
new Claim(JwtRegisteredClaimNames.Sub, admin.Id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Name, admin.Account),
|
||||
new Claim(JwtRegisteredClaimNames.Nickname, admin.NickName),
|
||||
new Claim(ClaimTypes.Role, SystemRoles.Admin.ToString()),
|
||||
]);
|
||||
|
||||
return claimsIdentity;
|
||||
}
|
||||
|
||||
public int GetCurrentUserId()
|
||||
{
|
||||
var currUserId = _httpContextAccessor.HttpContext
|
||||
?.User.FindFirstValue(JwtRegisteredClaimNames.Sub);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(currUserId))
|
||||
throw new InvalidOperationException("在错误的位置获取当前登录用户");
|
||||
|
||||
return Convert.ToInt32(currUserId);
|
||||
}
|
||||
}
|
||||
240
StopShopping.Services/Implementions/DistrictService.cs
Normal file
240
StopShopping.Services/Implementions/DistrictService.cs
Normal file
@@ -0,0 +1,240 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StopShopping.EF;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
using O = StopShopping.OpenPlatform;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class DistrictService : IDistrictService
|
||||
{
|
||||
public DistrictService(
|
||||
StopShoppingContext dbContext,
|
||||
O.IDistrictService districtService,
|
||||
ILogger<DistrictService> logger)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_oDistrictService = districtService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private readonly StopShoppingContext _dbContext;
|
||||
private readonly O.IDistrictService _oDistrictService;
|
||||
private readonly ILogger<DistrictService> _logger;
|
||||
|
||||
public async Task InitialDatabaseAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("开始初始化行政区划");
|
||||
|
||||
if (await _dbContext.Districts.AnyAsync(cancellationToken))
|
||||
{
|
||||
_logger.LogInformation("已经存在数据,无需初始化");
|
||||
}
|
||||
else
|
||||
{
|
||||
var districtsResp = await _oDistrictService.GetTop3LevelDistrictsAsync(cancellationToken);
|
||||
if (districtsResp?.IsSucced ?? false)
|
||||
{
|
||||
if (null != districtsResp?.Districts)
|
||||
{
|
||||
using var transaction = await _dbContext.Database.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
short order = 1;
|
||||
int parentId = 0;
|
||||
var districtsProvince = districtsResp.Districts
|
||||
.Select(d => Cast(d, parentId, order++))
|
||||
.ToList();
|
||||
await _dbContext.Districts.AddRangeAsync(districtsProvince, cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
foreach (var province in districtsResp.Districts)
|
||||
{
|
||||
parentId = districtsProvince.First(p => p.Code == province.Code).Id;
|
||||
order = 1;
|
||||
|
||||
var citys = province.Districts;
|
||||
|
||||
if (null == citys)
|
||||
{
|
||||
await transaction.RollbackAsync(cancellationToken);
|
||||
_logger.LogError("行政区划下级数据不完整:{Name}", province.Name);
|
||||
return;
|
||||
}
|
||||
|
||||
var districtsCity = citys
|
||||
.Select(d => Cast(d, parentId, order++))
|
||||
.ToList();
|
||||
await _dbContext.Districts.AddRangeAsync(districtsCity, cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 直辖市下属区
|
||||
// var directlyCitysDistrict = citys
|
||||
// .Where(d => d.Districts?.Count == 0)
|
||||
// .ToList();
|
||||
// if (0 == directlyCitysDistrict.Count)
|
||||
// {
|
||||
// await transaction.RollbackAsync(cancellationToken);
|
||||
// _logger.LogError("行政区划数据不完整,缺少直辖市数据");
|
||||
// return;
|
||||
// }
|
||||
|
||||
var semaphoreSlim = new SemaphoreSlim(50);
|
||||
//(parent id, district response)
|
||||
List<Task<(int, O.DistrictResponse?)>> districtTasks = [];
|
||||
|
||||
foreach (var city in citys)
|
||||
{
|
||||
parentId = districtsCity.First(c => c.Code == city.Code).Id;
|
||||
order = 1;
|
||||
if (city.Districts == null || city.Districts.Count == 0)//直辖市
|
||||
{
|
||||
try
|
||||
{
|
||||
await semaphoreSlim.WaitAsync(cancellationToken);
|
||||
districtTasks.Add(
|
||||
Task.FromResult((
|
||||
parentId
|
||||
, await _oDistrictService.GetChildrenAsync(city.Code, cancellationToken)))
|
||||
);
|
||||
}
|
||||
finally
|
||||
{
|
||||
semaphoreSlim.Release();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var districtsDistrict = city.Districts
|
||||
.Select(d => Cast(d, parentId, order++))
|
||||
.ToList();
|
||||
await _dbContext.Districts.AddRangeAsync(districtsDistrict, cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
var districtTask = city.Districts!.Select(async (d) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await semaphoreSlim.WaitAsync();
|
||||
|
||||
return (
|
||||
districtsDistrict.First(dd => dd.Code == d.Code).Id
|
||||
, await _oDistrictService.GetChildrenAsync(d.Code, cancellationToken)
|
||||
);
|
||||
}
|
||||
finally
|
||||
{
|
||||
semaphoreSlim.Release();
|
||||
}
|
||||
});
|
||||
|
||||
districtTasks.AddRange(districtTask);
|
||||
}
|
||||
}
|
||||
|
||||
var streets = await Task.WhenAll(districtTasks);
|
||||
foreach (var s in streets)
|
||||
{
|
||||
order = 1;
|
||||
parentId = s.Item1;
|
||||
if (s.Item2?.IsSucced ?? false)
|
||||
{
|
||||
var districtsStreet = s.Item2.Districts
|
||||
?.Select(ds => Cast(ds, parentId, order++))
|
||||
.ToList();
|
||||
|
||||
if (null != districtsStreet)
|
||||
{
|
||||
await _dbContext.Districts.AddRangeAsync(districtsStreet, cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await transaction.RollbackAsync(cancellationToken);
|
||||
_logger.LogError("街道数据请求失败:{Message}", s.Item2?.Message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
await transaction.CommitAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError("行政区划接口错误:{Message}", districtsResp?.Message);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("结束初始化行政区划");
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<List<District>>> GetTop3LevelDistrictsAsync()
|
||||
{
|
||||
List<District> districts = [];
|
||||
|
||||
var top3Districts = await _dbContext.Districts
|
||||
.Where(d => d.Level <= 3)
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
|
||||
if (0 != top3Districts.Count)
|
||||
{
|
||||
Dictionary<int, District> idDicts = [];
|
||||
foreach (var dbDistrict in top3Districts)
|
||||
{
|
||||
idDicts.Add(dbDistrict.Id, Cast(dbDistrict));
|
||||
}
|
||||
foreach (var kv in idDicts)
|
||||
{
|
||||
if (kv.Value.ParentId > 0)
|
||||
idDicts[kv.Value.ParentId].Children.Add(kv.Value);
|
||||
else
|
||||
districts.Add(kv.Value);
|
||||
}
|
||||
}
|
||||
|
||||
var result = new ApiResponse<List<District>>(districts);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public ApiResponse<List<District>> GetChildren(DistrictParentIdParams model)
|
||||
{
|
||||
var children = _dbContext.Districts
|
||||
.Where(d => d.ParentId == model.ParentId)
|
||||
.AsNoTracking()
|
||||
.Select(Cast)
|
||||
.ToList();
|
||||
|
||||
return new ApiResponse<List<District>>(children);
|
||||
}
|
||||
|
||||
private District Cast(EF.Models.District district)
|
||||
{
|
||||
return new District
|
||||
{
|
||||
Children = [],
|
||||
FullName = district.FullName,
|
||||
Id = district.Id,
|
||||
Level = district.Level,
|
||||
ParentId = district.ParentId
|
||||
};
|
||||
}
|
||||
|
||||
private EF.Models.District Cast(O.DistrictResponse.District district, int parentId, short order)
|
||||
{
|
||||
return new EF.Models.District
|
||||
{
|
||||
Code = district.Code,
|
||||
FullName = district.FullName,
|
||||
Latitude = district.Latitude.ToString(),
|
||||
Level = (short)(district.Level ?? 0),
|
||||
Longitude = district.Longitude.ToString(),
|
||||
Name = district.Name,
|
||||
Order = order,
|
||||
ParentId = parentId,
|
||||
Pinyin = district.PinYin ?? ""
|
||||
};
|
||||
}
|
||||
}
|
||||
65
StopShopping.Services/Implementions/FileService.cs
Normal file
65
StopShopping.Services/Implementions/FileService.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using StopShopping.Services.Extensions;
|
||||
using StopShopping.Services.Models;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class FileService : IFileService
|
||||
{
|
||||
public FileService(IOptions<AppOptions> appOptions,
|
||||
IWebHostEnvironment webHostEnvironment)
|
||||
{
|
||||
_appOptions = appOptions.Value;
|
||||
_env = webHostEnvironment;
|
||||
}
|
||||
|
||||
private readonly AppOptions _appOptions;
|
||||
private readonly IWebHostEnvironment _env;
|
||||
|
||||
public async Task<ApiResponse<FileUpload>> UploadFileAsync(UploadParams payload)
|
||||
{
|
||||
var newName = Guid.NewGuid().ToString("N").ToLower();
|
||||
var extension = Path.GetExtension(payload.File!.FileName);
|
||||
var newFullName = $"{newName}{extension}";
|
||||
var relativeToRootPath = GetRelativeToRootPath(payload.Scences, newFullName);
|
||||
var targetPath = Path.Combine(_env.WebRootPath, GetRelativeToRootPath(payload.Scences));
|
||||
|
||||
if (!Directory.Exists(targetPath))
|
||||
{
|
||||
Directory.CreateDirectory(targetPath);
|
||||
}
|
||||
|
||||
using var file = new FileStream(
|
||||
Path.Combine(_env.WebRootPath, relativeToRootPath),
|
||||
FileMode.CreateNew,
|
||||
FileAccess.Write);
|
||||
|
||||
await payload.File.CopyToAsync(file);
|
||||
|
||||
FileUpload result = new()
|
||||
{
|
||||
NewName = newFullName,
|
||||
Url = GetFileUrl(payload.Scences, newFullName)
|
||||
};
|
||||
|
||||
return new ApiResponse<FileUpload>(result);
|
||||
}
|
||||
|
||||
public string GetFileUrl(UploadScences scences, string fileName)
|
||||
{
|
||||
var relativeToRootPath = GetRelativeToRootPath(scences, fileName);
|
||||
|
||||
return $"{_appOptions.DomainPath}/{relativeToRootPath.Replace(Path.DirectorySeparatorChar, '/')}";
|
||||
}
|
||||
|
||||
private string GetRelativeToRootPath(UploadScences scences, string fileName = "")
|
||||
{
|
||||
return Path.Combine(
|
||||
"images",
|
||||
scences.GetTargetDirectory(),
|
||||
fileName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using NanoidDotNet;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class NanoidSerialNoGenerator : ISerialNoGenerator
|
||||
{
|
||||
public string GenerateProductNo()
|
||||
{
|
||||
string no = Nanoid.Generate(Nanoid.Alphabets.Digits, 8);
|
||||
|
||||
return $"P{no}";
|
||||
}
|
||||
|
||||
public string GenerateRandomPassword()
|
||||
{
|
||||
string pwd = Nanoid.Generate(Nanoid.Alphabets.Default, 8);
|
||||
|
||||
return pwd;
|
||||
}
|
||||
|
||||
public string GenerateRequestNo()
|
||||
{
|
||||
string no = Nanoid.Generate(Nanoid.Alphabets.Digits, 8);
|
||||
|
||||
return $"R{no}";
|
||||
}
|
||||
}
|
||||
193
StopShopping.Services/Implementions/ProductService.cs
Normal file
193
StopShopping.Services/Implementions/ProductService.cs
Normal file
@@ -0,0 +1,193 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using StopShopping.EF;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class ProductService : IProductService
|
||||
{
|
||||
public ProductService(
|
||||
StopShoppingContext dbContext,
|
||||
IClaimsService claimsService,
|
||||
IFileService fileService,
|
||||
ISerialNoGenerator serialNoGenerator)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_claimsService = claimsService;
|
||||
_fileService = fileService;
|
||||
_serilNoGenerator = serialNoGenerator;
|
||||
}
|
||||
|
||||
private readonly StopShoppingContext _dbContext;
|
||||
private readonly IClaimsService _claimsService;
|
||||
private readonly IFileService _fileService;
|
||||
private readonly ISerialNoGenerator _serilNoGenerator;
|
||||
|
||||
public ApiResponse<ProductInfo> Detail(ProductIdParams model)
|
||||
{
|
||||
var detail = _dbContext.Products
|
||||
.AsNoTracking()
|
||||
.Where(p => p.Id == model.ProductId && !p.Deleted)
|
||||
.Include(p => p.Category)
|
||||
.Select(Cast<ProductInfo>)
|
||||
.FirstOrDefault();
|
||||
if (null == detail)
|
||||
return new ApiResponse<ProductInfo>().Failed("商品已不存在,请刷新重试");
|
||||
|
||||
return new ApiResponse<ProductInfo>(detail);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<PagedResult<Product>>> SearchAsync(ProductSearchParms model)
|
||||
{
|
||||
var userId = _claimsService.GetCurrentUserId();
|
||||
var qry = _dbContext.Products
|
||||
.AsNoTracking()
|
||||
.Include(p => p.Category)
|
||||
.Where(p => !p.Deleted && p.UserId == userId);
|
||||
if (model.CategoryId > 0)
|
||||
{
|
||||
var categoryPath = (await _dbContext.Categories
|
||||
.FirstOrDefaultAsync(c1 => c1.Id == model.CategoryId))
|
||||
?.Path ?? "";
|
||||
|
||||
qry = qry.Where(p =>
|
||||
_dbContext.Categories
|
||||
.Where(c => c.Path.StartsWith(categoryPath))
|
||||
.Select(c => c.Id)
|
||||
.Contains(p.CategoryId));
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(model.Keyword))
|
||||
qry = qry.Where(p =>
|
||||
p.Name.Contains(model.Keyword)
|
||||
|| (p.Description != null && p.Description.Contains(model.Keyword)));
|
||||
|
||||
var orderedQry = WithOrderBys(qry, model.OrderBys);
|
||||
|
||||
var result = await orderedQry!
|
||||
.Select(Cast<Product>)
|
||||
.ToAsyncEnumerable()
|
||||
.ToPagedAsync(model.PageIndex, model.PageSize);
|
||||
|
||||
return new ApiResponse<PagedResult<Product>>(result);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> EditAsync(EditProductParams model)
|
||||
{
|
||||
var userId = _claimsService.GetCurrentUserId();
|
||||
EF.Models.Product? product = null;
|
||||
if (model.Id > 0)
|
||||
{
|
||||
product = await _dbContext.Products
|
||||
.FirstOrDefaultAsync(p =>
|
||||
p.Id == model.Id.Value
|
||||
&& p.UserId == userId
|
||||
&& !p.Deleted);
|
||||
if (null == product)
|
||||
return ApiResponse.Failed("商品已不存在,请刷新重试");
|
||||
}
|
||||
else
|
||||
{
|
||||
product = new()
|
||||
{
|
||||
SerialNo = _serilNoGenerator.GenerateProductNo(),
|
||||
UserId = userId,
|
||||
Logo = string.IsNullOrWhiteSpace(model.LogoName)
|
||||
? Consts.DEFAULT_PRODUCT
|
||||
: model.LogoName
|
||||
};
|
||||
await _dbContext.Products.AddAsync(product);
|
||||
}
|
||||
product.CategoryId = model.CategoryId;
|
||||
product.Description = model.Description;
|
||||
product.Detail = model.Detail;
|
||||
product.MinimumUnit = model.MinimumUnit ?? "";
|
||||
product.Name = model.Name ?? "";
|
||||
product.UnitPrice = model.UnitPrice;
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> DeleteAsync(ProductIdParams model)
|
||||
{
|
||||
var userId = _claimsService.GetCurrentUserId();
|
||||
var product = await _dbContext.Products
|
||||
.FirstOrDefaultAsync(p =>
|
||||
p.Id == model.ProductId
|
||||
&& p.UserId == userId
|
||||
&& !p.Deleted);
|
||||
if (null == product)
|
||||
return ApiResponse.Failed("此商品已不存在,请刷新重试");
|
||||
|
||||
product.Deleted = true;
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
|
||||
#region private methods
|
||||
|
||||
private IOrderedQueryable<EF.Models.Product>? WithOrderBys(IQueryable<EF.Models.Product> qry, IEnumerable<ProductOrderBys> orderBys)
|
||||
{
|
||||
IOrderedQueryable<EF.Models.Product>? orderedQry = null;
|
||||
foreach (var orderby in orderBys)
|
||||
{
|
||||
if (null == orderedQry)
|
||||
orderedQry = orderby switch
|
||||
{
|
||||
ProductOrderBys.CreateTime => qry.OrderBy(p => p.CreateTime),
|
||||
ProductOrderBys.CreateTimeDesc => qry.OrderByDescending(p => p.CreateTime),
|
||||
ProductOrderBys.Category => qry.OrderBy(p => p.Category),
|
||||
ProductOrderBys.CategoryDesc => qry.OrderByDescending(p => p.Category),
|
||||
ProductOrderBys.SoldAmount => qry.OrderBy(p => p.SoldAmount),
|
||||
ProductOrderBys.SoldAmountDesc => qry.OrderByDescending(p => p.SoldAmount),
|
||||
_ => qry.OrderBy(p => p.CreateTime),
|
||||
};
|
||||
else
|
||||
orderedQry = orderby switch
|
||||
{
|
||||
ProductOrderBys.CreateTime => orderedQry.ThenBy(p => p.CreateTime),
|
||||
ProductOrderBys.CreateTimeDesc => orderedQry.ThenByDescending(p => p.CreateTime),
|
||||
ProductOrderBys.Category => orderedQry.ThenBy(p => p.Category),
|
||||
ProductOrderBys.CategoryDesc => orderedQry.ThenByDescending(p => p.Category),
|
||||
ProductOrderBys.SoldAmount => orderedQry.ThenBy(p => p.SoldAmount),
|
||||
ProductOrderBys.SoldAmountDesc => orderedQry.ThenByDescending(p => p.SoldAmount),
|
||||
_ => orderedQry.ThenBy(p => p.CreateTime),
|
||||
};
|
||||
}
|
||||
return orderedQry;
|
||||
}
|
||||
|
||||
private T Cast<T>(EF.Models.Product product)
|
||||
where T : Product, new()
|
||||
{
|
||||
var result = new T
|
||||
{
|
||||
CategoryName = product.Category.Name,
|
||||
CreateTime = product.CreateTime.ToFormatted(),
|
||||
Description = product.Description,
|
||||
Id = product.Id,
|
||||
MinimumUnit = product.MinimumUnit,
|
||||
Name = product.Name,
|
||||
UnitPrice = product.UnitPrice,
|
||||
SoldAmount = product.SoldAmount,
|
||||
LogoUrl = _fileService.GetFileUrl(Models.UploadScences.Product, product.Logo)
|
||||
};
|
||||
if (result is ProductInfo)
|
||||
{
|
||||
(result as ProductInfo)!.CategoryId = product.CategoryId;
|
||||
(result as ProductInfo)!.Detail = product.Detail;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private string BuildCategoryPath(int categoryId)
|
||||
{
|
||||
return $"/{categoryId}/";
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
102
StopShopping.Services/Implementions/ReplyService.cs
Normal file
102
StopShopping.Services/Implementions/ReplyService.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.Data.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StopShopping.EF;
|
||||
using StopShopping.Services.Extensions;
|
||||
using StopShopping.Services.Models;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class ReplyService : IReplyService
|
||||
{
|
||||
public ReplyService(
|
||||
IClaimsService claimsService,
|
||||
StopShoppingContext dbContext,
|
||||
ILogger<ReplyService> logger)
|
||||
{
|
||||
_claimsService = claimsService;
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private readonly IClaimsService _claimsService;
|
||||
private readonly StopShoppingContext _dbContext;
|
||||
private readonly ILogger<ReplyService> _logger;
|
||||
|
||||
public async Task<ApiResponse<List<Reply>>> GetRepliesAsync(RequestIdParams model)
|
||||
{
|
||||
var request = await _dbContext.Requests
|
||||
.Include(r => r.Replies)
|
||||
.ThenInclude(r => r.Product)
|
||||
.Include(r => r.Replies)
|
||||
.ThenInclude(r => r.User)
|
||||
.AsNoTracking()
|
||||
.Where(r => r.Id == model.RequestId && !r.Deleted)
|
||||
.FirstOrDefaultAsync();
|
||||
if (null == request)
|
||||
return new ApiResponse<List<Reply>>().Failed("此需求已不存在,请刷新重试");
|
||||
|
||||
var replies = request.Replies
|
||||
.Where(r => !r.Rejected)
|
||||
.Select(r => new Reply
|
||||
{
|
||||
Amount = r.Amount,
|
||||
Id = r.Id,
|
||||
Memo = r.Memo,
|
||||
ProductId = r.ProductId,
|
||||
ProductName = r.Product.Name,
|
||||
Replier = r.User.NickName,
|
||||
ReplyTime = r.ReplyTime.ToFormatted(),
|
||||
UnitPrice = r.Product.UnitPrice,
|
||||
MinimumUnit = r.Product.MinimumUnit
|
||||
}).ToList();
|
||||
|
||||
return new ApiResponse<List<Reply>>(replies);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> ReplyAsync(ReplyParams model)
|
||||
{
|
||||
var userId = _claimsService.GetCurrentUserId();
|
||||
|
||||
using var trans = await _dbContext.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
var request = await _dbContext.Requests
|
||||
.Where(r => r.Id == model.RequestId && !r.Deleted)
|
||||
.FirstOrDefaultAsync();
|
||||
if (null == request)
|
||||
return ApiResponse.Failed("此需求已不存在,请刷新重试");
|
||||
|
||||
var status = (RequestStatus)request.Status;
|
||||
if (!status.CanReply())
|
||||
return ApiResponse.Failed("此需求已完成,请尝试其他需求");
|
||||
|
||||
request.Status = (short)RequestStatus.Replied;
|
||||
|
||||
EF.Models.Reply reply = new()
|
||||
{
|
||||
Amount = model.Amount,
|
||||
Memo = model.Memo,
|
||||
Price = model.Price,
|
||||
ProductId = model.ProductId,
|
||||
RequestId = model.RequestId,
|
||||
UserId = userId
|
||||
};
|
||||
|
||||
await _dbContext.Replies.AddAsync(reply);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
await trans.CommitAsync();
|
||||
}
|
||||
catch (DbException ex)
|
||||
{
|
||||
await trans.RollbackAsync();
|
||||
_logger.LogError(ex, "提交竞标失败");
|
||||
return ApiResponse.Failed("服务器错误,请刷新重试");
|
||||
}
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
}
|
||||
191
StopShopping.Services/Implementions/RequestService.cs
Normal file
191
StopShopping.Services/Implementions/RequestService.cs
Normal file
@@ -0,0 +1,191 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using StopShopping.EF;
|
||||
using StopShopping.Services.Extensions;
|
||||
using StopShopping.Services.Models;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class RequestService : IRequestService
|
||||
{
|
||||
public RequestService(
|
||||
StopShoppingContext dbContext,
|
||||
IClaimsService claimsService,
|
||||
ISerialNoGenerator serialNoGenerator)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_claimService = claimsService;
|
||||
_serialNoGenerator = serialNoGenerator;
|
||||
}
|
||||
|
||||
private readonly StopShoppingContext _dbContext;
|
||||
private readonly IClaimsService _claimService;
|
||||
private readonly ISerialNoGenerator _serialNoGenerator;
|
||||
|
||||
public async Task<ApiResponse> PublishRequestAsync(CreateRequestParams model)
|
||||
{
|
||||
var serialNo = _serialNoGenerator.GenerateRequestNo();
|
||||
var userId = _claimService.GetCurrentUserId();
|
||||
|
||||
EF.Models.Request req = new()
|
||||
{
|
||||
CategoryId = model.CategoryId,
|
||||
Deadline = DateOnly.Parse(model.Deadline),
|
||||
Description = model.Description,
|
||||
Name = model.Name,
|
||||
PublisherId = userId,
|
||||
SerialNo = serialNo,
|
||||
Status = (short)RequestStatus.Publish,
|
||||
};
|
||||
|
||||
await _dbContext.Requests.AddAsync(req);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<PagedResult<Request>>> SearchAsync(RequestSearchParams model)
|
||||
{
|
||||
return await DoSearchAsync(model, null);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<PagedResult<Request>>> RequestOrderSearchAsync(RequestSearchWithStatusParams model)
|
||||
{
|
||||
return await DoSearchAsync(model, UserRoles.Buyer);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<PagedResult<Request>>> ReplyOrderSearchAsync(RequestSearchWithStatusParams model)
|
||||
{
|
||||
return await DoSearchAsync(model, UserRoles.Seller);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> DeleteRequestAsync(RequestIdParams model)
|
||||
{
|
||||
var userId = _claimService.GetCurrentUserId();
|
||||
|
||||
var request = await _dbContext.Requests
|
||||
.Include(r => r.Replies)
|
||||
.Where(r => r.Id == model.RequestId && !r.Deleted)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (null == request)
|
||||
return ApiResponse.Failed("此需求已不存在,请刷新重试");
|
||||
|
||||
var status = (RequestStatus)request.Status;
|
||||
if (status.CanDelete())
|
||||
return ApiResponse.Failed("此需求状态已改变,请刷新重试");
|
||||
|
||||
request.Deleted = true;
|
||||
request.Status = (short)RequestStatus.Completed;
|
||||
|
||||
foreach (var reply in request.Replies)
|
||||
{
|
||||
reply.Rejected = true;
|
||||
}
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
|
||||
private async Task<ApiResponse<PagedResult<Request>>> DoSearchAsync<T>(T model, UserRoles? userRoles)
|
||||
where T : RequestSearchParams, new()
|
||||
{
|
||||
var qry = _dbContext.Requests
|
||||
.AsNoTracking()
|
||||
.Where(q => !q.Deleted);
|
||||
|
||||
if (model is RequestSearchWithStatusParams statusParams)
|
||||
{
|
||||
if (statusParams.Status != RequestStatus.All)
|
||||
qry = qry.Where(q => q.Status == (short)statusParams.Status);
|
||||
}
|
||||
else
|
||||
{
|
||||
qry = qry.Where(q => q.Status == (short)RequestStatus.Publish
|
||||
|| q.Status == (short)RequestStatus.Replied);
|
||||
}
|
||||
|
||||
if (model.CategoryId > 0)
|
||||
{
|
||||
string categoryPath = $"/{model.CategoryId.Value}/";
|
||||
qry = qry.Where(q =>
|
||||
_dbContext.Categories
|
||||
.Where(c => c.Path.StartsWith(categoryPath) && !c.Deleted)
|
||||
.Select(c => c.Id)
|
||||
.Contains(q.CategoryId));
|
||||
}
|
||||
if (!string.IsNullOrEmpty(model.Keyword))
|
||||
{
|
||||
qry = qry.Where(q =>
|
||||
q.Name.Contains(model.Keyword)
|
||||
|| (q.Description != null && q.Description.Contains(model.Keyword))
|
||||
|| q.SerialNo.Contains(model.Keyword));
|
||||
}
|
||||
|
||||
if (userRoles.HasValue)
|
||||
{
|
||||
var userId = _claimService.GetCurrentUserId();
|
||||
qry = userRoles.Value switch
|
||||
{
|
||||
UserRoles.Seller => qry.Where(q => q.Replies.Any(r => r.UserId == userId)),
|
||||
UserRoles.Buyer => qry.Where(q => q.PublisherId == userId),
|
||||
_ => qry
|
||||
};
|
||||
}
|
||||
|
||||
var firstOrderBy = model.OrderBys!.First();
|
||||
var included = qry
|
||||
.Include(r => r.Publisher).AsNoTracking()
|
||||
.Include(r => r.Category).AsNoTracking()
|
||||
.Select(r => new
|
||||
{
|
||||
ReplyAmount = r.Replies.Count,
|
||||
r
|
||||
});
|
||||
|
||||
var ordered = firstOrderBy switch
|
||||
{
|
||||
RequestOrderBys.PublishTime => included.OrderBy(q => q.r.PublishTime),
|
||||
RequestOrderBys.PublishTimeDesc => included.OrderByDescending(q => q.r.PublishTime),
|
||||
RequestOrderBys.CategoryId => included.OrderBy(q => q.r.CategoryId),
|
||||
RequestOrderBys.CategoryIdDesc => included.OrderByDescending(q => q.r.CategoryId),
|
||||
RequestOrderBys.ReplyAmount => included.OrderBy(q => q.ReplyAmount),
|
||||
RequestOrderBys.ReplyAmountDesc => included.OrderByDescending(q => q.ReplyAmount),
|
||||
_ => included.OrderBy(q => q.r.PublishTime)
|
||||
};
|
||||
|
||||
foreach (var orderBy in model.OrderBys!.Skip(1))
|
||||
{
|
||||
ordered = orderBy switch
|
||||
{
|
||||
RequestOrderBys.PublishTime => ordered!.ThenBy(q => q.r.PublishTime),
|
||||
RequestOrderBys.PublishTimeDesc => ordered!.ThenByDescending(q => q.r.PublishTime),
|
||||
RequestOrderBys.CategoryId => ordered!.ThenBy(q => q.r.CategoryId),
|
||||
RequestOrderBys.CategoryIdDesc => ordered!.ThenByDescending(q => q.r.CategoryId),
|
||||
RequestOrderBys.ReplyAmount => ordered!.ThenBy(q => q.ReplyAmount),
|
||||
RequestOrderBys.ReplyAmountDesc => ordered!.ThenByDescending(q => q.ReplyAmount),
|
||||
_ => ordered!.ThenBy(q => q.r.PublishTime)
|
||||
};
|
||||
}
|
||||
|
||||
var paged = await ordered.Select(r => new Request
|
||||
{
|
||||
CategoryId = r.r.Category.Id,
|
||||
CategoryName = r.r.Category.Name,
|
||||
Deadline = r.r.Deadline.ToFormatted(),
|
||||
Description = r.r.Description,
|
||||
Id = r.r.Id,
|
||||
Name = r.r.Name,
|
||||
Publisher = r.r.Publisher.NickName,
|
||||
PublishTime = r.r.PublishTime.ToFormatted(),
|
||||
ReplyAmount = r.ReplyAmount,
|
||||
SerialNo = r.r.SerialNo,
|
||||
Status = (RequestStatus)r.r.Status,
|
||||
|
||||
}).ToAsyncEnumerable().ToPagedAsync(model.PageIndex, model.PageSize);
|
||||
|
||||
return new ApiResponse<PagedResult<Request>>(paged);
|
||||
}
|
||||
}
|
||||
118
StopShopping.Services/Implementions/SerialNoGenerator.cs
Normal file
118
StopShopping.Services/Implementions/SerialNoGenerator.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class SerialNoGenerator : ISerialNoGenerator
|
||||
{
|
||||
public SerialNoGenerator(ILogger<SerialNoGenerator> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private readonly ILogger<SerialNoGenerator> _logger;
|
||||
|
||||
private const long START_UTC = 1767196800000; //2026-01-01 00:00:00.000
|
||||
private const int BITS_CALLBACK = 3;
|
||||
private const int BITS_SERIAL = 6;
|
||||
private const long MASK_CALLBACK = ~(-1L << BITS_CALLBACK); //0b_0111
|
||||
private const long MASK_SERIAL = ~(-1L << BITS_SERIAL);
|
||||
|
||||
private readonly object _lockObj = new();
|
||||
|
||||
private long _lastUtc = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
||||
private long _serial;
|
||||
private long _callbackSerial;
|
||||
|
||||
/// <summary>
|
||||
/// <list type="table">
|
||||
/// <item>
|
||||
/// <term>
|
||||
/// 0000000
|
||||
/// </term>
|
||||
/// <term>
|
||||
/// 00000000000
|
||||
/// </term>
|
||||
/// <term>
|
||||
/// 00000000000
|
||||
/// </term>
|
||||
/// <term>
|
||||
/// 0000000000
|
||||
/// </term>
|
||||
/// </item>
|
||||
///
|
||||
/// <item>
|
||||
/// <term>
|
||||
/// 符号位1bit
|
||||
/// </term>
|
||||
/// </item>
|
||||
/// <term>
|
||||
/// 毫秒时间戳56bit
|
||||
/// </term>
|
||||
/// <term>
|
||||
/// 时间回拨标识3bit
|
||||
/// </term>
|
||||
/// <term>
|
||||
/// 毫秒内序号6bit
|
||||
/// </term>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GenerateRequestNo()
|
||||
{
|
||||
lock (_lockObj)
|
||||
{
|
||||
var currUtc = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
||||
|
||||
if (currUtc < _lastUtc) //时间回拨
|
||||
{
|
||||
currUtc = _lastUtc;
|
||||
_callbackSerial = (_callbackSerial + 1) & MASK_CALLBACK;
|
||||
if (_callbackSerial == 0) //时间回拨标识数据段用完
|
||||
{
|
||||
_logger.LogWarning("服务器时间不准,序列号可能重复,请检查!");
|
||||
}
|
||||
}
|
||||
|
||||
if (currUtc > _lastUtc)
|
||||
{
|
||||
_serial = 1;
|
||||
_callbackSerial = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_serial = (_serial + 1) & MASK_SERIAL;
|
||||
if (_serial == 0) //毫秒内数据段用完
|
||||
{
|
||||
WaitNextMilliSecond(ref currUtc);
|
||||
_serial = 1;
|
||||
}
|
||||
}
|
||||
|
||||
_lastUtc = currUtc;
|
||||
|
||||
long serial = ((currUtc - START_UTC) << BITS_CALLBACK + BITS_SERIAL)
|
||||
| (_callbackSerial << BITS_SERIAL)
|
||||
| _serial;
|
||||
|
||||
return string.Format("R{0}", serial);
|
||||
}
|
||||
}
|
||||
|
||||
public string GenerateProductNo()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private void WaitNextMilliSecond(ref long curr)
|
||||
{
|
||||
while (_lastUtc >= curr)
|
||||
{
|
||||
curr = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
||||
}
|
||||
}
|
||||
|
||||
public string GenerateRandomPassword()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
284
StopShopping.Services/Implementions/UserService.cs
Normal file
284
StopShopping.Services/Implementions/UserService.cs
Normal file
@@ -0,0 +1,284 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StopShopping.EF;
|
||||
using StopShopping.Services.Extensions;
|
||||
using StopShopping.Services.Models;
|
||||
using StopShopping.Services.Models.Req;
|
||||
using StopShopping.Services.Models.Resp;
|
||||
|
||||
namespace StopShopping.Services.Implementions;
|
||||
|
||||
public class UserService : IUserService
|
||||
{
|
||||
public UserService(
|
||||
ILogger<UserService> logger,
|
||||
StopShoppingContext dbContext,
|
||||
ICipherService cipherService,
|
||||
IAccessTokenService accessTokenService,
|
||||
IClaimsService claimsService,
|
||||
IFileService fileService,
|
||||
ISerialNoGenerator serialNoGenerator)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dbContext;
|
||||
_cipherService = cipherService;
|
||||
_accessTokenService = accessTokenService;
|
||||
_claimsService = claimsService;
|
||||
_fileService = fileService;
|
||||
_serialNoGenerator = serialNoGenerator;
|
||||
}
|
||||
|
||||
private readonly ILogger<UserService> _logger;
|
||||
private readonly StopShoppingContext _dbContext;
|
||||
private readonly ICipherService _cipherService;
|
||||
private readonly IAccessTokenService _accessTokenService;
|
||||
private readonly IClaimsService _claimsService;
|
||||
private readonly IFileService _fileService;
|
||||
private readonly ISerialNoGenerator _serialNoGenerator;
|
||||
|
||||
public async Task<ApiResponse> SignUpAsync(SignUpParams model)
|
||||
{
|
||||
var user = await _dbContext.Users.FirstOrDefaultAsync(u => u.Account == model.Account);
|
||||
if (null != user)
|
||||
return ApiResponse.Failed("帐户名已存在");
|
||||
|
||||
user = new EF.Models.User
|
||||
{
|
||||
Account = model.Account!,
|
||||
Addresses = [],
|
||||
Avatar = Consts.DEFAULT_AVATAR,
|
||||
CurrentRole = model.DefaultRole.GetValue(),
|
||||
NickName = model.NickName!,
|
||||
Password = _cipherService.EncryptUserPassword(model.Password!)
|
||||
};
|
||||
|
||||
await _dbContext.AddAsync(user);
|
||||
if (await _dbContext.SaveChangesAsync() > 0)
|
||||
return ApiResponse.Succed();
|
||||
|
||||
return ApiResponse.Failed("数据库操作失败");
|
||||
}
|
||||
|
||||
public async Task<SignInResult<SignInUser>> SignInAsync(SignInParams model)
|
||||
{
|
||||
SignInResult<SignInUser> result = new();
|
||||
|
||||
var user = await _dbContext.Users
|
||||
.FirstOrDefaultAsync(u => u.Account == model.Account);
|
||||
if (null == user || user.Password != _cipherService.EncryptUserPassword(model.Password!))
|
||||
{
|
||||
result.IsSucced = false;
|
||||
result.Message = "账号或密码错误";
|
||||
|
||||
return result;
|
||||
}
|
||||
user.LastLoginTime = DateTime.Now;
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
var claimsIdentity = _claimsService.BuildIdentity(user);
|
||||
|
||||
result.RefreshToken = await _accessTokenService.SetRefreshTokenAsync(user.Id, SystemRoles.User);
|
||||
|
||||
result.User = new SignInUser
|
||||
{
|
||||
AvatarUrl = string.IsNullOrWhiteSpace(user.Avatar)
|
||||
? null
|
||||
: _fileService.GetFileUrl(UploadScences.Avatar, user.Avatar),
|
||||
NickName = user.NickName,
|
||||
DefaultRole = user.CurrentRole.ToUserRoles(),
|
||||
AccessToken = _accessTokenService.GenerateAccessToken(claimsIdentity)
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<SignInResult<SignInAdmin>> SignInAdminAsync(SignInParams model)
|
||||
{
|
||||
SignInResult<SignInAdmin> result = new();
|
||||
|
||||
var admin = await _dbContext.Administrators
|
||||
.FirstOrDefaultAsync(u => u.Account == model.Account);
|
||||
if (null == admin || admin.Password != _cipherService.EncryptUserPassword(model.Password!))
|
||||
{
|
||||
result.IsSucced = false;
|
||||
result.Message = "账号或密码错误";
|
||||
|
||||
return result;
|
||||
}
|
||||
admin.LastLoginTime = DateTime.Now;
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
var claimsIdentity = _claimsService.BuildAdminIdentity(admin);
|
||||
|
||||
result.RefreshToken = await _accessTokenService.SetRefreshTokenAsync(admin.Id, SystemRoles.Admin);
|
||||
|
||||
result.User = new SignInAdmin
|
||||
{
|
||||
NickName = admin.NickName,
|
||||
AccessToken = _accessTokenService.GenerateAccessToken(claimsIdentity)
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task GenerateDefaultAdminAsync()
|
||||
{
|
||||
var defaultAdmin = await _dbContext.Administrators
|
||||
.Where(a => a.Account == Consts.DEFAULT_ADMIN_ACCOUNT)
|
||||
.FirstOrDefaultAsync();
|
||||
if (null != defaultAdmin)
|
||||
{
|
||||
_logger.LogInformation("默认管理员已存在,已跳过");
|
||||
return;
|
||||
}
|
||||
|
||||
var pwd = _serialNoGenerator.GenerateRandomPassword();
|
||||
defaultAdmin = new()
|
||||
{
|
||||
Account = Consts.DEFAULT_ADMIN_ACCOUNT,
|
||||
NickName = "超级管理员",
|
||||
Password = _cipherService.EncryptUserPassword(pwd)
|
||||
};
|
||||
await _dbContext.Administrators.AddAsync(defaultAdmin);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation(
|
||||
"默认管理员({Account})已生成,请立马修改密码:{Password}",
|
||||
Consts.DEFAULT_ADMIN_ACCOUNT,
|
||||
pwd);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> ChangePasswordAsync(ChangePasswordParams model)
|
||||
{
|
||||
int userId = _claimsService.GetCurrentUserId();
|
||||
var user = await _dbContext.Users
|
||||
.FirstAsync(u => u.Id == userId);
|
||||
|
||||
if (_cipherService.EncryptUserPassword(model.OldPassword!) != user.Password)
|
||||
return ApiResponse.Failed("原密码错误");
|
||||
|
||||
user.Password = _cipherService.EncryptUserPassword(model.NewPassword!);
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<User>> GetUserInfoAsync()
|
||||
{
|
||||
var userId = _claimsService.GetCurrentUserId();
|
||||
var model = await _dbContext.Users
|
||||
.FirstAsync(u => u.Id == userId);
|
||||
|
||||
User user = new()
|
||||
{
|
||||
Account = model.Account,
|
||||
AvatarUrl = string.IsNullOrWhiteSpace(model.Avatar)
|
||||
? null
|
||||
: _fileService.GetFileUrl(UploadScences.Avatar, model.Avatar),
|
||||
DefaultRole = model.CurrentRole.ToUserRoles(),
|
||||
LastLoginTime = model.LastLoginTime?.ToFormatted(),
|
||||
NickName = model.NickName,
|
||||
Telephone = model.Telephone
|
||||
};
|
||||
|
||||
return new ApiResponse<User>(user);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> EditAsync(EditUserParams model)
|
||||
{
|
||||
int userId = _claimsService.GetCurrentUserId();
|
||||
var user = await _dbContext.Users.FirstAsync(u => u.Id == userId);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(model.AvatarFileName))
|
||||
user.Avatar = model.AvatarFileName;
|
||||
user.CurrentRole = model.DefaultRole.GetValue();
|
||||
if (!string.IsNullOrWhiteSpace(model.NickName))
|
||||
user.NickName = model.NickName;
|
||||
if (!string.IsNullOrWhiteSpace(model.Telephone))
|
||||
user.Telephone = model.Telephone;
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
|
||||
public ApiResponse<List<Address>> GetAddresses()
|
||||
{
|
||||
var userId = _claimsService.GetCurrentUserId();
|
||||
|
||||
var addresses = _dbContext.Addresses
|
||||
.Where(a => a.UserId == userId)
|
||||
.OrderByDescending(a => a.Default)
|
||||
.AsNoTracking()
|
||||
.Select(Cast)
|
||||
.ToList();
|
||||
|
||||
return new ApiResponse<List<Address>>(addresses);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> EditAddressAsync(EditAddressParams model)
|
||||
{
|
||||
EF.Models.Address? address = null;
|
||||
|
||||
var userId = _claimsService.GetCurrentUserId();
|
||||
|
||||
if (model.Id.HasValue && model.Id > 0)
|
||||
{
|
||||
address = await _dbContext.Addresses
|
||||
.FirstOrDefaultAsync(a => a.Id == model.Id && a.UserId == userId);
|
||||
if (null == address)
|
||||
return ApiResponse.Failed("地址已不存在");
|
||||
}
|
||||
else
|
||||
{
|
||||
address = new();
|
||||
await _dbContext.Addresses.AddAsync(address);
|
||||
}
|
||||
|
||||
address.Default = model.Default;
|
||||
address.Detail = model.Detail;
|
||||
address.DistrictLevel1Id = model.DistrictLevel1Id;
|
||||
address.DistrictLevel2Id = model.DistrictLevel2Id;
|
||||
address.DistrictLevel3Id = model.DistrictLevel3Id;
|
||||
address.DistrictLevel4Id = model.DistrictLevel4Id;
|
||||
address.Name = model.Name;
|
||||
address.Tag = model.Tag;
|
||||
address.Telephone = model.Telephone;
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> DeleteAddressAsync(int id)
|
||||
{
|
||||
var userId = _claimsService.GetCurrentUserId();
|
||||
await _dbContext.Addresses
|
||||
.Where(a => a.Id == id && a.UserId == userId)
|
||||
.ExecuteDeleteAsync();
|
||||
|
||||
return ApiResponse.Succed();
|
||||
}
|
||||
|
||||
#region private methods
|
||||
|
||||
private Address Cast(EF.Models.Address a)
|
||||
{
|
||||
return new Address
|
||||
{
|
||||
Default = a.Default,
|
||||
Detail = a.Detail,
|
||||
DistrictLevel1Id = a.DistrictLevel1Id,
|
||||
DistrictLevel2Id = a.DistrictLevel2Id,
|
||||
DistrictLevel3Id = a.DistrictLevel3Id,
|
||||
DistrictLevel4Id = a.DistrictLevel4Id,
|
||||
Id = a.Id,
|
||||
Name = a.Name,
|
||||
Tag = a.Tag,
|
||||
Telephone = a.Telephone
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user