diff --git a/backend/IMTest/bin/Debug/net8.0/IMTest.dll b/backend/IMTest/bin/Debug/net8.0/IMTest.dll index 4c5461c..a4a7827 100644 Binary files a/backend/IMTest/bin/Debug/net8.0/IMTest.dll and b/backend/IMTest/bin/Debug/net8.0/IMTest.dll differ diff --git a/backend/IMTest/bin/Debug/net8.0/IMTest.pdb b/backend/IMTest/bin/Debug/net8.0/IMTest.pdb index a90a420..d7f873d 100644 Binary files a/backend/IMTest/bin/Debug/net8.0/IMTest.pdb and b/backend/IMTest/bin/Debug/net8.0/IMTest.pdb differ diff --git a/backend/IMTest/bin/Debug/net8.0/IM_API.dll b/backend/IMTest/bin/Debug/net8.0/IM_API.dll index 16a00ed..562d8b5 100644 Binary files a/backend/IMTest/bin/Debug/net8.0/IM_API.dll and b/backend/IMTest/bin/Debug/net8.0/IM_API.dll differ diff --git a/backend/IMTest/bin/Debug/net8.0/IM_API.exe b/backend/IMTest/bin/Debug/net8.0/IM_API.exe index fd67847..1292e00 100644 Binary files a/backend/IMTest/bin/Debug/net8.0/IM_API.exe and b/backend/IMTest/bin/Debug/net8.0/IM_API.exe differ diff --git a/backend/IMTest/bin/Debug/net8.0/IM_API.pdb b/backend/IMTest/bin/Debug/net8.0/IM_API.pdb index 6fb8b01..632c921 100644 Binary files a/backend/IMTest/bin/Debug/net8.0/IM_API.pdb and b/backend/IMTest/bin/Debug/net8.0/IM_API.pdb differ diff --git a/backend/IMTest/obj/Debug/net8.0/IMTest.AssemblyInfo.cs b/backend/IMTest/obj/Debug/net8.0/IMTest.AssemblyInfo.cs index ff595c8..392f7ac 100644 --- a/backend/IMTest/obj/Debug/net8.0/IMTest.AssemblyInfo.cs +++ b/backend/IMTest/obj/Debug/net8.0/IMTest.AssemblyInfo.cs @@ -14,7 +14,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("IMTest")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5274f0d22d4fe646d03a3bc0ea6621d299074816")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+1df3a0f30e469671b3e81d88574871d99c98af1a")] [assembly: System.Reflection.AssemblyProductAttribute("IMTest")] [assembly: System.Reflection.AssemblyTitleAttribute("IMTest")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/backend/IMTest/obj/Debug/net8.0/IMTest.AssemblyInfoInputs.cache b/backend/IMTest/obj/Debug/net8.0/IMTest.AssemblyInfoInputs.cache index 815557e..3a83419 100644 --- a/backend/IMTest/obj/Debug/net8.0/IMTest.AssemblyInfoInputs.cache +++ b/backend/IMTest/obj/Debug/net8.0/IMTest.AssemblyInfoInputs.cache @@ -1 +1 @@ -7390b702e3c578dad3a8fa4fa4cc93b25ccd34a9b353beca60372a7182717d73 +9546071857f1b0fa09bedf887ca476b7ecfa579752230651321c2a145e579c92 diff --git a/backend/IMTest/obj/Debug/net8.0/IMTest.GeneratedMSBuildEditorConfig.editorconfig b/backend/IMTest/obj/Debug/net8.0/IMTest.GeneratedMSBuildEditorConfig.editorconfig index aa41851..c718a10 100644 --- a/backend/IMTest/obj/Debug/net8.0/IMTest.GeneratedMSBuildEditorConfig.editorconfig +++ b/backend/IMTest/obj/Debug/net8.0/IMTest.GeneratedMSBuildEditorConfig.editorconfig @@ -1,5 +1,7 @@ is_global = true build_property.TargetFramework = net8.0 +build_property.TargetFrameworkIdentifier = .NETCoreApp +build_property.TargetFrameworkVersion = v8.0 build_property.TargetPlatformMinVersion = build_property.UsingMicrosoftNETSdkWeb = build_property.ProjectTypeGuids = diff --git a/backend/IMTest/obj/Debug/net8.0/IMTest.GlobalUsings.g.cs b/backend/IMTest/obj/Debug/net8.0/IMTest.GlobalUsings.g.cs index 2cd3d38..fe43752 100644 --- a/backend/IMTest/obj/Debug/net8.0/IMTest.GlobalUsings.g.cs +++ b/backend/IMTest/obj/Debug/net8.0/IMTest.GlobalUsings.g.cs @@ -1,9 +1,9 @@ // -global using global::System; -global using global::System.Collections.Generic; -global using global::System.IO; -global using global::System.Linq; -global using global::System.Net.Http; -global using global::System.Threading; -global using global::System.Threading.Tasks; -global using global::Xunit; +global using System; +global using System.Collections.Generic; +global using System.IO; +global using System.Linq; +global using System.Net.Http; +global using System.Threading; +global using System.Threading.Tasks; +global using Xunit; diff --git a/backend/IMTest/obj/Debug/net8.0/IMTest.assets.cache b/backend/IMTest/obj/Debug/net8.0/IMTest.assets.cache index 21dec49..94ea5c2 100644 Binary files a/backend/IMTest/obj/Debug/net8.0/IMTest.assets.cache and b/backend/IMTest/obj/Debug/net8.0/IMTest.assets.cache differ diff --git a/backend/IMTest/obj/Debug/net8.0/IMTest.csproj.AssemblyReference.cache b/backend/IMTest/obj/Debug/net8.0/IMTest.csproj.AssemblyReference.cache index a6386cf..0eb737b 100644 Binary files a/backend/IMTest/obj/Debug/net8.0/IMTest.csproj.AssemblyReference.cache and b/backend/IMTest/obj/Debug/net8.0/IMTest.csproj.AssemblyReference.cache differ diff --git a/backend/IMTest/obj/Debug/net8.0/IMTest.dll b/backend/IMTest/obj/Debug/net8.0/IMTest.dll index 4c5461c..a4a7827 100644 Binary files a/backend/IMTest/obj/Debug/net8.0/IMTest.dll and b/backend/IMTest/obj/Debug/net8.0/IMTest.dll differ diff --git a/backend/IMTest/obj/Debug/net8.0/IMTest.pdb b/backend/IMTest/obj/Debug/net8.0/IMTest.pdb index a90a420..d7f873d 100644 Binary files a/backend/IMTest/obj/Debug/net8.0/IMTest.pdb and b/backend/IMTest/obj/Debug/net8.0/IMTest.pdb differ diff --git a/backend/IMTest/obj/Debug/net8.0/ref/IMTest.dll b/backend/IMTest/obj/Debug/net8.0/ref/IMTest.dll index 5f6f5ea..79b0187 100644 Binary files a/backend/IMTest/obj/Debug/net8.0/ref/IMTest.dll and b/backend/IMTest/obj/Debug/net8.0/ref/IMTest.dll differ diff --git a/backend/IMTest/obj/Debug/net8.0/refint/IMTest.dll b/backend/IMTest/obj/Debug/net8.0/refint/IMTest.dll index 5f6f5ea..79b0187 100644 Binary files a/backend/IMTest/obj/Debug/net8.0/refint/IMTest.dll and b/backend/IMTest/obj/Debug/net8.0/refint/IMTest.dll differ diff --git a/backend/IMTest/obj/IMTest.csproj.nuget.dgspec.json b/backend/IMTest/obj/IMTest.csproj.nuget.dgspec.json index 4e226be..ab66086 100644 --- a/backend/IMTest/obj/IMTest.csproj.nuget.dgspec.json +++ b/backend/IMTest/obj/IMTest.csproj.nuget.dgspec.json @@ -49,7 +49,7 @@ "auditLevel": "low", "auditMode": "direct" }, - "SdkAnalysisLevel": "9.0.300" + "SdkAnalysisLevel": "10.0.100" }, "frameworks": { "net8.0": { @@ -96,7 +96,7 @@ "privateAssets": "all" } }, - "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\9.0.304/PortableRuntimeIdentifierGraph.json" + "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\10.0.102/PortableRuntimeIdentifierGraph.json" } } }, @@ -141,7 +141,7 @@ "auditLevel": "low", "auditMode": "direct" }, - "SdkAnalysisLevel": "9.0.300" + "SdkAnalysisLevel": "10.0.100" }, "frameworks": { "net8.0": { @@ -155,6 +155,10 @@ "target": "Package", "version": "[12.0.0, )" }, + "MassTransit.RabbitMQ": { + "target": "Package", + "version": "[8.5.5, )" + }, "Microsoft.AspNetCore.Authentication.JwtBearer": { "target": "Package", "version": "[8.0.21, )" @@ -219,7 +223,7 @@ "privateAssets": "all" } }, - "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\9.0.304/PortableRuntimeIdentifierGraph.json" + "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\10.0.102/PortableRuntimeIdentifierGraph.json" } } } diff --git a/backend/IMTest/obj/IMTest.csproj.nuget.g.props b/backend/IMTest/obj/IMTest.csproj.nuget.g.props index a72789c..89b6bef 100644 --- a/backend/IMTest/obj/IMTest.csproj.nuget.g.props +++ b/backend/IMTest/obj/IMTest.csproj.nuget.g.props @@ -7,7 +7,7 @@ $(UserProfile)\.nuget\packages\ C:\Users\nanxun\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages PackageReference - 6.14.1 + 7.0.0 diff --git a/backend/IMTest/obj/project.assets.json b/backend/IMTest/obj/project.assets.json index 74d6407..411bfb3 100644 --- a/backend/IMTest/obj/project.assets.json +++ b/backend/IMTest/obj/project.assets.json @@ -53,6 +53,57 @@ "build/netstandard1.0/coverlet.collector.targets": {} } }, + "MassTransit/8.5.5": { + "type": "package", + "dependencies": { + "MassTransit.Abstractions": "8.5.5", + "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0", + "Microsoft.Extensions.Diagnostics.HealthChecks": "8.0.0", + "Microsoft.Extensions.Hosting.Abstractions": "8.0.0", + "Microsoft.Extensions.Logging.Abstractions": "8.0.0", + "Microsoft.Extensions.Options": "8.0.0" + }, + "compile": { + "lib/net8.0/MassTransit.dll": { + "related": ".xml" + } + }, + "runtime": { + "lib/net8.0/MassTransit.dll": { + "related": ".xml" + } + } + }, + "MassTransit.Abstractions/8.5.5": { + "type": "package", + "compile": { + "lib/net8.0/MassTransit.Abstractions.dll": { + "related": ".xml" + } + }, + "runtime": { + "lib/net8.0/MassTransit.Abstractions.dll": { + "related": ".xml" + } + } + }, + "MassTransit.RabbitMQ/8.5.5": { + "type": "package", + "dependencies": { + "MassTransit": "8.5.5", + "RabbitMQ.Client": "7.1.2" + }, + "compile": { + "lib/net8.0/MassTransit.RabbitMqTransport.dll": { + "related": ".xml" + } + }, + "runtime": { + "lib/net8.0/MassTransit.RabbitMqTransport.dll": { + "related": ".xml" + } + } + }, "Microsoft.AspNetCore.Authentication.Abstractions/2.3.0": { "type": "package", "dependencies": { @@ -664,6 +715,38 @@ "buildTransitive/net6.0/_._": {} } }, + "Microsoft.Extensions.Diagnostics.HealthChecks/8.0.0": { + "type": "package", + "dependencies": { + "Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions": "8.0.0", + "Microsoft.Extensions.Hosting.Abstractions": "8.0.0", + "Microsoft.Extensions.Logging.Abstractions": "8.0.0", + "Microsoft.Extensions.Options": "8.0.0" + }, + "compile": { + "lib/net8.0/Microsoft.Extensions.Diagnostics.HealthChecks.dll": { + "related": ".xml" + } + }, + "runtime": { + "lib/net8.0/Microsoft.Extensions.Diagnostics.HealthChecks.dll": { + "related": ".xml" + } + } + }, + "Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions/8.0.0": { + "type": "package", + "compile": { + "lib/net8.0/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.dll": { + "related": ".xml" + } + }, + "runtime": { + "lib/net8.0/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.dll": { + "related": ".xml" + } + } + }, "Microsoft.Extensions.FileProviders.Abstractions/8.0.0": { "type": "package", "dependencies": { @@ -1360,6 +1443,23 @@ } } }, + "RabbitMQ.Client/7.1.2": { + "type": "package", + "dependencies": { + "System.IO.Pipelines": "8.0.0", + "System.Threading.RateLimiting": "8.0.0" + }, + "compile": { + "lib/net8.0/RabbitMQ.Client.dll": { + "related": ".xml" + } + }, + "runtime": { + "lib/net8.0/RabbitMQ.Client.dll": { + "related": ".xml" + } + } + }, "runtime.debian.8-x64.runtime.native.System.Security.Cryptography.OpenSsl/4.3.0": { "type": "package", "runtimeTargets": { @@ -2655,6 +2755,22 @@ "buildTransitive/net6.0/_._": {} } }, + "System.Threading.RateLimiting/8.0.0": { + "type": "package", + "compile": { + "lib/net8.0/System.Threading.RateLimiting.dll": { + "related": ".xml" + } + }, + "runtime": { + "lib/net8.0/System.Threading.RateLimiting.dll": { + "related": ".xml" + } + }, + "build": { + "buildTransitive/net6.0/_._": {} + } + }, "System.Threading.Tasks/4.3.0": { "type": "package", "dependencies": { @@ -2859,6 +2975,7 @@ "dependencies": { "AutoMapper": "12.0.1", "AutoMapper.Extensions.Microsoft.DependencyInjection": "12.0.0", + "MassTransit.RabbitMQ": "8.5.5", "Microsoft.AspNetCore.Authentication.JwtBearer": "8.0.21", "Microsoft.AspNetCore.SignalR": "1.2.0", "Microsoft.VisualStudio.Azure.Containers.Tools.Targets": "1.22.1", @@ -2987,6 +3104,69 @@ "coverlet.collector.nuspec" ] }, + "MassTransit/8.5.5": { + "sha512": "bSg8k5q+rP1s+dIGXLLbctqDGdIkfDjdxwNWtCUH7xNCN9ZuM7mqSPQPIFgaYIi34e81m4FqAqo4CAHuWPkhRA==", + "type": "package", + "path": "masstransit/8.5.5", + "files": [ + ".nupkg.metadata", + ".signature.p7s", + "NuGet.README.md", + "lib/net472/MassTransit.dll", + "lib/net472/MassTransit.xml", + "lib/net8.0/MassTransit.dll", + "lib/net8.0/MassTransit.xml", + "lib/net9.0/MassTransit.dll", + "lib/net9.0/MassTransit.xml", + "lib/netstandard2.0/MassTransit.dll", + "lib/netstandard2.0/MassTransit.xml", + "masstransit.8.5.5.nupkg.sha512", + "masstransit.nuspec", + "mt-logo-small.png" + ] + }, + "MassTransit.Abstractions/8.5.5": { + "sha512": "0mn2Ay17dD6z5tgSLjbVRlldSbL9iowzFEfVgVfBXVG5ttz9dSWeR4TrdD6pqH93GWXp4CvSmF8i1HqxLX7DZw==", + "type": "package", + "path": "masstransit.abstractions/8.5.5", + "files": [ + ".nupkg.metadata", + ".signature.p7s", + "NuGet.README.md", + "lib/net472/MassTransit.Abstractions.dll", + "lib/net472/MassTransit.Abstractions.xml", + "lib/net8.0/MassTransit.Abstractions.dll", + "lib/net8.0/MassTransit.Abstractions.xml", + "lib/net9.0/MassTransit.Abstractions.dll", + "lib/net9.0/MassTransit.Abstractions.xml", + "lib/netstandard2.0/MassTransit.Abstractions.dll", + "lib/netstandard2.0/MassTransit.Abstractions.xml", + "masstransit.abstractions.8.5.5.nupkg.sha512", + "masstransit.abstractions.nuspec", + "mt-logo-small.png" + ] + }, + "MassTransit.RabbitMQ/8.5.5": { + "sha512": "UxWn4o90YVMF9PBkJeoskOFPneh6YtnI1fLJHtvZiSAG0eoiRrWPGa+6FQCvjkQ/ljCKfjzok2eGZc/vmNZ01A==", + "type": "package", + "path": "masstransit.rabbitmq/8.5.5", + "files": [ + ".nupkg.metadata", + ".signature.p7s", + "NuGet.README.md", + "lib/net472/MassTransit.RabbitMqTransport.dll", + "lib/net472/MassTransit.RabbitMqTransport.xml", + "lib/net8.0/MassTransit.RabbitMqTransport.dll", + "lib/net8.0/MassTransit.RabbitMqTransport.xml", + "lib/net9.0/MassTransit.RabbitMqTransport.dll", + "lib/net9.0/MassTransit.RabbitMqTransport.xml", + "lib/netstandard2.0/MassTransit.RabbitMqTransport.dll", + "lib/netstandard2.0/MassTransit.RabbitMqTransport.xml", + "masstransit.rabbitmq.8.5.5.nupkg.sha512", + "masstransit.rabbitmq.nuspec", + "mt-logo-small.png" + ] + }, "Microsoft.AspNetCore.Authentication.Abstractions/2.3.0": { "sha512": "ve6uvLwKNRkfnO/QeN9M8eUJ49lCnWv/6/9p6iTEuiI6Rtsz+myaBAjdMzLuTViQY032xbTF5AdZF5BJzJJyXQ==", "type": "package", @@ -3887,6 +4067,44 @@ "useSharedDesignerContext.txt" ] }, + "Microsoft.Extensions.Diagnostics.HealthChecks/8.0.0": { + "sha512": "P9SoBuVZhJPpALZmSq72aQEb9ryP67EdquaCZGXGrrcASTNHYdrUhnpgSwIipgM5oVC+dKpRXg5zxobmF9xr5g==", + "type": "package", + "path": "microsoft.extensions.diagnostics.healthchecks/8.0.0", + "files": [ + ".nupkg.metadata", + ".signature.p7s", + "Icon.png", + "THIRD-PARTY-NOTICES.TXT", + "lib/net462/Microsoft.Extensions.Diagnostics.HealthChecks.dll", + "lib/net462/Microsoft.Extensions.Diagnostics.HealthChecks.xml", + "lib/net8.0/Microsoft.Extensions.Diagnostics.HealthChecks.dll", + "lib/net8.0/Microsoft.Extensions.Diagnostics.HealthChecks.xml", + "lib/netstandard2.0/Microsoft.Extensions.Diagnostics.HealthChecks.dll", + "lib/netstandard2.0/Microsoft.Extensions.Diagnostics.HealthChecks.xml", + "microsoft.extensions.diagnostics.healthchecks.8.0.0.nupkg.sha512", + "microsoft.extensions.diagnostics.healthchecks.nuspec" + ] + }, + "Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions/8.0.0": { + "sha512": "AT2qqos3IgI09ok36Qag9T8bb6kHJ3uT9Q5ki6CySybFsK6/9JbvQAgAHf1pVEjST0/N4JaFaCbm40R5edffwg==", + "type": "package", + "path": "microsoft.extensions.diagnostics.healthchecks.abstractions/8.0.0", + "files": [ + ".nupkg.metadata", + ".signature.p7s", + "Icon.png", + "THIRD-PARTY-NOTICES.TXT", + "lib/net462/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.dll", + "lib/net462/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.xml", + "lib/net8.0/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.dll", + "lib/net8.0/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.xml", + "lib/netstandard2.0/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.dll", + "lib/netstandard2.0/Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions.xml", + "microsoft.extensions.diagnostics.healthchecks.abstractions.8.0.0.nupkg.sha512", + "microsoft.extensions.diagnostics.healthchecks.abstractions.nuspec" + ] + }, "Microsoft.Extensions.FileProviders.Abstractions/8.0.0": { "sha512": "ZbaMlhJlpisjuWbvXr4LdAst/1XxH3vZ6A0BsgTphZ2L4PGuxRLz7Jr/S7mkAAnOn78Vu0fKhEgNF5JO3zfjqQ==", "type": "package", @@ -4801,6 +5019,23 @@ "pomelo.entityframeworkcore.mysql.nuspec" ] }, + "RabbitMQ.Client/7.1.2": { + "sha512": "y3c6ulgULScWthHw5PLM1ShHRLhxg0vCtzX/hh61gRgNecL3ZC3WoBW2HYHoXOVRqTl99Br9E7CZEytGZEsCyQ==", + "type": "package", + "path": "rabbitmq.client/7.1.2", + "files": [ + ".nupkg.metadata", + ".signature.p7s", + "README.md", + "icon.png", + "lib/net8.0/RabbitMQ.Client.dll", + "lib/net8.0/RabbitMQ.Client.xml", + "lib/netstandard2.0/RabbitMQ.Client.dll", + "lib/netstandard2.0/RabbitMQ.Client.xml", + "rabbitmq.client.7.1.2.nupkg.sha512", + "rabbitmq.client.nuspec" + ] + }, "runtime.debian.8-x64.runtime.native.System.Security.Cryptography.OpenSsl/4.3.0": { "sha512": "HdSSp5MnJSsg08KMfZThpuLPJpPwE5hBXvHwoKWosyHHfe8Mh5WKT0ylEOf6yNzX6Ngjxe4Whkafh5q7Ymac4Q==", "type": "package", @@ -8051,6 +8286,35 @@ "useSharedDesignerContext.txt" ] }, + "System.Threading.RateLimiting/8.0.0": { + "sha512": "7mu9v0QDv66ar3DpGSZHg9NuNcxDaaAcnMULuZlaTpP9+hwXhrxNGsF5GmLkSHxFdb5bBc1TzeujsRgTrPWi+Q==", + "type": "package", + "path": "system.threading.ratelimiting/8.0.0", + "files": [ + ".nupkg.metadata", + ".signature.p7s", + "Icon.png", + "LICENSE.TXT", + "THIRD-PARTY-NOTICES.TXT", + "buildTransitive/net461/System.Threading.RateLimiting.targets", + "buildTransitive/net462/_._", + "buildTransitive/net6.0/_._", + "buildTransitive/netcoreapp2.0/System.Threading.RateLimiting.targets", + "lib/net462/System.Threading.RateLimiting.dll", + "lib/net462/System.Threading.RateLimiting.xml", + "lib/net6.0/System.Threading.RateLimiting.dll", + "lib/net6.0/System.Threading.RateLimiting.xml", + "lib/net7.0/System.Threading.RateLimiting.dll", + "lib/net7.0/System.Threading.RateLimiting.xml", + "lib/net8.0/System.Threading.RateLimiting.dll", + "lib/net8.0/System.Threading.RateLimiting.xml", + "lib/netstandard2.0/System.Threading.RateLimiting.dll", + "lib/netstandard2.0/System.Threading.RateLimiting.xml", + "system.threading.ratelimiting.8.0.0.nupkg.sha512", + "system.threading.ratelimiting.nuspec", + "useSharedDesignerContext.txt" + ] + }, "System.Threading.Tasks/4.3.0": { "sha512": "LbSxKEdOUhVe8BezB/9uOGGppt+nZf6e1VFyw6v3DN6lqitm0OSn2uXMOdtP0M3W4iMcqcivm2J6UgqiwwnXiA==", "type": "package", @@ -8541,7 +8805,7 @@ "auditLevel": "low", "auditMode": "direct" }, - "SdkAnalysisLevel": "9.0.300" + "SdkAnalysisLevel": "10.0.100" }, "frameworks": { "net8.0": { @@ -8588,8 +8852,16 @@ "privateAssets": "all" } }, - "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\9.0.304/PortableRuntimeIdentifierGraph.json" + "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\10.0.102/PortableRuntimeIdentifierGraph.json" } } - } + }, + "logs": [ + { + "code": "Undefined", + "level": "Warning", + "warningLevel": 1, + "message": "读取缓存文件 C:\\Users\\nanxun\\Documents\\IM\\backend\\IMTest\\obj\\project.nuget.cache 时遇到问题: '<' is an invalid start of a property name. Expected a '\"'. Path: $ | LineNumber: 2 | BytePositionInLine: 0." + } + ] } \ No newline at end of file diff --git a/backend/IMTest/obj/project.nuget.cache b/backend/IMTest/obj/project.nuget.cache index 696d138..18f8666 100644 --- a/backend/IMTest/obj/project.nuget.cache +++ b/backend/IMTest/obj/project.nuget.cache @@ -1,6 +1,6 @@ { "version": 2, - "dgSpecHash": "/x8TFp9yNjk=", + "dgSpecHash": "j7OjEXb1ZGE=", "success": true, "projectFilePath": "C:\\Users\\nanxun\\Documents\\IM\\backend\\IMTest\\IMTest.csproj", "expectedPackageFiles": [ @@ -8,6 +8,9 @@ "C:\\Users\\nanxun\\.nuget\\packages\\automapper.extensions.microsoft.dependencyinjection\\12.0.0\\automapper.extensions.microsoft.dependencyinjection.12.0.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\castle.core\\5.1.1\\castle.core.5.1.1.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\coverlet.collector\\6.0.0\\coverlet.collector.6.0.0.nupkg.sha512", + "C:\\Users\\nanxun\\.nuget\\packages\\masstransit\\8.5.5\\masstransit.8.5.5.nupkg.sha512", + "C:\\Users\\nanxun\\.nuget\\packages\\masstransit.abstractions\\8.5.5\\masstransit.abstractions.8.5.5.nupkg.sha512", + "C:\\Users\\nanxun\\.nuget\\packages\\masstransit.rabbitmq\\8.5.5\\masstransit.rabbitmq.8.5.5.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\microsoft.aspnetcore.authentication.abstractions\\2.3.0\\microsoft.aspnetcore.authentication.abstractions.2.3.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\microsoft.aspnetcore.authentication.jwtbearer\\8.0.21\\microsoft.aspnetcore.authentication.jwtbearer.8.0.21.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\microsoft.aspnetcore.authorization\\2.3.0\\microsoft.aspnetcore.authorization.2.3.0.nupkg.sha512", @@ -43,6 +46,8 @@ "C:\\Users\\nanxun\\.nuget\\packages\\microsoft.extensions.dependencyinjection\\8.0.1\\microsoft.extensions.dependencyinjection.8.0.1.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\microsoft.extensions.dependencyinjection.abstractions\\8.0.2\\microsoft.extensions.dependencyinjection.abstractions.8.0.2.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\microsoft.extensions.diagnostics.abstractions\\8.0.1\\microsoft.extensions.diagnostics.abstractions.8.0.1.nupkg.sha512", + "C:\\Users\\nanxun\\.nuget\\packages\\microsoft.extensions.diagnostics.healthchecks\\8.0.0\\microsoft.extensions.diagnostics.healthchecks.8.0.0.nupkg.sha512", + "C:\\Users\\nanxun\\.nuget\\packages\\microsoft.extensions.diagnostics.healthchecks.abstractions\\8.0.0\\microsoft.extensions.diagnostics.healthchecks.abstractions.8.0.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\microsoft.extensions.fileproviders.abstractions\\8.0.0\\microsoft.extensions.fileproviders.abstractions.8.0.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\microsoft.extensions.hosting.abstractions\\8.0.1\\microsoft.extensions.hosting.abstractions.8.0.1.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\microsoft.extensions.logging\\8.0.1\\microsoft.extensions.logging.8.0.1.nupkg.sha512", @@ -72,6 +77,7 @@ "C:\\Users\\nanxun\\.nuget\\packages\\nuget.frameworks\\6.5.0\\nuget.frameworks.6.5.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\pipelines.sockets.unofficial\\2.2.8\\pipelines.sockets.unofficial.2.2.8.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\pomelo.entityframeworkcore.mysql\\8.0.3\\pomelo.entityframeworkcore.mysql.8.0.3.nupkg.sha512", + "C:\\Users\\nanxun\\.nuget\\packages\\rabbitmq.client\\7.1.2\\rabbitmq.client.7.1.2.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\runtime.debian.8-x64.runtime.native.system.security.cryptography.openssl\\4.3.0\\runtime.debian.8-x64.runtime.native.system.security.cryptography.openssl.4.3.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\runtime.fedora.23-x64.runtime.native.system.security.cryptography.openssl\\4.3.0\\runtime.fedora.23-x64.runtime.native.system.security.cryptography.openssl.4.3.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\runtime.fedora.24-x64.runtime.native.system.security.cryptography.openssl\\4.3.0\\runtime.fedora.24-x64.runtime.native.system.security.cryptography.openssl.4.3.0.nupkg.sha512", @@ -148,6 +154,7 @@ "C:\\Users\\nanxun\\.nuget\\packages\\system.text.regularexpressions\\4.3.0\\system.text.regularexpressions.4.3.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\system.threading\\4.3.0\\system.threading.4.3.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\system.threading.channels\\8.0.0\\system.threading.channels.8.0.0.nupkg.sha512", + "C:\\Users\\nanxun\\.nuget\\packages\\system.threading.ratelimiting\\8.0.0\\system.threading.ratelimiting.8.0.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\system.threading.tasks\\4.3.0\\system.threading.tasks.4.3.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\system.threading.tasks.extensions\\4.3.0\\system.threading.tasks.extensions.4.3.0.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\system.threading.timer\\4.3.0\\system.threading.timer.4.3.0.nupkg.sha512", @@ -162,5 +169,15 @@ "C:\\Users\\nanxun\\.nuget\\packages\\xunit.extensibility.execution\\2.5.3\\xunit.extensibility.execution.2.5.3.nupkg.sha512", "C:\\Users\\nanxun\\.nuget\\packages\\xunit.runner.visualstudio\\2.5.3\\xunit.runner.visualstudio.2.5.3.nupkg.sha512" ], - "logs": [] + "logs": [ + { + "code": "Undefined", + "level": "Warning", + "message": "读取缓存文件 C:\\Users\\nanxun\\Documents\\IM\\backend\\IMTest\\obj\\project.nuget.cache 时遇到问题: '<' is an invalid start of a property name. Expected a '\"'. Path: $ | LineNumber: 2 | BytePositionInLine: 0.", + "projectPath": "C:\\Users\\nanxun\\Documents\\IM\\backend\\IMTest\\IMTest.csproj", + "warningLevel": 1, + "filePath": "C:\\Users\\nanxun\\Documents\\IM\\backend\\IMTest\\IMTest.csproj", + "targetGraphs": [] + } + ] } \ No newline at end of file diff --git a/backend/IM_API/Application/EventHandlers/ConversationEventHandler.cs b/backend/IM_API/Application/EventHandlers/ConversationEventHandler.cs deleted file mode 100644 index 057e5e5..0000000 --- a/backend/IM_API/Application/EventHandlers/ConversationEventHandler.cs +++ /dev/null @@ -1,88 +0,0 @@ -using AutoMapper; -using IM_API.Application.Interfaces; -using IM_API.Domain.Events; -using IM_API.Dtos; -using IM_API.Exceptions; -using IM_API.Interface.Services; -using IM_API.Models; -using IM_API.Services; -using IM_API.Tools; -using Microsoft.EntityFrameworkCore; -using Newtonsoft.Json; - -namespace IM_API.Application.EventHandlers -{ - public class ConversationEventHandler : IEventHandler - { - private readonly IConversationService _conversationService; - private readonly ILogger _logger; - private readonly ImContext _context; - private readonly IMapper _mapper; - public ConversationEventHandler( - IConversationService conversationService, - ILogger logger, - ImContext imContext, - IMapper mapper - ) - { - _conversationService = conversationService; - _logger = logger; - _context = imContext; - _mapper = mapper; - } - - /* - * 此方法有并发问题,当双方同时第一次发送消息时, - * 会出现同时创建的情况,其中一方会报错, - * 导致也未走到更新逻辑,会话丢失 - */ - public async Task Handle(MessageCreatedEvent @event) - { - //此处仅处理私聊会话创建 - if (@event.ChatType == ChatType.GROUP) - { - return; - } - var conversation = await _context.Conversations.FirstOrDefaultAsync( - x => x.UserId == @event.MsgSenderId && x.TargetId == @event.MsgRecipientId - ); - //如果首次发消息则创建双方会话 - if (conversation is null) - { - Conversation senderCon = _mapper.Map(@event); - Conversation ReceptCon = _mapper.Map(@event); - ReceptCon.UserId = @event.MsgRecipientId; - ReceptCon.TargetId = @event.MsgSenderId; - ReceptCon.UnreadCount += 1; - ReceptCon.LastReadMessageId = null; - _context.Conversations.AddRange(senderCon,ReceptCon); - await _context.SaveChangesAsync(); - } - else - { - Conversation senderCon = conversation; - Conversation? ReceptCon = await _context.Conversations.FirstOrDefaultAsync( - x => x.UserId == @event.MsgRecipientId && x.TargetId == @event.MsgSenderId); - if (ReceptCon is null) - { - _logger.LogError("ConversationEventHandlerError:接收者会话对象缺失!Event:{Event}", JsonConvert.SerializeObject(@event)); - throw new BaseException(CodeDefine.SYSTEM_ERROR); - } - - //更新发送者conversation - senderCon.UnreadCount = 0; - senderCon.LastReadMessageId = @event.MessageId; - senderCon.LastMessage = @event.MessageContent; - senderCon.LastMessageTime = DateTime.Now; - //更新接收者conversation - ReceptCon.UnreadCount += 1; - ReceptCon.LastMessage = @event.MessageContent; - senderCon.LastMessageTime = DateTime.Now; - _context.Conversations.UpdateRange(senderCon, ReceptCon); - await _context.SaveChangesAsync(); - - - } - } - } -} diff --git a/backend/IM_API/Application/EventHandlers/FriendAddHandler/FriendAddConversationHandler.cs b/backend/IM_API/Application/EventHandlers/FriendAddHandler/FriendAddConversationHandler.cs new file mode 100644 index 0000000..99f62fe --- /dev/null +++ b/backend/IM_API/Application/EventHandlers/FriendAddHandler/FriendAddConversationHandler.cs @@ -0,0 +1,13 @@ +using IM_API.Domain.Events; +using MassTransit; + +namespace IM_API.Application.EventHandlers.FriendAddHandler +{ + public class FriendAddConversationHandler : IConsumer + { + public Task Consume(ConsumeContext context) + { + throw new NotImplementedException(); + } + } +} diff --git a/backend/IM_API/Application/EventHandlers/FriendAddHandler/FriendAddSignalRHandler.cs b/backend/IM_API/Application/EventHandlers/FriendAddHandler/FriendAddSignalRHandler.cs new file mode 100644 index 0000000..5bf323e --- /dev/null +++ b/backend/IM_API/Application/EventHandlers/FriendAddHandler/FriendAddSignalRHandler.cs @@ -0,0 +1,31 @@ +using IM_API.Domain.Events; +using IM_API.Models; +using MassTransit; + +namespace IM_API.Application.EventHandlers.FriendAddHandler +{ + public class FriendAddSignalRHandler : IConsumer + { + private readonly ImContext _context; + public FriendAddSignalRHandler(ImContext context) + { + _context = context; + } + + public Task Consume(ConsumeContext context) + { + throw new NotImplementedException(); + var @event = context.Message; + + var RequestfriendShip = new Friend() + { + Avatar = @event.ResponseUser.Avatar, + UserId = @event.RequestUser.Id, + RemarkName = @event.RequestInfo.NickName, + Created = @event.RequestInfo.Created, + + + }; + } + } +} diff --git a/backend/IM_API/Application/EventHandlers/MessageCreatedHandler/ConversationEventHandler.cs b/backend/IM_API/Application/EventHandlers/MessageCreatedHandler/ConversationEventHandler.cs new file mode 100644 index 0000000..405f728 --- /dev/null +++ b/backend/IM_API/Application/EventHandlers/MessageCreatedHandler/ConversationEventHandler.cs @@ -0,0 +1,63 @@ +using AutoMapper; +using IM_API.Application.Interfaces; +using IM_API.Domain.Events; +using IM_API.Dtos; +using IM_API.Exceptions; +using IM_API.Interface.Services; +using IM_API.Models; +using IM_API.Services; +using IM_API.Tools; +using MassTransit; +using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; + +namespace IM_API.Application.EventHandlers.MessageCreatedHandler +{ + public class ConversationEventHandler : IConsumer + { + private readonly IConversationService _conversationService; + private readonly ILogger _logger; + private readonly ImContext _context; + private readonly IMapper _mapper; + public ConversationEventHandler( + IConversationService conversationService, + ILogger logger, + ImContext imContext, + IMapper mapper + ) + { + _conversationService = conversationService; + _logger = logger; + _context = imContext; + _mapper = mapper; + } + + public async Task Consume(ConsumeContext context) + { + var @event = context.Message; + + if (@event.ChatType == ChatType.PRIVATE) + { + Conversation? userAConversation = await _context.Conversations.FirstOrDefaultAsync( + x => x.UserId == @event.MsgSenderId && x.TargetId == @event.MsgRecipientId + ); + Conversation? userBConversation = await _context.Conversations.FirstOrDefaultAsync( + x => x.UserId == @event.MsgRecipientId && x.TargetId == @event.MsgSenderId + ); + if (userAConversation is null || userBConversation is null) + { + _logger.LogError("消息事件更新会话信息失败:{@event}", @event); + } + userAConversation.LastMessage = @event.MessageContent; + userAConversation.LastReadMessageId = @event.MessageId; + userAConversation.LastMessageTime = @event.MessageCreated; + userBConversation.LastMessage = @event.MessageContent; + userBConversation.UnreadCount += 1; + userBConversation.LastMessageTime = @event.MessageCreated; + _context.UpdateRange(userAConversation, userBConversation); + await _context.SaveChangesAsync(); + } + } + + } +} diff --git a/backend/IM_API/Application/EventHandlers/MessageCreatedHandler/SignalREventHandler.cs b/backend/IM_API/Application/EventHandlers/MessageCreatedHandler/SignalREventHandler.cs new file mode 100644 index 0000000..643642c --- /dev/null +++ b/backend/IM_API/Application/EventHandlers/MessageCreatedHandler/SignalREventHandler.cs @@ -0,0 +1,43 @@ +using AutoMapper; +using IM_API.Application.Interfaces; +using IM_API.Domain.Events; +using IM_API.Dtos; +using IM_API.Hubs; +using IM_API.Models; +using IM_API.Tools; +using MassTransit; +using Microsoft.AspNetCore.SignalR; + +namespace IM_API.Application.EventHandlers.MessageCreatedHandler +{ + public class SignalREventHandler : IConsumer + { + private readonly IHubContext _hub; + private readonly IMapper _mapper; + public SignalREventHandler(IHubContext hub, IMapper mapper) + { + _hub = hub; + _mapper = mapper; + } + + public async Task Consume(ConsumeContext context) + { + var @event = context.Message; + if (@event.ChatType == Models.ChatType.PRIVATE) + { + MessageBaseDto messageBaseDto = new MessageBaseDto + { + MsgId = @event.MessageId.ToString(), + ChatType = @event.ChatType.ToString(), + Content = @event.MessageContent, + GroupMemberId = null, + ReceiverId = @event.MsgRecipientId, + SenderId = @event.MsgSenderId, + TimeStamp = @event.MessageCreated, + Type = @event.MessageMsgType.ToString() + }; + await _hub.Clients.Users(@event.MsgRecipientId.ToString()).SendAsync("ReceiveMessage", messageBaseDto); + } + } + } +} diff --git a/backend/IM_API/Application/EventHandlers/SignalREventHandler.cs b/backend/IM_API/Application/EventHandlers/SignalREventHandler.cs deleted file mode 100644 index 970336f..0000000 --- a/backend/IM_API/Application/EventHandlers/SignalREventHandler.cs +++ /dev/null @@ -1,23 +0,0 @@ -using IM_API.Application.Interfaces; -using IM_API.Domain.Events; -using IM_API.Hubs; -using IM_API.Tools; -using Microsoft.AspNetCore.SignalR; - -namespace IM_API.Application.EventHandlers -{ - public class SignalREventHandler : IEventHandler - { - private readonly IHubContext _hub; - public SignalREventHandler(IHubContext hub) - { - _hub = hub; - } - - public async Task Handle(MessageCreatedEvent @event) - { - var streamKey = @event.StreamKey; - await _hub.Clients.Group(streamKey).SendAsync(SignalRMethodDefine.ReceiveMessage, @event); - } - } -} diff --git a/backend/IM_API/Configs/MQConfig.cs b/backend/IM_API/Configs/MQConfig.cs new file mode 100644 index 0000000..70c8d9e --- /dev/null +++ b/backend/IM_API/Configs/MQConfig.cs @@ -0,0 +1,40 @@ +using IM_API.Application.EventHandlers.FriendAddHandler; +using IM_API.Application.EventHandlers.MessageCreatedHandler; +using MassTransit; + +namespace IM_API.Configs +{ + public static class MQConfig + { + public static IServiceCollection AddRabbitMQ(this IServiceCollection services, RabbitMqOptions options) + { + services.AddMassTransit(x => + { + x.AddConsumer(); + x.AddConsumer(); + x.AddConsumer(); + x.AddConsumer(); + + x.UsingRabbitMq((ctx,cfg) => + { + cfg.Host(options.Host, "/", h => + { + h.Username(options.Username); + h.Password(options.Password); + }); + }); + }); + + + return services; + } + } + + public class RabbitMqOptions + { + public string Host { get; set; } + public int Port { get; set; } + public string Username { get; set; } + public string Password { get;set; } + } +} diff --git a/backend/IM_API/Configs/MapperConfig.cs b/backend/IM_API/Configs/MapperConfig.cs index 0ec907d..4475c94 100644 --- a/backend/IM_API/Configs/MapperConfig.cs +++ b/backend/IM_API/Configs/MapperConfig.cs @@ -26,20 +26,22 @@ namespace IM_API.Configs .ForMember(dest => dest.IsDeleted,opt => opt.MapFrom(src => 0)) ; //好友信息模型转换 - CreateMap(); + CreateMap() + .ForMember(dest => dest.UserInfo, opt => opt.MapFrom(src => src.FriendNavigation)) + ; //好友请求通过后新增好友关系 - CreateMap() - .ForMember(dest => dest.UserId , opt => opt.MapFrom(src => src.RequestUser)) - .ForMember(dest => dest.FriendId , opt => opt.MapFrom(src => src.ResponseUser)) - .ForMember(dest => dest.StatusEnum , opt =>opt.MapFrom(src => FriendStatus.Added)) - .ForMember(dest => dest.RemarkName , opt => opt.MapFrom(src => src.ResponseUserNavigation.NickName)) - .ForMember(dest => dest.Created , opt => opt.MapFrom(src => DateTime.Now)) + CreateMap() + .ForMember(dest => dest.UserId , opt => opt.MapFrom(src => src.FromUserId)) + .ForMember(dest => dest.FriendId , opt => opt.MapFrom(src => src.ToUserId)) + .ForMember(dest => dest.StatusEnum , opt =>opt.MapFrom(src => FriendStatus.Pending)) + .ForMember(dest => dest.RemarkName , opt => opt.MapFrom(src => src.RemarkName)) + .ForMember(dest => dest.Created , opt => opt.MapFrom(src => DateTime.UtcNow)) ; //发起好友请求转换请求对象 CreateMap() .ForMember(dest => dest.RequestUser , opt => opt.MapFrom(src => src.FromUserId)) .ForMember(dest => dest.ResponseUser , opt => opt.MapFrom(src => src.ToUserId)) - .ForMember(dest => dest.Created , opt => opt.MapFrom(src => DateTime.Now)) + .ForMember(dest => dest.Created , opt => opt.MapFrom(src => DateTime.UtcNow)) .ForMember(dest => dest.StateEnum , opt => opt.MapFrom(src => FriendRequestState.Pending)) .ForMember(dest => dest.Description , opt => opt.MapFrom(src => src.Description)) ; @@ -52,6 +54,7 @@ namespace IM_API.Configs .ForMember(dest => dest.ReceiverId, opt => opt.MapFrom(src => src.Recipient)) .ForMember(dest => dest.Content, opt => opt.MapFrom(src => src.Content)) .ForMember(dest => dest.TimeStamp, opt => opt.MapFrom(src => src.Created)) + .ForMember(dest => dest.GroupMemberId , opt => opt.MapFrom(src => src.GroupMemberId)) ; CreateMap() .ForMember(dest => dest.Sender, opt => opt.MapFrom(src => src.SenderId)) @@ -62,6 +65,7 @@ namespace IM_API.Configs .ForMember(dest => dest.Recipient, opt => opt.MapFrom(src => src.ReceiverId)) .ForMember(dest => dest.StreamKey, opt => opt.Ignore() ) .ForMember(dest => dest.StateEnum, opt => opt.MapFrom(src => MessageState.Sent)) + .ForMember(dest => dest.GroupMemberId, opt => opt.MapFrom(src => src.GroupMemberId)) .ForMember(dest => dest.ChatType, opt => opt.Ignore()) .ForMember(dest => dest.MsgType, opt => opt.Ignore()) ; @@ -97,9 +101,28 @@ namespace IM_API.Configs .ForMember(dest => dest.TargetId, opt => opt.MapFrom(src => src.MsgRecipientId)) .ForMember(dest => dest.UnreadCount, opt => opt.MapFrom(src => 0)) .ForMember(dest => dest.StreamKey, opt => opt.MapFrom(src => src.StreamKey)) - .ForMember(dest => dest.LastMessageTime, opt => opt.MapFrom(src => DateTime.Now)) + .ForMember(dest => dest.LastMessageTime, opt => opt.MapFrom(src => DateTime.UtcNow)) ; + //创建会话对象 + CreateMap() + .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id)) + .ForMember(dest => dest.LastMessage, opt => opt.MapFrom(src => src.LastMessage)) + .ForMember(dest => dest.LastReadMessage, opt => opt.MapFrom(src => src.LastReadMessage)) + .ForMember(dest => dest.LastReadMessageId, opt => opt.MapFrom(src => src.LastReadMessageId)) + .ForMember(dest => dest.ChatType, opt => opt.MapFrom(src => src.ChatType)) + .ForMember(dest => dest.DateTime, opt => opt.MapFrom(src => src.LastMessageTime)) + .ForMember(dest => dest.TargetId, opt => opt.MapFrom(src => src.TargetId)) + .ForMember(dest => dest.UnreadCount, opt => opt.MapFrom(src => src.UnreadCount)) + .ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.UserId)); + + CreateMap() + .ForMember(dest => dest.TargetAvatar, opt => opt.MapFrom(src => src.Avatar)) + .ForMember(dest => dest.TargetName, opt => opt.MapFrom(src => src.RemarkName)); + + CreateMap() + .ForMember(dest => dest.TargetAvatar, opt => opt.MapFrom(src => src.Avatar)) + .ForMember(dest => dest.TargetName, opt => opt.MapFrom(src => src.Name)); } } } diff --git a/backend/IM_API/Configs/ServiceCollectionExtensions.cs b/backend/IM_API/Configs/ServiceCollectionExtensions.cs index 6293182..770b8ac 100644 --- a/backend/IM_API/Configs/ServiceCollectionExtensions.cs +++ b/backend/IM_API/Configs/ServiceCollectionExtensions.cs @@ -23,8 +23,6 @@ namespace IM_API.Configs services.AddTransient(); services.AddTransient(); services.AddScoped(); - services.AddScoped, SignalREventHandler>(); - services.AddScoped, ConversationEventHandler>(); services.AddSingleton(); services.AddSingleton(); return services; diff --git a/backend/IM_API/Controllers/AuthController.cs b/backend/IM_API/Controllers/AuthController.cs index 7e15bcc..612e647 100644 --- a/backend/IM_API/Controllers/AuthController.cs +++ b/backend/IM_API/Controllers/AuthController.cs @@ -53,6 +53,7 @@ namespace IM_API.Controllers return Ok(res); } [HttpPost] + [ProducesResponseType(typeof(BaseResponse),StatusCodes.Status200OK)] public async Task Refresh(RefreshDto dto) { (bool ok,int userId) = await _refreshTokenService.ValidateRefreshTokenAsync(dto.refreshToken); diff --git a/backend/IM_API/Controllers/ConversationController.cs b/backend/IM_API/Controllers/ConversationController.cs index bca8f94..61c98fd 100644 --- a/backend/IM_API/Controllers/ConversationController.cs +++ b/backend/IM_API/Controllers/ConversationController.cs @@ -28,6 +28,14 @@ namespace IM_API.Controllers var res = new BaseResponse>(list); return Ok(res); } + [HttpGet] + public async Task Get([FromQuery]int conversationId) + { + var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier); + var conversation = await _conversationSerivice.GetConversationByIdAsync(int.Parse(userIdStr), conversationId); + var res = new BaseResponse(conversation); + return Ok(res); + } [HttpPost] public async Task Clear() { diff --git a/backend/IM_API/Controllers/FriendController.cs b/backend/IM_API/Controllers/FriendController.cs index 477f5c7..e077d54 100644 --- a/backend/IM_API/Controllers/FriendController.cs +++ b/backend/IM_API/Controllers/FriendController.cs @@ -44,12 +44,12 @@ namespace IM_API.Controllers /// /// [HttpGet] - public async Task Requests(bool isReceived,int page,int limit,bool desc) + public async Task Requests(int page,int limit,bool desc) { var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier); int userId = int.Parse(userIdStr); - var list = await _friendService.GetFriendRequestListAsync(userId,isReceived,page,limit,desc); - var res = new BaseResponse>(list); + var list = await _friendService.GetFriendRequestListAsync(userId,page,limit,desc); + var res = new BaseResponse>(list); return Ok(res); } /// @@ -59,7 +59,9 @@ namespace IM_API.Controllers /// /// [HttpPost] - public async Task HandleRequest([FromRoute]int id, [FromBody]FriendRequestHandleDto dto) + public async Task HandleRequest( + [FromRoute]int id, [FromBody]FriendRequestHandleDto dto + ) { await _friendService.HandleFriendRequestAsync(new HandleFriendRequestDto() { diff --git a/backend/IM_API/Controllers/MessageController.cs b/backend/IM_API/Controllers/MessageController.cs index bb12e65..bfc40b6 100644 --- a/backend/IM_API/Controllers/MessageController.cs +++ b/backend/IM_API/Controllers/MessageController.cs @@ -3,6 +3,7 @@ using IM_API.Interface.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using System.ComponentModel.DataAnnotations; using System.Security.Claims; namespace IM_API.Controllers @@ -33,5 +34,13 @@ namespace IM_API.Controllers await _messageService.SendGroupMessageAsync(int.Parse(userIdstr), dto.ReceiverId, dto); return Ok(new BaseResponse()); } + [HttpGet] + public async Task GetMessageList([Required]int conversationId, int? msgId, int? pageSize) + { + var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier); + var msgList = await _messageService.GetMessagesAsync(int.Parse(userIdStr), conversationId, msgId, pageSize, false); + var res = new BaseResponse>(msgList); + return Ok(res); + } } } diff --git a/backend/IM_API/Domain/Events/FriendAddEvent.cs b/backend/IM_API/Domain/Events/FriendAddEvent.cs new file mode 100644 index 0000000..044ac16 --- /dev/null +++ b/backend/IM_API/Domain/Events/FriendAddEvent.cs @@ -0,0 +1,24 @@ +using IM_API.Dtos; + +namespace IM_API.Domain.Events +{ + public record FriendAddEvent:DomainEvent + { + public override string EventType => "IM.FRIENDS_FRIEND_ADD"; + /// + /// 发起请求用户 + /// + public UserInfoDto RequestUser { get; set; } + /// + /// 接受请求用户 + /// + public UserInfoDto ResponseUser { get; set; } + + public FriendRequestResDto RequestInfo { get; set; } + /// + /// 好友关系创建时间 + /// + public DateTime Created { get; set; } + + } +} diff --git a/backend/IM_API/Dtos/BaseResponse.cs b/backend/IM_API/Dtos/BaseResponse.cs index 12a0501..fb34f0f 100644 --- a/backend/IM_API/Dtos/BaseResponse.cs +++ b/backend/IM_API/Dtos/BaseResponse.cs @@ -73,6 +73,10 @@ namespace IM_API.Dtos this.Code = codeDefine.Code; this.Message = codeDefine.Message; } - public BaseResponse() { } + public BaseResponse() + { + this.Code = CodeDefine.SUCCESS.Code; + this.Message = CodeDefine.SUCCESS.Message; + } } } diff --git a/backend/IM_API/Dtos/ConversationDto.cs b/backend/IM_API/Dtos/ConversationDto.cs index 6d7842f..585bde6 100644 --- a/backend/IM_API/Dtos/ConversationDto.cs +++ b/backend/IM_API/Dtos/ConversationDto.cs @@ -46,6 +46,6 @@ namespace IM_API.Dtos /// 对方头像 /// public string? TargetAvatar { get; set; } - public virtual Message? LastReadMessage { get; set; } + public MessageBaseDto? LastReadMessage { get; set; } } } diff --git a/backend/IM_API/Dtos/FriendDto.cs b/backend/IM_API/Dtos/FriendDto.cs index 1a12449..55ed3c7 100644 --- a/backend/IM_API/Dtos/FriendDto.cs +++ b/backend/IM_API/Dtos/FriendDto.cs @@ -18,6 +18,7 @@ namespace IM_API.Dtos public string RemarkName { get; init; } = string.Empty; public string? Avatar { get; init; } + public UserInfoDto UserInfo { get; init; } } public record FriendRequestHandleDto diff --git a/backend/IM_API/Dtos/FriendRequestDto.cs b/backend/IM_API/Dtos/FriendRequestDto.cs index 34460a4..106117b 100644 --- a/backend/IM_API/Dtos/FriendRequestDto.cs +++ b/backend/IM_API/Dtos/FriendRequestDto.cs @@ -4,7 +4,7 @@ namespace IM_API.Dtos { public class FriendRequestDto { - public int FromUserId { get; set; } + public int? FromUserId { get; set; } public int ToUserId { get; set; } [Required(ErrorMessage = "备注名必填")] [StringLength(20, ErrorMessage = "备注名不能超过20位字符")] diff --git a/backend/IM_API/Dtos/FriendRequestResDto.cs b/backend/IM_API/Dtos/FriendRequestResDto.cs new file mode 100644 index 0000000..1fbf76d --- /dev/null +++ b/backend/IM_API/Dtos/FriendRequestResDto.cs @@ -0,0 +1,36 @@ +using IM_API.Models; + +namespace IM_API.Dtos +{ + public class FriendRequestResDto + { + public int Id { get; set; } + + /// + /// 申请人 + /// + public int RequestUser { get; set; } + + /// + /// 被申请人 + /// + public int ResponseUser { get; set; } + public string Avatar { get; set; } + public string NickName { get; set; } + + /// + /// 申请时间 + /// + public DateTime Created { get; set; } + + /// + /// 申请附言 + /// + public string? Description { get; set; } + + /// + /// 申请状态(0:待通过,1:拒绝,2:同意,3:拉黑) + /// + public FriendRequestState State { get; set; } + } +} diff --git a/backend/IM_API/Dtos/MessageDto.cs b/backend/IM_API/Dtos/MessageDto.cs index 5953a7c..c7bcac6 100644 --- a/backend/IM_API/Dtos/MessageDto.cs +++ b/backend/IM_API/Dtos/MessageDto.cs @@ -1,5 +1,16 @@ namespace IM_API.Dtos { - public record MessageBaseDto( - string Type,string ChatType, string? MsgId,int SenderId,int ReceiverId,string Content,DateTime TimeStamp); + public record MessageBaseDto + { + // 使用 { get; init; } 确保对象创建后不可修改,且支持无参构造 + public string Type { get; init; } = default!; + public string ChatType { get; init; } = default!; + public string? MsgId { get; init; } + public int SenderId { get; init; } + public int ReceiverId { get; init; } + public int? GroupMemberId { get; init; } + public string Content { get; init; } = default!; + public DateTime TimeStamp { get; init; } + public MessageBaseDto() { } + } } diff --git a/backend/IM_API/Hubs/ChatHub.cs b/backend/IM_API/Hubs/ChatHub.cs index 12cb1b4..37a26eb 100644 --- a/backend/IM_API/Hubs/ChatHub.cs +++ b/backend/IM_API/Hubs/ChatHub.cs @@ -1,5 +1,9 @@ -using IM_API.Dtos; +using AutoMapper; +using IM_API.Application.Interfaces; +using IM_API.Domain.Events; +using IM_API.Dtos; using IM_API.Interface.Services; +using IM_API.Models; using IM_API.Tools; using Microsoft.AspNetCore.SignalR; using System.Security.Claims; @@ -10,10 +14,14 @@ namespace IM_API.Hubs { private IMessageSevice _messageService; private readonly IConversationService _conversationService; - public ChatHub(IMessageSevice messageService, IConversationService conversationService) + private readonly IEventBus _eventBus; + private readonly IMapper _mapper; + public ChatHub(IMessageSevice messageService, IConversationService conversationService, IEventBus eventBus, IMapper mapper) { _messageService = messageService; _conversationService = conversationService; + _eventBus = eventBus; + _mapper = mapper; } public async override Task OnConnectedAsync() @@ -32,7 +40,7 @@ namespace IM_API.Hubs } await base.OnConnectedAsync(); } - public async Task SendPrivateMessage(MessageBaseDto dto) + public async Task SendMessage(MessageBaseDto dto) { if (!Context.User.Identity.IsAuthenticated) { @@ -41,8 +49,27 @@ namespace IM_API.Hubs return; } var userIdStr = Context.User.FindFirstValue(ClaimTypes.NameIdentifier); - await _messageService.SendPrivateMessageAsync(int.Parse(userIdStr),dto.ReceiverId,dto); - await Clients.Users(dto.ReceiverId.ToString()).SendAsync("ReceiveMessage", userIdStr, dto.Content); + MessageBaseDto msgInfo = null; + if(dto.ChatType.ToLower() == ChatType.PRIVATE.ToString().ToLower()) + { + msgInfo = await _messageService.SendPrivateMessageAsync(int.Parse(userIdStr), dto.ReceiverId, dto); + } + else + { + msgInfo = await _messageService.SendGroupMessageAsync(int.Parse(userIdStr), dto.ReceiverId, dto); + } + return; + } + public async Task ClearUnreadCount(int conversationId) + { + if (!Context.User.Identity.IsAuthenticated) + { + await Clients.Caller.SendAsync("ReceiveMessage", new BaseResponse(CodeDefine.AUTH_FAILED)); + Context.Abort(); + return; + } + var userIdStr = Context.User.FindFirstValue(ClaimTypes.NameIdentifier); + await _conversationService.ClearUnreadCountAsync(int.Parse(userIdStr), conversationId); return; } } diff --git a/backend/IM_API/IM_API.csproj b/backend/IM_API/IM_API.csproj index 7d9b73e..92d775a 100644 --- a/backend/IM_API/IM_API.csproj +++ b/backend/IM_API/IM_API.csproj @@ -12,6 +12,7 @@ + diff --git a/backend/IM_API/Interface/Services/IConversationService.cs b/backend/IM_API/Interface/Services/IConversationService.cs index 10fb99d..474bb16 100644 --- a/backend/IM_API/Interface/Services/IConversationService.cs +++ b/backend/IM_API/Interface/Services/IConversationService.cs @@ -29,5 +29,18 @@ namespace IM_API.Interface.Services /// /// Task> GetUserAllStreamKeyAsync(int userId); + /// + /// 获取单个conversation信息 + /// + /// + /// + Task GetConversationByIdAsync(int userId, int conversationId); + /// + /// 清空未读消息 + /// + /// + /// + /// + Task ClearUnreadCountAsync(int userId, int conversationId); } } diff --git a/backend/IM_API/Interface/Services/IFriendSerivce.cs b/backend/IM_API/Interface/Services/IFriendSerivce.cs index 804cb73..16aa4c5 100644 --- a/backend/IM_API/Interface/Services/IFriendSerivce.cs +++ b/backend/IM_API/Interface/Services/IFriendSerivce.cs @@ -27,7 +27,7 @@ namespace IM_API.Interface.Services /// /// /// - Task> GetFriendRequestListAsync(int userId,bool isReceived,int page,int limit, bool desc); + Task> GetFriendRequestListAsync(int userId,int page,int limit, bool desc); /// /// 处理好友请求 /// diff --git a/backend/IM_API/Interface/Services/IMessageSevice.cs b/backend/IM_API/Interface/Services/IMessageSevice.cs index b42eed9..a7fd68f 100644 --- a/backend/IM_API/Interface/Services/IMessageSevice.cs +++ b/backend/IM_API/Interface/Services/IMessageSevice.cs @@ -11,7 +11,7 @@ namespace IM_API.Interface.Services /// 接收人 /// /// - Task SendPrivateMessageAsync(int senderId,int receiverId,MessageBaseDto dto); + Task SendPrivateMessageAsync(int senderId,int receiverId,MessageBaseDto dto); /// /// 发送群聊消息 /// @@ -19,26 +19,16 @@ namespace IM_API.Interface.Services /// 接收群id /// /// - Task SendGroupMessageAsync(int senderId,int groupId,MessageBaseDto dto); + Task SendGroupMessageAsync(int senderId,int groupId,MessageBaseDto dto); /// - /// 获取私聊消息列表 + /// 获取消息列表 /// - /// - /// - /// - /// + /// 会话id(用于获取指定用户间聊天消息) + /// 消息id + /// 获取消息数量 /// /// - Task> GetPrivateMessagesAsync(int userAId,int userBId,int page,int pageSize,bool desc); - /// - /// 获取群聊消息列表 - /// - /// - /// - /// - /// - /// - Task> GetGroupMessagesAsync(int groupId, int page, int pageSize, bool desc); + Task> GetMessagesAsync(int userId, int conversationId,int? msgId,int? pageSize,bool desc); /// /// 获取未读消息数 /// diff --git a/backend/IM_API/Models/ImContext.cs b/backend/IM_API/Models/ImContext.cs index 6d796cd..eac2179 100644 --- a/backend/IM_API/Models/ImContext.cs +++ b/backend/IM_API/Models/ImContext.cs @@ -1,16 +1,11 @@ using System; using System.Collections.Generic; using Microsoft.EntityFrameworkCore; -using Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal; namespace IM_API.Models; public partial class ImContext : DbContext { - public ImContext() - { - } - public ImContext(DbContextOptions options) : base(options) { @@ -50,10 +45,6 @@ public partial class ImContext : DbContext public virtual DbSet Users { get; set; } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) -#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263. - => optionsBuilder.UseMySql("server=frp-era.com;port=26582;database=IM;user=product;password=12345678", Microsoft.EntityFrameworkCore.ServerVersion.Parse("5.7.44-mysql")); - protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder @@ -547,6 +538,9 @@ public partial class ImContext : DbContext entity.Property(e => e.Created) .HasComment("发送时间 ") .HasColumnType("datetime"); + entity.Property(e => e.GroupMemberId) + .HasComment("若为群消息则表示具体的成员id") + .HasColumnType("int(11)"); entity.Property(e => e.MsgType) .HasComment("消息类型\r\n(0:文本,1:图片,2:语音,3:视频,4:文件,5:语音聊天,6:视频聊天)") .HasColumnType("tinyint(4)"); diff --git a/backend/IM_API/Models/ImDbContext.cs b/backend/IM_API/Models/ImDbContext.cs deleted file mode 100644 index 67a195e..0000000 --- a/backend/IM_API/Models/ImDbContext.cs +++ /dev/null @@ -1,732 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.EntityFrameworkCore; -using Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal; - -namespace IM_API.Models; - -public partial class ImDbContext : DbContext -{ - public ImDbContext() - { - } - - public ImDbContext(DbContextOptions options) - : base(options) - { - } - - public virtual DbSet Admins { get; set; } - - public virtual DbSet Conversations { get; set; } - - public virtual DbSet Devices { get; set; } - - public virtual DbSet Files { get; set; } - - public virtual DbSet Friends { get; set; } - - public virtual DbSet FriendRequests { get; set; } - - public virtual DbSet Groups { get; set; } - - public virtual DbSet GroupInvites { get; set; } - - public virtual DbSet GroupMembers { get; set; } - - public virtual DbSet GroupRequests { get; set; } - - public virtual DbSet LoginLogs { get; set; } - - public virtual DbSet Messages { get; set; } - - public virtual DbSet Notifications { get; set; } - - public virtual DbSet Permissions { get; set; } - - public virtual DbSet Permissionaroles { get; set; } - - public virtual DbSet Roles { get; set; } - - public virtual DbSet Users { get; set; } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) -#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263. - => optionsBuilder.UseMySql("server=frp-era.com;port=26582;database=IM;user=product;password=12345678", Microsoft.EntityFrameworkCore.ServerVersion.Parse("5.7.44-mysql")); - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .UseCollation("latin1_swedish_ci") - .HasCharSet("latin1"); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("admins") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.RoleId, "RoleId"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Created) - .HasComment("创建时间 ") - .HasColumnType("datetime"); - entity.Property(e => e.Password) - .HasMaxLength(50) - .HasComment("密码"); - entity.Property(e => e.RoleId) - .HasComment("角色") - .HasColumnType("int(11)"); - entity.Property(e => e.State) - .HasComment("状态(0:正常,2:封禁) ") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.Updated) - .HasComment("更新时间 ") - .HasColumnType("datetime"); - entity.Property(e => e.Username) - .HasMaxLength(50) - .HasComment("用户名"); - - entity.HasOne(d => d.Role).WithMany(p => p.Admins) - .HasForeignKey(d => d.RoleId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("admins_ibfk_1"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("conversations") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.UserId, "Userid"); - - entity.HasIndex(e => e.LastReadMessageId, "lastReadMessageId"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.ChatType).HasColumnType("int(11)"); - entity.Property(e => e.LastReadMessageId) - .HasComment("最后一条已读消息ID ") - .HasColumnType("int(11)") - .HasColumnName("lastMessageId"); - entity.Property(e => e.StreamKey) - .HasMaxLength(255) - .HasComment("消息推送唯一标识符"); - entity.Property(e => e.TargetId) - .HasComment("对方ID(群聊为群聊ID,单聊为单聊ID) ") - .HasColumnType("int(11)"); - entity.Property(e => e.UnreadCount) - .HasComment("未读消息数 ") - .HasColumnType("int(11)"); - entity.Property(e => e.UserId) - .HasComment("用户") - .HasColumnType("int(11)"); - - entity.HasOne(d => d.User).WithMany(p => p.Conversations) - .HasForeignKey(d => d.UserId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("conversations_ibfk_1"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("devices") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.UserId, "Userid"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Dtype) - .HasComment("设备类型(\r\n0:Android,1:Ios,2:PC,3:Pad,4:未知)") - .HasColumnType("tinyint(4)") - .HasColumnName("DType"); - entity.Property(e => e.LastLogin) - .HasComment("最后一次登录 ") - .HasColumnType("datetime"); - entity.Property(e => e.UserId) - .HasComment("设备所属用户 ") - .HasColumnType("int(11)"); - - entity.HasOne(d => d.User).WithMany(p => p.Devices) - .HasForeignKey(d => d.UserId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("devices_ibfk_1"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("files") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.MessageId, "Messageld"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Created) - .HasComment("创建时间 ") - .HasColumnType("datetime"); - entity.Property(e => e.MessageId) - .HasComment("关联消息ID ") - .HasColumnType("int(11)"); - entity.Property(e => e.Name) - .HasMaxLength(50) - .HasComment("文件名 "); - entity.Property(e => e.Size) - .HasComment("文件大小(单位:KB) ") - .HasColumnType("int(11)"); - entity.Property(e => e.Type) - .HasMaxLength(10) - .HasComment("文件类型 "); - entity.Property(e => e.Url) - .HasMaxLength(100) - .HasComment("文件储存URL ") - .HasColumnName("URL"); - - entity.HasOne(d => d.Message).WithMany(p => p.Files) - .HasForeignKey(d => d.MessageId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("files_ibfk_1"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("friends") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.Id, "ID"); - - entity.HasIndex(e => new { e.UserId, e.FriendId }, "Userld"); - - entity.HasIndex(e => e.FriendId, "用户2id"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Avatar) - .HasMaxLength(255) - .HasComment("好友头像"); - entity.Property(e => e.Created) - .HasComment("好友关系创建时间") - .HasColumnType("datetime"); - entity.Property(e => e.FriendId) - .HasComment("用户2ID") - .HasColumnType("int(11)"); - entity.Property(e => e.RemarkName) - .HasMaxLength(20) - .HasComment("好友备注名"); - entity.Property(e => e.Status) - .HasComment("当前好友关系状态\r\n(0:待通过,1:已添加,2:已拒绝,3:已拉黑)") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.UserId) - .HasComment("用户ID") - .HasColumnType("int(11)"); - - entity.HasOne(d => d.FriendNavigation).WithMany(p => p.FriendFriendNavigations) - .HasForeignKey(d => d.FriendId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("用户2id"); - - entity.HasOne(d => d.User).WithMany(p => p.FriendUsers) - .HasForeignKey(d => d.UserId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("用户id"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("friend_request") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.RequestUser, "RequestUser"); - - entity.HasIndex(e => e.ResponseUser, "ResponseUser"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Created) - .HasComment("申请时间 ") - .HasColumnType("datetime"); - entity.Property(e => e.Description) - .HasComment("申请附言 ") - .HasColumnType("text"); - entity.Property(e => e.RequestUser) - .HasComment("申请人 ") - .HasColumnType("int(11)"); - entity.Property(e => e.ResponseUser) - .HasComment("被申请人 ") - .HasColumnType("int(11)"); - entity.Property(e => e.State) - .HasComment("申请状态(0:待通过,1:拒绝,2:同意,3:拉黑) ") - .HasColumnType("tinyint(4)"); - - entity.HasOne(d => d.RequestUserNavigation).WithMany(p => p.FriendRequestRequestUserNavigations) - .HasForeignKey(d => d.RequestUser) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("friend_request_ibfk_1"); - - entity.HasOne(d => d.ResponseUserNavigation).WithMany(p => p.FriendRequestResponseUserNavigations) - .HasForeignKey(d => d.ResponseUser) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("friend_request_ibfk_2"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("groups") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.GroupMaster, "GroupMaster"); - - entity.HasIndex(e => e.Id, "ID"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.AllMembersBanned) - .HasComment("全员禁言(0允许发言,2全员禁言)") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.Announcement) - .HasComment("群公告") - .HasColumnType("text"); - entity.Property(e => e.Auhority) - .HasComment("群权限\r\n(0:需管理员同意,1:任意人可加群,2:不允许任何人加入)") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.Created) - .HasComment("群聊创建时间") - .HasColumnType("datetime"); - entity.Property(e => e.GroupMaster) - .HasComment("群主") - .HasColumnType("int(11)"); - entity.Property(e => e.Name) - .HasMaxLength(20) - .HasComment("群聊名称"); - entity.Property(e => e.Status) - .HasComment("群聊状态\r\n(1:正常,2:封禁)") - .HasColumnType("tinyint(4)"); - - entity.HasOne(d => d.GroupMasterNavigation).WithMany(p => p.Groups) - .HasForeignKey(d => d.GroupMaster) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("groups_ibfk_1"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("group_invite") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.GroupId, "GroupId"); - - entity.HasIndex(e => e.InviteUser, "InviteUser"); - - entity.HasIndex(e => e.InvitedUser, "InvitedUser"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Created) - .HasComment("创建时间") - .HasColumnType("datetime"); - entity.Property(e => e.GroupId) - .HasComment("群聊编号") - .HasColumnType("int(11)"); - entity.Property(e => e.InviteUser) - .HasComment("邀请用户") - .HasColumnType("int(11)"); - entity.Property(e => e.InvitedUser) - .HasComment("被邀请用户") - .HasColumnType("int(11)"); - entity.Property(e => e.State) - .HasComment("当前状态(0:待被邀请人同意\r\n1:被邀请人已同意)") - .HasColumnType("tinyint(4)"); - - entity.HasOne(d => d.Group).WithMany(p => p.GroupInvites) - .HasForeignKey(d => d.GroupId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("group_invite_ibfk_2"); - - entity.HasOne(d => d.InviteUserNavigation).WithMany(p => p.GroupInviteInviteUserNavigations) - .HasForeignKey(d => d.InviteUser) - .HasConstraintName("group_invite_ibfk_1"); - - entity.HasOne(d => d.InvitedUserNavigation).WithMany(p => p.GroupInviteInvitedUserNavigations) - .HasForeignKey(d => d.InvitedUser) - .HasConstraintName("group_invite_ibfk_3"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("group_member") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.GroupId, "Groupld"); - - entity.HasIndex(e => e.Id, "ID"); - - entity.HasIndex(e => e.UserId, "Userld"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Created) - .HasDefaultValueSql("'1970-01-01 00:00:00'") - .HasComment("加入群聊时间") - .HasColumnType("datetime"); - entity.Property(e => e.GroupId) - .HasComment("群聊编号") - .HasColumnType("int(11)"); - entity.Property(e => e.Role) - .HasComment("成员角色(0:普通成员,1:管理员,2:群主)") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.UserId) - .HasComment("用户编号") - .HasColumnType("int(11)"); - - entity.HasOne(d => d.Group).WithMany(p => p.GroupMemberGroups) - .HasForeignKey(d => d.GroupId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("group_member_ibfk_2"); - - entity.HasOne(d => d.User).WithMany(p => p.GroupMemberUsers) - .HasForeignKey(d => d.UserId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("group_member_ibfk_1"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("group_request") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.GroupId, "GroupId"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Created) - .HasComment("创建时间") - .HasColumnType("datetime"); - entity.Property(e => e.Description) - .HasComment("入群附言") - .HasColumnType("text"); - entity.Property(e => e.GroupId) - .HasComment("群聊编号\r\n") - .HasColumnType("int(11)"); - entity.Property(e => e.State) - .HasComment("申请状态(0:待管理员同意,1:已拒绝,2:已同意)") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.UserId) - .HasComment("申请人 ") - .HasColumnType("int(11)"); - - entity.HasOne(d => d.Group).WithMany(p => p.GroupRequests) - .HasForeignKey(d => d.GroupId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("group_request_ibfk_1"); - - entity.HasOne(d => d.GroupNavigation).WithMany(p => p.GroupRequests) - .HasForeignKey(d => d.GroupId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("group_request_ibfk_2"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("login_log") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.UserId, "Userld"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Dtype) - .HasComment("设备类型(通Devices/DType) ") - .HasColumnType("tinyint(4)") - .HasColumnName("DType"); - entity.Property(e => e.Logined) - .HasComment("登录时间 ") - .HasColumnType("datetime"); - entity.Property(e => e.State) - .HasComment("登录状态(0:登陆成功,1:未验证,2:已被拒绝) ") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.UserId) - .HasComment("登录用户 ") - .HasColumnType("int(11)"); - - entity.HasOne(d => d.User).WithMany(p => p.LoginLogs) - .HasForeignKey(d => d.UserId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("login_log_ibfk_1"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("messages") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.Sender, "Sender"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.ChatType) - .HasComment("聊天类型\r\n(0:私聊,1:群聊)") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.Content) - .HasComment("消息内容 ") - .HasColumnType("text"); - entity.Property(e => e.Created) - .HasComment("发送时间 ") - .HasColumnType("datetime"); - entity.Property(e => e.MsgType) - .HasComment("消息类型\r\n(0:文本,1:图片,2:语音,3:视频,4:文件,5:语音聊天,6:视频聊天)") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.Recipient) - .HasComment("接收者(私聊为用户ID,群聊为群聊ID) ") - .HasColumnType("int(11)"); - entity.Property(e => e.Sender) - .HasComment("发送者 ") - .HasColumnType("int(11)"); - entity.Property(e => e.State) - .HasComment("消息状态(0:已发送,1:已撤回) ") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.StreamKey) - .HasMaxLength(255) - .HasComment("消息推送唯一标识符"); - - entity.HasOne(d => d.SenderNavigation).WithMany(p => p.Messages) - .HasForeignKey(d => d.Sender) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("messages_ibfk_1"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("notifications") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.UserId, "Userld"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Content) - .HasComment("通知内容") - .HasColumnType("text"); - entity.Property(e => e.Created) - .HasComment("创建时间") - .HasColumnType("datetime"); - entity.Property(e => e.Ntype) - .HasComment("通知类型(0:文本)") - .HasColumnType("tinyint(4)") - .HasColumnName("NType"); - entity.Property(e => e.Title) - .HasMaxLength(40) - .HasComment("通知标题"); - entity.Property(e => e.UserId) - .HasComment("接收人(为空为全体通知)") - .HasColumnType("int(11)"); - - entity.HasOne(d => d.User).WithMany(p => p.Notifications) - .HasForeignKey(d => d.UserId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("notifications_ibfk_1"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("permissions") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Code) - .HasComment("权限编码 ") - .HasColumnType("int(11)"); - entity.Property(e => e.Created) - .HasComment("创建时间 ") - .HasColumnType("datetime"); - entity.Property(e => e.Name) - .HasMaxLength(50) - .HasComment("权限名称 "); - entity.Property(e => e.Ptype) - .HasComment("权限类型(0:增,1:删,2:改,3:查) ") - .HasColumnType("int(11)") - .HasColumnName("PType"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("permissionarole") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.PermissionId, "Permissionld"); - - entity.HasIndex(e => e.RoleId, "Roleld"); - - entity.Property(e => e.Id) - .ValueGeneratedNever() - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.PermissionId) - .HasComment("权限 ") - .HasColumnType("int(11)"); - entity.Property(e => e.RoleId) - .HasComment("角色 ") - .HasColumnType("int(11)"); - - entity.HasOne(d => d.Permission).WithMany(p => p.Permissionaroles) - .HasForeignKey(d => d.PermissionId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("permissionarole_ibfk_2"); - - entity.HasOne(d => d.Role).WithMany(p => p.Permissionaroles) - .HasForeignKey(d => d.RoleId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("permissionarole_ibfk_1"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("roles") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Created) - .HasComment("创建时间 ") - .HasColumnType("datetime"); - entity.Property(e => e.Description) - .HasComment("角色描述 ") - .HasColumnType("text"); - entity.Property(e => e.Name) - .HasMaxLength(20) - .HasComment("角色名称 "); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PRIMARY"); - - entity - .ToTable("users") - .HasCharSet("utf8mb4") - .UseCollation("utf8mb4_general_ci"); - - entity.HasIndex(e => e.Id, "ID"); - - entity.HasIndex(e => e.Username, "Username").IsUnique(); - - entity.Property(e => e.Id) - .HasColumnType("int(11)") - .HasColumnName("ID"); - entity.Property(e => e.Avatar) - .HasMaxLength(255) - .HasComment("用户头像链接"); - entity.Property(e => e.Created) - .HasDefaultValueSql("'1970-01-01 00:00:00'") - .HasComment("创建时间") - .HasColumnType("datetime"); - entity.Property(e => e.IsDeleted) - .HasComment("软删除标识\r\n0:账号正常\r\n1:账号已删除") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.NickName) - .HasMaxLength(50) - .HasComment("用户昵称"); - entity.Property(e => e.OnlineStatus) - .HasComment("用户在线状态\r\n0(默认):不在线\r\n1:在线") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.Password) - .HasMaxLength(50) - .HasComment("密码"); - entity.Property(e => e.Status) - .HasDefaultValueSql("'1'") - .HasComment("账户状态\r\n(0:未激活,1:正常,2:封禁)") - .HasColumnType("tinyint(4)"); - entity.Property(e => e.Updated) - .HasComment("修改时间") - .HasColumnType("datetime"); - entity.Property(e => e.Username) - .HasMaxLength(50) - .HasComment("唯一用户名"); - }); - - OnModelCreatingPartial(modelBuilder); - } - - partial void OnModelCreatingPartial(ModelBuilder modelBuilder); -} diff --git a/backend/IM_API/Models/Message.cs b/backend/IM_API/Models/Message.cs index fcd7e9f..49d7839 100644 --- a/backend/IM_API/Models/Message.cs +++ b/backend/IM_API/Models/Message.cs @@ -49,6 +49,11 @@ public partial class Message /// public string StreamKey { get; set; } = null!; + /// + /// 若为群消息则表示具体的成员id + /// + public int? GroupMemberId { get; set; } + public virtual ICollection Conversations { get; set; } = new List(); public virtual ICollection Files { get; set; } = new List(); diff --git a/backend/IM_API/Program.cs b/backend/IM_API/Program.cs index 9695881..2ad90d3 100644 --- a/backend/IM_API/Program.cs +++ b/backend/IM_API/Program.cs @@ -3,6 +3,7 @@ using IM_API.Configs; using IM_API.Filters; using IM_API.Hubs; using IM_API.Models; +using IM_API.Tools; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; @@ -32,6 +33,8 @@ namespace IM_API //עredis var redis = ConnectionMultiplexer.Connect(redisConStr); builder.Services.AddSingleton(redis); + + builder.Services.AddRabbitMQ(configuration.GetSection("RabbitMqOptions").Get()); builder.Services.AddAllService(configuration); @@ -44,7 +47,13 @@ namespace IM_API policy.AllowAnyHeader() .AllowAnyMethod() .AllowAnyHeader() - .AllowAnyOrigin(); + .AllowCredentials() + .SetIsOriginAllowed(origin => + { + // Աػضε + var host = new Uri(origin).Host; + return host == "localhost" || host.StartsWith("192.168."); + }); }); }); //ƾ֤ @@ -86,11 +95,7 @@ namespace IM_API context.Token = accessToken; } return Task.CompletedTask; - } - }; - - options.Events = new JwtBearerEvents - { + }, OnAuthenticationFailed = context => { Console.WriteLine("Authentication failed: " + context.Exception.Message); @@ -101,6 +106,10 @@ namespace IM_API builder.Services.AddControllers(options => { options.Filters.Add(); + }).AddJsonOptions(options => + { + // ISO 8601 ʽ + options.JsonSerializerOptions.Converters.Add(new UtcDateTimeConverter()); }); builder.Services.AddModelValidation(configuration); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle diff --git a/backend/IM_API/Services/ConversationService.cs b/backend/IM_API/Services/ConversationService.cs index 41ae75f..9cfb3cc 100644 --- a/backend/IM_API/Services/ConversationService.cs +++ b/backend/IM_API/Services/ConversationService.cs @@ -1,4 +1,5 @@ -using IM_API.Dtos; +using AutoMapper; +using IM_API.Dtos; using IM_API.Exceptions; using IM_API.Interface.Services; using IM_API.Models; @@ -10,9 +11,11 @@ namespace IM_API.Services public class ConversationService : IConversationService { private readonly ImContext _context; - public ConversationService(ImContext context) + private readonly IMapper _mapper; + public ConversationService(ImContext context, IMapper mapper) { _context = context; + _mapper = mapper; } #region 删除用户会话 public async Task ClearConversationsAsync(int userId) @@ -26,47 +29,41 @@ namespace IM_API.Services #region 获取用户会话列表 public async Task> GetConversationsAsync(int userId) { - var privateQuery = from c in _context.Conversations - join f in _context.Friends on new { c.UserId, c.TargetId} - equals new { UserId = f.UserId, TargetId = f.FriendId} - where c.UserId == userId && c.ChatType == (int)ChatType.PRIVATE - select new ConversationDto - { - Id = c.Id, - UserId = c.UserId, - TargetId = c.TargetId, - LastReadMessageId = c.LastReadMessageId, - LastReadMessage = c.LastReadMessage, - UnreadCount = c.UnreadCount, - ChatType = c.ChatType, - LastMessage = c.LastMessage, - TargetAvatar = f.Avatar, - TargetName = f.RemarkName, - DateTime = c.LastMessageTime + // 1. 获取私聊会话 + var privateList = await (from c in _context.Conversations + join f in _context.Friends on new { c.UserId, c.TargetId } + equals new { UserId = f.UserId, TargetId = f.FriendId } + where c.UserId == userId && c.ChatType == (int)ChatType.PRIVATE + select new { c, f.Avatar, f.RemarkName }) + .ToListAsync(); - }; + // 2. 获取群聊会话 + var groupList = await (from c in _context.Conversations + join g in _context.Groups on c.TargetId equals g.Id + where c.UserId == userId && c.ChatType == (int)ChatType.GROUP + select new { c, g.Avatar, g.Name }) + .ToListAsync(); - var groupQuery = from c in _context.Conversations - join g in _context.Groups on c.TargetId equals g.Id - where c.UserId == userId && c.ChatType == (int)ChatType.GROUP - select new ConversationDto - { - Id = c.Id, - UserId = c.UserId, - TargetId = c.TargetId, - LastReadMessageId = c.LastReadMessageId, - LastReadMessage = c.LastReadMessage, - UnreadCount = c.UnreadCount, - ChatType = c.ChatType, - LastMessage = c.LastMessage, - TargetAvatar = g.Avatar, - TargetName = g.Name, - DateTime = c.LastMessageTime - }; - return await privateQuery - .Concat(groupQuery) - .OrderByDescending(x => x.DateTime) - .ToListAsync(); + var privateDtos = privateList.Select(x => + { + var dto = _mapper.Map(x.c); + dto.TargetAvatar = x.Avatar; + dto.TargetName = x.RemarkName; + return dto; + }); + + var groupDtos = groupList.Select(x => + { + var dto = _mapper.Map(x.c); + dto.TargetAvatar = x.Avatar; + dto.TargetName = x.Name; + return dto; + }); + + // 4. 合并并排序 + return privateDtos.Concat(groupDtos) + .OrderByDescending(x => x.DateTime) + .ToList(); } #endregion #region 删除单个会话 @@ -81,6 +78,7 @@ namespace IM_API.Services #endregion + #region 获取用户所有统一聊天凭证 public async Task> GetUserAllStreamKeyAsync(int userId) { return await _context.Conversations.Where(x => x.UserId == userId) @@ -88,5 +86,53 @@ namespace IM_API.Services .Distinct() .ToListAsync(); } + #endregion + + #region 获取单个会话信息 + public async Task GetConversationByIdAsync(int userId, int conversationId) + { + var conversation = await _context.Conversations + .Include(x => x.LastReadMessage) + .FirstOrDefaultAsync( + x => x.UserId == userId && x.Id == conversationId + ); + if (conversation is null) throw new BaseException(CodeDefine.CONVERSATION_NOT_FOUND); + var dto = _mapper.Map(conversation); + //dto.LastReadMessage = _mapper.Map(conversation); + if(conversation.ChatType == (int)ChatType.PRIVATE) + { + var friendInfo = await _context.Friends.FirstOrDefaultAsync( + x => x.UserId == userId && x.FriendId == conversation.TargetId + ); + if (friendInfo is null) throw new BaseException(CodeDefine.FRIEND_RELATION_NOT_FOUND); + _mapper.Map(friendInfo,dto); + } + if(conversation.ChatType == (int)ChatType.GROUP) + { + var groupInfo = await _context.Groups.FirstOrDefaultAsync( + x => x.Id == conversation.TargetId + ); + if (groupInfo is null) throw new BaseException(CodeDefine.GROUP_NOT_FOUND); + _mapper.Map(groupInfo, dto); + } + return dto; + } + #endregion + + public async Task ClearUnreadCountAsync(int userId, int conversationId) + { + var conversation = await _context.Conversations.FirstOrDefaultAsync(x => x.UserId == userId && x.Id == conversationId); + if (conversation is null) throw new BaseException(CodeDefine.CONVERSATION_NOT_FOUND); + var message = await _context.Messages + .Where(x => x.StreamKey == conversation.StreamKey) + .OrderByDescending(x => x.Id) + .FirstOrDefaultAsync(); + conversation.LastReadMessage = message; + conversation.UnreadCount = 0; + _context.Conversations.Update(conversation); + await _context.SaveChangesAsync(); + return true; + + } } } \ No newline at end of file diff --git a/backend/IM_API/Services/FriendService.cs b/backend/IM_API/Services/FriendService.cs index e5ef1f1..eae08d8 100644 --- a/backend/IM_API/Services/FriendService.cs +++ b/backend/IM_API/Services/FriendService.cs @@ -63,7 +63,7 @@ namespace IM_API.Services public async Task> GetFriendListAsync(int userId, int page, int limit, bool desc) { - var query = _context.Friends.Where(x => x.UserId == userId && x.Status == (sbyte)FriendStatus.Added); + var query = _context.Friends.Include(u => u.FriendNavigation).Where(x => x.UserId == userId && x.Status == (sbyte)FriendStatus.Added); if (desc) { query = query.OrderByDescending(x => x.UserId); @@ -73,23 +73,29 @@ namespace IM_API.Services } #endregion #region 获取好友请求列表 - public async Task> GetFriendRequestListAsync(int userId, bool isReceived, int page, int limit, bool desc) + public async Task> GetFriendRequestListAsync(int userId, int page, int limit, bool desc) { - var query = _context.FriendRequests.AsQueryable(); - //是否为请求方 - if (isReceived) - { - query = _context.FriendRequests.Where(x => x.ResponseUser == userId); - } - else - { - query = _context.FriendRequests.Where(x => x.RequestUser == userId); - } - if (desc) - { - query = query.OrderByDescending(x => x.Id); - } - var friendRequestList = await query.Skip((page - 1 * limit)).Take(limit).ToListAsync(); + var query = _context.FriendRequests + .Include(x => x.ResponseUserNavigation) + .Include(x => x.RequestUserNavigation) + .Where( + x => (x.ResponseUser == userId) || + x.RequestUser == userId + ) + .Select(s => new FriendRequestResDto + { + Id = s.Id, + RequestUser = s.RequestUser, + ResponseUser = s.ResponseUser, + Avatar = s.RequestUser == userId ? s.ResponseUserNavigation.Avatar : s.RequestUserNavigation.Avatar, + Created = s.Created, + NickName = s.RequestUser == userId ? s.ResponseUserNavigation.NickName : s.RequestUserNavigation.NickName, + Description = s.Description, + State = (FriendRequestState)s.State + }) + ; + query = query.OrderByDescending(x => x.Id); + var friendRequestList = await query.Skip(((page - 1) * limit)).Take(limit).ToListAsync(); return friendRequestList; } #endregion @@ -154,18 +160,17 @@ namespace IM_API.Services if (alreadyExists) throw new BaseException(CodeDefine.FRIEND_REQUEST_EXISTS); + var friendShip = await _context.Friends.FirstOrDefaultAsync(x => x.UserId == dto.FromUserId && x.FriendId == dto.ToUserId); + //检查是否被对方拉黑 - bool isBlocked = await _context.Friends.AnyAsync(x => - x.UserId == dto.FromUserId && x.FriendId == dto.ToUserId && x.Status == (sbyte)FriendStatus.Blocked - ); + bool isBlocked = friendShip != null && friendShip.StatusEnum == FriendStatus.Blocked; if (isBlocked) throw new BaseException(CodeDefine.FRIEND_REQUEST_REJECTED); - + if (friendShip != null) + throw new BaseException(CodeDefine.ALREADY_FRIENDS); //生成实体 var friendRequst = _mapper.Map(dto); - var friend = _mapper.Map(friendRequst); _context.FriendRequests.Add(friendRequst); - _context.Friends.Add(friend); await _context.SaveChangesAsync(); return true; } diff --git a/backend/IM_API/Services/MessageService.cs b/backend/IM_API/Services/MessageService.cs index 2c13695..0a824c8 100644 --- a/backend/IM_API/Services/MessageService.cs +++ b/backend/IM_API/Services/MessageService.cs @@ -1,9 +1,12 @@ using AutoMapper; +using IM_API.Application.Interfaces; +using IM_API.Domain.Events; using IM_API.Dtos; using IM_API.Exceptions; using IM_API.Interface.Services; using IM_API.Models; using IM_API.Tools; +using MassTransit; using Microsoft.EntityFrameworkCore; namespace IM_API.Services @@ -13,21 +16,53 @@ namespace IM_API.Services private readonly ImContext _context; private readonly ILogger _logger; private readonly IMapper _mapper; - public MessageService(ImContext context, ILogger logger, IMapper mapper) + //废弃,此处已使用rabbitMQ替代 + //private readonly IEventBus _eventBus; + private readonly IPublishEndpoint _endpoint; + public MessageService( + ImContext context, ILogger logger, IMapper mapper, IEventBus eventBus, + IPublishEndpoint publishEndpoint + ) { _context = context; _logger = logger; _mapper = mapper; + //_eventBus = eventBus; + _endpoint = publishEndpoint; } - public Task> GetGroupMessagesAsync(int groupId, int page, int pageSize, bool desc) + public async Task> GetMessagesAsync(int userId, int conversationId, int? msgId, int? pageSize, bool desc) { - throw new NotImplementedException(); - } - - public Task> GetPrivateMessagesAsync(int userAId, int userBId, int page, int pageSize, bool desc) - { - throw new NotImplementedException(); + //获取会话信息,用于获取双方聊天的唯一标识streamkey + Conversation? conversation = await _context.Conversations.FirstOrDefaultAsync( + x => x.Id == conversationId && x.UserId == userId + ); + if (conversation is null) throw new BaseException(CodeDefine.CONVERSATION_NOT_FOUND); + var query = _context.Messages.AsQueryable(); + if(msgId != null) + { + query = query.Where( + x => x.StreamKey == conversation.StreamKey && x.Id < msgId.Value + ) + .OrderByDescending(x => x.Id); + } + else + { + query = query.Where( + x => x.StreamKey == conversation.StreamKey && x.Id > conversation.LastReadMessageId + ); + } + if(pageSize != null) + { + query = query.Take(pageSize.Value); + } + var msgList = await query + .ToListAsync(); + msgList = msgList + .OrderBy(x => x.Created) + .ThenBy(t => t.Id) + .ToList(); + return _mapper.Map>(msgList); } public Task GetUnreadCountAsync(int userId) @@ -55,7 +90,7 @@ namespace IM_API.Services throw new NotImplementedException(); } #region 发送群消息 - public async Task SendGroupMessageAsync(int senderId, int groupId, MessageBaseDto dto) + public async Task SendGroupMessageAsync(int senderId, int groupId, MessageBaseDto dto) { //判断群存在 var isExist = await _context.Groups.AnyAsync(x => x.Id == groupId); @@ -65,15 +100,18 @@ namespace IM_API.Services if (!isMember) throw new BaseException(CodeDefine.NO_GROUP_PERMISSION); var message = _mapper.Map(dto); message.Sender = senderId; - message.StreamKey = StreamKeyBuilder.Group(groupId); + message.StreamKey = StreamKeyBuilder.Group( + + groupId); _context.Messages.Add(message); await _context.SaveChangesAsync(); - return true; + await _endpoint.Publish(_mapper.Map(message)); + return _mapper.Map(message); } #endregion #region 发送私聊消息 - public async Task SendPrivateMessageAsync(int senderId, int receiverId, MessageBaseDto dto) + public async Task SendPrivateMessageAsync(int senderId, int receiverId, MessageBaseDto dto) { bool isExist = await _context.Friends.AnyAsync(x => x.FriendId == receiverId); if (!isExist) throw new BaseException(CodeDefine.FRIEND_RELATION_NOT_FOUND); @@ -83,7 +121,8 @@ namespace IM_API.Services message.StreamKey = StreamKeyBuilder.Private(dto.SenderId, dto.ReceiverId); _context.Messages.Add(message); await _context.SaveChangesAsync(); - return true; + await _endpoint.Publish(_mapper.Map(message)); + return _mapper.Map(message); } #endregion } diff --git a/backend/IM_API/Services/RedisRefreshTokenService.cs b/backend/IM_API/Services/RedisRefreshTokenService.cs index 260041d..60b3a81 100644 --- a/backend/IM_API/Services/RedisRefreshTokenService.cs +++ b/backend/IM_API/Services/RedisRefreshTokenService.cs @@ -53,8 +53,8 @@ namespace IM_API.Services if (json.IsNullOrEmpty) return (false,-1); try { - var doc = JsonConvert.DeserializeObject(json); - var userId = doc.GetProperty("UserId").GetInt32(); + using var doc = JsonDocument.Parse(json.ToString()); + var userId = doc.RootElement.GetProperty("UserId").GetInt32(); return (true,userId); } catch diff --git a/backend/IM_API/Tools/UTCConverter.cs b/backend/IM_API/Tools/UTCConverter.cs new file mode 100644 index 0000000..bc422cd --- /dev/null +++ b/backend/IM_API/Tools/UTCConverter.cs @@ -0,0 +1,18 @@ +using System.Text.Json; + +namespace IM_API.Tools +{ + public class UtcDateTimeConverter : System.Text.Json.Serialization.JsonConverter + { + public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => reader.GetDateTime(); + + public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) + { + // 如果 Kind 已经是 Utc,直接输出。 + // 如果不是,先 SpecifyKind 再输出,确保不会发生时区偏移计算 + var utcDate = value.Kind == DateTimeKind.Utc ? value : DateTime.SpecifyKind(value, DateTimeKind.Utc); + writer.WriteStringValue(utcDate.ToString("yyyy-MM-ddTHH:mm:ssZ")); + } + } +} diff --git a/backend/IM_API/appsettings.json b/backend/IM_API/appsettings.json index 9da907c..fc895fb 100644 --- a/backend/IM_API/appsettings.json +++ b/backend/IM_API/appsettings.json @@ -16,5 +16,11 @@ "ConnectionStrings": { "DefaultConnection": "Server=frp-era.com;Port=26582;Database=IM;User=product;Password=12345678;", "Redis": "192.168.5.100:6379" + }, + "RabbitMQOptions": { + "Host": "192.168.5.100", + "Port": 5672, + "Username": "test", + "Password": "123456" } } diff --git a/docs/后端代码规范文档.md b/docs/后端代码规范文档.md index 69fd33b..aba6311 100644 --- a/docs/后端代码规范文档.md +++ b/docs/后端代码规范文档.md @@ -71,3 +71,12 @@ public WeatherForecastController(IDemo demo) ## 4. 模型类字段使用规范 ### 4.1 类中状态相关字段,例如:Status,返回值为sbyte。若类中有同名字段+后缀Enum,则优先使用后者,StatusEnum。 + +## 5.数据库相关 + +### 5.1 若数据库表结构更新,请在软件包控制台执行如下命令: + +```cmd +Scaffold-DbContext "Name=ConnectionStrings:DefaultConnection" Pomelo.EntityFrameworkCore.MySql -OutputDir Models -Context ImContext -Force -NoOnConfiguring +``` + diff --git a/frontend/app/android/build.gradle.kts b/frontend/app/android/build.gradle.kts index dbee657..224f8a9 100644 --- a/frontend/app/android/build.gradle.kts +++ b/frontend/app/android/build.gradle.kts @@ -1,7 +1,10 @@ allprojects { repositories { + maven { url = uri("https://maven.aliyun.com/repository/google") } + maven { url = uri("https://maven.aliyun.com/repository/public") } google() mavenCentral() + } } diff --git a/frontend/app/android/gradle/wrapper/gradle-wrapper.properties b/frontend/app/android/gradle/wrapper/gradle-wrapper.properties index e4ef43f..968027a 100644 --- a/frontend/app/android/gradle/wrapper/gradle-wrapper.properties +++ b/frontend/app/android/gradle/wrapper/gradle-wrapper.properties @@ -2,4 +2,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-all.zip +#distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-all.zip +# ?????????? +distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-8.14-all.zip \ No newline at end of file diff --git a/frontend/app/android/settings.gradle.kts b/frontend/app/android/settings.gradle.kts index ca7fe06..d36178c 100644 --- a/frontend/app/android/settings.gradle.kts +++ b/frontend/app/android/settings.gradle.kts @@ -11,6 +11,8 @@ pluginManagement { includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") repositories { + maven { url = uri("https://maven.aliyun.com/repository/google") } + maven { url = uri("https://maven.aliyun.com/repository/public") } google() mavenCentral() gradlePluginPortal() diff --git a/frontend/app/lib/app.dart b/frontend/app/lib/app.dart new file mode 100644 index 0000000..7d117d5 --- /dev/null +++ b/frontend/app/lib/app.dart @@ -0,0 +1,14 @@ +import 'package:app/core/router/app_router.dart'; +import 'package:flutter/material.dart'; + +class MyApp extends StatelessWidget { + const MyApp({super.key}); + + @override + Widget build(BuildContext context) { + return MaterialApp.router( + title: '测试应用', + routerConfig:appRouter + ); + } +} \ No newline at end of file diff --git a/frontend/app/lib/core/constants/app_colors.dart b/frontend/app/lib/core/constants/app_colors.dart new file mode 100644 index 0000000..68485b2 --- /dev/null +++ b/frontend/app/lib/core/constants/app_colors.dart @@ -0,0 +1,5 @@ +import 'dart:ui'; + +class AppColors { + static const Color primaryColor = Color(0xFF4FDBFF); // 微信绿 +} \ No newline at end of file diff --git a/frontend/app/lib/core/constants/app_strings.dart b/frontend/app/lib/core/constants/app_strings.dart new file mode 100644 index 0000000..e69de29 diff --git a/frontend/app/lib/core/constants/assets_path.dart b/frontend/app/lib/core/constants/assets_path.dart new file mode 100644 index 0000000..e69de29 diff --git a/frontend/app/lib/core/router/app_router.dart b/frontend/app/lib/core/router/app_router.dart new file mode 100644 index 0000000..1e8e01e --- /dev/null +++ b/frontend/app/lib/core/router/app_router.dart @@ -0,0 +1,26 @@ + + +import 'package:app/features/auth/pages/login_page.dart'; +import 'package:app/features/home/pages/index_page.dart'; +import 'package:go_router/go_router.dart'; + +import '../../features/home/pages/main_page.dart'; + +final appRouter = GoRouter( + initialLocation: '/auth/login', + routes: [ + GoRoute(path: '/auth/login', builder: (context, state) => const LoginPage()), + ShellRoute( + builder: (context, state, child) { + return MainPage(child: child); + }, + routes: [ + GoRoute( + path: '/', + builder: (context, state) => const IndexPage() + ) + ] + ) + ], + +); \ No newline at end of file diff --git a/frontend/app/lib/features/auth/bloc/login_page_state.dart b/frontend/app/lib/features/auth/bloc/login_page_state.dart new file mode 100644 index 0000000..7a410df --- /dev/null +++ b/frontend/app/lib/features/auth/bloc/login_page_state.dart @@ -0,0 +1,167 @@ +import 'package:app/core/constants/app_colors.dart'; +import 'package:app/features/auth/pages/login_page.dart'; +import 'package:flutter/material.dart'; + +class LoginPageState extends State { + @override + Widget build(BuildContext context) { + return Scaffold( + backgroundColor: Colors.white, + appBar: AppBar( + backgroundColor: Colors.transparent, + elevation: 0, + actions: [ + TextButton( + onPressed: () {}, + child: const Text("找回密码", style: TextStyle(color: Colors.grey)), + ), + ], + ), + body: SingleChildScrollView( + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 40), + child: Column( + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + const SizedBox(height: 20), + // 1. 品牌Logo/头像区域 + Container( + width: 100, + height: 100, + decoration: BoxDecoration( + color: AppColors.primaryColor.withOpacity(0.1), + shape: BoxShape.circle, + ), + child: const Icon( + Icons.person_rounded, + size: 60, + color: AppColors.primaryColor, + ), + ), + const SizedBox(height: 24), + const Text( + "登录您的聊天账号", + style: TextStyle( + fontSize: 24, + fontWeight: FontWeight.bold, + color: Colors.black87, + ), + ), + const SizedBox(height: 60), + + // 2. 账号输入框 (极简下划线风格) + TextField( + decoration: InputDecoration( + labelText: "账号 / 邮箱 / 手机号", + labelStyle: const TextStyle(color: Colors.grey, fontSize: 14), + floatingLabelStyle: const TextStyle(color: AppColors.primaryColor), + enabledBorder: UnderlineInputBorder( + borderSide: BorderSide(color: Colors.grey.shade300), + ), + focusedBorder: const UnderlineInputBorder( + borderSide: BorderSide(color: AppColors.primaryColor, width: 2), + ), + ), + ), + const SizedBox(height: 25), + + // 3. 密码输入框 + TextField( + obscureText: true, + decoration: InputDecoration( + labelText: "请输入密码", + labelStyle: const TextStyle(color: Colors.grey, fontSize: 14), + floatingLabelStyle: const TextStyle(color: AppColors.primaryColor), + enabledBorder: UnderlineInputBorder( + borderSide: BorderSide(color: Colors.grey.shade300), + ), + focusedBorder: const UnderlineInputBorder( + borderSide: BorderSide(color: AppColors.primaryColor, width: 2), + ), + ), + ), + const SizedBox(height: 60), + + // 4. 登录按钮 (圆润大按钮) + SizedBox( + width: double.infinity, + height: 50, + child: ElevatedButton( + onPressed: () {}, + style: ElevatedButton.styleFrom( + backgroundColor: AppColors.primaryColor, + foregroundColor: Colors.white, + elevation: 0, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(10), + ), + ), + child: const Text( + "登 录", + style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500), + ), + ), + ), + const SizedBox(height: 20), + + // 5. 注册/切换登录方式 + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + TextButton( + onPressed: () {}, + child: const Text( + "注册账号", + style: TextStyle(color: Color(0xFF576B95)), + ), // 经典的链接蓝 + ), + const SizedBox( + height: 20, + child: VerticalDivider(color: Colors.grey), + ), + TextButton( + onPressed: () {}, + child: const Text( + "验证码登录", + style: TextStyle(color: Color(0xFF576B95)), + ), + ), + ], + ), + + const SizedBox(height: 80), + // 6. 底部协议 (社交App必有) + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Checkbox( + value: true, + activeColor: AppColors.primaryColor, + onChanged: (v) {}, + ), + const Text( + "我已阅读并同意", + style: TextStyle(color: Colors.grey, fontSize: 12), + ), + const Text( + "《用户协议》", + style: TextStyle(color: Color(0xFF576B95), fontSize: 12), + ), + const Text( + "与", + style: TextStyle(color: Colors.grey, fontSize: 12), + ), + const Text( + "《隐私政策》", + style: TextStyle(color: Color(0xFF576B95), fontSize: 12), + ), + ], + ), + ], + ), + ), + ), + ); + } + +} \ No newline at end of file diff --git a/frontend/app/lib/features/auth/pages/login_page.dart b/frontend/app/lib/features/auth/pages/login_page.dart new file mode 100644 index 0000000..5a90fe8 --- /dev/null +++ b/frontend/app/lib/features/auth/pages/login_page.dart @@ -0,0 +1,12 @@ +import 'package:app/features/auth/bloc/login_page_state.dart'; +import 'package:flutter/cupertino.dart'; + +class LoginPage extends StatefulWidget { + const LoginPage({super.key}); + + @override + State createState() { + return LoginPageState(); + } + +} \ No newline at end of file diff --git a/frontend/app/lib/features/home/bloc/index_page_state.dart b/frontend/app/lib/features/home/bloc/index_page_state.dart new file mode 100644 index 0000000..afc6a16 --- /dev/null +++ b/frontend/app/lib/features/home/bloc/index_page_state.dart @@ -0,0 +1,10 @@ +import 'package:app/features/home/pages/index_page.dart'; +import 'package:flutter/cupertino.dart'; + +class IndexPageState extends State { + @override + Widget build(BuildContext context) { + return Text('test'); + } + +} \ No newline at end of file diff --git a/frontend/app/lib/features/home/bloc/main_page_state.dart b/frontend/app/lib/features/home/bloc/main_page_state.dart new file mode 100644 index 0000000..0086cc9 --- /dev/null +++ b/frontend/app/lib/features/home/bloc/main_page_state.dart @@ -0,0 +1,19 @@ +import 'package:app/features/home/pages/main_page.dart'; +import 'package:flutter/material.dart'; + +class MainPageState extends State { + + int count = 0; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text(count.toString()), + backgroundColor: Colors.blue, + ), + body: widget.child + ); + } + +} \ No newline at end of file diff --git a/frontend/app/lib/features/home/pages/index_page.dart b/frontend/app/lib/features/home/pages/index_page.dart new file mode 100644 index 0000000..1336e1e --- /dev/null +++ b/frontend/app/lib/features/home/pages/index_page.dart @@ -0,0 +1,12 @@ +import 'package:app/features/home/bloc/index_page_state.dart'; +import 'package:flutter/cupertino.dart'; + +class IndexPage extends StatefulWidget { + const IndexPage({super.key}); + + @override + State createState() { + return IndexPageState(); + } + +} \ No newline at end of file diff --git a/frontend/app/lib/features/home/pages/main_page.dart b/frontend/app/lib/features/home/pages/main_page.dart new file mode 100644 index 0000000..ab280de --- /dev/null +++ b/frontend/app/lib/features/home/pages/main_page.dart @@ -0,0 +1,13 @@ +import 'package:app/features/home/bloc/main_page_state.dart'; +import 'package:flutter/material.dart'; + +class MainPage extends StatefulWidget { + final Widget child; + const MainPage({super.key,required this.child}); + + @override + State createState() { + return MainPageState(); + } + +} \ No newline at end of file diff --git a/frontend/app/lib/main.dart b/frontend/app/lib/main.dart index 77ca2ee..8a7315f 100644 --- a/frontend/app/lib/main.dart +++ b/frontend/app/lib/main.dart @@ -1,178 +1,10 @@ +import 'package:flutter_web_plugins/url_strategy.dart'; +import 'package:app/app.dart'; import 'package:flutter/material.dart'; -void main() => runApp( - const MaterialApp(home: IMLoginPage(), debugShowCheckedModeBanner: false), -); +void main(List args){ -class IMLoginPage extends StatefulWidget { - const IMLoginPage({super.key}); + usePathUrlStrategy(); - @override - State createState() => _IMLoginPageState(); -} - -class _IMLoginPageState extends State { - @override - Widget build(BuildContext context) { - // 主题色定义:这里用类似微信的深绿或经典的社交蓝 - const Color primaryColor = Color(0xFF07C160); // 微信绿,你也可以换成 0xFF0084FF (社交蓝) - - return Scaffold( - backgroundColor: Colors.white, - appBar: AppBar( - backgroundColor: Colors.transparent, - elevation: 0, - actions: [ - TextButton( - onPressed: () {}, - child: const Text("找回密码", style: TextStyle(color: Colors.grey)), - ), - ], - ), - body: SingleChildScrollView( - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 40), - child: Column( - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - const SizedBox(height: 20), - // 1. 品牌Logo/头像区域 - Container( - width: 100, - height: 100, - decoration: BoxDecoration( - color: primaryColor.withOpacity(0.1), - shape: BoxShape.circle, - ), - child: const Icon( - Icons.person_rounded, - size: 60, - color: primaryColor, - ), - ), - const SizedBox(height: 24), - const Text( - "登录您的聊天账号", - style: TextStyle( - fontSize: 24, - fontWeight: FontWeight.bold, - color: Colors.black87, - ), - ), - const SizedBox(height: 60), - - // 2. 账号输入框 (极简下划线风格) - TextField( - decoration: InputDecoration( - labelText: "账号 / 邮箱 / 手机号", - labelStyle: const TextStyle(color: Colors.grey, fontSize: 14), - floatingLabelStyle: const TextStyle(color: primaryColor), - enabledBorder: UnderlineInputBorder( - borderSide: BorderSide(color: Colors.grey.shade300), - ), - focusedBorder: const UnderlineInputBorder( - borderSide: BorderSide(color: primaryColor, width: 2), - ), - ), - ), - const SizedBox(height: 25), - - // 3. 密码输入框 - TextField( - obscureText: true, - decoration: InputDecoration( - labelText: "请输入密码", - labelStyle: const TextStyle(color: Colors.grey, fontSize: 14), - floatingLabelStyle: const TextStyle(color: primaryColor), - enabledBorder: UnderlineInputBorder( - borderSide: BorderSide(color: Colors.grey.shade300), - ), - focusedBorder: const UnderlineInputBorder( - borderSide: BorderSide(color: primaryColor, width: 2), - ), - ), - ), - const SizedBox(height: 60), - - // 4. 登录按钮 (圆润大按钮) - SizedBox( - width: double.infinity, - height: 50, - child: ElevatedButton( - onPressed: () {}, - style: ElevatedButton.styleFrom( - backgroundColor: primaryColor, - foregroundColor: Colors.white, - elevation: 0, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(10), - ), - ), - child: const Text( - "登 录", - style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500), - ), - ), - ), - const SizedBox(height: 20), - - // 5. 注册/切换登录方式 - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - TextButton( - onPressed: () {}, - child: const Text( - "注册账号", - style: TextStyle(color: Color(0xFF576B95)), - ), // 经典的链接蓝 - ), - const SizedBox( - height: 20, - child: VerticalDivider(color: Colors.grey), - ), - TextButton( - onPressed: () {}, - child: const Text( - "验证码登录", - style: TextStyle(color: Color(0xFF576B95)), - ), - ), - ], - ), - - const SizedBox(height: 80), - // 6. 底部协议 (社交App必有) - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Checkbox( - value: true, - activeColor: primaryColor, - onChanged: (v) {}, - ), - const Text( - "我已阅读并同意", - style: TextStyle(color: Colors.grey, fontSize: 12), - ), - const Text( - "《用户协议》", - style: TextStyle(color: Color(0xFF576B95), fontSize: 12), - ), - const Text( - "与", - style: TextStyle(color: Colors.grey, fontSize: 12), - ), - const Text( - "《隐私政策》", - style: TextStyle(color: Color(0xFF576B95), fontSize: 12), - ), - ], - ), - ], - ), - ), - ), - ); - } -} + runApp(const MyApp()); +} \ No newline at end of file diff --git a/frontend/app/pubspec.lock b/frontend/app/pubspec.lock index 3ae5fce..8b91c6e 100644 --- a/frontend/app/pubspec.lock +++ b/frontend/app/pubspec.lock @@ -6,7 +6,7 @@ packages: description: name: async sha256: "758e6d74e971c3e5aceb4110bfd6698efc7f501675bcfe0c775459a8140750eb" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "2.13.0" boolean_selector: @@ -14,7 +14,7 @@ packages: description: name: boolean_selector sha256: "8aab1771e1243a5063b8b0ff68042d67334e3feab9e95b9490f9a6ebf73b42ea" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "2.1.2" characters: @@ -22,7 +22,7 @@ packages: description: name: characters sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803 - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "1.4.0" clock: @@ -30,7 +30,7 @@ packages: description: name: clock sha256: fddb70d9b5277016c77a80201021d40a2247104d9f4aa7bab7157b7e3f05b84b - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "1.1.2" collection: @@ -38,7 +38,7 @@ packages: description: name: collection sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "1.19.1" cupertino_icons: @@ -46,7 +46,7 @@ packages: description: name: cupertino_icons sha256: ba631d1c7f7bef6b729a622b7b752645a2d076dba9976925b8f25725a30e1ee6 - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "1.0.8" fake_async: @@ -54,7 +54,7 @@ packages: description: name: fake_async sha256: "5368f224a74523e8d2e7399ea1638b37aecfca824a3cc4dfdf77bf1fa905ac44" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "1.3.3" flutter: @@ -67,7 +67,7 @@ packages: description: name: flutter_lints sha256: "3105dc8492f6183fb076ccf1f351ac3d60564bff92e20bfc4af9cc1651f4e7e1" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "6.0.0" flutter_test: @@ -75,12 +75,25 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" + go_router: + dependency: "direct main" + description: + name: go_router + sha256: eff94d2a6fc79fa8b811dde79c7549808c2346037ee107a1121b4a644c745f2a + url: "https://pub.flutter-io.cn" + source: hosted + version: "17.0.1" leak_tracker: dependency: transitive description: name: leak_tracker sha256: "33e2e26bdd85a0112ec15400c8cbffea70d0f9c3407491f672a2fad47915e2de" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "11.0.2" leak_tracker_flutter_testing: @@ -88,7 +101,7 @@ packages: description: name: leak_tracker_flutter_testing sha256: "1dbc140bb5a23c75ea9c4811222756104fbcd1a27173f0c34ca01e16bea473c1" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "3.0.10" leak_tracker_testing: @@ -96,7 +109,7 @@ packages: description: name: leak_tracker_testing sha256: "8d5a2d49f4a66b49744b23b018848400d23e54caf9463f4eb20df3eb8acb2eb1" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "3.0.2" lints: @@ -104,15 +117,23 @@ packages: description: name: lints sha256: a5e2b223cb7c9c8efdc663ef484fdd95bb243bff242ef5b13e26883547fce9a0 - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "6.0.0" + logging: + dependency: transitive + description: + name: logging + sha256: c8245ada5f1717ed44271ed1c26b8ce85ca3228fd2ffdb75468ab01979309d61 + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.3.0" matcher: dependency: transitive description: name: matcher sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2 - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "0.12.17" material_color_utilities: @@ -120,7 +141,7 @@ packages: description: name: material_color_utilities sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "0.11.1" meta: @@ -128,7 +149,7 @@ packages: description: name: meta sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "1.17.0" path: @@ -136,7 +157,7 @@ packages: description: name: path sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "1.9.1" sky_engine: @@ -149,7 +170,7 @@ packages: description: name: source_span sha256: "254ee5351d6cb365c859e20ee823c3bb479bf4a293c22d17a9f1bf144ce86f7c" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "1.10.1" stack_trace: @@ -157,7 +178,7 @@ packages: description: name: stack_trace sha256: "8b27215b45d22309b5cddda1aa2b19bdfec9df0e765f2de506401c071d38d1b1" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "1.12.1" stream_channel: @@ -165,7 +186,7 @@ packages: description: name: stream_channel sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "2.1.4" string_scanner: @@ -173,7 +194,7 @@ packages: description: name: string_scanner sha256: "921cd31725b72fe181906c6a94d987c78e3b98c2e205b397ea399d4054872b43" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "1.4.1" term_glyph: @@ -181,7 +202,7 @@ packages: description: name: term_glyph sha256: "7f554798625ea768a7518313e58f83891c7f5024f88e46e7182a4558850a4b8e" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "1.2.2" test_api: @@ -189,7 +210,7 @@ packages: description: name: test_api sha256: ab2726c1a94d3176a45960b6234466ec367179b87dd74f1611adb1f3b5fb9d55 - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "0.7.7" vector_math: @@ -197,7 +218,7 @@ packages: description: name: vector_math sha256: d530bd74fea330e6e364cda7a85019c434070188383e1cd8d9777ee586914c5b - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "2.2.0" vm_service: @@ -205,9 +226,9 @@ packages: description: name: vm_service sha256: "45caa6c5917fa127b5dbcfbd1fa60b14e583afdc08bfc96dda38886ca252eb60" - url: "https://mirrors.tuna.tsinghua.edu.cn/dart-pub/" + url: "https://pub.flutter-io.cn" source: hosted version: "15.0.2" sdks: dart: ">=3.10.4 <4.0.0" - flutter: ">=3.18.0-18.0.pre.54" + flutter: ">=3.35.0" diff --git a/frontend/app/pubspec.yaml b/frontend/app/pubspec.yaml index 7e5495e..f953d81 100644 --- a/frontend/app/pubspec.yaml +++ b/frontend/app/pubspec.yaml @@ -31,9 +31,13 @@ dependencies: flutter: sdk: flutter + flutter_web_plugins: + sdk: flutter + # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8 + go_router: ^17.0.1 dev_dependencies: flutter_test: diff --git a/frontend/app/test/widget_test.dart b/frontend/app/test/widget_test.dart index 70de33c..6851c91 100644 --- a/frontend/app/test/widget_test.dart +++ b/frontend/app/test/widget_test.dart @@ -5,15 +5,14 @@ // gestures. You can also use WidgetTester to find child widgets in the widget // tree, read text, and verify that the values of widget properties are correct. +import 'package:app/features/auth/pages/login_page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:app/main.dart'; - void main() { testWidgets('Counter increments smoke test', (WidgetTester tester) async { // Build our app and trigger a frame. - await tester.pumpWidget(const IMLoginPage()); + await tester.pumpWidget(const LoginPage()); // Verify that our counter starts at 0. expect(find.text('0'), findsOneWidget); diff --git a/frontend/web/.env b/frontend/web/.env index ba350c4..c7dcb08 100644 --- a/frontend/web/.env +++ b/frontend/web/.env @@ -1 +1,4 @@ -VITE_API_BASE_URL = http://192.168.5.116:7070/api \ No newline at end of file +#VITE_API_BASE_URL = http://localhost:5202/api +#VITE_SIGNALR_BASE_URL = http://localhost:5202/chat +VITE_API_BASE_URL = https://im.test.nxsir.cn/api +VITE_SIGNALR_BASE_URL = https://im.test.nxsir.cn/chat/ \ No newline at end of file diff --git a/frontend/web/package-lock.json b/frontend/web/package-lock.json index 5fa7968..4499a81 100644 --- a/frontend/web/package-lock.json +++ b/frontend/web/package-lock.json @@ -8,10 +8,12 @@ "name": "web", "version": "0.0.0", "dependencies": { + "@microsoft/signalr": "^10.0.0", "@vuelidate/core": "^2.0.3", "@vuelidate/validators": "^2.0.4", "axios": "^1.13.2", "feather-icons": "^4.29.2", + "idb": "^8.0.3", "pinia": "^3.0.3", "vee-validate": "^4.15.1", "vue": "^3.5.22", @@ -103,6 +105,7 @@ "integrity": "sha512-2BCOP7TN8M+gVDj7/ht3hsaO/B/n5oDbiAyyvnRlNOs+u1o+JWNYTQrmpuNp1/Wq2gcFrI01JAW+paEKDMx/CA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@babel/code-frame": "^7.27.1", "@babel/generator": "^7.28.3", @@ -655,6 +658,7 @@ } ], "license": "MIT", + "peer": true, "engines": { "node": ">=18" }, @@ -701,6 +705,7 @@ } ], "license": "MIT", + "peer": true, "engines": { "node": ">=18" } @@ -1458,6 +1463,40 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@microsoft/signalr": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@microsoft/signalr/-/signalr-10.0.0.tgz", + "integrity": "sha512-0BRqz/uCx3JdrOqiqgFhih/+hfTERaUfCZXFB52uMaZJrKaPRzHzMuqVsJC/V3pt7NozcNXGspjKiQEK+X7P2w==", + "license": "MIT", + "dependencies": { + "abort-controller": "^3.0.0", + "eventsource": "^2.0.2", + "fetch-cookie": "^2.0.3", + "node-fetch": "^2.6.7", + "ws": "^7.5.10" + } + }, + "node_modules/@microsoft/signalr/node_modules/ws": { + "version": "7.5.10", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz", + "integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==", + "license": "MIT", + "engines": { + "node": ">=8.3.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": "^5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -2613,12 +2652,25 @@ "node": "^14.17.0 || ^16.13.0 || >=18.0.0" } }, + "node_modules/abort-controller": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", + "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", + "license": "MIT", + "dependencies": { + "event-target-shim": "^5.0.0" + }, + "engines": { + "node": ">=6.5" + } + }, "node_modules/acorn": { "version": "8.15.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz", "integrity": "sha512-NZyJarBfL7nWwIq+FDL6Zp/yHEhePMNnnJ0y3qfieCrmNvYct8uvtiV41UvlSe6apAfk0fY1FbWx+NwfmpvtTg==", "dev": true, "license": "MIT", + "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -2822,6 +2874,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "baseline-browser-mapping": "^2.8.9", "caniuse-lite": "^1.0.30001746", @@ -3436,6 +3489,7 @@ "integrity": "sha512-XyLmROnACWqSxiGYArdef1fItQd47weqB7iwtfr9JHwRrqIXZdcFMvvEcL9xHCmL0SNsOvF0c42lWyM1U5dgig==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.8.0", "@eslint-community/regexpp": "^4.12.1", @@ -3497,6 +3551,7 @@ "integrity": "sha512-82GZUjRS0p/jganf6q1rEO25VSoHH0hKPCTrgillPjdI/3bgBhAE1QzHrHTizjpRvy6pGAvKjDJtk2pF9NDq8w==", "dev": true, "license": "MIT", + "peer": true, "bin": { "eslint-config-prettier": "bin/cli.js" }, @@ -3716,6 +3771,24 @@ "node": ">=0.10.0" } }, + "node_modules/event-target-shim": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", + "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/eventsource": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/eventsource/-/eventsource-2.0.2.tgz", + "integrity": "sha512-IzUmBGPR3+oUG9dUeXynyNmf91/3zUSJg1lCktzKw47OXuhco54U3r9B7O4XX+Rb1Itm9OZ2b0RkTs10bICOxA==", + "license": "MIT", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/execa": { "version": "9.6.0", "resolved": "https://registry.npmjs.org/execa/-/execa-9.6.0.tgz", @@ -3831,6 +3904,31 @@ "core-js": "^3.1.3" } }, + "node_modules/fetch-cookie": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/fetch-cookie/-/fetch-cookie-2.2.0.tgz", + "integrity": "sha512-h9AgfjURuCgA2+2ISl8GbavpUdR+WGAM2McW/ovn4tVccegp8ZqCKWSBR8uRdM8dDNlx5WdKRWxBYUwteLDCNQ==", + "license": "Unlicense", + "dependencies": { + "set-cookie-parser": "^2.4.8", + "tough-cookie": "^4.0.0" + } + }, + "node_modules/fetch-cookie/node_modules/tough-cookie": { + "version": "4.1.4", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.1.4.tgz", + "integrity": "sha512-Loo5UUvLD9ScZ6jh8beX1T6sO1w2/MpCRpEP7V280GKMVUQ0Jzar2U3UJPsrdbziLEMMhu3Ujnq//rhiFuIeag==", + "license": "BSD-3-Clause", + "dependencies": { + "psl": "^1.1.33", + "punycode": "^2.1.1", + "universalify": "^0.2.0", + "url-parse": "^1.5.3" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/figures": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/figures/-/figures-6.1.0.tgz", @@ -4230,6 +4328,12 @@ "node": ">=0.10.0" } }, + "node_modules/idb": { + "version": "8.0.3", + "resolved": "https://registry.npmjs.org/idb/-/idb-8.0.3.tgz", + "integrity": "sha512-LtwtVyVYO5BqRvcsKuB2iUMnHwPVByPCXFXOpuU96IZPPoPN6xjOGxZQ74pgSVVLQWtUOYgyeL4GE98BY5D3wg==", + "license": "ISC" + }, "node_modules/ignore": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", @@ -4813,6 +4917,48 @@ "dev": true, "license": "MIT" }, + "node_modules/node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "license": "MIT", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, + "node_modules/node-fetch/node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", + "license": "MIT" + }, + "node_modules/node-fetch/node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", + "license": "BSD-2-Clause" + }, + "node_modules/node-fetch/node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "license": "MIT", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "node_modules/node-releases": { "version": "2.0.23", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.23.tgz", @@ -5127,6 +5273,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "nanoid": "^3.3.11", "picocolors": "^1.1.1", @@ -5166,6 +5313,7 @@ "integrity": "sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ==", "dev": true, "license": "MIT", + "peer": true, "bin": { "prettier": "bin/prettier.cjs" }, @@ -5224,16 +5372,33 @@ "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", "license": "MIT" }, + "node_modules/psl": { + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.15.0.tgz", + "integrity": "sha512-JZd3gMVBAVQkSs6HdNZo9Sdo0LNcQeMNP3CozBJb3JYC/QUYZTnKxP+f8oWRX4rHP5EurWxqAHTSwUCjlNKa1w==", + "license": "MIT", + "dependencies": { + "punycode": "^2.3.1" + }, + "funding": { + "url": "https://github.com/sponsors/lupomontero" + } + }, "node_modules/punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", - "dev": true, "license": "MIT", "engines": { "node": ">=6" } }, + "node_modules/querystringify": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz", + "integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==", + "license": "MIT" + }, "node_modules/queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", @@ -5265,6 +5430,12 @@ "node": ">=0.10.0" } }, + "node_modules/requires-port": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz", + "integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==", + "license": "MIT" + }, "node_modules/resolve-from": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz", @@ -5411,6 +5582,12 @@ "node": ">=10" } }, + "node_modules/set-cookie-parser": { + "version": "2.7.2", + "resolved": "https://registry.npmjs.org/set-cookie-parser/-/set-cookie-parser-2.7.2.tgz", + "integrity": "sha512-oeM1lpU/UvhTxw+g3cIfxXHyJRc/uidd3yK1P242gzHds0udQBYzs3y8j4gCCW+ZJ7ad0yctld8RYO+bdurlvw==", + "license": "MIT" + }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", @@ -5760,6 +5937,7 @@ "integrity": "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==", "dev": true, "license": "MIT", + "peer": true, "engines": { "node": ">=12" }, @@ -5938,6 +6116,15 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/universalify": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.2.0.tgz", + "integrity": "sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg==", + "license": "MIT", + "engines": { + "node": ">= 4.0.0" + } + }, "node_modules/unplugin-utils": { "version": "0.3.1", "resolved": "https://registry.npmjs.org/unplugin-utils/-/unplugin-utils-0.3.1.tgz", @@ -6009,6 +6196,16 @@ "punycode": "^2.1.0" } }, + "node_modules/url-parse": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.10.tgz", + "integrity": "sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==", + "license": "MIT", + "dependencies": { + "querystringify": "^2.1.1", + "requires-port": "^1.0.0" + } + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", @@ -6035,6 +6232,7 @@ "integrity": "sha512-ITcnkFeR3+fI8P1wMgItjGrR10170d8auB4EpMLPqmx6uxElH3a/hHGQabSHKdqd4FXWO1nFIp9rRn7JQ34ACQ==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "esbuild": "^0.25.0", "fdir": "^6.5.0", @@ -6296,6 +6494,7 @@ "integrity": "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==", "dev": true, "license": "MIT", + "peer": true, "engines": { "node": ">=12" }, @@ -6309,6 +6508,7 @@ "integrity": "sha512-LUCP5ev3GURDysTWiP47wRRUpLKMOfPh+yKTx3kVIEiu5KOMeqzpnYNsKyOoVrULivR8tLcks4+lga33Whn90A==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@types/chai": "^5.2.2", "@vitest/expect": "3.2.4", @@ -6394,6 +6594,7 @@ "resolved": "https://registry.npmjs.org/vue/-/vue-3.5.22.tgz", "integrity": "sha512-toaZjQ3a/G/mYaLSbV+QsQhIdMo9x5rrqIpYRObsJ6T/J+RyCSFwN2LHNVH9v8uIcljDNa3QzPVdv3Y6b9hAJQ==", "license": "MIT", + "peer": true, "dependencies": { "@vue/compiler-dom": "3.5.22", "@vue/compiler-sfc": "3.5.22", @@ -6423,7 +6624,6 @@ "integrity": "sha512-CydUvFOQKD928UzZhTp4pr2vWz1L+H99t7Pkln2QSPdvmURT0MoC4wUccfCnuEaihNsu9aYYyk+bep8rlfkUXw==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "debug": "^4.4.0", "eslint-scope": "^8.2.0", @@ -6448,7 +6648,6 @@ "integrity": "sha512-Uhdk5sfqcee/9H/rCOJikYz67o0a2Tw2hGRPOG2Y1R2dg7brRe1uG0yaNQDHu+TO/uQPF/5eCapvYSmHUjt7JQ==", "dev": true, "license": "Apache-2.0", - "peer": true, "engines": { "node": "^18.18.0 || ^20.9.0 || >=21.1.0" }, diff --git a/frontend/web/package.json b/frontend/web/package.json index 3897f07..4e58dd6 100644 --- a/frontend/web/package.json +++ b/frontend/web/package.json @@ -7,7 +7,7 @@ "node": "^20.19.0 || >=22.12.0" }, "scripts": { - "dev": "vite", + "dev": "vite --host", "build": "vite build", "preview": "vite preview", "test:unit": "vitest", @@ -15,10 +15,12 @@ "format": "prettier --write src/" }, "dependencies": { + "@microsoft/signalr": "^10.0.0", "@vuelidate/core": "^2.0.3", "@vuelidate/validators": "^2.0.4", "axios": "^1.13.2", "feather-icons": "^4.29.2", + "idb": "^8.0.3", "pinia": "^3.0.3", "vee-validate": "^4.15.1", "vue": "^3.5.22", diff --git a/frontend/web/src/App.vue b/frontend/web/src/App.vue index 5668470..79bb67b 100644 --- a/frontend/web/src/App.vue +++ b/frontend/web/src/App.vue @@ -7,8 +7,19 @@ +import { onMounted } from 'vue'; +import { useAuthStore } from './stores/auth'; +//import { useSignalRStore } from './stores/signalr'; +onMounted(async () => { + const { useSignalRStore } = await import('./stores/signalr'); + const authStore = useAuthStore(); + const signalRStore = useSignalRStore(); + if(authStore.token){ + signalRStore.initSignalR(); + } +}) + \ No newline at end of file diff --git a/frontend/web/src/components/groups/GroupChatModal.vue b/frontend/web/src/components/groups/GroupChatModal.vue new file mode 100644 index 0000000..b6ce216 --- /dev/null +++ b/frontend/web/src/components/groups/GroupChatModal.vue @@ -0,0 +1,256 @@ + + + + + \ No newline at end of file diff --git a/frontend/web/src/components/user/RequestFriend.vue b/frontend/web/src/components/user/RequestFriend.vue new file mode 100644 index 0000000..e69de29 diff --git a/frontend/web/src/components/user/SearchUser.vue b/frontend/web/src/components/user/SearchUser.vue new file mode 100644 index 0000000..2312c39 --- /dev/null +++ b/frontend/web/src/components/user/SearchUser.vue @@ -0,0 +1,399 @@ + + + + + \ No newline at end of file diff --git a/frontend/web/src/handler/messageHandler.js b/frontend/web/src/handler/messageHandler.js new file mode 100644 index 0000000..484f53e --- /dev/null +++ b/frontend/web/src/handler/messageHandler.js @@ -0,0 +1,14 @@ +import { useConversationStore } from "@/stores/conversation" + +export const messageHandler = (msg) => { + const conversationStore = useConversationStore(); + const conversation = conversationStore.conversations.find(x => x.targetId == msg.senderId || x.targetId == msg.receiverId); + conversation.lastMessage = msg.content; + if (conversation.targetId == msg.receiverId) { + conversation.unreadCount = 0; + } else { + conversation.unreadCount += 1; + } + conversation.dateTime = new Date().toISOString(); + +} \ No newline at end of file diff --git a/frontend/web/src/router/index.js b/frontend/web/src/router/index.js index 6a254fb..363ec76 100644 --- a/frontend/web/src/router/index.js +++ b/frontend/web/src/router/index.js @@ -11,7 +11,10 @@ const routes = [ { path: '/', component: MainView, - + redirect: '/messages', + meta: { + requiresAuth: true + }, children: [ { path: '/messages', @@ -32,7 +35,30 @@ const routes = [ } ] }, - { path: '/contacts', name: 'userContacts', component: () => import('@/views/contact/ContactList.vue') }, + { + path: '/contacts', + name: 'userContacts', + redirect: '/contacts/index', + component: () => import('@/views/contact/ContactList.vue'), + children: [ + { + path: '/contacts/index', + name: "contactDefault", + component: () => import('@/views/contact/ContactDefault.vue') + }, + { + path: '/contacts/info/:id', + name: 'contactInfo', + component: () => import('@/views/contact/UserInfoContent.vue'), + props: true + }, + { + path: '/contacts/requests', + name: 'friendRequests', + component: () => import('@/views/contact/FriendRequestList.vue') + } + ] + }, { path: '/settings', name: 'userSettings', component: () => import('@/views/settings/SettingMenu.vue') } ], meta: { @@ -42,6 +68,7 @@ const routes = [ { path: '/index', component: MainView, + redirect: '/messages', meta: { requiresAuth: true } @@ -56,10 +83,18 @@ const router = createRouter({ router.beforeEach((to, from, next) => { const authStore = useAuthStore(); + if (to.path == '/auth/login') { + if (authStore.isLoggedIn) { + message.info('已登录,即将跳转...'); + next('/'); + } + next(); + } if (to.meta.requiresAuth && !authStore.isLoggedIn) { message.info('未登录,即将跳转...'); next('auth/login'); - } else { + } + else { next(); } }) diff --git a/frontend/web/src/services/api.js b/frontend/web/src/services/api.js index e0040ca..ea2eb94 100644 --- a/frontend/web/src/services/api.js +++ b/frontend/web/src/services/api.js @@ -2,10 +2,15 @@ import axios from 'axios' import { useMessage } from '@/components/messages/useAlert'; import router from '@/router'; import { useAuthStore } from '@/stores/auth'; +import { authService } from './auth'; const message = useMessage(); const authStore = useAuthStore(); +let waitqueue = []; +let isRefreshing = false; +const authURL = ['/auth/login', '/auth/register', '/auth/refresh']; + const api = axios.create({ baseURL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:3000/api', // 从环境变量中读取基础 URL timeout: 10000, @@ -31,16 +36,49 @@ api.interceptors.response.use( response => { return response.data; }, - err => { - if (err.response) { - switch (err.response.status) { + async err => { + const { config, response } = err; + if (response) { + switch (response.status) { case 401: + if (authURL.some(x => config.url.includes(x))) { + authStore.logout(); + message.error('未登录,请登录后操作。'); + router.push('/auth/login') + break; + } + if (config._retry) { + break; + } + + config._retry = true; + // 已经在刷新 → 排队 + if (isRefreshing) { + return new Promise(resolve => { + waitqueue.push(token => { + config.headers.Authorization = `Bearer ${token}` + resolve(api(config)) + }) + }) + } + + isRefreshing = true; + const refreshToken = authStore.refreshToken; + if (refreshToken != null && refreshToken != '') { + const res = await authService.refresh(refreshToken) + authStore.setLoginInfo(res.data.token, res.data.refreshToken, res.data.userInfo) + waitqueue.forEach(cb => cb(authStore.token)); + waitqueue = []; + config.headers.Authorization = `Bearer ${authStore.token}` + return api(config) + } + authStore.logout(); message.error('未登录,请登录后操作。'); router.push('/auth/login') break; case 400: - if (err.response.data && err.response.data.code == 1003) { - message.error(err.response.data.message); + if (response.data && response.data.code == 1003) { + message.error(response.data.message); break; } default: diff --git a/frontend/web/src/services/auth.js b/frontend/web/src/services/auth.js index e3f62e5..8654008 100644 --- a/frontend/web/src/services/auth.js +++ b/frontend/web/src/services/auth.js @@ -13,5 +13,11 @@ export const authService = { * @param {*} data * @returns */ - register: (data) => request.post('/auth/register', data) + register: (data) => request.post('/auth/register', data), + /** + * 刷新用户凭证 + * @param {*} data + * @returns + */ + refresh: (refreshToken) => request.post('/auth/refresh', { refreshToken }) } \ No newline at end of file diff --git a/frontend/web/src/services/friend.js b/frontend/web/src/services/friend.js index c4dc0f2..90b8788 100644 --- a/frontend/web/src/services/friend.js +++ b/frontend/web/src/services/friend.js @@ -8,7 +8,25 @@ export const friendService = { * @param {*} limit 页大小 * @returns */ - getFriendList: (page = 1, limit = 100) => request.get(`/friend/list?page=${page}&limit=${limit}`) - + getFriendList: (page = 1, limit = 100) => request.get(`/friend/list?page=${page}&limit=${limit}`), + /** + * 搜索好友 + * @param {*} username + * @returns + */ + findUser: (username) => request.get(`/user/findbyusername?username=${username}`), + /** + * 申请添加好友 + * @param {*} params + * @returns + */ + requestFriend: (params) => request.post('/friend/request', params), + /** + * 获取好友请求列表 + * @param {*} page + * @param {*} limit + * @returns + */ + getFriendRequests: (page = 1, limit = 100) => request.get(`/friend/requests?page=${page}&limit=${limit}`) } \ No newline at end of file diff --git a/frontend/web/src/services/message.js b/frontend/web/src/services/message.js index 491e7f6..9949563 100644 --- a/frontend/web/src/services/message.js +++ b/frontend/web/src/services/message.js @@ -11,5 +11,25 @@ export const messageService = { * 清空所有会话消息 * @returns */ - clearConversation: () => request.post('') + clearConversation: () => request.post(''), + /** + * 获取单个会话信息 + * @param {*} conversationId + * @returns + */ + getConversationById: (conversationId) => request.get(`/conversation/get?conversationId=${conversationId}`), + /** + * 获取历史消息列表 + * @param {*} conversationId 指定会话 + * @param {*} msgId + * @param {*} pageSize + * @returns + */ + getHistoryMessages: (conversationId, msgId, pageSize = 10) => request.get(`/message/getmessageList?conversationId=${conversationId}&msgId=${msgId}&pageSize=${pageSize}`), + /** + * 获取最新消息 + * @param {*} conversationId + * @returns + */ + getMessages: (conversationId) => request.get(`/message/getmessageList?conversationId=${conversationId}`) } \ No newline at end of file diff --git a/frontend/web/src/services/useBrowserNotification.js b/frontend/web/src/services/useBrowserNotification.js new file mode 100644 index 0000000..d43fed5 --- /dev/null +++ b/frontend/web/src/services/useBrowserNotification.js @@ -0,0 +1,21 @@ +export function useBrowserNotification() { + const requestPermission = async () => { + if ("Notification" in window && Notification.permission === "default") { + await Notification.requestPermission(); + } + }; + + const send = (title, options = {}) => { + if ("Notification" in window && Notification.permission === "granted") { + // 如果页面正处于激活状态,通常不需要弹窗提醒,以免干扰用户 + /* + if (document.visibilityState === 'visible' && document.hasFocus()) { + return; + } + */ + return new Notification(title, options); + } + }; + + return { requestPermission, send }; +} diff --git a/frontend/web/src/stores/auth.js b/frontend/web/src/stores/auth.js index 2193a06..7b70aca 100644 --- a/frontend/web/src/stores/auth.js +++ b/frontend/web/src/stores/auth.js @@ -4,10 +4,35 @@ import { defineStore } from 'pinia' export const useAuthStore = defineStore('auth', () => { const token = ref(localStorage.getItem('user_token') || ''); const refreshToken = ref(localStorage.getItem('refresh_token') || ''); - const userInfo = ref(localStorage.getItem('user_info') || ''); + const userInfo = ref(JSON.parse(localStorage.getItem('user_info')) || {}); //判断是否已登录 - const isLoggedIn = computed(() => !!token.value); + const isLoggedIn = computed(() => !!refreshToken.value); + /** + * 安全解析 JWT + */ + const getPayload = (t) => { + try { + const base64Url = t.split('.')[1]; + const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); + // 处理 Unicode 字符解码 + return JSON.parse(decodeURIComponent(atob(base64).split('').map(c => + '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) + ).join(''))); + } catch (e) { + return null; + } + }; + + // 检查 Token 是否过期 + const isTokenExpired = computed(() => { + if (!token.value) return true; + const payload = getPayload(token.value); + if (!payload || !payload.exp) return true; + + const now = Math.floor(Date.now() / 1000); + return (payload.exp - now) < 30; // 预留 30 秒缓冲 + }); /** * 登录成功保存状态 @@ -15,12 +40,13 @@ export const useAuthStore = defineStore('auth', () => { * @param {*} user 用户信息 */ function setLoginInfo(newToken, newRefreshToken, user) { + console.log(`设置凭证:\ntoken:${newToken}\nrefreshToken:${newRefreshToken}`) token.value = newToken; refreshToken.value = newRefreshToken userInfo.value = user; localStorage.setItem('user_token', newToken); - localStorage.setItem('refresh_token', refreshToken) - localStorage.setItem('user_info', user) + localStorage.setItem('refresh_token', newRefreshToken) + localStorage.setItem('user_info', JSON.stringify(user)) } /** @@ -29,10 +55,11 @@ export const useAuthStore = defineStore('auth', () => { function logout() { token.value = ''; userInfo.value = null; + refreshToken.value = '' localStorage.removeItem('user_token'); localStorage.removeItem('refresh_token') localStorage.removeItem('user_info') } - return { token, userInfo, isLoggedIn, setLoginInfo, logout }; + return { token, refreshToken, userInfo, isLoggedIn, isTokenExpired, setLoginInfo, logout }; }) diff --git a/frontend/web/src/stores/chat.js b/frontend/web/src/stores/chat.js new file mode 100644 index 0000000..c061e4f --- /dev/null +++ b/frontend/web/src/stores/chat.js @@ -0,0 +1,108 @@ +import { defineStore } from "pinia"; +import { messagesDb } from "@/utils/db/messageDB"; +import { messageService } from "@/services/message"; + +export const useChatStore = defineStore('chat', { + state: () => ({ + activeSessionId: null, + activeConversationId: null, + messages: [], + pageSize: 20 + }), + actions: { + // 抽取统一的排序去重方法 + pushAndSortMessages(newMsgs) { + const combined = [...this.messages, ...newMsgs]; + // 1. 根据 msgId 或唯一 key 去重 + const uniqueMap = new Map(); + combined.forEach(m => uniqueMap.set(m.msgId || m.id, m)); + + // 2. 转换为数组并按时间戳升序排序 (旧的在前,新的在后) + this.messages = Array.from(uniqueMap.values()).sort((a, b) => { + return new Date(a.timeStamp).getTime() - new Date(b.timeStamp).getTime(); + }); + }, + async addMessage(msg, sessionId) { + await messagesDb.save({ ...msg, sessionId, isLoading: false }); + this.messages.push({ ...msg, sessionId }) + }, + /** + * 切换会话加载当前会话消息列表 + * @param {*} sessionId + */ + async swtichSession(sessionId, conversationId) { + this.activeSessionId = sessionId; + this.activeConversationId = conversationId; + this.messages = []; + //先从浏览器缓存加载一部分消息列表 + const localHistory = await messagesDb.getPageMessages(sessionId, new Date().toISOString(), this.pageSize); + console.log(localHistory) + if (localHistory.length > 0) { + this.messages = localHistory; + } else { + //如果本地没有消息数据则从后端拉取数据 + const conversation = (await messageService.getConversationById(this.activeConversationId)).data; + const serverHistoryMsg = await this.fetchHistoryFromServer(this.activeConversationId, conversation.lastReadMessageId); + //对消息进行过滤,防止重复消息 + const filterMsg = serverHistoryMsg.filter(m => !this.messages.find(exist => exist.msgId === m.msgId)); + this.pushAndSortMessages([...filterMsg, ...this.messages]); + } + //拉取新消息 + this.fetchNewMsgFromServier(this.activeConversationId).then((newMsg) => { + //去重 + const filterNewMsg = newMsg.filter(m => !this.messages.find(exist => exist.msgId === m.msgId)); + this.pushAndSortMessages([...filterNewMsg, ...this.messages]) + }); + }, + /** + * 从服务器加载新消息 + * @param {*} sessionId + * @returns + */ + async fetchNewMsgFromServier(conversationId) { + const newMsg = (await messageService.getMessages(conversationId)).data; + if (newMsg.length > 0) { + const sessionId = this.activeSessionId; + await Promise.all(newMsg.map(msg => + messagesDb.save({ ...msg, sessionId }) + )); + return newMsg; + } else { + return []; + } + }, + /** + * 从服务器加载历史消息 + * @param {*} sessionId + * @param {*} msgId + * @returns + */ + async fetchHistoryFromServer(conversationId, msgId) { + const res = (await messageService.getHistoryMessages(conversationId, msgId, this.pageSize)).data; + + if (res.length > 0) { + const sessionId = this.activeSessionId; + await Promise.all(res.map(msg => + messagesDb.save({ ...msg, sessionId }) + )); + return res; + } else { + return []; + } + }, + /** + * 加载更多历史消息 + */ + async loadMoreMessages() { + const lastTimeStamp = this.messages.length > 0 ? this.messages[0].timeStamp : new Date().toISOString(); + const history = await messagesDb.getPageMessages(this.activeSessionId, lastTimeStamp, this.pageSize); + if (history.length > 0) { + this.messages = [...history, ...this.messages] + } else { + const fetchMsg = await this.fetchHistoryFromServer(this.conversationId, this.messages[0].msgId); + const newMsgs = fetchMsg.filter(m => !this.messages.find(exist => exist.msgId === m.msgId)); + this.pushAndSortMessages([...newMsgs, ...this.messages]) + } + } + } +}) \ No newline at end of file diff --git a/frontend/web/src/stores/contact.js b/frontend/web/src/stores/contact.js new file mode 100644 index 0000000..50c39ba --- /dev/null +++ b/frontend/web/src/stores/contact.js @@ -0,0 +1,45 @@ +import { defineStore } from "pinia"; +import { contactDb } from "@/utils/db/contactDB"; +import { friendService } from "@/services/friend"; +import { useMessage } from "@/components/messages/useAlert"; + +export const useContactStore = defineStore('contact', { + state: () => ({ + contacts: [], + + }), + actions: { + async addContact(contact) { + this.contacts.push(contact); + await contactDb.save(contact); + }, + async loadContactList() { + if (this.contacts.length == 0) { + this.contacts = await contactDb.getAll(); + } + this.fetchContactFromServer(); + }, + async fetchContactFromServer() { + const message = useMessage(); + const res = await friendService.getFriendList(); + if (res.code == 0) { + const localMap = new Map(this.contacts.map(item => [item.id, item])); + res.data.forEach(item => { + const existingItem = localMap.get(item.id); + if (existingItem) { + // --- 局部更新 --- + // 使用 Object.assign 将新数据合并到旧对象上,保持响应式引用 + Object.assign(existingItem, item); + } else { + // --- 插入新会话 --- + this.contacts.push(item); + } + // 同步到本地数据库 + contactDb.save(item); + }); + } else { + message.error(res.message); + } + } + } +}) \ No newline at end of file diff --git a/frontend/web/src/stores/conversation.js b/frontend/web/src/stores/conversation.js new file mode 100644 index 0000000..f97ec90 --- /dev/null +++ b/frontend/web/src/stores/conversation.js @@ -0,0 +1,72 @@ +import { defineStore } from "pinia"; +import { messageService } from "@/services/message"; +import { conversationDb } from "@/utils/db/conversationDB"; +import { useMessage } from "@/components/messages/useAlert"; + +const message = useMessage(); + +export const useConversationStore = defineStore('conversation', { + state: () => ({ + conversations: [] + }), + // stores/conversation.js + getters: { + // 始终根据时间戳倒序排列 + sortedConversations: (state) => { + return [...state.conversations].sort((a, b) => + new Date(b.dateTime) - new Date(a.dateTime) + ); + } + }, + actions: { + async addConversation(conversation) { + await conversationDb.save(conversation); + this.conversations.unshift(conversation) + }, + /** + * 加载当前会话消息列表 + */ + async loadUserConversations() { + if (this.conversations.length == 0) { + try { + const covnersationsCache = await conversationDb.getAll(); + if (covnersationsCache && covnersationsCache.length > 0) { + covnersationsCache.sort((a, b) => { + return new Date(a.dateTime) - new Date(b.dateTime); + }) + } + } catch (e) { + message.error('读取本地会话缓存失败...'); + console.log('读取本地会话缓存失败:', e); + } + } + await this.fetchConversationsFromServier() + }, + /** + * 从服务器加载新消息 + * @param {*} sessionId + * @returns + */ + async fetchConversationsFromServier() { + const newConversations = (await messageService.getConversations()).data; + if (newConversations.length > 0) { + // 1. 将当前的本地数据转为 Map,方便通过 ID 快速查找 (O(1) 复杂度) + const localMap = new Map(this.conversations.map(item => [item.id, item])); + newConversations.forEach(item => { + const existingItem = localMap.get(item.id); + if (existingItem) { + // --- 局部更新 --- + // 使用 Object.assign 将新数据合并到旧对象上,保持响应式引用 + Object.assign(existingItem, item); + } else { + // --- 插入新会话 --- + this.conversations.unshift(item); + } + // 同步到本地数据库 + conversationDb.save(item); + }); + + } + } + } +}) \ No newline at end of file diff --git a/frontend/web/src/stores/signalr.js b/frontend/web/src/stores/signalr.js new file mode 100644 index 0000000..8570905 --- /dev/null +++ b/frontend/web/src/stores/signalr.js @@ -0,0 +1,102 @@ +import { defineStore } from "pinia"; +import * as signalR from '@microsoft/signalr'; +import { useMessage } from "@/components/messages/useAlert"; +import { useAuthStore } from "./auth"; +import { useChatStore } from "./chat"; +import { authService } from "@/services/auth"; +import { generateSessionId } from "@/utils/sessionIdTools"; +import { messageHandler } from "@/handler/messageHandler"; +import { useBrowserNotification } from "@/services/useBrowserNotification"; +import { useConversationStore } from "./conversation"; + +export const useSignalRStore = defineStore('signalr', { + state: () => ({ + connection: null, + isConnected: false + }), + actions: { + async initSignalR() { + const message = useMessage() + const authStore = useAuthStore() + const url = import.meta.env.VITE_SIGNALR_BASE_URL || 'http://localhost:5202/chat/'; + this.connection = new signalR.HubConnectionBuilder() + .withUrl(url, + { + + accessTokenFactory: async () => { + if (authStore.isTokenExpired) { + const res = await authService.refresh(authStore.refreshToken) + authStore.setLoginInfo(res.data.token, res.data.refreshToken, res.data.userInfo) + } + return authStore.token; + } + }) + .withAutomaticReconnect() + .build(); + this.registerHandlers(); + try { + await this.connection.start(); + this.isConnected = true; + console.log('SignalR建立通信成功!') + } catch (e) { + message.error('与服务器建立通信失败,请检查网络连接...'); + } + }, + registerHandlers() { + const chatStore = useChatStore() + const browserNotification = useBrowserNotification(); + + this.connection.on('ReceiveMessage', (msg) => { + const sessionId = generateSessionId(msg.senderId, msg.receiverId); + messageHandler(msg); + chatStore.addMessage(msg, sessionId); + const conversation = useConversationStore().conversations.find(x => x.targetId == msg.senderId); + browserNotification.send(`${conversation.targetName}发来一条消息`, { + body: msg.content, + icon: conversation.targetAvatar + }); + }); + + this.connection.onclose(() => { this.isConnected = false }); + this.connection.onreconnected(() => { this.isConnected = true }); + + }, + /** + * 通过signalr发送消息 + * @param {*} msg + * @returns + */ + async sendMsg(msg) { + const message = useMessage() + const chatStore = useChatStore() + if (!this.isConnected) { + message.error('与服务器连接中断,请重连后尝试...'); + return; + } + try { + // 后端 Hub 定义的方法名通常为 SendMessage + // 参数顺序需要与后端 ChatHub 中的方法签名一致 + if (msg.msgId == null) { + msg.msgId = self.crypto.randomUUID(); + } + const sessionId = generateSessionId(msg.senderId, msg.receiverId); + this.connection.invoke("SendMessage", msg).then(() => { + const msga = chatStore.messages.find(x => x.msgId == msg.msgId) + if (msga.isLoading) { + msga.isLoading = false; + } + }) + ; + chatStore.addMessage({ ...msg, isLoading: true }, sessionId); + messageHandler(msg); + console.log("消息发送成功!"); + } catch (err) { + console.error("消息发送失败:", err); + message.error("消息发送失败"); + } + }, + async clearUnreadCount(conversationId) { + await this.connection.invoke("ClearUnreadCount", conversationId) + } + } +}) diff --git a/frontend/web/src/utils/codeHelper.js b/frontend/web/src/utils/codeHelper.js new file mode 100644 index 0000000..9428a61 --- /dev/null +++ b/frontend/web/src/utils/codeHelper.js @@ -0,0 +1,10 @@ +export function getChatCodeStr(code) { + switch (code) { + case 0: + return '私聊' + case 1: + return '群聊' + default: + return '未知类型' + } +} \ No newline at end of file diff --git a/frontend/web/src/utils/db/baseDb.js b/frontend/web/src/utils/db/baseDb.js new file mode 100644 index 0000000..b28674a --- /dev/null +++ b/frontend/web/src/utils/db/baseDb.js @@ -0,0 +1,26 @@ +import { openDB } from "idb"; + +const DBNAME = 'IM_DB'; +const STORE_NAME = 'messages'; +const CONVERSARION_STORE_NAME = 'conversations'; +const CONTACT_STORE_NAME = 'contacts'; + +export const dbPromise = openDB(DBNAME, 4, { + upgrade(db) { + if (!db.objectStoreNames.contains(STORE_NAME)) { + const store = db.createObjectStore(STORE_NAME, { keyPath: 'msgId' }); + store.createIndex('by-sessionId', 'sessionId'); + store.createIndex('by-time', 'timeStamp'); + store.createIndex('by-session-time', ['sessionId', 'timeStamp']); + } + if (!db.objectStoreNames.contains(CONVERSARION_STORE_NAME)) { + const store = db.createObjectStore(CONVERSARION_STORE_NAME, { keyPath: 'id' }); + store.createIndex('by-id', 'id'); + } + if (!db.objectStoreNames.contains(CONTACT_STORE_NAME)) { + const store = db.createObjectStore(CONTACT_STORE_NAME, { keyPath: 'id' }); + store.createIndex('by-id', 'id'); + store.createIndex('by-username', 'username'); + } + } +}) \ No newline at end of file diff --git a/frontend/web/src/utils/db/contactDB.js b/frontend/web/src/utils/db/contactDB.js new file mode 100644 index 0000000..6e25378 --- /dev/null +++ b/frontend/web/src/utils/db/contactDB.js @@ -0,0 +1,18 @@ +import { dbPromise } from "./baseDb" + +const STORE_NAME = 'contacts'; + +export const contactDb = { + async save(contact) { + (await dbPromise).put(STORE_NAME, contact); + }, + async getById(id) { + return (await dbPromise).getFromIndex(STORE_NAME, 'by-id', id); + }, + async getByUsername(username) { + return (await dbPromise).getFromIndex(STORE_NAME, 'by-username', username); + }, + async getAll() { + return (await dbPromise).getAll(STORE_NAME); + } +} \ No newline at end of file diff --git a/frontend/web/src/utils/db/conversationDB.js b/frontend/web/src/utils/db/conversationDB.js new file mode 100644 index 0000000..0dc6995 --- /dev/null +++ b/frontend/web/src/utils/db/conversationDB.js @@ -0,0 +1,18 @@ +import { dbPromise } from "./baseDb"; + +const STORE_NAME = 'conversations'; + +export const conversationDb = { + async save(conversation) { + (await dbPromise).put(STORE_NAME, conversation); + }, + async getById(id) { + return (await dbPromise).getFromIndex(STORE_NAME, 'by-id', id); + }, + async getAll() { + return (await dbPromise).getAll(STORE_NAME); + }, + async clearAll() { + (await dbPromise).clear(STORE_NAME); + } +} \ No newline at end of file diff --git a/frontend/web/src/utils/db/messageDB.js b/frontend/web/src/utils/db/messageDB.js new file mode 100644 index 0000000..cde60bc --- /dev/null +++ b/frontend/web/src/utils/db/messageDB.js @@ -0,0 +1,40 @@ +import { dbPromise } from "./baseDb"; + +const STORE_NAME = 'messages'; + +export const messagesDb = { + async save(msg) { + return (await dbPromise).put(STORE_NAME, msg); + }, + async getBySession(sessionId) { + return (await dbPromise).getAllFromIndex(STORE_NAME, 'by-sessionId', sessionId); + }, + async clearAll() { + return (await dbPromise).clear(STORE_NAME); + }, + async getPageMessages(sessionId, beforeTimeStamp, limit = 20) { + const db = await dbPromise; + const tx = db.transaction(STORE_NAME, 'readonly'); + const index = tx.store.index('by-session-time'); // 使用复合索引 + + // 定义范围:从 [sessionId, 最早时间] 到 [sessionId, beforeTimeStamp) + // 注意:IDBKeyRange.bound([sessionId, ""], [sessionId, beforeTimeStamp], false, true) + // 或者简单使用 upperbound 限制最大值 + const range = IDBKeyRange.upperBound([sessionId, beforeTimeStamp], true); + + // 'prev' 表示从最新的往回找(倒序) + let cursor = await index.openCursor(range, 'prev'); + const results = []; + + while (cursor && results.length < limit) { + // 关键安全检查:因为 upperBound 可能会越界捞到其他 sessionId 的数据 + //(复合索引的特性决定了 sessionId 不一致的数据会排在后面) + if (cursor.value.sessionId !== sessionId) break; + + results.unshift(cursor.value); // 放入结果集开头,保证返回的是时间升序 + cursor = await cursor.continue(); + } + + return results; + } +} \ No newline at end of file diff --git a/frontend/web/src/utils/formatDate.js b/frontend/web/src/utils/formatDate.js new file mode 100644 index 0000000..6ac814e --- /dev/null +++ b/frontend/web/src/utils/formatDate.js @@ -0,0 +1,18 @@ +export function formatDate(dateStr) { + const date = new Date(dateStr); + const year = date.getFullYear(); + const month = String(date.getMonth() + 1).padStart(2, '0'); // 补零 + const day = String(date.getDate()).padStart(2, '0'); + const hours = String(date.getHours()).padStart(2, '0'); + const minutes = String(date.getMinutes()).padStart(2, '0'); + const seconds = String(date.getSeconds()).padStart(2, '0'); + const nowDate = new Date(); + if (year == nowDate.getFullYear() && month == String(nowDate.getMonth() + 1).padStart(2, '0') && day == String(nowDate.getDate()).padStart(2, '0')) { + return `${hours}:${minutes}:${seconds}`; + } + if (year == nowDate.getFullYear()) { + return `${month}/${day} ${hours}:${minutes}:${seconds}`; + } + + return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`; +} \ No newline at end of file diff --git a/frontend/web/src/utils/sessionIdTools.js b/frontend/web/src/utils/sessionIdTools.js new file mode 100644 index 0000000..95d2b1d --- /dev/null +++ b/frontend/web/src/utils/sessionIdTools.js @@ -0,0 +1,11 @@ +/** + * 生成唯一的会话 ID (私聊) + * @param {string|number} id1 用户A的ID + * @param {string|number} id2 用户B的ID + */ +export const generateSessionId = (id1, id2) => { + // 1. 转换为字符串并放入数组 + // 2. 排序(确保顺序一致性) + // 3. 用下划线或其他分隔符拼接 + return [String(id1), String(id2)].sort().join('_'); +}; \ No newline at end of file diff --git a/frontend/web/src/views/Main.vue b/frontend/web/src/views/Main.vue index 0c0f47c..8b46c35 100644 --- a/frontend/web/src/views/Main.vue +++ b/frontend/web/src/views/Main.vue @@ -4,9 +4,15 @@
- 💬 - 👤 - ⚙️ + + + + + + + + + @@ -18,6 +24,7 @@ import { ref, watch } from 'vue' import { useAuthStore } from '@/stores/auth'; import defaultAvatar from '@/assets/default_avatar.png' import { useRouter } from 'vue-router'; +import feather from 'feather-icons'; const router = useRouter(); const authStore = useAuthStore(); @@ -55,13 +62,19 @@ function handleStartChat(contact) { .nav-sidebar { width: 60px; flex-shrink: 0; - background: #282828; + background: #e9e9e9; display: flex; flex-direction: column; align-items: center; padding: 20px 0; gap: 24px; } + +.menuIcon { + background-color: #e9e9e9; + color: black; +} + .user-self { margin-bottom: 10px; } /* 2. 列表区修复 */ diff --git a/frontend/web/src/views/Test.vue b/frontend/web/src/views/Test.vue index 2c63449..f5eb261 100644 --- a/frontend/web/src/views/Test.vue +++ b/frontend/web/src/views/Test.vue @@ -1,30 +1,232 @@ - \ No newline at end of file diff --git a/frontend/web/src/views/auth/Login.vue b/frontend/web/src/views/auth/Login.vue index c4803a6..402c14b 100644 --- a/frontend/web/src/views/auth/Login.vue +++ b/frontend/web/src/views/auth/Login.vue @@ -57,10 +57,12 @@ import MyButton from '@/components/MyButton.vue' import { required, maxLength, helpers } from '@vuelidate/validators' import useVuelidate from '@vuelidate/core' import { useAuthStore } from '@/stores/auth' +import { useSignalRStore } from '@/stores/signalr' const message = useMessage(); const router = useRouter(); const authStore = useAuthStore(); +const signalRStore = useSignalRStore(); const loading = ref(false) const form = reactive({ @@ -96,6 +98,7 @@ const handleLogin = async () => { if(res.code === 0){ // Assuming 0 is success message.success('登录成功') authStore.setLoginInfo(res.data.token, res.data.refreshToken, res.data.userInfo); + signalRStore.initSignalR(); router.push('/messages') }else{ message.error(res.message || '登录失败') diff --git a/frontend/web/src/views/contact/ContactDefault.vue b/frontend/web/src/views/contact/ContactDefault.vue new file mode 100644 index 0000000..f3e5567 --- /dev/null +++ b/frontend/web/src/views/contact/ContactDefault.vue @@ -0,0 +1,136 @@ + + + \ No newline at end of file diff --git a/frontend/web/src/views/contact/ContactList.vue b/frontend/web/src/views/contact/ContactList.vue index 231d4c5..56adb59 100644 --- a/frontend/web/src/views/contact/ContactList.vue +++ b/frontend/web/src/views/contact/ContactList.vue @@ -3,23 +3,23 @@ - -
-
-
-
-

- {{ currentContact.name }} - - {{ currentContact.gender === 'm' ? '♂' : '♀' }} - -

-

微信号:{{ currentContact.wxid }}

-

地区:{{ currentContact.region }}

-
- -
- -
-
- 备注名 - {{ currentContact.alias || '未设置' }} -
-
- 个性签名 - {{ currentContact.signature || '这个家伙很懒,什么都没留下' }} -
-
- 来源 - 通过搜索微信号添加 -
-
- -
- - -
-
- -
- -

请在左侧选择联系人查看详情

-
-
+ + + + @@ -185,6 +157,19 @@ onMounted(async () => { align-items: center; cursor: pointer; transition: background 0.2s; + text-decoration: none; /* 去除下划线 */ + color: inherit; /* 继承父元素的文本颜色 */ + outline: none; /* 去除点击时的蓝框 */ + -webkit-tap-highlight-color: transparent; /* 移动端点击高亮 */ +} + +/* 去除 hover、active 等状态的效果 */ +a:hover, +a:active, +a:focus { + text-decoration: none; + color: inherit; /* 保持颜色不变 */ + cursor: pointer; } .list-item:hover { background: #e2e2e2; } @@ -210,118 +195,4 @@ onMounted(async () => { .icon-box.orange { background: #faad14; } .icon-box.green { background: #52c41a; } .icon-box.blue { background: #1890ff; } - -/* --- 右侧名片区 --- */ -.profile-main { - flex: 1; - display: flex; - align-items: center; - justify-content: center; - background: #f5f5f5; - min-width: 0; -} - -.profile-card { - width: 420px; - background: transparent; - padding: 20px; -} - -.profile-header { - display: flex; - justify-content: space-between; - align-items: flex-start; - padding-bottom: 30px; - border-bottom: 1px solid #e7e7e7; - margin-bottom: 30px; -} - -.display-name { - font-size: 24px; - color: #000; - margin-bottom: 8px; - display: flex; - align-items: center; - gap: 8px; -} - -.gender-tag { font-size: 16px; } -.gender-tag.m { color: #1890ff; } -.gender-tag.f { color: #ff4d4f; } - -.sub-text { - font-size: 13px; - color: #888; - margin: 3px 0; -} - -.big-avatar { - width: 70px; - height: 70px; - border-radius: 6px; - object-fit: cover; -} - -.profile-body { - margin-bottom: 40px; -} - -.info-row { - display: flex; - margin-bottom: 15px; - font-size: 14px; -} - -.info-row .label { - width: 80px; - color: #999; -} - -.info-row .value { - color: #333; - flex: 1; -} - -.profile-footer { - display: flex; - flex-direction: column; - align-items: center; - gap: 12px; -} - -.btn-primary { - width: 160px; - padding: 10px; - background: #07c160; - color: #fff; - border: none; - border-radius: 4px; - font-weight: bold; - cursor: pointer; -} - -.btn-ghost { - width: 160px; - padding: 10px; - background: #fff; - border: 1px solid #e0e0e0; - color: #333; - border-radius: 4px; - cursor: pointer; -} - -.btn-primary:hover, .btn-ghost:hover { - opacity: 0.8; -} - -.empty-state { - text-align: center; - color: #ccc; -} - -.empty-logo { - font-size: 80px; - margin-bottom: 10px; - opacity: 0.2; -} \ No newline at end of file diff --git a/frontend/web/src/views/contact/FriendRequestList.vue b/frontend/web/src/views/contact/FriendRequestList.vue new file mode 100644 index 0000000..7915d83 --- /dev/null +++ b/frontend/web/src/views/contact/FriendRequestList.vue @@ -0,0 +1,200 @@ + + + + + \ No newline at end of file diff --git a/frontend/web/src/views/contact/UserInfoContent.vue b/frontend/web/src/views/contact/UserInfoContent.vue index 41a40c8..8cf42f0 100644 --- a/frontend/web/src/views/contact/UserInfoContent.vue +++ b/frontend/web/src/views/contact/UserInfoContent.vue @@ -1 +1,199 @@ - \ No newline at end of file + + + + + \ No newline at end of file diff --git a/frontend/web/src/views/messages/MessageContent.vue b/frontend/web/src/views/messages/MessageContent.vue index 8b5293a..5bb6cdc 100644 --- a/frontend/web/src/views/messages/MessageContent.vue +++ b/frontend/web/src/views/messages/MessageContent.vue @@ -1,32 +1,39 @@