From 4cb7801984361d7795e02ee09e6a3c95df4a5b99 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?old=E6=98=93?= <156663459@qq.com>
Date: Mon, 18 May 2026 13:12:54 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B5=84=E8=AE=AF=20api?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Controllers/Front/NewsInfoController.cs | 307 +++++++++++++++++-
New_College.Api/New_College.Model.xml | 50 +++
New_College.Api/New_College.xml | 20 ++
New_College.Api/appsettings.json | 1 +
New_College.Model/Request/NewsRequest.cs | 55 ++++
5 files changed, 423 insertions(+), 10 deletions(-)
diff --git a/New_College.Api/Controllers/Front/NewsInfoController.cs b/New_College.Api/Controllers/Front/NewsInfoController.cs
index 9ad4114..e6a150c 100644
--- a/New_College.Api/Controllers/Front/NewsInfoController.cs
+++ b/New_College.Api/Controllers/Front/NewsInfoController.cs
@@ -1,30 +1,52 @@
-using System;
+
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
+using New_College.IServices;
+using New_College.Model;
+using New_College.Model.Models;
+using New_College.Model.Request;
+using New_College.Model.ViewModels;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Mvc;
-using New_College.IServices;
-using New_College.Model;
-using New_College.Model.Request;
-using New_College.Model.ViewModels;
-
namespace New_College.Api.Controllers.Front
{
[Route("api/front/[controller]/[action]")]
[ApiController]
public class NewsInfoController : ControllerBase
{
-
+ private readonly IConfiguration _configuration;
private readonly ID_NewsCategoryServices iD_NewsCategory;
private readonly ID_NewsInfoServices d_NewsInfo;
- public NewsInfoController(ID_NewsCategoryServices d_NewsCategoryServices, ID_NewsInfoServices d_NewsInfoServices)
+ public NewsInfoController(ID_NewsCategoryServices d_NewsCategoryServices, ID_NewsInfoServices d_NewsInfoServices, IConfiguration configuration)
{
this.iD_NewsCategory = d_NewsCategoryServices;
this.d_NewsInfo = d_NewsInfoServices;
+ this._configuration = configuration;
}
+ ///
+ /// 校验新闻导入Key
+ ///
+ ///
+ private bool CheckNewsImportKey()
+ {
+ var configKey = _configuration["newskey"];
+ if (string.IsNullOrWhiteSpace(configKey))
+ {
+ return false;
+ }
+
+ if (!Request.Headers.TryGetValue("X-Import-Key", out var requestKey))
+ {
+ return false;
+ }
+
+ return string.Equals(requestKey.ToString(), configKey, StringComparison.Ordinal);
+ }
///
/// 获取新闻分类
///
@@ -173,5 +195,270 @@ namespace New_College.Api.Controllers.Front
}
}
+
+
+ ///
+ /// 单独写入新闻资讯
+ ///
+ ///
+ ///
+ [HttpPost]
+ [AllowAnonymous]
+ public async Task> AddNewsInfo([FromBody] NewsInfoAddRequest request)
+ {
+ try
+ {
+ if (!CheckNewsImportKey())
+ {
+ return new MessageModel()
+ {
+ msg = "无权限访问,Key校验失败",
+ success = false
+ };
+ }
+
+ if (request == null)
+ {
+ return new MessageModel()
+ {
+ msg = "参数不能为空",
+ success = false
+ };
+ }
+
+ if (request.CategoryId <= 0)
+ {
+ return new MessageModel()
+ {
+ msg = "新闻分类不能为空",
+ success = false
+ };
+ }
+
+ if (string.IsNullOrWhiteSpace(request.Title))
+ {
+ return new MessageModel()
+ {
+ msg = "新闻标题不能为空",
+ success = false
+ };
+ }
+
+ if (string.IsNullOrWhiteSpace(request.ProvinceCode))
+ {
+ return new MessageModel()
+ {
+ msg = "省份编号不能为空",
+ success = false
+ };
+ }
+
+ request.Title = request.Title.Trim();
+
+ // 标题判重:标题已存在则不重复添加
+ var existNews = await d_NewsInfo.Query(s =>
+ s.IsDelete == false &&
+ s.Title == request.Title
+ );
+
+ if (existNews != null && existNews.Any())
+ {
+ return new MessageModel()
+ {
+ msg = "新闻标题已存在,未重复添加",
+ success = true,
+ response = 0
+ };
+ }
+
+ var entity = new D_NewsInfo()
+ {
+ CategoryId = request.CategoryId,
+ Author = request.Author,
+ ProvinceCode = request.ProvinceCode,
+ Title = request.Title,
+ CoverImg = request.CoverImg,
+ Summary = request.Summary,
+ Detail = request.Detail,
+ Click = request.Click,
+ IsDelete = false,
+ CreateTime = DateTime.Now
+ };
+
+ var id = await d_NewsInfo.Add(entity);
+
+ return new MessageModel()
+ {
+ msg = "success",
+ success = true,
+ response = id
+ };
+ }
+ catch (Exception ex)
+ {
+ return new MessageModel()
+ {
+ msg = ex.Message,
+ success = false
+ };
+ }
+ }
+
+
+ ///
+ /// 批量写入新闻资讯
+ ///
+ ///
+ ///
+ [HttpPost]
+ [AllowAnonymous]
+ public async Task> AddNewsInfoBatch([FromBody] NewsInfoBatchAddRequest request)
+ {
+ try
+ {
+ if (!CheckNewsImportKey())
+ {
+ return new MessageModel()
+ {
+ msg = "无权限访问,Key校验失败",
+ success = false
+ };
+ }
+
+ if (request == null || request.NewsList == null || !request.NewsList.Any())
+ {
+ return new MessageModel()
+ {
+ msg = "新闻列表不能为空",
+ success = false
+ };
+ }
+
+ var errorList = new List();
+
+ for (int i = 0; i < request.NewsList.Count; i++)
+ {
+ var item = request.NewsList[i];
+
+ if (item.CategoryId <= 0)
+ {
+ errorList.Add($"第{i + 1}条新闻分类不能为空");
+ }
+
+ if (string.IsNullOrWhiteSpace(item.Title))
+ {
+ errorList.Add($"第{i + 1}条新闻标题不能为空");
+ }
+
+ if (string.IsNullOrWhiteSpace(item.ProvinceCode))
+ {
+ errorList.Add($"第{i + 1}条省份编号不能为空");
+ }
+ }
+
+ if (errorList.Any())
+ {
+ return new MessageModel()
+ {
+ msg = string.Join(";", errorList),
+ success = false
+ };
+ }
+
+ // 处理标题空格
+ foreach (var item in request.NewsList)
+ {
+ item.Title = item.Title.Trim();
+ }
+
+ // 获取本次提交的标题
+ var requestTitles = request.NewsList
+ .Select(s => s.Title)
+ .Distinct()
+ .ToList();
+
+ // 查询数据库中已存在的标题
+ var existNewsList = await d_NewsInfo.Query(s =>
+ s.IsDelete == false &&
+ requestTitles.Contains(s.Title)
+ );
+
+ var existTitleList = existNewsList
+ .Select(s => s.Title)
+ .ToList();
+
+ var newsList = new List();
+
+ // 用于处理本次批量数据内部标题重复
+ var currentAddTitleList = new List();
+
+ foreach (var item in request.NewsList)
+ {
+ // 数据库已存在的标题,跳过
+ if (existTitleList.Contains(item.Title))
+ {
+ continue;
+ }
+
+ // 本次批量中已经添加过的标题,跳过
+ if (currentAddTitleList.Contains(item.Title))
+ {
+ continue;
+ }
+
+ currentAddTitleList.Add(item.Title);
+
+ newsList.Add(new D_NewsInfo()
+ {
+ CategoryId = item.CategoryId,
+ Author = item.Author,
+ ProvinceCode = item.ProvinceCode,
+ Title = item.Title,
+ CoverImg = item.CoverImg,
+ Summary = item.Summary,
+ Detail = item.Detail,
+ Click = item.Click,
+ IsDelete = false,
+ CreateTime = DateTime.Now
+ });
+ }
+
+ if (!newsList.Any())
+ {
+ return new MessageModel()
+ {
+ msg = "新闻标题均已存在,未新增数据",
+ success = true,
+ response = 0
+ };
+ }
+
+ var successCount = 0;
+
+ foreach (var news in newsList)
+ {
+ var id = await d_NewsInfo.Add(news);
+ if (id > 0)
+ {
+ successCount++;
+ }
+ }
+
+ return new MessageModel()
+ {
+ msg = $"success,本次成功新增{successCount}条,跳过重复标题{request.NewsList.Count - successCount}条",
+ success = true,
+ response = successCount
+ };
+ }
+ catch (Exception ex)
+ {
+ return new MessageModel()
+ {
+ msg = ex.Message,
+ success = false
+ };
+ }
+ }
}
}
diff --git a/New_College.Api/New_College.Model.xml b/New_College.Api/New_College.Model.xml
index 4f6f1bf..134c8dc 100644
--- a/New_College.Api/New_College.Model.xml
+++ b/New_College.Api/New_College.Model.xml
@@ -3530,6 +3530,56 @@
+
+
+ 新增新闻资讯请求
+
+
+
+
+ 新闻分类Id
+
+
+
+
+ 作者/来源
+
+
+
+
+ 省份编号
+
+
+
+
+ 新闻标题
+
+
+
+
+ 封面图
+
+
+
+
+ 摘要
+
+
+
+
+ 新闻详情
+
+
+
+
+ 点击量,不传默认0
+
+
+
+
+ 批量新增新闻资讯请求
+
+
这里
diff --git a/New_College.Api/New_College.xml b/New_College.Api/New_College.xml
index 6e9f6b2..f94f755 100644
--- a/New_College.Api/New_College.xml
+++ b/New_College.Api/New_College.xml
@@ -629,6 +629,12 @@
+
+
+ 校验新闻导入Key
+
+
+
获取新闻分类
@@ -656,6 +662,20 @@
+
+
+ 单独写入新闻资讯
+
+
+
+
+
+
+ 批量写入新闻资讯
+
+
+
+
下订单
diff --git a/New_College.Api/appsettings.json b/New_College.Api/appsettings.json
index 8ff34fa..fa0b4fe 100644
--- a/New_College.Api/appsettings.json
+++ b/New_College.Api/appsettings.json
@@ -26,6 +26,7 @@
"Name": "New_College"
}
},
+ "newskey": "6bf23404dce9c86b0f1",
"urls": "http://*:8081", // IIS 部署,注释掉
"AllowedHosts": "*",
"AppSettings": {
diff --git a/New_College.Model/Request/NewsRequest.cs b/New_College.Model/Request/NewsRequest.cs
index 125635b..8317e6b 100644
--- a/New_College.Model/Request/NewsRequest.cs
+++ b/New_College.Model/Request/NewsRequest.cs
@@ -16,4 +16,59 @@ namespace New_College.Model.Request
public string provinceCode { get; set; } = "370000";
}
+
+
+ ///
+ /// 新增新闻资讯请求
+ ///
+ public class NewsInfoAddRequest
+ {
+ ///
+ /// 新闻分类Id
+ ///
+ public int CategoryId { get; set; }
+
+ ///
+ /// 作者/来源
+ ///
+ public string Author { get; set; }
+
+ ///
+ /// 省份编号
+ ///
+ public string ProvinceCode { get; set; }
+
+ ///
+ /// 新闻标题
+ ///
+ public string Title { get; set; }
+
+ ///
+ /// 封面图
+ ///
+ public string CoverImg { get; set; }
+
+ ///
+ /// 摘要
+ ///
+ public string Summary { get; set; }
+
+ ///
+ /// 新闻详情
+ ///
+ public string Detail { get; set; }
+
+ ///
+ /// 点击量,不传默认0
+ ///
+ public int Click { get; set; } = 0;
+ }
+
+ ///
+ /// 批量新增新闻资讯请求
+ ///
+ public class NewsInfoBatchAddRequest
+ {
+ public List NewsList { get; set; }
+ }
}