Parcourir la source

feat(前端):前端部署无法访问fcc端口处理完毕

Zhenghanjv il y a 3 mois
Parent
commit
4d9bf147e8

+ 31 - 0
src/FccLife.Web/Program.cs

@@ -18,6 +18,7 @@ using FccLite.Web.Repositories.FccOrderInfo;
 using FccLite.Web.Services.FccMachineInfo;
 using FccLite.Web.Repositories.FccMachineInfo;
 using OfficeOpenXml;
+using System.Text.Json;
 
 var builder = WebApplication.CreateBuilder(args);
 // 自动加载 appsettings.json 和 appsettings.{Environment}.json
@@ -87,8 +88,38 @@ builder.Services.AddSwaggerGen(c =>
                  instantiateResults.Where(r => r.Succeed).Select(r => r.ProcessorInstance),
                  "Main starting");
 }
+
+builder.Services.AddControllers()
+    .AddJsonOptions(options =>
+    {
+        options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; // 支持 camelCase
+        options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
+    });
+// 添加CORS服务,并允许所有来源
+builder.Services.AddCors(options =>
+{
+    options.AddPolicy("AllowAll", policy =>
+    {
+        policy.AllowAnyOrigin()    // 允许所有来源(生产环境应指定具体域名)
+              .AllowAnyMethod()    // 允许所有 HTTP 方法
+              .AllowAnyHeader()    // 允许所有请求头
+              .SetPreflightMaxAge(TimeSpan.FromSeconds(86400)); // 预检请求缓存时间
+    });
+});
 var app = builder.Build();
 
+//跨域
+app.UseCors("AllowAll");
+app.Use(async (context, next) =>
+{
+    context.Response.Headers["Referrer-Policy"] = "strict-origin-when-cross-origin";
+    context.Request.EnableBuffering(); // 允许多次读取
+    var requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync();
+    Console.WriteLine($"Request Body: {requestBody}");
+    context.Request.Body.Position = 0; // 重置流的位置
+    await next();
+});
+
 //controller
 app.UseHttpsRedirection();
 app.UseAuthorization();

+ 1 - 0
src/FccWeb/admin.ui.plus-master/.env.development

@@ -5,6 +5,7 @@ ENV = 'development'
 # VITE_API_URL = 'http://ipos.biz:8070'
 # VITE_API_URL = 'http://localhost:8000'
 VITE_API_URL = 'http://dev.hsfuel.com:8070'
+VITE_API_URL_FCC = 'http://localhost:9999'
 # VITE_API_URL = 'http://47.101.220.106:8070'
 # VITE_API_URL = 'http://ipos.biz:8070'
 

+ 1 - 0
src/FccWeb/admin.ui.plus-master/.env.production

@@ -11,5 +11,6 @@ VITE_PUBLIC_PATH = '/'
 # VITE_API_URL = 'http://ipos.biz:8070'
 # VITE_API_URL = 'http://localhost:8000'
 VITE_API_URL = 'http://dev.hsfuel.com:8070'
+VITE_API_URL_FCC = 'http://localhost:9999'
 # VITE_API_URL = 'http://47.101.220.106:8070'
 # VITE_API_URL = 'http://ipos.biz:8070'

+ 1 - 1
src/FccWeb/admin.ui.plus-master/src/api/request.ts

@@ -6,7 +6,7 @@ const axios = Axios.create({
 		'Content-Type': 'application/json'
 	},
 	timeout: 5000, // 超时
-	baseURL: '/api',
+	baseURL: import.meta.env.VITE_API_URL_FCC,
 })
 
 

+ 0 - 6
src/FccWeb/admin.ui.plus-master/vite.config.ts

@@ -42,12 +42,6 @@ const viteConfig = defineConfig(({ mode, command }: ConfigEnv) => {
           changeOrigin: true,
           rewrite: (path) => path.replace(/^\/gitee/, ''),
         },
-        '/api': {
-          target: 'http://localhost:9999/',
-          ws: true,
-          changeOrigin: true,
-          rewrite: (path) => path.replace(/^\/api/, ''),
-        },
       },
     },
     build: {