diff --git a/AGSS/AGSS.csproj b/AGSS/AGSS.csproj index 32f5c9f..f827cdf 100644 --- a/AGSS/AGSS.csproj +++ b/AGSS/AGSS.csproj @@ -1,11 +1,14 @@ - net8.0 + net9.0 enable enable + true + $(NoWarn);1591 + @@ -21,7 +24,6 @@ - diff --git a/AGSS/Controllers/User/UserInfoController.cs b/AGSS/Controllers/User/UserInfoController.cs index 8005490..79d9c41 100644 --- a/AGSS/Controllers/User/UserInfoController.cs +++ b/AGSS/Controllers/User/UserInfoController.cs @@ -1,8 +1,10 @@ using AGSS.Models; +using AGSS.Models.DTOs; using AGSS.Models.Entities; using AGSS.Models.Template; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; namespace AGSS.Controllers.User; @@ -22,13 +24,52 @@ public class UserInfoController : ControllerBase public async Task My() { + + + // _context.UserModels.Add(user); + // await _context.SaveChangesAsync(); + // return Ok(new ReturnTemplate(200, "成功",user)); + return Ok(); + } + + + /// + /// 更新或创建用户信息。 + /// + /// 包含用户详细信息的UserRequest对象,包括性别、生日、描述、职业名称和职业代码。 + /// 一个IActionResult,表示操作的成功或失败。如果成功,则返回200状态码以及消息和用户信息。用户信息封装在ReturnTemplate中,包含一个成功代码、一条消息和数据。 + /// 此方法需要授权,并接受POST请求。它使用从认证令牌中提取的AuthId来创建或更新数据库中的用户信息。 + [HttpPost] + [Authorize] + public async Task NextInfo([FromBody]UserRequest data) + { + var authId = this.User.FindFirst("sub")!.Value; UserModel user = new UserModel { - AuthId = "123" + AuthId = authId, + Sex = data.Sex, + Birthday = data.Birthday, + Description = data.Description, + JobName = data.JobName, + JobCode = data.JobCode }; _context.UserModels.Add(user); await _context.SaveChangesAsync(); - return Ok(new ReturnTemplate(200, "成功",user)); + return Ok(new ReturnTemplate(200, "成功",user)); + } + + /// + /// 检查当前认证用户是否为新用户。 + /// + /// 一个IActionResult,表示操作的成功或失败。如果成功,则返回200状态码以及消息和isNewUser布尔值。该布尔值封装在ReturnTemplate中,包含一个成功代码、一条消息和数据。 + /// 此方法需要授权,并接受GET请求。它通过检查数据库中是否存在具有相同AuthId的用户来确定用户是否为新用户。 + [HttpGet] + [Authorize] + public async Task IsNewUser() + { + var authId = this.User.FindFirst("sub")!.Value; + var isNewUser=await _context.UserModels.AnyAsync(a => a.AuthId == authId); + return Ok(new ReturnTemplate(200, "成功",isNewUser)); } } \ No newline at end of file diff --git a/AGSS/Models/DTOs/UserRequest.cs b/AGSS/Models/DTOs/UserRequest.cs new file mode 100644 index 0000000..38181c2 --- /dev/null +++ b/AGSS/Models/DTOs/UserRequest.cs @@ -0,0 +1,42 @@ +using System.ComponentModel.DataAnnotations; + +namespace AGSS.Models.DTOs; + +public class UserRequest +{ + [MaxLength(20)] + public string? Sex { get; set; } + [MaxLength(100)] + public string? Description { get; set; } + [MaxLength(200)] + public string? Config { get; set; } + [MaxLength(10)] + public string? JobCode { get; set; } + [MaxLength(10)] + public string? JobName { get; set; } + [MaxLength(20)] + public string? Birthday { get; set; } + +} +public class UserReturn +{ + public Guid Id { get; set; } + [MaxLength(50)] + public required string AuthId { get; set; } + [MaxLength(20)] + public string? Sex { get; set; } + [MaxLength(100)] + public string? Description { get; set; } + [MaxLength(200)] + public string? Config { get; set; } + [MaxLength(10)] + public string? JobCode { get; set; } + [MaxLength(10)] + public string? JobName { get; set; } + [MaxLength(20)] + public string? Birthday { get; set; } + public string Email { get; set; } + public string NickName { get; set; } + public string Name { get; set; } + +} \ No newline at end of file diff --git a/AGSS/Models/Entities/User.cs b/AGSS/Models/Entities/User.cs index 5c4f2d0..066da57 100644 --- a/AGSS/Models/Entities/User.cs +++ b/AGSS/Models/Entities/User.cs @@ -1,8 +1,23 @@ +using System.ComponentModel.DataAnnotations; + namespace AGSS.Models.Entities; public class UserModel { public Guid Id { get; set; } + [MaxLength(50)] public required string AuthId { get; set; } + [MaxLength(20)] + public string? Sex { get; set; } + [MaxLength(100)] + public string? Description { get; set; } + [MaxLength(200)] + public string? Config { get; set; } + [MaxLength(10)] + public string? JobCode { get; set; } + [MaxLength(10)] + public string? JobName { get; set; } + [MaxLength(20)] + public string? Birthday { get; set; } } \ No newline at end of file diff --git a/AGSS/Program.cs b/AGSS/Program.cs index 0117340..f8d9863 100644 --- a/AGSS/Program.cs +++ b/AGSS/Program.cs @@ -1,17 +1,10 @@ +using System.Reflection; using AGSS.Models; using AGSS.Models.Template; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; - - - - - - - - - +using Microsoft.OpenApi.Models; var builder = WebApplication.CreateBuilder(args); @@ -22,6 +15,17 @@ var builder = WebApplication.CreateBuilder(args); var domain = builder.Configuration["Auth0:Domain"]; var audience =builder.Configuration["Auth0:Audience"]; +builder.Services.AddCors(options => +{ + options.AddPolicy("AllowAll", builder => + { + builder.AllowAnyOrigin() // 允许所有来源 + .AllowAnyHeader() + .AllowAnyMethod(); + }); +}); + + //数据库配置(PGSQL) builder.Services.AddDbContext(opt => opt.UseNpgsql(builder.Configuration.GetConnectionString("DBContext"))); @@ -59,16 +63,21 @@ builder.Services.AddAuthorization(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddSwaggerGen(c => +{ + c.SwaggerDoc("v1",new OpenApiInfo{Title = "ZeroNode后端文档", Version = "1.0.0",Description = "使用了Dotnet9.0,数据库采用PGSql,作者:罗澜,7000"}); + var xmlFil = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; + var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFil); + c.IncludeXmlComments(xmlPath); +}); var app = builder.Build(); +app.UseCors("AllowAll"); + // 配置Swagger -if (app.Environment.IsDevelopment()) -{ - app.UseSwagger(); - app.UseSwaggerUI(); -} +app.UseSwagger(); +app.UseSwaggerUI(); app.UseHttpsRedirection(); @@ -88,7 +97,7 @@ app.Use(async (context, next) => context.Response.ContentType = "application/json"; // 自定义响应内容 - await context.Response.WriteAsJsonAsync(new ReturnTemplate(404,"未能找到资源吖!",null)); + await context.Response.WriteAsJsonAsync(new ReturnTemplate(404,"未能找到资源吖!",null!)); } }); diff --git a/AGSS/appsettings.Development.json b/AGSS/appsettings.Development.json index bda9442..b2b21da 100644 --- a/AGSS/appsettings.Development.json +++ b/AGSS/appsettings.Development.json @@ -7,11 +7,11 @@ }, "AllowedHosts": "*", "Auth0": { - "Domain": "", - "Audience": "", - "ClientId": "" + "Domain": "https://dev-f8lrenkd107vqnti.us.auth0.com/", + "Audience": "https://zeronode.AGSBackend.com", + "ClientId": "4JenP8xcKJsj251mUvRFbkJKEuPlBs6p" }, - "DataBase": { - "ConnectionString": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;" + "ConnectionStrings": { + "DBContext": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;" } } diff --git a/AGSS/appsettings.json b/AGSS/appsettings.json index 99ba56a..b2b21da 100644 --- a/AGSS/appsettings.json +++ b/AGSS/appsettings.json @@ -7,9 +7,9 @@ }, "AllowedHosts": "*", "Auth0": { - "Domain": "", - "Audience": "", - "ClientId": "" + "Domain": "https://dev-f8lrenkd107vqnti.us.auth0.com/", + "Audience": "https://zeronode.AGSBackend.com", + "ClientId": "4JenP8xcKJsj251mUvRFbkJKEuPlBs6p" }, "ConnectionStrings": { "DBContext": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=luolan12323;" diff --git a/global.json b/global.json new file mode 100644 index 0000000..f4fd385 --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "9.0.0", + "rollForward": "latestMajor", + "allowPrerelease": true + } +} \ No newline at end of file