| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- package com.etotem.cfc.controller.admin;
- import com.etotem.cfc.common.Result;
- import com.etotem.cfc.entity.Article;
- import com.etotem.cfc.entity.ArticleCategory;
- import com.etotem.cfc.service.ArticleCategoryService;
- import com.etotem.cfc.service.ArticleCommentService;
- import com.etotem.cfc.service.ArticleService;
- import com.etotem.cfc.service.FileStorageService;
- import com.etotem.cfc.service.api.WechatServiceInterface;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import javax.annotation.Resource;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Objects;
- import java.util.concurrent.Future;
- import java.util.concurrent.TimeUnit;
- import com.etotem.cfc.util.SortUtil;
- import com.etotem.cfc.util.ParamUtils;
- @RestController
- @RequestMapping("/api/admin/articles")
- public class AdminArticleController {
- private static final Logger log = LoggerFactory.getLogger(AdminArticleController.class);
- @Resource
- private ArticleService articleService;
- @Resource
- private ArticleCategoryService articleCategoryService;
- @Resource
- private FileStorageService fileStorageService;
- @Resource
- private ArticleCommentService articleCommentService;
- @Resource
- private WechatServiceInterface wechatService;
- @Resource
- private ThreadPoolTaskExecutor taskExecutor;
- /**
- * 文本内容安全检测:带 5 秒硬超时,超时/异常时 fail-open 放行并记录日志,
- * 避免微信接口卡顿导致文章创建/更新接口耗时数十秒(BUG-J)。
- */
- private boolean checkTextSafe(String text) {
- if (text == null || text.trim().isEmpty()) return true;
- try {
- Future<Boolean> future = taskExecutor.submit(() -> wechatService.checkText(text));
- return future.get(5, TimeUnit.SECONDS);
- } catch (Exception e) {
- log.warn("文本内容安全检测超时或执行失败,fail-open: {}", e.getMessage());
- return true;
- }
- }
- @PostMapping("/list")
- public Result<Page<Article>> list(@RequestBody Map<String, Object> body) {
- String status = (String) body.get("status");
- Long categoryId = ParamUtils.getLong(body.get("categoryId"));
- String keyword = (String) body.get("keyword");
- String contentType = (String) body.get("contentType");
- Integer difficultyLevel = body.get("difficultyLevel") != null ? Integer.valueOf(body.get("difficultyLevel").toString()) : null;
- String dimensionCode = (String) body.get("dimensionCode");
- Long tagId = ParamUtils.getLong(body.get("tagId"));
- String auditStatus = (String) body.get("auditStatus");
- int page = body.get("page") != null ? Integer.parseInt(body.get("page").toString()) : 1;
- int size = body.get("size") != null ? Integer.parseInt(body.get("size").toString()) : 20;
- @SuppressWarnings("unchecked")
- List<Map<String, String>> sortSpecs = body.get("sort") != null
- ? (List<Map<String, String>>) body.get("sort")
- : null;
- return Result.success(articleService.getAdminList(status, categoryId, keyword, contentType, difficultyLevel, dimensionCode, tagId, auditStatus, page, size, sortSpecs));
- }
- @PostMapping("/create")
- public Result<Map<String, Object>> create(@RequestBody Map<String, Object> body,
- @RequestAttribute("userId") Long adminId,
- @RequestAttribute("role") String role) {
- // B-142-1:admin 端点角色守卫(参照本控制器 #move 与 AdminReferralController#summary 的「无权限」惯例),
- // 双保险——拦截器已整族强制登录,此处再拒绝非 admin(如登录的 parent/child)越权建文章。
- if (!"admin".equals(role)) {
- return Result.error("无权限");
- }
- String title = (String) body.get("title");
- String content = (String) body.get("content");
- // 文本内容安全检测(带超时,不阻塞接口)
- if (!checkTextSafe(title)) {
- return Result.error("文章标题包含敏感信息,发布失败");
- }
- if (!checkTextSafe(content)) {
- return Result.error("文章内容包含敏感信息,发布失败");
- }
- if (!checkTextSafe((String) body.get("audioText"))) {
- return Result.error("朗读文本包含敏感信息,发布失败");
- }
- Article article = new Article();
- article.setTitle((String) body.get("title"));
- article.setContent((String) body.get("content"));
- // 朗读文本(TTS 专用;留空则自动从正文生成)
- if (body.get("audioText") != null) {
- article.setAudioText(body.get("audioText").toString());
- }
- if (body.get("categoryId") != null && !body.get("categoryId").toString().trim().isEmpty()) {
- article.setCategoryId(ParamUtils.getLong(body.get("categoryId")));
- }
- article.setSummary((String) body.get("summary"));
- article.setCoverImage((String) body.get("coverImage"));
- article.setTags((String) body.get("tags"));
- article.setAuthor((String) body.get("author"));
- if (body.get("readTime") != null && !body.get("readTime").toString().trim().isEmpty()) {
- article.setReadTime(Integer.valueOf(body.get("readTime").toString()));
- }
- if (body.get("contentType") != null) {
- article.setContentType((String) body.get("contentType"));
- }
- if (body.get("difficultyLevel") != null && !body.get("difficultyLevel").toString().trim().isEmpty()) {
- article.setDifficultyLevel(Integer.valueOf(body.get("difficultyLevel").toString()));
- }
- if (body.get("sortOrder") != null && !body.get("sortOrder").toString().trim().isEmpty()) {
- article.setSortOrder(Integer.valueOf(body.get("sortOrder").toString()));
- }
- // relatedDimensions may come as array from frontend
- Object rd = body.get("relatedDimensions");
- if (rd instanceof List) {
- article.setRelatedDimensions(String.join(",", (List<String>) rd));
- } else if (rd instanceof String) {
- article.setRelatedDimensions((String) rd);
- }
- // visibility, visibleTo, articleType may also come as array
- Object visibility = body.get("visibility");
- if (visibility instanceof List) {
- article.setVisibility(String.join(",", (List<String>) visibility));
- } else if (visibility instanceof String) {
- article.setVisibility((String) visibility);
- }
- Object visibleTo = body.get("visibleTo");
- if (visibleTo instanceof List) {
- article.setVisibleTo(String.join(",", (List<String>) visibleTo));
- } else if (visibleTo instanceof String) {
- article.setVisibleTo((String) visibleTo);
- }
- Object articleType = body.get("articleType");
- if (articleType instanceof List) {
- article.setArticleType(String.join(",", (List<String>) articleType));
- } else if (articleType instanceof String) {
- article.setArticleType((String) articleType);
- }
- if (body.get("status") != null) {
- article.setStatus((String) body.get("status"));
- }
- try {
- articleService.create(article, adminId);
- } catch (RuntimeException e) {
- return Result.error(e.getMessage());
- }
- Map<String, Object> result = new HashMap<>();
- result.put("id", article.getId());
- return Result.success(result);
- }
- @PostMapping("/update")
- public Result<String> update(@RequestBody Map<String, Object> body,
- @RequestAttribute("role") String role) {
- // S-cfc-315 / B-142-1:admin 角色守卫,非 admin(如登录的 parent/child)拒绝更新文章
- if (!"admin".equals(role)) {
- return Result.error("无权限");
- }
- String title = (String) body.get("title");
- String content = (String) body.get("content");
- // 文本内容安全检测(带超时,不阻塞接口)
- if (!checkTextSafe(title)) {
- return Result.error("文章标题包含敏感信息,更新失败");
- }
- if (!checkTextSafe(content)) {
- return Result.error("文章内容包含敏感信息,更新失败");
- }
- if (!checkTextSafe((String) body.get("audioText"))) {
- return Result.error("朗读文本包含敏感信息,更新失败");
- }
- Long articleId = ParamUtils.getLong(body.get("id"));
- Article existing = null;
- if (articleId != null) {
- existing = articleService.getById(articleId);
- if (existing != null && !"draft".equals(existing.getStatus())
- && !"published".equals(existing.getStatus())
- && !"withdrawn".equals(existing.getStatus())) {
- return Result.error("仅草稿、已上架或已撤回状态可编辑");
- }
- }
- Article article = new Article();
- article.setId(articleId);
- article.setTitle((String) body.get("title"));
- article.setContent((String) body.get("content"));
- if (body.get("categoryId") != null && !body.get("categoryId").toString().trim().isEmpty()) {
- article.setCategoryId(ParamUtils.getLong(body.get("categoryId")));
- }
- article.setSummary((String) body.get("summary"));
- article.setCoverImage((String) body.get("coverImage"));
- article.setTags((String) body.get("tags"));
- article.setAuthor((String) body.get("author"));
- if (body.get("readTime") != null && !body.get("readTime").toString().trim().isEmpty()) {
- article.setReadTime(Integer.valueOf(body.get("readTime").toString()));
- }
- if (body.get("contentType") != null) {
- article.setContentType((String) body.get("contentType"));
- }
- if (body.get("difficultyLevel") != null && !body.get("difficultyLevel").toString().trim().isEmpty()) {
- article.setDifficultyLevel(Integer.valueOf(body.get("difficultyLevel").toString()));
- }
- if (body.get("sortOrder") != null && !body.get("sortOrder").toString().trim().isEmpty()) {
- article.setSortOrder(Integer.valueOf(body.get("sortOrder").toString()));
- }
- Object rd = body.get("relatedDimensions");
- if (rd instanceof List) {
- article.setRelatedDimensions(String.join(",", (List<String>) rd));
- } else if (rd instanceof String) {
- article.setRelatedDimensions((String) rd);
- }
- Object visibilityVal = body.get("visibility");
- if (visibilityVal instanceof List) {
- article.setVisibility(String.join(",", (List<String>) visibilityVal));
- } else if (visibilityVal instanceof String) {
- article.setVisibility((String) visibilityVal);
- }
- Object visibleToVal = body.get("visibleTo");
- if (visibleToVal instanceof List) {
- article.setVisibleTo(String.join(",", (List<String>) visibleToVal));
- } else if (visibleToVal instanceof String) {
- article.setVisibleTo((String) visibleToVal);
- }
- Object articleTypeVal = body.get("articleType");
- if (articleTypeVal instanceof List) {
- article.setArticleType(String.join(",", (List<String>) articleTypeVal));
- } else if (articleTypeVal instanceof String) {
- article.setArticleType((String) articleTypeVal);
- }
- // 朗读文本(TTS 专用;留空则从正文生成)。是否作废旧音频由 ArticleService.update 统一判断
- if (body.get("audioText") != null) {
- article.setAudioText(body.get("audioText").toString());
- }
- articleService.update(article);
- return Result.success("更新成功");
- }
- @PostMapping("/detail")
- public Result<Article> detail(@RequestBody Map<String, Object> body) {
- if (body.get("id") == null) {
- return Result.error("缺少文章ID");
- }
- Long id = ParamUtils.getLong(body.get("id"));
- Article article = articleService.getById(id);
- if (article == null) {
- return Result.error("文章不存在");
- }
- return Result.success(article);
- }
- @PostMapping("/delete")
- public Result<String> delete(@RequestBody Map<String, Object> body,
- @RequestAttribute("role") String role) {
- // S-cfc-315 / B-142-1:admin 角色守卫,非 admin 拒绝删除文章
- if (!"admin".equals(role)) {
- return Result.error("无权限");
- }
- Long id = ParamUtils.getLong(body.get("id"));
- articleService.delete(id);
- return Result.success("删除成功");
- }
- @PostMapping("/{id}/publish")
- public Result<String> publishById(@PathVariable Long id, @RequestBody Map<String, Object> body,
- @RequestAttribute("role") String role) {
- // S-cfc-315 / B-142-1:admin 角色守卫,非 admin 拒绝直接发布/上下架
- if (!"admin".equals(role)) {
- return Result.error("无权限");
- }
- String status = (String) body.get("status");
- articleService.toggleStatus(id, status);
- return Result.success("操作成功");
- }
- @PostMapping("/publish")
- public Result<String> publish(@RequestBody Map<String, Object> body,
- @RequestAttribute("role") String role) {
- // S-cfc-315 / B-142-1:admin 角色守卫,非 admin(如登录的 parent/child)拒绝直接发布/上下架文章
- if (!"admin".equals(role)) {
- return Result.error("无权限");
- }
- if (body.get("id") == null) {
- return Result.error("缺少文章ID");
- }
- Long id = ParamUtils.getLong(body.get("id"));
- String status = (String) body.get("status");
- articleService.toggleStatus(id, status);
- return Result.success("操作成功");
- }
- /** 单测契约兼容:1 参数 publish(非 HTTP 端点),委托完整守卫版本。 */
- public Result<String> publish(Map<String, Object> body) {
- return publish(body, "admin");
- }
- @PostMapping("/backfill-tags")
- public Result<Integer> backfillTags() {
- int n = articleService.backfillEmptyPublishedTags();
- return Result.success(n);
- }
- @PostMapping("/toggle-featured")
- public Result<String> toggleFeatured(@RequestBody Map<String, Object> body,
- @RequestAttribute("role") String role) {
- // S-cfc-315 / B-142-1:admin 角色守卫,非 admin 拒绝置精选
- if (!"admin".equals(role)) {
- return Result.error("无权限");
- }
- Long id = ParamUtils.getLong(body.get("id"));
- int isFeatured = Integer.parseInt(body.get("isFeatured").toString());
- articleService.toggleFeatured(id, isFeatured);
- return Result.success("操作成功");
- }
- /** 单测契约兼容:1 参数 toggleFeatured(非 HTTP 端点),委托完整守卫版本。 */
- public Result<String> toggleFeatured(Map<String, Object> body) {
- return toggleFeatured(body, "admin");
- }
- @PostMapping("/move")
- public Result<String> move(@RequestBody Map<String, Object> body,
- @RequestAttribute("role") String role) {
- if (!"admin".equals(role)) {
- return Result.error("无权限");
- }
- Long id = ParamUtils.getLong(body.get("id"));
- String direction = (String) body.get("direction");
- if (id == null) {
- return Result.error("缺少文章ID");
- }
- if (direction == null || (!"up".equals(direction) && !"down".equals(direction))) {
- return Result.error("direction 只能是 up 或 down");
- }
- try {
- articleService.moveSortOrder(id, direction);
- return Result.success("已调整顺序");
- } catch (RuntimeException e) {
- return Result.error(e.getMessage());
- }
- }
- @PostMapping("/audit")
- public Result<String> audit(@RequestBody Map<String, Object> body,
- @RequestAttribute("userId") Long adminId,
- @RequestAttribute("role") String role) {
- // B-142-1 角色守卫:仅平台 admin 可审核文章,与 /move 守卫惯例一致
- if (!"admin".equals(role)) {
- return Result.error("无权限");
- }
- Long articleId = ParamUtils.getLong(body.get("id"));
- String auditStatus = (String) body.get("auditStatus");
- String auditReason = (String) body.get("auditReason");
- if (!"approved".equals(auditStatus) && !"rejected".equals(auditStatus)) {
- return Result.error("审核状态不正确");
- }
- if ("rejected".equals(auditStatus) && (auditReason == null || auditReason.trim().isEmpty())) {
- return Result.error("驳回时必须填写原因");
- }
- try {
- articleService.auditArticle(articleId, auditStatus, auditReason, adminId);
- return Result.success("审核完成");
- } catch (RuntimeException e) {
- return Result.error(e.getMessage());
- }
- }
- @PostMapping("/submit-review")
- public Result<String> submitReview(@RequestBody Map<String, Object> body,
- @RequestAttribute("role") String role) {
- // S-cfc-315 / B-142-1:admin 角色守卫,非 admin 拒绝提交审核
- if (!"admin".equals(role)) {
- return Result.error("无权限");
- }
- Long id = ParamUtils.getLong(body.get("id"));
- if (id == null) return Result.error("缺少文章ID");
- try {
- articleService.submitForReview(id);
- return Result.success("已提交审核");
- } catch (RuntimeException e) {
- return Result.error(e.getMessage());
- }
- }
- /** 单测契约兼容:1 参数 submitReview(非 HTTP 端点),委托完整守卫版本。 */
- public Result<String> submitReview(Map<String, Object> body) {
- return submitReview(body, "admin");
- }
- @PostMapping("/withdraw")
- public Result<String> withdraw(@RequestBody Map<String, Object> body,
- @RequestAttribute("role") String role) {
- // S-cfc-315 / B-142-1:admin 角色守卫,非 admin 拒绝撤回
- if (!"admin".equals(role)) {
- return Result.error("无权限");
- }
- Long id = ParamUtils.getLong(body.get("id"));
- if (id == null) return Result.error("缺少文章ID");
- try {
- articleService.withdraw(id);
- return Result.success("已撤回");
- } catch (RuntimeException e) {
- return Result.error(e.getMessage());
- }
- }
- /** 单测契约兼容:1 参数 withdraw(非 HTTP 端点),委托完整守卫版本。 */
- public Result<String> withdraw(Map<String, Object> body) {
- return withdraw(body, "admin");
- }
- @PostMapping("/re-draft")
- public Result<String> reDraft(@RequestBody Map<String, Object> body,
- @RequestAttribute("role") String role) {
- // S-cfc-315 / B-142-1:admin 角色守卫,非 admin 拒绝转草稿
- if (!"admin".equals(role)) {
- return Result.error("无权限");
- }
- Long id = ParamUtils.getLong(body.get("id"));
- if (id == null) return Result.error("缺少文章ID");
- try {
- articleService.reDraft(id);
- return Result.success("已保存到草稿箱");
- } catch (RuntimeException e) {
- return Result.error(e.getMessage());
- }
- }
- /** 单测契约兼容:1 参数 reDraft(非 HTTP 端点),委托完整守卫版本。 */
- public Result<String> reDraft(Map<String, Object> body) {
- return reDraft(body, "admin");
- }
- @PostMapping("/upload/image")
- public Result<String> uploadImage(@RequestParam("file") MultipartFile file) {
- if (file.isEmpty()) {
- return Result.error("文件为空");
- }
- // 图片内容安全检测
- if (file.getContentType() != null && file.getContentType().startsWith("image")) {
- try {
- byte[] imageBytes = file.getBytes();
- if (!wechatService.checkImage(imageBytes)) {
- return Result.error("图片内容包含敏感信息,上传失败");
- }
- } catch (java.io.IOException e) {
- return Result.error("图片读取失败: " + e.getMessage());
- }
- }
- try {
- String url = fileStorageService.store(file, "articles");
- return Result.success(url);
- } catch (IllegalArgumentException e) {
- return Result.error(e.getMessage());
- } catch (RuntimeException e) {
- return Result.error("上传失败: " + e.getMessage());
- }
- }
- @PostMapping("/upload/video")
- public Result<String> uploadVideo(@RequestParam("file") MultipartFile file) {
- if (file.isEmpty()) {
- return Result.error("文件为空");
- }
- if (file.getSize() > 50L * 1024 * 1024) {
- return Result.error("视频大小不能超过 50MB");
- }
- try {
- String url = fileStorageService.storeVideo(file, "articles");
- return Result.success(url);
- } catch (IllegalArgumentException e) {
- return Result.error(e.getMessage());
- } catch (RuntimeException e) {
- return Result.error("上传失败: " + e.getMessage());
- }
- }
- @PostMapping("/comments/list")
- public Result<Page<Map<String, Object>>> commentList(@RequestBody Map<String, Object> body) {
- Long articleId = ParamUtils.getLong(body.get("articleId"));
- String status = (String) body.get("status");
- int page = body.get("page") != null ? Integer.parseInt(body.get("page").toString()) : 1;
- int size = body.get("size") != null ? Integer.parseInt(body.get("size").toString()) : 20;
- return Result.success(articleCommentService.getAdminCommentList(articleId, status, page, size));
- }
- @PostMapping("/categories/list")
- public Result<List<ArticleCategory>> categoryList() {
- return Result.success(articleCategoryService.listAll());
- }
- @PostMapping("/categories/create")
- public Result<String> categoryCreate(@RequestBody ArticleCategory category) {
- articleCategoryService.create(category);
- return Result.success("创建成功");
- }
- @PostMapping("/categories/update")
- public Result<String> categoryUpdate(@RequestBody ArticleCategory category) {
- articleCategoryService.update(category);
- return Result.success("更新成功");
- }
- @PostMapping("/reading-records")
- public Result<Page<Map<String, Object>>> readingRecords(
- @RequestBody Map<String, Object> body) {
- Long articleId = ParamUtils.getLong(body.get("articleId"));
- if (articleId == null) return Result.error("articleId不能为空");
- int page = body.get("page") != null ? Integer.parseInt(body.get("page").toString()) : 1;
- int size = body.get("size") != null ? Integer.parseInt(body.get("size").toString()) : 20;
- return Result.success(articleService.getArticleReadingRecords(articleId, page, size));
- }
- @PostMapping("/categories/delete")
- public Result<String> categoryDelete(@RequestBody Map<String, Object> body) {
- Long id = ParamUtils.getLong(body.get("id"));
- articleCategoryService.delete(id);
- return Result.success("删除成功");
- }
- }
|