This commit is contained in:
2026-03-25 14:55:34 +08:00
commit 2c44b3a4b2
131 changed files with 7453 additions and 0 deletions

9
.editorconfig Normal file
View File

@@ -0,0 +1,9 @@
root = true
[*.cs]
# switch expression必须覆盖所有成员
dotnet_diagnostic.IDE0072.severity = error
dotnet_diagnostic.IDE0010.severity = error
# 非必要的using
dotnet_diagnostic.IDE0005.severity = warning

487
.gitignore vendored Normal file
View File

@@ -0,0 +1,487 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from `dotnet new gitignore`
# dotenv files
.env
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET
project.lock.json
project.fragment.lock.json
artifacts/
# Tye
.tye/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
# but not Directory.Build.rsp, as it configures directory-level build defaults
!Directory.Build.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.tlog
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
*.vbp
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
*.dsw
*.dsp
# Visual Studio 6 technical files
*.ncb
*.aps
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# Visual Studio History (VSHistory) files
.vshistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd
# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
# Local History for Visual Studio Code
.history/
# Windows Installer files from build outputs
*.cab
*.msi
*.msix
*.msm
*.msp
# JetBrains Rider
*.sln.iml
.idea/
##
## Visual studio for Mac
##
# globs
Makefile.in
*.userprefs
*.usertasks
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.tar.gz
tarballs/
test-results/
# content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# Vim temporary swap files
*.swp
doc/password.txt
StopShopping.Api/wwwroot/images/
appsettings.json
appsettings.Development.json

35
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,35 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md.
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/StopShopping.Api/bin/Debug/net10.0/StopShopping.Api.dll",
"args": [],
"cwd": "${workspaceFolder}/StopShopping.Api",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}

41
.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/StopShopping.Api/StopShopping.Api.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/StopShopping.Api/StopShopping.Api.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/StopShopping.Api/StopShopping.Api.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

9
Directory.Build.props Normal file
View File

@@ -0,0 +1,9 @@
<Project>
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>CS1591;CA1707;CA1304;CA1848;CA1305;CA1311;CA1822</NoWarn>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>Recommended</AnalysisMode>
</PropertyGroup>
</Project>

60
README.md Normal file
View File

@@ -0,0 +1,60 @@
# 简介
**停止购物**不同于传统由商户上架商品消费者进行下单的购物模式,提供相反的由消费者发布需求商户竞标的商品、服务交易的方式。
## 系统角色
- 管理员
后台管理员,维护系统的基础信息。
- 用户
用户同时是买家和卖家,用户可以在客户端设置偏好的系统功能(买家/卖家)。
## 业务流程
卖家:上架商品➡️寻找需求➡️推荐商品➡️⬇️➡️➡️发货➡️⬇️
买家:发布需求➡️➡️➡️➡️➡️➡️➡️➡️➡️️️️️下单付款➡️⬆️➡️➡️收货
## 系统功能(用户端)
- **通用功能**
+ [x] 注册
账号、密码、默认角色(买家、卖家)
+ [x] 登录
+ [x] 退出登录
+ 个人中心
* [x] 昵称、头像等维护
* [x] 收货地址维护
* [x] 角色切换(买家版、卖家版)
- **作为卖家时:**
+ 上架商品
* [x] 商品基本信息维护
* [x] 商品详情维护
+ 浏览需求
* [ ] 分类检索
* [ ] 关键词检索
* [ ] 地区检索
* [ ] 排序展示(发布时间、距离、竞标者数量)
+ 竞标
* [ ] 详情页展示(买家信息、需求描述、竞标者列表)
* [ ] 竞标,选择商品
+ 订单管理
* [ ] 竞标中:查看详情
* [ ] 已中标:查看详情、发货、在线沟通
* [ ] 已发货:查看详情、物流
* [ ] 已完成(买家已收货):查看详情、评价买家
* [ ] 已关闭(未中标):查看详情、删除
- **作为买家时:**
+ 发布需求
* [ ] 发布页:分类、简短描述、详细描述、预算
+ 订单管理
* [ ] 已发布:查看详情、修改详情
* [ ] 有竞标:查看详情、查看竞标详情、接受
* [ ] 待发货:查看详情、留言、在线沟通
* [ ] 已发货:查看详情、物流、确认收货
* [ ] 已完成:评价卖家
## 系统功能(后台端)
- **首页**
+ [ ] 交易数据可视化
- **基础信息**
+ [x] 商品分类维护
+ [x] 行政区划管理
- **用户管理**
+ [ ] 群发公告

View File

@@ -0,0 +1,56 @@
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Net.Http.Headers;
using StopShopping.Services;
namespace StopShopping.Api.Extensions;
public static class JwtExtensions
{
public static IServiceCollection AddAuthServices(this IServiceCollection services, IConfiguration jwtOptions)
{
services.Configure<JwtOptions>(jwtOptions);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(jwtBearerOptions =>
{
var jwtConfiguration = jwtOptions.Get<JwtOptions>()!;
var signingKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(jwtConfiguration.SigningKey!)
);
jwtBearerOptions.MapInboundClaims = false;
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
{
ValidAudience = jwtConfiguration.ValidAudience,
ValidIssuer = jwtConfiguration.ValidIssuer,
IssuerSigningKey = signingKey,
ClockSkew = TimeSpan.FromSeconds(30) //宽容时间30秒后才失效
};
jwtBearerOptions.Events = new JwtBearerEvents
{
OnMessageReceived = async (context) =>
{
var accessTokenService = context.HttpContext.RequestServices.GetRequiredService<IAccessTokenService>();
var authorizationHeader = context.Request.Headers[HeaderNames.Authorization];
if (authorizationHeader.Count == 0)
{
context.Fail($"未找到{HeaderNames.Authorization}请求头");
}
else
{
var token = authorizationHeader.First()!.Split(" ").Last();
if (string.IsNullOrWhiteSpace(token))
context.Fail("未找到token");
if (await accessTokenService.IsAccessTokenBlacklistAsync(token))
context.Fail("token已失效");
}
}
};
});
services.AddAuthorization();
return services;
}
}

View File

@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi;
namespace StopShopping.Api.Extensions;
// 来自网络。。。
public class BearerOpenApiDocumentTransformer : IOpenApiDocumentTransformer
{
public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
{
var bearerOpenApiSecurityScheme = new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Scheme = JwtBearerDefaults.AuthenticationScheme,
Type = SecuritySchemeType.Http,
Description = "jwt"
};
document.Components ??= new OpenApiComponents();
document.Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>();
document.Components.SecuritySchemes[JwtBearerDefaults.AuthenticationScheme] = bearerOpenApiSecurityScheme;
var securityRequirement = new OpenApiSecurityRequirement
{
{
new OpenApiSecuritySchemeReference(JwtBearerDefaults.AuthenticationScheme),
new List<string>()
}
};
document.Security ??= [];
document.Security.Add(securityRequirement);
return Task.CompletedTask;
}
}

View File

@@ -0,0 +1,79 @@
using System.Text.Json.Serialization;
using StopShopping.Api.Middlewares;
using StopShopping.Services;
using StopShopping.Services.Extensions;
namespace StopShopping.Api.Extensions;
public static class CommonServiceCollections
{
public static IServiceCollection AddCommonServices(
this IServiceCollection services,
string corsPolicy,
IConfigurationSection jwtConfiguration,
IConfigurationSection appConfiguration,
bool isDevelopment)
{
var appOptions = appConfiguration.Get<AppOptions>();
services.AddCors(options =>
{
options.AddPolicy(corsPolicy, policy =>
{
policy.AllowAnyHeader();
policy.AllowAnyMethod();
policy.WithOrigins(appOptions!.CorsAllowedOrigins);
policy.AllowCredentials();
});
});
services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.Converters.Add(
new JsonStringEnumConverter(namingPolicy: null, allowIntegerValues: true));
});
services.AddHttpContextAccessor();
services.AddOpenApi(options =>
{
options.AddDocumentTransformer<BearerOpenApiDocumentTransformer>();
options.AddSchemaTransformer<EnumOpenApiSchemaTransformer>();
});
services.AddProblemDetails(options =>
{
options.CustomizeProblemDetails = (context) =>
{
if (context.ProblemDetails is HttpValidationProblemDetails problemDetails)
{
problemDetails.AddErrorCode(ProblemDetailsCodes.ParametersValidationFailed);
var errors = problemDetails.Errors.Select(e => string.Join(',', e.Value));
if (null != errors)
problemDetails.Detail = string.Join(',', errors);
}
};
});
services.AddValidation();
services.AddDistributedMemoryCache();
services.AddAuthServices(jwtConfiguration);
services.AddAntiforgery(options =>
{
var jwtOptions = jwtConfiguration.Get<JwtOptions>();
options.HeaderName = appOptions!.CSRFHeaderName;
options.Cookie.MaxAge = TimeSpan.FromSeconds(jwtOptions!.RefreshTokenExpiresIn);
options.Cookie.HttpOnly = true;
options.Cookie.Name = appOptions.CSRFCookieName;
if (isDevelopment)
{
options.Cookie.SameSite = SameSiteMode.Lax;
}
else
{
options.Cookie.SameSite = SameSiteMode.None;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.Cookie.Domain = appOptions.CookieDomain;
}
});
return services;
}
}

View File

@@ -0,0 +1,49 @@
using System.ComponentModel;
using System.Text.Json.Nodes;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi;
namespace StopShopping.Api.Extensions;
/// <summary>
/// 处理enum类型openapi显示
/// </summary>
public class EnumOpenApiSchemaTransformer : IOpenApiSchemaTransformer
{
public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, CancellationToken cancellationToken)
{
if (context.JsonTypeInfo.Type.IsEnum)
{
schema.Type = JsonSchemaType.Integer;
var enumValues = Enum.GetValues(context.JsonTypeInfo.Type)
.Cast<object>()
.Select(v => JsonNode.Parse(Convert.ToInt32(v).ToString())!)
.ToList();
schema.Enum = enumValues;
var enumNames = Enum.GetNames(context.JsonTypeInfo.Type);
schema.Extensions ??= new Dictionary<string, IOpenApiExtension>();
var namesExtension = new JsonNodeExtension(new JsonArray(
enumNames
.Select(n => (JsonNode)n)
.ToArray()));
schema.Extensions.Add("x-enumNames", namesExtension);
var descMap = new JsonObject();
foreach (var name in enumNames)
{
if (context.JsonTypeInfo.Type.GetField(name)
?.GetCustomAttributes(typeof(DescriptionAttribute), false)
.FirstOrDefault() is DescriptionAttribute attr)
{
descMap[name] = attr.Description;
}
}
schema.Extensions.Add("x-enumDescriptions", new JsonNodeExtension(descMap));
}
return Task.CompletedTask;
}
}

View File

@@ -0,0 +1,36 @@
using StopShopping.Services.Extensions;
namespace Microsoft.AspNetCore.Http;
public static class HttpExtensions
{
public const string REFRESH_TOKEN_COOKIE_KEY = "refresh_token";
public static IResponseCookies AppendRefreshToken(
this IResponseCookies cookies,
IWebHostEnvironment env,
AppOptions appOptions,
TimeSpan maxAge,
string token)
{
CookieOptions options = new()
{
MaxAge = maxAge,
HttpOnly = true,
SameSite = SameSiteMode.Lax,
};
if (!env.IsDevelopment())
{
options.SameSite = SameSiteMode.None;
options.Secure = true;
options.Domain = appOptions.CookieDomain;
}
cookies.Append(
REFRESH_TOKEN_COOKIE_KEY,
token,
options);
return cookies;
}
}

View File

@@ -0,0 +1,25 @@
using StopShopping.Api.Middlewares;
namespace Microsoft.AspNetCore.Builder;
public static class MiddlewareExtensions
{
public static IApplicationBuilder UseGlobalExceptionHandler(this IApplicationBuilder applicationBuilder)
{
applicationBuilder.UseMiddleware<GlobalExceptionHandlerMiddleware>();
return applicationBuilder;
}
/// <summary>
/// 解决开发时多客户端localhost端口串cookie的问题
/// </summary>
/// <param name="applicationBuilder"></param>
/// <returns></returns>
public static IApplicationBuilder UseDevelopmentCookie(this IApplicationBuilder applicationBuilder)
{
applicationBuilder.UseMiddleware<DevelopmentCookieMiddleware>();
return applicationBuilder;
}
}

View File

@@ -0,0 +1,142 @@
using System.Text;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using StopShopping.Services.Extensions;
namespace StopShopping.Api.Middlewares;
public class DevelopmentCookieMiddleware
{
public DevelopmentCookieMiddleware(
RequestDelegate requestDelegate,
IOptions<AppOptions> options)
{
_next = requestDelegate;
_appOptions = options.Value;
}
private readonly RequestDelegate _next;
private readonly AppOptions _appOptions;
public async Task InvokeAsync(HttpContext httpContext)
{
int? port = null;
var origin = httpContext.Request.Headers[HeaderNames.Origin];
if (origin.Count > 0 && null != origin[0])
{
Uri uri = new(origin[0]!);
port = uri.Port;
}
else
{
var referer = httpContext.Request.Headers[HeaderNames.Referer];
if (referer.Count > 0 && null != referer[0])
{
Uri uri = new(referer[0]!);
port = uri.Port;
}
}
if (port.HasValue)
{
var modified = ModifyCookie(httpContext.Request.Headers[HeaderNames.Cookie], port.Value);
httpContext.Request.Headers[HeaderNames.Cookie] = modified;
}
httpContext.Response.OnStarting(() =>
{
if (port.HasValue)
{
var cookieHeader = httpContext.Response.Headers[HeaderNames.SetCookie];
ModifyResponseCookie(cookieHeader, httpContext, port.Value);
}
return Task.CompletedTask;
});
await _next(httpContext);
}
private void ModifyResponseCookie(StringValues cookieHeader, HttpContext httpContext, int port)
{
foreach (var cookieItem in cookieHeader)
{
if (cookieItem != null)
{
var cookies = cookieItem.Split(';');
foreach (var item in cookies)
{
if (null != item)
{
var pairs = item.Split('=');
if (2 == pairs.Length)
{
if (pairs[0].Trim() == _appOptions.CSRFCookieName)
{
var val = pairs[1];
httpContext.Response.Cookies.Delete(pairs[0]);
httpContext.Response.Cookies.Append($"{_appOptions.CSRFCookieName}_{port}", val);
}
else if (pairs[0].Trim() == HttpExtensions.REFRESH_TOKEN_COOKIE_KEY)
{
var val = pairs[1];
httpContext.Response.Cookies.Delete(pairs[0]);
httpContext.Response.Cookies.Append($"{HttpExtensions.REFRESH_TOKEN_COOKIE_KEY}_{port}", val);
}
}
}
}
}
}
}
private StringValues ModifyCookie(StringValues cookie, int port)
{
List<string> result = [];
var csrfCookieName = $"{_appOptions.CSRFCookieName}_{port}";
var refreshTokenCookieName = $"{HttpExtensions.REFRESH_TOKEN_COOKIE_KEY}_{port}";
foreach (var cookieItem in cookie)
{
if (null != cookieItem)
{
StringBuilder itemBuilder = new();
var cookies = cookieItem.Split(';');
foreach (var item in cookies)
{
if (null != item)
{
var pairs = item.Split('=');
if (pairs.Length == 2)
{
if (0 != itemBuilder.Length)
itemBuilder.Append(';');
if (pairs[0].Trim() == csrfCookieName)
{
itemBuilder.Append(_appOptions.CSRFCookieName);
itemBuilder.Append('=');
itemBuilder.Append(pairs[1].Trim());
}
else if (pairs[0].Trim() == refreshTokenCookieName)
{
itemBuilder.Append(HttpExtensions.REFRESH_TOKEN_COOKIE_KEY);
itemBuilder.Append('=');
itemBuilder.Append(pairs[1].Trim());
}
else
{
itemBuilder.Append(item);
}
}
else
{
itemBuilder.Append(item);
}
}
}
result.Add(itemBuilder.ToString());
}
}
return new StringValues(result.ToArray());
}
}

View File

@@ -0,0 +1,100 @@
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Mvc;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Middlewares;
public class GlobalExceptionHandlerMiddleware
{
public GlobalExceptionHandlerMiddleware(RequestDelegate requestDelegate,
ILogger<GlobalExceptionHandlerMiddleware> logger,
IProblemDetailsService problemDetailsService)
{
_next = requestDelegate;
_logger = logger;
_problemDetailsService = problemDetailsService;
}
private readonly RequestDelegate _next;
private readonly ILogger<GlobalExceptionHandlerMiddleware> _logger;
private readonly IProblemDetailsService _problemDetailsService;
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
await _next(httpContext);
}
catch (BadHttpRequestException ex) when (ex.InnerException is AntiforgeryValidationException)
{
var problemDetails = new ProblemDetails
{
Detail = ex.InnerException.Message,
Instance = httpContext.Request.Path,
Status = StatusCodes.Status400BadRequest,
Title = "CSRF 错误",
};
problemDetails.AddErrorCode(ProblemDetailsCodes.CsrfValidationFailed);
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
httpContext.Response.ContentType = "application/problem+json";
await _problemDetailsService.WriteAsync(new ProblemDetailsContext
{
HttpContext = httpContext,
ProblemDetails = problemDetails
});
}
catch (BadHttpRequestException ex)
{
_logger.LogError(ex, "参数错误");
var problemDetails = new ProblemDetails
{
Detail = ex.Message,
Instance = httpContext.Request.Path,
Status = StatusCodes.Status400BadRequest,
Title = "参数错误",
};
problemDetails.AddErrorCode(ProblemDetailsCodes.BadParameters);
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
httpContext.Response.ContentType = "application/problem+json";
await _problemDetailsService.WriteAsync(new ProblemDetailsContext
{
HttpContext = httpContext,
ProblemDetails = problemDetails
});
}
catch (ServiceException ex)//业务层抛出提示
{
await httpContext.Response.WriteAsJsonAsync(ApiResponse.Failed(ex.Message));
}
catch (Exception ex)
{
_logger.LogCritical(ex, "意外的异常");
var problemDetails = new ProblemDetails
{
Detail = ex.Message,
Instance = httpContext.Request.Path,
Status = StatusCodes.Status500InternalServerError,
Title = "服务器错误",
};
problemDetails.AddErrorCode(ProblemDetailsCodes.ServerError);
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
httpContext.Response.ContentType = "application/problem+json";
await _problemDetailsService.WriteAsync(new ProblemDetailsContext
{
HttpContext = httpContext,
ProblemDetails = problemDetails,
});
}
}
}

View File

@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Mvc;
namespace StopShopping.Api.Middlewares;
public static class ProblemDetailsExtensions
{
private const string CODE_FIELD = "code";
public static ProblemDetails AddErrorCode(this ProblemDetails problemDetails, ProblemDetailsCodes code)
{
problemDetails.Extensions ??= new Dictionary<string, object?>();
problemDetails.Extensions.Add(CODE_FIELD, (int)code);
return problemDetails;
}
}
public enum ProblemDetailsCodes
{
CsrfValidationFailed = 1000,
ParametersValidationFailed = 1001,
BadParameters = 1002,
ServerError = 1003,
}

View File

@@ -0,0 +1,84 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Scalar.AspNetCore;
using Serilog;
using StopShopping.Api.Extensions;
using StopShopping.Api.Routes;
using StopShopping.Api.Workers;
const string CORS_POLICY = "default";
// 将启动日志写入控制台用于捕获启动时异常启动后WriteTo被后续配置替代
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
try
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSerilog((services, seriLogger) =>
{
seriLogger.ReadFrom.Configuration(builder.Configuration)
.ReadFrom.Services(services);
});
var jwtConfiguration = builder.Configuration.GetSection("JwtOptions");
var appConfiguration = builder.Configuration.GetSection("AppOptions");
builder.Services.AddCommonServices(
CORS_POLICY,
jwtConfiguration,
appConfiguration,
builder.Environment.IsDevelopment());
builder.Services.AddServices(dbContextOptions =>
{
dbContextOptions.UseNpgsql(
builder.Configuration.GetConnectionString("StopShopping"));
if (builder.Environment.IsDevelopment())
dbContextOptions.EnableSensitiveDataLogging();
},
appConfiguration,
builder.Configuration.GetSection("OpenPlatformOptions"));
builder.Services.AddHostedService<DbSeederBackgroundService>();
/**********************************************************************/
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDevelopmentCookie();
app.MapOpenApi();
app.MapScalarApiReference(options =>
{
options.AddPreferredSecuritySchemes(JwtBearerDefaults.AuthenticationScheme);
});
}
app.UseGlobalExceptionHandler();
app.UseRouting();
app.UseCors(CORS_POLICY);
app.UseAuthentication();
app.UseAuthorization();
app.MapStaticAssets().ShortCircuit();
Root.MapRoutes(app);
app.UseAntiforgery();
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "启动异常!");
}
finally
{
Log.CloseAndFlush();
}

View File

@@ -0,0 +1,25 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "scalar",
"applicationUrl": "http://localhost:5162",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "scalar",
"applicationUrl": "https://localhost:7121;http://localhost:5162",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,44 @@
using Microsoft.Extensions.Options;
using StopShopping.Services;
using StopShopping.Services.Extensions;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class Admin
{
public static RouteGroupBuilder MapAdmin(this RouteGroupBuilder routes)
{
routes.MapPost("/admin/signin", SignInAsync)
.AllowAnonymous().WithTags(OpenApiTags..ToString());
return routes;
}
private static async Task<ApiResponse<SignInAdmin>> SignInAsync(
SignInParams model,
IUserService userService,
HttpContext httpContext,
IWebHostEnvironment env,
IOptions<AppOptions> options)
{
var result = await userService.SignInAdminAsync(model);
var resp = new ApiResponse<SignInAdmin>
{
IsSucced = result.IsSucced,
Data = result.User,
Message = result.Message
};
if (result.IsSucced)
{
httpContext.Response.Cookies.AppendRefreshToken(
env,
options.Value,
TimeSpan.FromSeconds(result.RefreshToken!.ExpiresIn),
result.RefreshToken.Token!
);
}
return resp;
}
}

View File

@@ -0,0 +1,60 @@
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class Category
{
public static RouteGroupBuilder MapCategoryCommon(this RouteGroupBuilder routes)
{
routes.MapGet("/category/list", GetTree)
.WithTags(OpenApiTags..ToString())
.AllowAnonymous();
return routes;
}
public static RouteGroupBuilder MapCategory(this RouteGroupBuilder routes)
{
routes.MapPost("/category/edit", EditCategoryAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/category/resort", ResortCategoryAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/category/delete", DeleteCategoryAsync)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static ApiResponse<List<Services.Models.Resp.Category>> GetTree(
ICategoryService categoryService
)
{
return categoryService.GetCategoriesTree();
}
private static async Task<ApiResponse<Services.Models.Resp.Category>> EditCategoryAsync(
EditCategoryParams model,
ICategoryService categoryService)
{
return await categoryService.EditCategoryAsync(model);
}
private static async Task<ApiResponse> ResortCategoryAsync(
ResortCategoryParams model,
ICategoryService categoryService)
{
return await categoryService.ResortCategoryAsync(model);
}
private static async Task<ApiResponse> DeleteCategoryAsync(
CategoryIdParams model,
ICategoryService categoryService
)
{
return await categoryService.DeleteCategoryAsync(model);
}
}

View File

@@ -0,0 +1,87 @@
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class Common
{
public static RouteGroupBuilder MapCommon(this RouteGroupBuilder routes)
{
routes.MapPost("/common/upload", UploadAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/common/refreshtoken", RefreshTokenAsync)
.AllowAnonymous()
.Produces<ApiResponse<AccessToken>>()
.WithTags(OpenApiTags..ToString());
routes.MapPost("/common/signout", SignOutAsync)
.AllowAnonymous().WithTags(OpenApiTags..ToString());
routes.MapPost("/common/antiforgery-token", AntiForgeryToken)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static async Task<ApiResponse<FileUpload>> UploadAsync(
[FromForm] UploadParams payload,
IFileService fileService,
HttpContext httpContext)
{
return await fileService.UploadFileAsync(payload);
}
private static ApiResponse<AntiForgeryToken> AntiForgeryToken(
HttpContext httpContext,
IAntiforgery antiforgery)
{
var antiforgeryToken = antiforgery.GetAndStoreTokens(httpContext);
return new ApiResponse<AntiForgeryToken>(new AntiForgeryToken
{
Token = antiforgeryToken.RequestToken,
HeaderName = antiforgeryToken.HeaderName
});
}
private static async Task<IResult> RefreshTokenAsync(
HttpContext httpContext,
IAccessTokenService accessTokenService)
{
var refreshToken = httpContext.Request.Cookies[HttpExtensions.REFRESH_TOKEN_COOKIE_KEY];
if (string.IsNullOrWhiteSpace(refreshToken))
return Results.Unauthorized();
var accessToken = await accessTokenService.GenerateAccessTokenAsync(refreshToken);
if (null == accessToken)
return Results.Unauthorized();
return Results.Ok(new ApiResponse<AccessToken>(accessToken));
}
public static async Task<ApiResponse> SignOutAsync(
HttpContext httpContext,
IAccessTokenService accessTokenService)
{
var accessTokenHeader = httpContext.Request.Headers[HeaderNames.Authorization];
if (accessTokenHeader.Count != 0)
{
var accessToken = accessTokenHeader.First()!.Split(" ").Last();
if (!string.IsNullOrWhiteSpace(accessToken))
await accessTokenService.AddAccessTokenBlacklistAsync(accessToken);
}
var refreshToken = httpContext.Request.Cookies[HttpExtensions.REFRESH_TOKEN_COOKIE_KEY];
if (!string.IsNullOrWhiteSpace(refreshToken))
{
await accessTokenService.RevokeRefreshTokenAsync(refreshToken);
httpContext.Response.Cookies.Delete(HttpExtensions.REFRESH_TOKEN_COOKIE_KEY);
}
return ApiResponse.Succed();
}
}

View File

@@ -0,0 +1,34 @@
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class District
{
public static RouteGroupBuilder MapDistrict(this RouteGroupBuilder routes)
{
routes.MapGet("/district/top3level", GetTop3LevelDistrictsAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/district/children", GetChildrenDistricts)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static ApiResponse<List<Services.Models.Resp.District>> GetChildrenDistricts(
[AsParameters] DistrictParentIdParams model,
IDistrictService districtService
)
{
return districtService.GetChildren(model);
}
private static async Task<ApiResponse<List<Services.Models.Resp.District>>> GetTop3LevelDistrictsAsync(
IDistrictService districtService
)
{
return await districtService.GetTop3LevelDistrictsAsync();
}
}

View File

@@ -0,0 +1,61 @@
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
/// <summary>
/// 商品相关路由
/// </summary>
public static class Product
{
public static RouteGroupBuilder MapProduct(this RouteGroupBuilder routes)
{
routes.MapGet("/product/list", SearchProductsAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/product/detail", Detail)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/product/edit", EditAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/product/delete", DeleteAsync)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static async
Task<ApiResponse<PagedResult<Services.Models.Resp.Product>>>
SearchProductsAsync(
[AsParameters] ProductSearchParms model,
IProductService productService
)
{
return await productService.SearchAsync(model);
}
private static ApiResponse<ProductInfo> Detail(
[AsParameters] ProductIdParams model,
IProductService productService)
{
return productService.Detail(model);
}
private static async Task<ApiResponse> EditAsync(
EditProductParams model,
IProductService productService
)
{
return await productService.EditAsync(model);
}
private static async Task<ApiResponse> DeleteAsync(
ProductIdParams model,
IProductService productService
)
{
return await productService.DeleteAsync(model);
}
}

View File

@@ -0,0 +1,45 @@
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class Reply
{
public static RouteGroupBuilder MapReply(this RouteGroupBuilder routes)
{
routes.MapPost("/reply/post", SubmitReplyAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/reply/list", ListAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/reply/orders", OrderSearchAsync)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static async Task<ApiResponse> SubmitReplyAsync(
ReplyParams model,
IReplyService replyService)
{
return await replyService.ReplyAsync(model);
}
private static async Task<ApiResponse<List<Services.Models.Resp.Reply>>> ListAsync(
[AsParameters] RequestIdParams model,
IReplyService replyService
)
{
return await replyService.GetRepliesAsync(model);
}
private static async Task<ApiResponse<PagedResult<Services.Models.Resp.Request>>> OrderSearchAsync(
[AsParameters] RequestSearchWithStatusParams model,
IRequestService requestService
)
{
return await requestService.RequestOrderSearchAsync(model);
}
}

View File

@@ -0,0 +1,57 @@
using StopShopping.Services;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class Request
{
public static RouteGroupBuilder MapRequest(this RouteGroupBuilder routes)
{
routes.MapPost("/request/publish", PublishRequestAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/request/search", SearchAsync)
.WithTags(OpenApiTags..ToString())
.AllowAnonymous();
routes.MapGet("/request/orders", OrderSearchAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/request/delete", DeleteRequestAsync)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static async Task<ApiResponse> PublishRequestAsync(
CreateRequestParams model,
IRequestService requestService)
{
return await requestService.PublishRequestAsync(model);
}
private static async Task<ApiResponse<PagedResult<Services.Models.Resp.Request>>> SearchAsync(
[AsParameters] RequestSearchParams model,
IRequestService requestService
)
{
return await requestService.SearchAsync(model);
}
private static async Task<ApiResponse<PagedResult<Services.Models.Resp.Request>>> OrderSearchAsync(
[AsParameters] RequestSearchWithStatusParams model,
IRequestService requestService
)
{
return await requestService.RequestOrderSearchAsync(model);
}
private static async Task<ApiResponse> DeleteRequestAsync(
RequestIdParams model,
IRequestService requestService
)
{
return await requestService.DeleteRequestAsync(model);
}
}

View File

@@ -0,0 +1,45 @@
using Scalar.AspNetCore;
using StopShopping.Services.Models;
namespace StopShopping.Api.Routes;
/// <summary>
/// 其他路由从RouteGroupBuilder扩展并添加到MapGroup之后
/// </summary>
public static class Root
{
public static void MapRoutes(WebApplication app)
{
app.MapGroup("")
.MapUser()
.MapProduct()
.MapRequest()
.MapReply()
.MapDistrict()
.WithDescription("用户端调用")
.RequireAuthorization(policy => policy.RequireRole(SystemRoles.User.ToString()));
app.MapGroup("")
.MapCommon()
.MapCategoryCommon()
.WithDescription("公共调用")
.RequireAuthorization();
app.MapGroup("")
.MapAdmin()
.MapCategory()
.WithDescription("管理端调用")
.RequireAuthorization(policy => policy.RequireRole(SystemRoles.Admin.ToString()));
}
}
public enum OpenApiTags
{
,
,
,
,
,
,
,
,
}

View File

@@ -0,0 +1,95 @@
using Microsoft.Extensions.Options;
using StopShopping.Services;
using StopShopping.Services.Extensions;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Api.Routes;
public static class User
{
public static RouteGroupBuilder MapUser(this RouteGroupBuilder routes)
{
routes.MapPost("/user/signup", SignUpAsync)
.AllowAnonymous().WithTags(OpenApiTags..ToString());
routes.MapPost("/user/signin", SignInAsync)
.AllowAnonymous().WithTags(OpenApiTags..ToString());
routes.MapPost("/user/changepassword", ChangePasswordAsync)
.WithTags(OpenApiTags..ToString());
routes.MapGet("/user/info", GetUserAsync)
.WithTags(OpenApiTags..ToString());
routes.MapPost("/user/edit", EditUserAsync)
.WithTags(OpenApiTags..ToString());
return routes;
}
private static async Task<ApiResponse> SignUpAsync(
SignUpParams model,
IUserService userService)
{
await userService.SignUpAsync(model);
return ApiResponse.Succed();
}
private static async Task<ApiResponse<SignInUser>> SignInAsync(
SignInParams model,
IUserService userService,
HttpContext httpContext,
IWebHostEnvironment env,
IOptions<AppOptions> options)
{
var result = await userService.SignInAsync(model);
var resp = new ApiResponse<SignInUser>
{
IsSucced = result.IsSucced,
Data = result.User,
Message = result.Message
};
if (result.IsSucced)
{
httpContext.Response.Cookies.AppendRefreshToken(
env,
options.Value,
TimeSpan.FromSeconds(result.RefreshToken!.ExpiresIn),
result.RefreshToken.Token!
);
}
return resp;
}
private static async Task<ApiResponse> ChangePasswordAsync(
ChangePasswordParams model,
IUserService userService,
HttpContext httpContext,
IAccessTokenService accessTokenService
)
{
var resp = await userService.ChangePasswordAsync(model);
if (resp.IsSucced)
await Common.SignOutAsync(httpContext, accessTokenService);
return resp;
}
private static async Task<ApiResponse<Services.Models.Resp.User>> GetUserAsync(
IUserService userService
)
{
return await userService.GetUserInfoAsync();
}
private static async Task<ApiResponse> EditUserAsync(
EditUserParams model,
IUserService userService
)
{
return await userService.EditAsync(model);
}
}

View File

@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.3" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.1" />
<PackageReference Include="Scalar.AspNetCore" Version="2.13.5" />
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StopShopping.Services\StopShopping.Services.csproj" />
</ItemGroup>
<ItemGroup>
<Content Update="wwwroot\images\**\*">
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</Content>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,27 @@
using StopShopping.Services;
namespace StopShopping.Api.Workers;
public class DbSeederBackgroundService : BackgroundService
{
public DbSeederBackgroundService(IServiceProvider sp)
{
_sp = sp;
}
private readonly IServiceProvider _sp;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var scope = _sp.CreateScope();
using var scope1 = _sp.CreateScope();
var districtService = scope.ServiceProvider.GetRequiredService<IDistrictService>();
var userService = scope1.ServiceProvider.GetRequiredService<IUserService>();
var districtTask = districtService.InitialDatabaseAsync(stoppingToken);
var adminTask = userService.GenerateDefaultAdminAsync();
await Task.WhenAll(districtTask, adminTask);
}
}

View File

@@ -0,0 +1,36 @@
{
"ConnectionStrings": {
"StopShopping": "PG_CONNECTION_STRING"
},
"JwtOptions": {
"ValidAudience": "StopShopping.Client",
"ValidIssuer": "StopShopping.Api",
"SigningKey": "128_BIT_SIGNING_KEY",
"AccessTokenExpiresIn": "3600",
"RefreshTokenExpiresIn": "604800"
},
"AppOptions": {
"CookieDomain": ".example.com或者localhost开发环境",
"DomainPath": "https://example.com或者http://localhost开发环境",
"CSRFHeaderName": "X-CSRF-TOKEN",
"CSRFCookieName": "csrf_token",
"CorsAllowedOrigins": ["https://web.example.com跨域"]
},
"OpenPlatformOptions": {
"TencentDistrict": {
"SecretKey": "TENCENT_API_KEY",
"Top3LevelUrl": "https://apis.map.qq.com/ws/district/v1/list",
"GetChildrenUrl": "https://apis.map.qq.com/ws/district/v1/getchildren"
}
},
"Serilog": {
"Using": ["Serilog.Sinks.Console"],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.AspNetCore": "Warning"
}
},
"WriteTo": [{ "Name": "Console" }]
}
}

View File

@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 收货地址
/// </summary>
public partial class Address
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 用户表id
/// </summary>
public int UserId { get; set; }
/// <summary>
/// 收货人地址
/// </summary>
public string Name { get; set; } = null!;
/// <summary>
/// 联系电话
/// </summary>
public string Telephone { get; set; } = null!;
/// <summary>
/// 自定义标签:学校、家等
/// </summary>
public string? Tag { get; set; }
/// <summary>
/// 是否默认地址
/// </summary>
public bool Default { get; set; }
/// <summary>
/// 行政区域id表示省/直辖市
/// </summary>
public int DistrictLevel1Id { get; set; }
/// <summary>
/// 行政区域id表示市/直辖市为空
/// </summary>
public int? DistrictLevel2Id { get; set; }
/// <summary>
/// 行政区域id表示区
/// </summary>
public int DistrictLevel3Id { get; set; }
/// <summary>
/// 详细地址
/// </summary>
public string? Detail { get; set; }
/// <summary>
/// 行政区域id表示街道/镇
/// </summary>
public int DistrictLevel4Id { get; set; }
public virtual User User { get; set; } = null!;
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 管理员
/// </summary>
public partial class Administrator
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 登录账号
/// </summary>
public string Account { get; set; } = null!;
/// <summary>
/// 昵称
/// </summary>
public string NickName { get; set; } = null!;
/// <summary>
/// 登录密码(已加密)
/// </summary>
public string Password { get; set; } = null!;
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreateTime { get; set; }
/// <summary>
/// 最后登录时间
/// </summary>
public DateTime LastLoginTime { get; set; }
}

View File

@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 商品分类
/// </summary>
public partial class Category
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 父级id顶级为0
/// </summary>
public int ParentId { get; set; }
/// <summary>
/// id路径枚举/1/2/3/
/// </summary>
public string Path { get; set; } = null!;
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; } = null!;
/// <summary>
/// 层级从1开始
/// </summary>
public short Level { get; set; }
/// <summary>
/// logo图片名后台生成地址
/// </summary>
public string? Logo { get; set; }
/// <summary>
/// 层级中序号
/// </summary>
public short Order { get; set; }
/// <summary>
/// 软删除标识
/// </summary>
public bool Deleted { get; set; }
public virtual ICollection<Product> Products { get; set; } = new List<Product>();
public virtual ICollection<Request> Requests { get; set; } = new List<Request>();
}

View File

@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 行政区划
/// </summary>
public partial class District
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 父级id顶级时为0
/// </summary>
public int ParentId { get; set; }
/// <summary>
/// 编码
/// </summary>
public string Code { get; set; } = null!;
/// <summary>
/// 简称
/// </summary>
public string? Name { get; set; }
/// <summary>
/// 全称
/// </summary>
public string FullName { get; set; } = null!;
/// <summary>
/// 名称拼音
/// </summary>
public string? Pinyin { get; set; }
/// <summary>
/// 层级1-省/直辖市2-市/直辖市无3-区4-街道
/// </summary>
public short Level { get; set; }
/// <summary>
/// 经度
/// </summary>
public string? Latitude { get; set; }
/// <summary>
/// 纬度
/// </summary>
public string? Longitude { get; set; }
/// <summary>
/// 层级中序号
/// </summary>
public short Order { get; set; }
}

View File

@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 物流
/// </summary>
public partial class Logistic
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 快递单号
/// </summary>
public string OrderNo { get; set; } = null!;
/// <summary>
/// 快递公司
/// </summary>
public string Company { get; set; } = null!;
/// <summary>
/// 物流状态1-揽收0-在途5-派件6-退回4-退签3-签收2-疑难7-转投8。。。-清关
/// </summary>
public short Status { get; set; }
/// <summary>
/// 到达时间
/// </summary>
public DateTime ArrivalTime { get; set; }
/// <summary>
/// 到达地点
/// </summary>
public string Location { get; set; } = null!;
/// <summary>
/// 详情
/// </summary>
public string Context { get; set; } = null!;
/// <summary>
/// 需求id
/// </summary>
public int RequestId { get; set; }
/// <summary>
/// 入库时间
/// </summary>
public DateTime CreateTime { get; set; }
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 用户消息
/// </summary>
public partial class Message
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 发出用户id
/// </summary>
public int FromUserId { get; set; }
/// <summary>
/// 接收用户id
/// </summary>
public int ToUserId { get; set; }
/// <summary>
/// 发出时间
/// </summary>
public DateTime SentTime { get; set; }
/// <summary>
/// 是否已读
/// </summary>
public bool Read { get; set; }
/// <summary>
/// 是否已撤回
/// </summary>
public bool Recalled { get; set; }
/// <summary>
/// 内容
/// </summary>
public string Content { get; set; } = null!;
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 系统通知
/// </summary>
public partial class Notice
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 内容
/// </summary>
public string Content { get; set; } = null!;
/// <summary>
/// 是否已审核
/// </summary>
public bool Verified { get; set; }
/// <summary>
/// 发布时间
/// </summary>
public DateTime PublishTime { get; set; }
}

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 操作日志
/// </summary>
public partial class OperateLog
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 操作类型c-创建u-修改d-删除
/// </summary>
public char OperateType { get; set; }
/// <summary>
/// 目标类型0-行政区划1-商品分类。。。
/// </summary>
public int TargetType { get; set; }
/// <summary>
/// 关联id目标表主键
/// </summary>
public int RelatedId { get; set; }
/// <summary>
/// 管理员id
/// </summary>
public int OperaterId { get; set; }
/// <summary>
/// 操作描述
/// </summary>
public string? Description { get; set; }
/// <summary>
/// 操作时间
/// </summary>
public DateTime OperateTime { get; set; }
/// <summary>
/// 入库时间
/// </summary>
public DateTime CreateTime { get; set; }
}

View File

@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 卖家商品表
/// </summary>
public partial class Product
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 系统唯一编号
/// </summary>
public string SerialNo { get; set; } = null!;
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; } = null!;
/// <summary>
/// 商品描述
/// </summary>
public string? Description { get; set; }
/// <summary>
/// 商品图片
/// </summary>
public string Logo { get; set; } = null!;
/// <summary>
/// 商品分类id
/// </summary>
public int CategoryId { get; set; }
/// <summary>
/// 用户id
/// </summary>
public int UserId { get; set; }
/// <summary>
/// 最小销售单元
/// </summary>
public string MinimumUnit { get; set; } = null!;
/// <summary>
/// 单价
/// </summary>
public decimal UnitPrice { get; set; }
/// <summary>
/// 详情
/// </summary>
public string? Detail { get; set; }
/// <summary>
/// 已售数量
/// </summary>
public int SoldAmount { get; set; }
/// <summary>
/// 软删除标识
/// </summary>
public bool Deleted { get; set; }
/// <summary>
/// 添加时间
/// </summary>
public DateTime CreateTime { get; set; }
public virtual Category Category { get; set; } = null!;
public virtual ICollection<Reply> Replies { get; set; } = new List<Reply>();
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 刷新令牌
/// </summary>
public partial class RefreshToken
{
/// <summary>
/// 主键
/// </summary>
public long Id { get; set; }
/// <summary>
/// 系统角色a-管理员u-用户
/// </summary>
public char SystemRole { get; set; }
/// <summary>
/// 令牌
/// </summary>
public string Token { get; set; } = null!;
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreateTime { get; set; }
/// <summary>
/// 失效时间
/// </summary>
public DateTime ExpiresAt { get; set; }
/// <summary>
/// 用户/管理员id
/// </summary>
public int UserId { get; set; }
}

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 竞标表
/// </summary>
public partial class Reply
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 商品id
/// </summary>
public int ProductId { get; set; }
/// <summary>
/// 数量
/// </summary>
public int Amount { get; set; }
/// <summary>
/// 价格,自动计算的价格(product.unit_price * amount)之后的优惠价格
/// </summary>
public decimal Price { get; set; }
/// <summary>
/// 竞标者id
/// </summary>
public int UserId { get; set; }
/// <summary>
/// 回应时间
/// </summary>
public DateTime ReplyTime { get; set; }
/// <summary>
/// 需求id
/// </summary>
public int RequestId { get; set; }
/// <summary>
/// 留言
/// </summary>
public string? Memo { get; set; }
/// <summary>
/// 是否已拒绝
/// </summary>
public bool Rejected { get; set; }
public virtual Product Product { get; set; } = null!;
public virtual Request Request { get; set; } = null!;
public virtual User User { get; set; } = null!;
}

View File

@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 用户需求
/// </summary>
public partial class Request
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 需求单号,系统唯一,后台生成
/// </summary>
public string SerialNo { get; set; } = null!;
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; } = null!;
/// <summary>
/// 需求描述
/// </summary>
public string? Description { get; set; }
/// <summary>
/// 商品分类id
/// </summary>
public int CategoryId { get; set; }
/// <summary>
/// 状态0-发布1-有竞标2-待发货3-待收货4-已完成5-已评价
/// </summary>
public short Status { get; set; }
/// <summary>
/// 发布者id
/// </summary>
public int PublisherId { get; set; }
/// <summary>
/// 发布时间
/// </summary>
public DateTime PublishTime { get; set; }
/// <summary>
/// 截止日期
/// </summary>
public DateOnly Deadline { get; set; }
/// <summary>
/// 是否已删除
/// </summary>
public bool Deleted { get; set; }
public virtual Category Category { get; set; } = null!;
public virtual User Publisher { get; set; } = null!;
public virtual ICollection<Reply> Replies { get; set; } = new List<Reply>();
}

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
namespace StopShopping.EF.Models;
/// <summary>
/// 用户
/// </summary>
public partial class User
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 登录账号
/// </summary>
public string Account { get; set; } = null!;
/// <summary>
/// 昵称
/// </summary>
public string NickName { get; set; } = null!;
/// <summary>
/// 头像图片名,后台生成链接
/// </summary>
public string? Avatar { get; set; }
/// <summary>
/// 当前角色c-买家s-卖家
/// </summary>
public char CurrentRole { get; set; }
/// <summary>
/// 电话
/// </summary>
public string? Telephone { get; set; }
/// <summary>
/// 密码(已加密)
/// </summary>
public string Password { get; set; } = null!;
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreateTime { get; set; }
/// <summary>
/// 最后登录时间
/// </summary>
public DateTime? LastLoginTime { get; set; }
public virtual ICollection<Address> Addresses { get; set; } = new List<Address>();
public virtual ICollection<Reply> Replies { get; set; } = new List<Reply>();
public virtual ICollection<Request> Requests { get; set; } = new List<Request>();
}

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>IDE0005;CS1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,597 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using StopShopping.EF.Models;
namespace StopShopping.EF;
public partial class StopShoppingContext : DbContext
{
public StopShoppingContext(DbContextOptions<StopShoppingContext> options)
: base(options)
{
}
public virtual DbSet<Address> Addresses { get; set; }
public virtual DbSet<Administrator> Administrators { get; set; }
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<District> Districts { get; set; }
public virtual DbSet<Logistic> Logistics { get; set; }
public virtual DbSet<Message> Messages { get; set; }
public virtual DbSet<Notice> Notices { get; set; }
public virtual DbSet<OperateLog> OperateLogs { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<RefreshToken> RefreshTokens { get; set; }
public virtual DbSet<Reply> Replies { get; set; }
public virtual DbSet<Request> Requests { get; set; }
public virtual DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Address>(entity =>
{
entity.HasKey(e => e.Id).HasName("address_pkey");
entity.ToTable("address", tb => tb.HasComment("收货地址"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.Default)
.HasComment("是否默认地址")
.HasColumnName("default");
entity.Property(e => e.Detail)
.HasMaxLength(200)
.HasComment("详细地址")
.HasColumnName("detail");
entity.Property(e => e.DistrictLevel1Id)
.HasComment("行政区域id表示省/直辖市")
.HasColumnName("district_level1_id");
entity.Property(e => e.DistrictLevel2Id)
.HasComment("行政区域id表示市/直辖市为空")
.HasColumnName("district_level2_id");
entity.Property(e => e.DistrictLevel3Id)
.HasComment("行政区域id表示区")
.HasColumnName("district_level3_id");
entity.Property(e => e.DistrictLevel4Id)
.HasComment("行政区域id表示街道/镇")
.HasColumnName("district_level4_id");
entity.Property(e => e.Name)
.HasMaxLength(20)
.HasComment("收货人地址")
.HasColumnName("name");
entity.Property(e => e.Tag)
.HasMaxLength(20)
.HasComment("自定义标签:学校、家等")
.HasColumnName("tag");
entity.Property(e => e.Telephone)
.HasMaxLength(20)
.HasComment("联系电话")
.HasColumnName("telephone");
entity.Property(e => e.UserId)
.HasComment("用户表id")
.HasColumnName("user_id");
entity.HasOne(d => d.User).WithMany(p => p.Addresses)
.HasForeignKey(d => d.UserId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_useraddress");
});
modelBuilder.Entity<Administrator>(entity =>
{
entity.HasKey(e => e.Id).HasName("administrator_pkey");
entity.ToTable("administrator", tb => tb.HasComment("管理员"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.Account)
.HasMaxLength(20)
.HasComment("登录账号")
.HasColumnName("account");
entity.Property(e => e.CreateTime)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.HasComment("创建时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("create_time");
entity.Property(e => e.LastLoginTime)
.HasComment("最后登录时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("last_login_time");
entity.Property(e => e.NickName)
.HasMaxLength(50)
.HasComment("昵称")
.HasColumnName("nick_name");
entity.Property(e => e.Password)
.HasMaxLength(128)
.HasComment("登录密码(已加密)")
.HasColumnName("password");
});
modelBuilder.Entity<Category>(entity =>
{
entity.HasKey(e => e.Id).HasName("category_pkey");
entity.ToTable("category", tb => tb.HasComment("商品分类"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.Deleted)
.HasComment("软删除标识")
.HasColumnName("deleted");
entity.Property(e => e.Level)
.HasComment("层级从1开始")
.HasColumnName("level");
entity.Property(e => e.Logo)
.HasMaxLength(50)
.HasComment("logo图片名后台生成地址")
.HasColumnName("logo");
entity.Property(e => e.Name)
.HasMaxLength(50)
.HasComment("名称")
.HasColumnName("name");
entity.Property(e => e.Order)
.HasComment("层级中序号")
.HasColumnName("order");
entity.Property(e => e.ParentId)
.HasComment("父级id顶级为0")
.HasColumnName("parent_id");
entity.Property(e => e.Path)
.HasMaxLength(50)
.HasComment("id路径枚举/1/2/3/")
.HasColumnName("path");
});
modelBuilder.Entity<District>(entity =>
{
entity.HasKey(e => e.Id).HasName("district_pkey");
entity.ToTable("district", tb => tb.HasComment("行政区划"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.Code)
.HasMaxLength(10)
.HasComment("编码")
.HasColumnName("code");
entity.Property(e => e.FullName)
.HasMaxLength(100)
.HasComment("全称")
.HasColumnName("full_name");
entity.Property(e => e.Latitude)
.HasMaxLength(20)
.HasComment("经度")
.HasColumnName("latitude");
entity.Property(e => e.Level)
.HasComment("层级1-省/直辖市2-市/直辖市无3-区4-街道")
.HasColumnName("level");
entity.Property(e => e.Longitude)
.HasMaxLength(20)
.HasComment("纬度")
.HasColumnName("longitude");
entity.Property(e => e.Name)
.HasMaxLength(20)
.HasComment("简称")
.HasColumnName("name");
entity.Property(e => e.Order)
.HasComment("层级中序号")
.HasColumnName("order");
entity.Property(e => e.ParentId)
.HasComment("父级id顶级时为0")
.HasColumnName("parent_id");
entity.Property(e => e.Pinyin)
.HasMaxLength(200)
.HasComment("名称拼音")
.HasColumnName("pinyin");
});
modelBuilder.Entity<Logistic>(entity =>
{
entity.HasKey(e => e.Id).HasName("logistics_pkey");
entity.ToTable("logistics", tb => tb.HasComment("物流"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.ArrivalTime)
.HasComment("到达时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("arrival_time");
entity.Property(e => e.Company)
.HasMaxLength(30)
.HasComment("快递公司")
.HasColumnName("company");
entity.Property(e => e.Context)
.HasMaxLength(200)
.HasComment("详情")
.HasColumnName("context");
entity.Property(e => e.CreateTime)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.HasComment("入库时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("create_time");
entity.Property(e => e.Location)
.HasMaxLength(100)
.HasComment("到达地点")
.HasColumnName("location");
entity.Property(e => e.OrderNo)
.HasMaxLength(20)
.HasComment("快递单号")
.HasColumnName("order_no");
entity.Property(e => e.RequestId)
.HasComment("需求id")
.HasColumnName("request_id");
entity.Property(e => e.Status)
.HasComment("物流状态1-揽收0-在途5-派件6-退回4-退签3-签收2-疑难7-转投8。。。-清关")
.HasColumnName("status");
});
modelBuilder.Entity<Message>(entity =>
{
entity.HasKey(e => e.Id).HasName("message_pkey");
entity.ToTable("message", tb => tb.HasComment("用户消息"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.Content)
.HasComment("内容")
.HasColumnName("content");
entity.Property(e => e.FromUserId)
.HasComment("发出用户id")
.HasColumnName("from_user_id");
entity.Property(e => e.Read)
.HasComment("是否已读")
.HasColumnName("read");
entity.Property(e => e.Recalled)
.HasComment("是否已撤回")
.HasColumnName("recalled");
entity.Property(e => e.SentTime)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.HasComment("发出时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("sent_time");
entity.Property(e => e.ToUserId)
.HasComment("接收用户id")
.HasColumnName("to_user_id");
});
modelBuilder.Entity<Notice>(entity =>
{
entity.HasKey(e => e.Id).HasName("notice_pkey");
entity.ToTable("notice", tb => tb.HasComment("系统通知"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.Content)
.HasComment("内容")
.HasColumnName("content");
entity.Property(e => e.PublishTime)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.HasComment("发布时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("publish_time");
entity.Property(e => e.Verified)
.HasComment("是否已审核")
.HasColumnName("verified");
});
modelBuilder.Entity<OperateLog>(entity =>
{
entity.HasKey(e => e.Id).HasName("operate_log_pkey");
entity.ToTable("operate_log", tb => tb.HasComment("操作日志"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.CreateTime)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.HasComment("入库时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("create_time");
entity.Property(e => e.Description)
.HasMaxLength(500)
.HasComment("操作描述")
.HasColumnName("description");
entity.Property(e => e.OperateTime)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.HasComment("操作时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("operate_time");
entity.Property(e => e.OperateType)
.HasMaxLength(1)
.HasComment("操作类型c-创建u-修改d-删除")
.HasColumnName("operate_type");
entity.Property(e => e.OperaterId)
.HasComment("管理员id")
.HasColumnName("operater_id");
entity.Property(e => e.RelatedId)
.HasComment("关联id目标表主键")
.HasColumnName("related_id");
entity.Property(e => e.TargetType)
.HasComment("目标类型0-行政区划1-商品分类。。。")
.HasColumnName("target_type");
});
modelBuilder.Entity<Product>(entity =>
{
entity.HasKey(e => e.Id).HasName("product_pkey");
entity.ToTable("product", tb => tb.HasComment("卖家商品表"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.CategoryId)
.HasComment("商品分类id")
.HasColumnName("category_id");
entity.Property(e => e.CreateTime)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.HasComment("添加时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("create_time");
entity.Property(e => e.Deleted)
.HasComment("软删除标识")
.HasColumnName("deleted");
entity.Property(e => e.Description)
.HasMaxLength(200)
.HasComment("商品描述")
.HasColumnName("description");
entity.Property(e => e.Detail)
.HasComment("详情")
.HasColumnName("detail");
entity.Property(e => e.Logo)
.HasMaxLength(50)
.HasComment("商品图片")
.HasColumnName("logo");
entity.Property(e => e.MinimumUnit)
.HasMaxLength(20)
.HasComment("最小销售单元")
.HasColumnName("minimum_unit");
entity.Property(e => e.Name)
.HasMaxLength(100)
.HasComment("名称")
.HasColumnName("name");
entity.Property(e => e.SerialNo)
.HasMaxLength(20)
.HasComment("系统唯一编号")
.HasColumnName("serial_no");
entity.Property(e => e.SoldAmount)
.HasComment("已售数量")
.HasColumnName("sold_amount");
entity.Property(e => e.UnitPrice)
.HasPrecision(11, 2)
.HasComment("单价")
.HasColumnName("unit_price");
entity.Property(e => e.UserId)
.HasComment("用户id")
.HasColumnName("user_id");
entity.HasOne(d => d.Category).WithMany(p => p.Products)
.HasForeignKey(d => d.CategoryId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_categoryproduct");
});
modelBuilder.Entity<RefreshToken>(entity =>
{
entity.HasKey(e => e.Id).HasName("refresh_token_pkey");
entity.ToTable("refresh_token", tb => tb.HasComment("刷新令牌"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.CreateTime)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.HasComment("创建时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("create_time");
entity.Property(e => e.ExpiresAt)
.HasComment("失效时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("expires_at");
entity.Property(e => e.SystemRole)
.HasMaxLength(1)
.HasComment("系统角色a-管理员u-用户")
.HasColumnName("system_role");
entity.Property(e => e.Token)
.HasMaxLength(50)
.HasComment("令牌")
.HasColumnName("token");
entity.Property(e => e.UserId)
.HasComment("用户/管理员id")
.HasColumnName("user_id");
});
modelBuilder.Entity<Reply>(entity =>
{
entity.HasKey(e => e.Id).HasName("reply_pkey");
entity.ToTable("reply", tb => tb.HasComment("竞标表"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.Amount)
.HasComment("数量")
.HasColumnName("amount");
entity.Property(e => e.Memo)
.HasMaxLength(200)
.HasComment("留言")
.HasColumnName("memo");
entity.Property(e => e.Price)
.HasPrecision(11, 2)
.HasComment("价格,自动计算的价格(product.unit_price * amount)之后的优惠价格")
.HasColumnName("price");
entity.Property(e => e.ProductId)
.HasComment("商品id")
.HasColumnName("product_id");
entity.Property(e => e.Rejected)
.HasComment("是否已拒绝")
.HasColumnName("rejected");
entity.Property(e => e.ReplyTime)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.HasComment("回应时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("reply_time");
entity.Property(e => e.RequestId)
.HasComment("需求id")
.HasColumnName("request_id");
entity.Property(e => e.UserId)
.HasComment("竞标者id")
.HasColumnName("user_id");
entity.HasOne(d => d.Product).WithMany(p => p.Replies)
.HasForeignKey(d => d.ProductId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_replyproduct");
entity.HasOne(d => d.Request).WithMany(p => p.Replies)
.HasForeignKey(d => d.RequestId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_requestreply");
entity.HasOne(d => d.User).WithMany(p => p.Replies)
.HasForeignKey(d => d.UserId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_replyuser");
});
modelBuilder.Entity<Request>(entity =>
{
entity.HasKey(e => e.Id).HasName("request_pkey");
entity.ToTable("request", tb => tb.HasComment("用户需求"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.CategoryId)
.HasComment("商品分类id")
.HasColumnName("category_id");
entity.Property(e => e.Deadline)
.HasComment("截止日期")
.HasColumnName("deadline");
entity.Property(e => e.Deleted)
.HasComment("是否已删除")
.HasColumnName("deleted");
entity.Property(e => e.Description)
.HasMaxLength(1000)
.HasComment("需求描述")
.HasColumnName("description");
entity.Property(e => e.Name)
.HasMaxLength(100)
.HasComment("名称")
.HasColumnName("name");
entity.Property(e => e.PublishTime)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.HasComment("发布时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("publish_time");
entity.Property(e => e.PublisherId)
.HasComment("发布者id")
.HasColumnName("publisher_id");
entity.Property(e => e.SerialNo)
.HasMaxLength(20)
.HasComment("需求单号,系统唯一,后台生成")
.HasColumnName("serial_no");
entity.Property(e => e.Status)
.HasComment("状态0-发布1-有竞标2-待发货3-待收货4-已完成5-已评价")
.HasColumnName("status");
entity.HasOne(d => d.Category).WithMany(p => p.Requests)
.HasForeignKey(d => d.CategoryId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_categoryrequest");
entity.HasOne(d => d.Publisher).WithMany(p => p.Requests)
.HasForeignKey(d => d.PublisherId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_userrequest");
});
modelBuilder.Entity<User>(entity =>
{
entity.HasKey(e => e.Id).HasName("user_pkey");
entity.ToTable("user", tb => tb.HasComment("用户"));
entity.Property(e => e.Id)
.HasComment("主键")
.UseIdentityAlwaysColumn()
.HasColumnName("id");
entity.Property(e => e.Account)
.HasMaxLength(20)
.HasComment("登录账号")
.HasColumnName("account");
entity.Property(e => e.Avatar)
.HasMaxLength(50)
.HasComment("头像图片名,后台生成链接")
.HasColumnName("avatar");
entity.Property(e => e.CreateTime)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.HasComment("创建时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("create_time");
entity.Property(e => e.CurrentRole)
.HasMaxLength(1)
.HasDefaultValueSql("'c'::bpchar")
.HasComment("当前角色c-买家s-卖家")
.HasColumnName("current_role");
entity.Property(e => e.LastLoginTime)
.HasComment("最后登录时间")
.HasColumnType("timestamp without time zone")
.HasColumnName("last_login_time");
entity.Property(e => e.NickName)
.HasMaxLength(50)
.HasComment("昵称")
.HasColumnName("nick_name");
entity.Property(e => e.Password)
.HasMaxLength(128)
.HasComment("密码(已加密)")
.HasColumnName("password");
entity.Property(e => e.Telephone)
.HasMaxLength(20)
.HasComment("电话")
.HasColumnName("telephone");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

View File

@@ -0,0 +1,20 @@
namespace StopShopping.OpenPlatform;
public class DistrictResponse
{
public bool IsSucced { get; set; }
public string? Message { get; set; }
public List<District>? Districts { get; set; }
public class District
{
public string Code { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string FullName { get; set; } = string.Empty;
public int? Level { get; set; }
public string? PinYin { get; set; }
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
public List<District>? Districts { get; set; }
}
}

View File

@@ -0,0 +1,20 @@
using Microsoft.Extensions.Configuration;
using StopShopping.OpenPlatform;
using StopShopping.OpenPlatform.Extensions;
using StopShopping.OpenPlatform.TencentLocationApi;
namespace Microsoft.Extensions.DependencyInjection;
public static class OpenPlatformExtensions
{
public static IServiceCollection AddOpenPlatformServices(this IServiceCollection services, IConfiguration openPlatformOptions)
{
services.Configure<OpenPlatformOptions>(openPlatformOptions);
services.AddHttpClient();
services.AddScoped<IDistrictService, TencentDistrictService>();
return services;
}
}

View File

@@ -0,0 +1,13 @@
namespace StopShopping.OpenPlatform.Extensions;
public class OpenPlatformOptions
{
public DistrictOptions TencentDistrict { get; set; } = new();
public class DistrictOptions
{
public string? SecretKey { get; set; }
public string? Top3LevelUrl { get; set; }
public string? GetChildrenUrl { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
namespace StopShopping.OpenPlatform;
public interface IDistrictService
{
Task<DistrictResponse?> GetTop3LevelDistrictsAsync(CancellationToken cancellationToken = default);
Task<DistrictResponse?> GetChildrenAsync(string code, CancellationToken cancellationToken = default);
}

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Polly.Extensions" Version="8.6.5" />
<PackageReference Include="Polly.RateLimiting" Version="8.6.5" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,73 @@
using System.Text.Json.Serialization;
namespace StopShopping.OpenPlatform.TencentLocationApi;
/// <summary>
/// 区划
/// </summary>
/// <value></value>
public record District
{
/// <summary>
/// 行政区划唯一标识adcode
/// </summary>
/// <value></value>
[JsonPropertyName("id")]
public string Id { get; set; } = string.Empty;
/// <summary>
/// 简称,如“内蒙古”
/// </summary>
/// <value></value>
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
/// <summary>
/// 行政区划级别
/// </summary>
/// <value></value>
[JsonPropertyName("level")]
public int? Level { get; set; }
/// <summary>
/// 全称,如“内蒙古自治区”
/// </summary>
/// <value></value>
[JsonPropertyName("fullname")]
public string FullName { get; set; } = string.Empty;
/// <summary>
/// 行政区划拼音,每一下标为一个字的全拼,如:[“nei”,“meng”,“gu”]
/// </summary>
/// <value></value>
[JsonPropertyName("pinyin")]
public string[]? PinYin { get; set; }
/// <summary>
/// 经纬度
/// </summary>
/// <returns></returns>
[JsonPropertyName("location")]
public Location Location { get; set; } = new();
/// <summary>
/// 当前区划的下级区划信息,结构与当前区划一致,如果没有下级区划则不返回此字段
/// </summary>
/// <value></value>
[JsonPropertyName("districts")]
public District[]? Districts { get; set; }
}
/// <summary>
/// 经纬度
/// </summary>
/// <value></value>
public record Location
{
/// <summary>
/// 纬度
/// </summary>
/// <value></value>
[JsonPropertyName("lat")]
public decimal Latitude { get; set; }
/// <summary>
/// 经度
/// </summary>
/// <value></value>
[JsonPropertyName("lng")]
public decimal Longitude { get; set; }
}

View File

@@ -0,0 +1,19 @@
using System.Text.Json.Serialization;
namespace StopShopping.OpenPlatform.TencentLocationApi;
public abstract record ResponseBase
{
/// <summary>
/// 状态码
/// </summary>
/// <value></value>
[JsonPropertyName("status")]
public int Status { get; set; }
/// <summary>
/// 状态说明
/// </summary>
/// <value></value>
[JsonPropertyName("message")]
public string? Message { get; set; }
}

View File

@@ -0,0 +1,19 @@
using System.Text.Json.Serialization;
namespace StopShopping.OpenPlatform.TencentLocationApi;
public record ResponseDistrict<T> : ResponseBase
{
/// <summary>
/// 数据版本,日期
/// </summary>
/// <value></value>
[JsonPropertyName("data_version")]
public string DataVersion { get; set; } = string.Empty;
/// <summary>
/// 结果数组
/// </summary>
/// <value></value>
[JsonPropertyName("result")]
public T[]? Result { get; set; }
}

View File

@@ -0,0 +1,154 @@
using System.Text.Json;
using System.Threading.RateLimiting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Polly;
using Polly.RateLimiting;
using Polly.Timeout;
using StopShopping.OpenPlatform.Extensions;
namespace StopShopping.OpenPlatform.TencentLocationApi;
public class TencentDistrictService : IDistrictService
{
public TencentDistrictService(
IHttpClientFactory httpClientFactory,
IOptions<OpenPlatformOptions> openPlatformOptions,
ILogger<TencentDistrictService> logger)
{
_httpContextFactory = httpClientFactory;
_options = openPlatformOptions.Value;
_logger = logger;
_pipeline = new ResiliencePipelineBuilder()
.AddRetry(new Polly.Retry.RetryStrategyOptions
{
Delay = TimeSpan.FromSeconds(2),
MaxRetryAttempts = 5,
ShouldHandle = (arg) =>
ValueTask.FromResult(arg.Outcome.Exception is
TimeoutRejectedException or RateLimiterRejectedException)
})
.AddRateLimiter(new SlidingWindowRateLimiter(new SlidingWindowRateLimiterOptions
{
PermitLimit = 2,
QueueLimit = int.MaxValue,
SegmentsPerWindow = 5,
Window = TimeSpan.FromSeconds(1)
}))
.AddTimeout(TimeSpan.FromSeconds(20))
.Build();
}
private readonly IHttpClientFactory _httpContextFactory;
private readonly OpenPlatformOptions _options;
private readonly ILogger<TencentDistrictService> _logger;
private readonly ResiliencePipeline _pipeline;
public async Task<DistrictResponse?> GetChildrenAsync(string code, CancellationToken cancellationToken = default)
{
var uri = new Uri(
string.Format("{0}?key={1}&id={2}"
, _options.TencentDistrict.GetChildrenUrl!
, Uri.EscapeDataString(_options.TencentDistrict.SecretKey!)
, code));
var districts = await Get<District[]?>(uri, cancellationToken);
if (null == districts)
return null;
DistrictResponse response = new()
{
IsSucced = districts.Status == 0,
Message = $"{districts.Status}:{districts.Message}",
Districts = districts.Result
?.FirstOrDefault()
?.Select(Cast)
.ToList()
};
return response;
}
public async Task<DistrictResponse?> GetTop3LevelDistrictsAsync(CancellationToken cancellationToken = default)
{
var uri = new Uri(
string.Format("{0}?key={1}&struct_type=1"
, _options.TencentDistrict.Top3LevelUrl!
, _options.TencentDistrict.SecretKey!)
);
var districts = await Get<District>(uri, cancellationToken);
if (null == districts)
return null;
DistrictResponse response = new()
{
IsSucced = districts.Status == 0,
Message = $"{districts.Status}:{districts.Message}",
Districts = null != districts.Result
? [.. RecurCast(districts.Result)] : null
};
return response;
}
private async Task<ResponseDistrict<T>?> Get<T>(Uri uri, CancellationToken cancellationToken = default)
{
using var httpClient = _httpContextFactory.CreateClient();
var districts = await _pipeline.ExecuteAsync(async (token) =>
{
var jsonStream = await httpClient.GetStreamAsync(uri, token);
try
{
return await JsonSerializer.DeserializeAsync<ResponseDistrict<T>>(
jsonStream, cancellationToken: token);
}
catch (JsonException e)
{
_logger.LogError("序列化异常:{Path}", e.Path);
return null;
}
}, cancellationToken);
if (null == districts)
{
_logger.LogError("接口请求失败:{Url}", uri.ToString());
return null;
}
return districts;
}
private IEnumerable<DistrictResponse.District> RecurCast(District[] districts)
{
foreach (var d in districts)
{
var cd = Cast(d);
if (d.Districts?.Length > 0)
cd.Districts = [.. RecurCast(d.Districts!)];
yield return cd;
}
}
private DistrictResponse.District Cast(District d)
{
return new DistrictResponse.District
{
Code = d.Id,
FullName = d.FullName,
Latitude = d.Location.Latitude,
Level = d.Level,
Longitude = d.Location.Longitude,
Name = d.Name,
PinYin = d.PinYin?.Length > 0 ? string.Join(',', d.PinYin) : null,
};
}
}

View File

@@ -0,0 +1 @@
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]

View File

@@ -0,0 +1,53 @@
using Microsoft.Extensions.Logging;
using StopShopping.Services.Implementions;
namespace StopShopping.Services.Test;
[TestClass]
public sealed class SerialNoGeneratorTests
{
[ClassInitialize]
public static void Init(TestContext testContext)
{
_serialNoGenerator = new SerialNoGenerator(
LoggerFactory.Create(options => options.AddConsole()).CreateLogger<SerialNoGenerator>());
_nanoidGenerator = new NanoidSerialNoGenerator();
}
private static SerialNoGenerator? _serialNoGenerator;
private static NanoidSerialNoGenerator? _nanoidGenerator;
[TestMethod]
[DataRow(10)]
public void Gen_Right_Count(int count)
{
List<string> serialNos = [];
for (var i = 0; i < count; i++)
{
var no = _serialNoGenerator?.GenerateRequestNo();
if (!string.IsNullOrWhiteSpace(no))
serialNos.Add(no);
}
serialNos.ForEach(s => Console.WriteLine("{0} - {1}", s, Convert.ToString(long.Parse(s[1..]), toBase: 2)));
Assert.HasCount(count, serialNos);
}
[TestMethod]
[DataRow(10)]
public void Nanoid_Gen_Right_Count(int count)
{
List<string> serialNos = [];
for (var i = 0; i < count; i++)
{
var no = _nanoidGenerator?.GenerateRequestNo();
if (!string.IsNullOrWhiteSpace(no))
serialNos.Add(no);
}
serialNos.ForEach(s => Console.WriteLine(s));
Assert.HasCount(count, serialNos);
}
}

View File

@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StopShopping.Services\StopShopping.Services.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,29 @@
namespace StopShopping.Services;
/// <summary>
/// 常量
/// </summary>
public static class Consts
{
/// <summary>
/// 默认头像
/// </summary>
public const string DEFAULT_AVATAR = "avatar.png";
/// <summary>
/// 默认商品图片
/// </summary>
public const string DEFAULT_PRODUCT = "product.png";
/// <summary>
/// 默认管理员账号
/// </summary>
public const string DEFAULT_ADMIN_ACCOUNT = "stopshopping";
public static class CacheKeys
{
public static string AccessTokenBlacklist(string token)
{
return $"accesstoken_blacklist:{token}";
}
}
}

View File

@@ -0,0 +1,33 @@
namespace StopShopping.Services.Extensions;
/// <summary>
/// 业务配置
/// </summary>
public record AppOptions
{
/// <summary>
/// .bjbj.me
/// </summary>
/// <value></value>
public string CookieDomain { get; set; } = string.Empty;
/// <summary>
/// 域名http(s)://www.xxx.xx
/// </summary>
/// <value></value>
public string DomainPath { get; set; } = string.Empty;
/// <summary>
/// anti-forgery 请求头
/// </summary>
/// <value></value>
public string CSRFHeaderName { get; set; } = string.Empty;
/// <summary>
/// anti-forgery cookie's name
/// </summary>
/// <value></value>
public string CSRFCookieName { get; set; } = string.Empty;
/// <summary>
/// 跨域站点
/// </summary>
/// <value></value>
public string[] CorsAllowedOrigins { get; set; } = [];
}

View File

@@ -0,0 +1,49 @@
using StopShopping.Services.Models;
namespace StopShopping.Services.Extensions;
public static class EnumExtensions
{
public static string GetTargetDirectory(this UploadScences uploadScences)
{
return uploadScences switch
{
UploadScences.Avatar => "avatar",
UploadScences.Product => "product",
UploadScences.Category => "category",
_ => throw new ArgumentOutOfRangeException(nameof(uploadScences))
};
}
public static char GetValue(this UserRoles userRoles)
{
return userRoles switch
{
UserRoles.Seller => 's',
UserRoles.Buyer => 'c',
_ => throw new ArgumentOutOfRangeException(nameof(userRoles))
};
}
public static UserRoles ToUserRoles(this char userRole)
{
return userRole switch
{
's' => UserRoles.Seller,
'c' => UserRoles.Buyer,
_ => throw new ArgumentOutOfRangeException(nameof(userRole), "valid: 'c','s'")
};
}
public static bool CanDelete(this RequestStatus requestStatus)
{
return requestStatus == RequestStatus.Publish
|| requestStatus == RequestStatus.Replied;
}
public static bool CanReply(this RequestStatus requestStatus)
{
return requestStatus == RequestStatus.Publish
|| requestStatus == RequestStatus.Replied;
}
}

View File

@@ -0,0 +1,11 @@
namespace System;
/// <summary>
/// 业务异常
/// </summary>
public class ServiceException : ApplicationException
{
public ServiceException() : base() { }
public ServiceException(string? message) : base(message) { }
}

View File

@@ -0,0 +1,45 @@
using FileSignatures;
using FileSignatures.Formats;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using StopShopping.EF;
using StopShopping.Services;
using StopShopping.Services.Extensions;
using StopShopping.Services.Implementions;
namespace Microsoft.Extensions.DependencyInjection;
public static class ServicesExtensions
{
public static IServiceCollection AddServices(
this IServiceCollection services,
Action<DbContextOptionsBuilder> dbContextOptions,
IConfiguration appOptions,
IConfiguration openPlatformOptions)
{
services.AddDbContext<StopShoppingContext>(dbContextOptions);
services.Configure<AppOptions>(appOptions);
var imageFormats = FileFormatLocator.GetFormats().OfType<Image>();
var imageInspector = new FileFormatInspector(imageFormats);
services.AddSingleton<IFileFormatInspector>(imageInspector);
services.AddSingleton<ICipherService, CipherService>();
services.AddSingleton<ISerialNoGenerator, NanoidSerialNoGenerator>();
services.AddScoped<IDistrictService, DistrictService>();
services.AddScoped<IClaimsService, ClaimsService>();
services.AddScoped<IFileService, FileService>();
services.AddScoped<IAccessTokenService, AccessTokenService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<ICategoryService, CategoryService>();
services.AddScoped<IProductService, ProductService>();
services.AddScoped<IRequestService, RequestService>();
services.AddScoped<IReplyService, ReplyService>();
services.AddOpenPlatformServices(openPlatformOptions);
return services;
}
}

View File

@@ -0,0 +1,14 @@
namespace System;
public static class SystemExtensions
{
public static string ToFormatted(this DateTime dateTime)
{
return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
}
public static string ToFormatted(this DateOnly date)
{
return date.ToString("yyyy-MM-dd");
}
}

View File

@@ -0,0 +1,49 @@
using System.Security.Claims;
using StopShopping.Services.Models;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Services;
/// <summary>
/// 访问令牌服务
/// </summary>
public interface IAccessTokenService
{
/// <summary>
/// 生成访问令牌
/// </summary>
/// <param name="claims"></param>
/// <returns></returns>
AccessToken GenerateAccessToken(ClaimsIdentity claims);
/// <summary>
/// 生成访问令牌
/// </summary>
/// <param name="refreshToken"></param>
/// <returns></returns>
Task<AccessToken?> GenerateAccessTokenAsync(string refreshToken);
/// <summary>
/// 添加访问令牌到黑名单
/// </summary>
/// <param name="accessToken"></param>
/// <returns></returns>
Task<bool> AddAccessTokenBlacklistAsync(string accessToken);
/// <summary>
/// 访问令牌是否在黑名单中
/// </summary>
/// <param name="accessToken"></param>
/// <returns></returns>
Task<bool> IsAccessTokenBlacklistAsync(string accessToken);
/// <summary>
/// 生成刷新令牌
/// </summary>
/// <param name="userId">管理员/用户id</param>
/// <param name="systemRole"></param>
/// <returns></returns>
Task<AccessToken> SetRefreshTokenAsync(int userId, SystemRoles systemRole);
/// <summary>
/// 回收刷新令牌
/// </summary>
/// <param name="refreshToken"></param>
/// <returns></returns>
Task RevokeRefreshTokenAsync(string refreshToken);
}

View File

@@ -0,0 +1,34 @@
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Services;
/// <summary>
/// 商品分类服务
/// </summary>
public interface ICategoryService
{
/// <summary>
/// 获取分类树
/// </summary>
/// <returns></returns>
ApiResponse<List<Category>> GetCategoriesTree();
/// <summary>
/// 新增/修改分类
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse<Category>> EditCategoryAsync(EditCategoryParams model);
/// <summary>
/// 调整层级内顺序
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> ResortCategoryAsync(ResortCategoryParams model);
/// <summary>
/// 删除分类
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> DeleteCategoryAsync(CategoryIdParams model);
}

View File

@@ -0,0 +1,14 @@
namespace StopShopping.Services;
/// <summary>
/// 加解密服务
/// </summary>
public interface ICipherService
{
/// <summary>
/// 用户密码加密
/// </summary>
/// <param name="input">明文</param>
/// <returns></returns>
string EncryptUserPassword(string input);
}

View File

@@ -0,0 +1,28 @@
using System.Security.Claims;
using StopShopping.EF.Models;
namespace StopShopping.Services;
/// <summary>
/// 身份服务
/// </summary>
public interface IClaimsService
{
/// <summary>
/// 创建用户身份标识
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
ClaimsIdentity BuildIdentity(User user);
/// <summary>
/// 创建管理员身份标识
/// </summary>
/// <param name="admin"></param>
/// <returns></returns>
ClaimsIdentity BuildAdminIdentity(Administrator admin);
/// <summary>
/// 获取当前登录用户id
/// </summary>
/// <returns></returns>
int GetCurrentUserId();
}

View File

@@ -0,0 +1,28 @@
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Services;
/// <summary>
/// 行政区划服务
/// </summary>
public interface IDistrictService
{
/// <summary>
/// 初始化行政区划数据库
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task InitialDatabaseAsync(CancellationToken cancellationToken);
/// <summary>
/// 获取到区的区域直辖市无level=2
/// </summary>
/// <returns></returns>
Task<ApiResponse<List<District>>> GetTop3LevelDistrictsAsync();
/// <summary>
/// 获取直接下级区域
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
ApiResponse<List<District>> GetChildren(DistrictParentIdParams model);
}

View File

@@ -0,0 +1,25 @@
using StopShopping.Services.Models;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Services;
/// <summary>
/// 文件服务
/// </summary>
public interface IFileService
{
/// <summary>
/// 上传文件
/// </summary>
/// <param name="payload"></param>
/// <returns></returns>
Task<ApiResponse<FileUpload>> UploadFileAsync(UploadParams payload);
/// <summary>
/// 获取文件链接
/// </summary>
/// <param name="scences"></param>
/// <param name="fileName"></param>
/// <returns></returns>
string GetFileUrl(UploadScences scences, string fileName);
}

View File

@@ -0,0 +1,35 @@
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Services;
/// <summary>
/// 商品服务
/// </summary>
public interface IProductService
{
/// <summary>
/// 分页搜索
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse<PagedResult<Product>>> SearchAsync(ProductSearchParms model);
/// <summary>
/// 详情
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
ApiResponse<ProductInfo> Detail(ProductIdParams model);
/// <summary>
/// 新增/修改商品
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> EditAsync(EditProductParams model);
/// <summary>
/// 删除商品
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> DeleteAsync(ProductIdParams model);
}

View File

@@ -0,0 +1,23 @@
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Services;
/// <summary>
/// 竞标服务
/// </summary>
public interface IReplyService
{
/// <summary>
/// 提交竞标
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> ReplyAsync(ReplyParams model);
/// <summary>
/// 查看竞标列表
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse<List<Reply>>> GetRepliesAsync(RequestIdParams model);
}

View File

@@ -0,0 +1,41 @@
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Services;
/// <summary>
/// 需求服务
/// </summary>
public interface IRequestService
{
/// <summary>
/// 发布需求
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> PublishRequestAsync(CreateRequestParams model);
/// <summary>
/// 分页搜索需求
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse<PagedResult<Request>>> SearchAsync(RequestSearchParams model);
/// <summary>
/// 需求订单检索
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse<PagedResult<Request>>> RequestOrderSearchAsync(RequestSearchWithStatusParams model);
/// <summary>
/// 竞标订单检索
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse<PagedResult<Request>>> ReplyOrderSearchAsync(RequestSearchWithStatusParams model);
/// <summary>
/// 删除需求
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> DeleteRequestAsync(RequestIdParams model);
}

View File

@@ -0,0 +1,20 @@
namespace StopShopping.Services;
public interface ISerialNoGenerator
{
/// <summary>
/// 生成需求单号
/// </summary>
/// <returns></returns>
string GenerateRequestNo();
/// <summary>
/// 生成商品编号
/// </summary>
/// <returns></returns>
string GenerateProductNo();
/// <summary>
/// 生成随机密码
/// </summary>
/// <returns></returns>
string GenerateRandomPassword();
}

View File

@@ -0,0 +1,73 @@
using StopShopping.Services.Models;
using StopShopping.Services.Models.Req;
using StopShopping.Services.Models.Resp;
namespace StopShopping.Services;
/// <summary>
/// 用户服务
/// </summary>
public interface IUserService
{
/// <summary>
/// 用户注册
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> SignUpAsync(SignUpParams model);
/// <summary>
/// 登录
/// </summary>
/// <param name="model"></param>
/// <returns>AccessToken,RefreshToken</returns>
Task<SignInResult<SignInUser>> SignInAsync(SignInParams model);
/// <summary>
/// 管理员登录
/// </summary>
/// <param name="model"></param>
/// <returns>AccessToken,RefreshToken</returns>
Task<SignInResult<SignInAdmin>> SignInAdminAsync(SignInParams model);
/// <summary>
/// 生成默认管理员
/// </summary>
/// <returns></returns>
Task GenerateDefaultAdminAsync();
/// <summary>
/// 修改密码
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> ChangePasswordAsync(ChangePasswordParams model);
/// <summary>
/// 获取用户信息
/// </summary>
/// <returns></returns>
Task<ApiResponse<User>> GetUserInfoAsync();
/// <summary>
/// 修改用户信息
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> EditAsync(EditUserParams model);
/// <summary>
/// 获取用户地址
/// </summary>
/// <returns></returns>
ApiResponse<List<Address>> GetAddresses();
/// <summary>
/// 新增/修改地址
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
Task<ApiResponse> EditAddressAsync(EditAddressParams model);
/// <summary>
/// 删除地址
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<ApiResponse> DeleteAddressAsync(int id);
}

View 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);
}
}

View 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
}

View 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);
}
}

View 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);
}
}

View 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 ?? ""
};
}
}

View 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);
}
}

View File

@@ -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}";
}
}

View 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
}

View 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();
}
}

View 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);
}
}

View 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();
}
}

View 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
}

View File

@@ -0,0 +1,10 @@
namespace StopShopping.Services;
public record JwtOptions
{
public string? ValidAudience { get; set; }
public string? ValidIssuer { get; set; }
public string? SigningKey { get; set; }
public int AccessTokenExpiresIn { get; set; }
public int RefreshTokenExpiresIn { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace StopShopping.Services.Models.Req;
public record CategoryIdParams
{
public int CategoryId { get; set; }
}

View File

@@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;
namespace StopShopping.Services.Models.Req;
/// <summary>
/// 修改密码
/// </summary>
/// <value></value>
public record ChangePasswordParams
{
/// <summary>
/// 原密码
/// </summary>
/// <value></value>
[Required]
public string? OldPassword { get; set; }
/// <summary>
/// 新密码
/// </summary>
/// <value></value>
[Required]
[MinLength(6)]
public string? NewPassword { get; set; }
}

View File

@@ -0,0 +1,36 @@
using System.ComponentModel.DataAnnotations;
namespace StopShopping.Services.Models.Req;
/// <summary>
/// 创建需求请求
/// </summary>
/// <value></value>
public record CreateRequestParams
{
/// <summary>
/// 商品名
/// </summary>
/// <value></value>
[Required]
[MaxLength(100)]
public string Name { get; set; } = string.Empty;
/// <summary>
/// 描述
/// </summary>
/// <value></value>
[MaxLength(1000)]
public string? Description { get; set; }
/// <summary>
/// 分类id
/// </summary>
/// <value></value>
public int CategoryId { get; set; }
/// <summary>
/// 截止日期
/// </summary>
/// <value></value>
[Required]
[RegularExpression(@"^\d{4}\D\d{1,2}\D\d{1,2}$")]
public string Deadline { get; set; } = string.Empty;
}

View File

@@ -0,0 +1,6 @@
namespace StopShopping.Services.Models.Req;
public record DistrictParentIdParams
{
public int ParentId { get; set; }
}

View File

@@ -0,0 +1,67 @@
using System.ComponentModel.DataAnnotations;
namespace StopShopping.Services.Models.Req;
/// <summary>
/// 新增/修改收货地址
/// </summary>
/// <value></value>
public record EditAddressParams
{
/// <summary>
/// 大于0时为修改
/// </summary>
/// <value></value>
public int? Id { get; set; }
/// <summary>
/// 姓名
/// </summary>
/// <value></value>
[Required]
[MaxLength(20)]
public string Name { get; set; } = string.Empty;
/// <summary>
/// 联系电话
/// </summary>
/// <value></value>
[Required]
[Phone]
public string Telephone { get; set; } = string.Empty;
/// <summary>
/// 自定义标签
/// </summary>
/// <value></value>
[MaxLength(20)]
public string? Tag { get; set; }
/// <summary>
/// 是否默认地址
/// </summary>
/// <value></value>
public bool Default { get; set; }
/// <summary>
/// 区域id表示省/直辖市
/// </summary>
/// <value></value>
public int DistrictLevel1Id { get; set; }
/// <summary>
/// 区域id表示市/直辖市时为空
/// </summary>
/// <value></value>
public int? DistrictLevel2Id { get; set; }
/// <summary>
/// 区域id表示区
/// </summary>
/// <value></value>
public int DistrictLevel3Id { get; set; }
/// <summary>
/// 区域id表示街道/镇
/// </summary>
/// <value></value>
public int DistrictLevel4Id { get; set; }
/// <summary>
/// 详细地址
/// </summary>
/// <value></value>
[MaxLength(200)]
public string? Detail { get; set; }
}

View File

@@ -0,0 +1,34 @@
using System.ComponentModel.DataAnnotations;
namespace StopShopping.Services.Models.Req;
/// <summary>
/// 新增/修改分类
/// </summary>
/// <value></value>
public record EditCategoryParams
{
/// <summary>
/// 大于0时修改
/// </summary>
/// <value></value>
public int? Id { get; set; }
/// <summary>
/// 顶级为0
/// </summary>
/// <value></value>
public int ParentId { get; set; }
/// <summary>
/// 名称j
/// </summary>
/// <value></value>
[Required]
[MaxLength(50)]
public string Name { get; set; } = string.Empty;
/// <summary>
/// 空时保持不变
/// </summary>
/// <value></value>
[MaxLength(50)]
public string? Logo { get; set; }
}

View File

@@ -0,0 +1,56 @@
using System.ComponentModel.DataAnnotations;
namespace StopShopping.Services.Models.Req;
/// <summary>
/// 新增/修改商品
/// </summary>
public record EditProductParams
{
/// <summary>
/// 大于0时修改
/// </summary>
/// <value></value>
public int? Id { get; set; }
/// <summary>
/// 商品名称
/// </summary>
/// <value></value>
[Required]
[MaxLength(100)]
public string? Name { get; set; }
/// <summary>
/// 简介
/// </summary>
/// <value></value>
[MaxLength(200)]
public string? Description { get; set; }
/// <summary>
/// 图片名,修改时传空保持不变
/// </summary>
/// <value></value>
[MaxLength(50)]
public string? LogoName { get; set; }
/// <summary>
/// 分类id
/// </summary>
/// <value></value>
public int CategoryId { get; set; }
/// <summary>
/// 最小销售单元
/// </summary>
/// <value></value>
[Required]
[MaxLength(20)]
public string? MinimumUnit { get; set; }
/// <summary>
/// 单价
/// </summary>
/// <value></value>
public decimal UnitPrice { get; set; }
/// <summary>
/// 详情
/// </summary>
/// <value></value>
public string? Detail { get; set; }
}

View File

@@ -0,0 +1,34 @@
using System.ComponentModel.DataAnnotations;
namespace StopShopping.Services.Models.Req;
/// <summary>
/// 修改用户资料
/// </summary>
/// <value></value>
public record EditUserParams
{
/// <summary>
/// 昵称
/// </summary>
/// <value></value>
[Length(2, 50)]
[Required]
public string NickName { get; set; } = string.Empty;
/// <summary>
/// 头像文件名
/// </summary>
/// <value></value>
public string? AvatarFileName { get; set; }
/// <summary>
/// 默认角色
/// </summary>
/// <value></value>
public UserRoles DefaultRole { get; set; }
/// <summary>
/// 联系电话
/// </summary>
/// <value></value>
[Phone]
public string? Telephone { get; set; }
}

View File

@@ -0,0 +1,18 @@
namespace StopShopping.Services.Models.Req;
/// <summary>
/// 分页搜索
/// </summary>
public record PagedSearch
{
/// <summary>
/// 页码
/// </summary>
/// <value></value>
public int PageIndex { get; set; }
/// <summary>
/// 页大小
/// </summary>
/// <value></value>
public int PageSize { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace StopShopping.Services.Models.Req;
public record ProductIdParams
{
public int ProductId { get; set; }
}

View File

@@ -0,0 +1,32 @@
namespace StopShopping.Services.Models.Req;
/// <summary>
/// 商品搜索排序
/// </summary>
public enum ProductOrderBys
{
/// <summary>
/// 添加时间
/// </summary>
CreateTime,
/// <summary>
/// 添加时间倒序
/// </summary>
CreateTimeDesc,
/// <summary>
/// 分类
/// </summary>
Category,
/// <summary>
/// 分类倒序
/// </summary>
CategoryDesc,
/// <summary>
/// 已售数量
/// </summary>
SoldAmount,
/// <summary>
/// 已售数量倒序
/// </summary>
SoldAmountDesc,
}

View File

@@ -0,0 +1,29 @@
using System.ComponentModel.DataAnnotations;
namespace StopShopping.Services.Models.Req;
/// <summary>
/// 分页搜索商品
/// </summary>
public record ProductSearchParms : PagedSearch
{
/// <summary>
/// 搜索此分类及下级所有商品
/// </summary>
/// <value></value>
public int? CategoryId { get; set; }
/// <summary>
/// 商品名、描述关键字
/// </summary>
/// <value></value>
public string? Keyword { get; set; }
/// <summary>
/// 排序条件
/// </summary>
/// <value></value>
[MinLength(1)]
public ProductOrderBys[] OrderBys { get; set; } = [
ProductOrderBys.CreateTimeDesc,
ProductOrderBys.Category
];
}

View File

@@ -0,0 +1,34 @@
namespace StopShopping.Services.Models.Req;
/// <summary>
/// 竞标参数
/// </summary>
/// <value></value>
public record ReplyParams
{
/// <summary>
/// 需求id
/// </summary>
/// <value></value>
public int RequestId { get; set; }
/// <summary>
/// 商品id
/// </summary>
/// <value></value>
public int ProductId { get; set; }
/// <summary>
/// 数量
/// </summary>
/// <value></value>
public int Amount { get; set; }
/// <summary>
/// 价格
/// </summary>
/// <value></value>
public decimal Price { get; set; }
/// <summary>
/// 留言
/// </summary>
/// <value></value>
public string? Memo { get; set; }
}

View File

@@ -0,0 +1,6 @@
namespace StopShopping.Services.Models.Req;
public record RequestIdParams
{
public int RequestId { get; set; }
}

Some files were not shown because too many files have changed in this diff Show More