From efdb99f8cecc4afb592afad79c761081d5d5cf22 Mon Sep 17 00:00:00 2001 From: lee <4766465@qq.com> Date: Wed, 18 Dec 2024 13:27:00 +0800 Subject: [PATCH] init --- yami-shop-api/src/main/java/com/yami/shop/api/controller/NoticeController.java | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 166 insertions(+), 0 deletions(-) diff --git a/yami-shop-api/src/main/java/com/yami/shop/api/controller/NoticeController.java b/yami-shop-api/src/main/java/com/yami/shop/api/controller/NoticeController.java new file mode 100644 index 0000000..df0c5fe --- /dev/null +++ b/yami-shop-api/src/main/java/com/yami/shop/api/controller/NoticeController.java @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. + * + * https://www.mall4j.com/ + * + * 未经允许,不可做商业用途! + * + * 版权所有,侵权必究! + */ +package com.yami.shop.api.controller; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.yami.shop.bean.app.dto.NoticeDto; +import com.yami.shop.bean.enums.NoticeType; +import com.yami.shop.bean.model.Notice; +import com.yami.shop.bean.model.NoticeUserLog; +import com.yami.shop.common.enums.StatusEnum; +import com.yami.shop.common.response.ServerResponseEntity; +import com.yami.shop.common.util.PageParam; +import com.yami.shop.security.api.model.YamiUser; +import com.yami.shop.security.api.util.SecurityUtils; +import com.yami.shop.security.common.bo.UserInfoInTokenBO; +import com.yami.shop.security.common.util.AuthUserContext; +import com.yami.shop.service.NoticeService; +import com.yami.shop.service.NoticeUserLogService; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Operation; +import lombok.AllArgsConstructor; +import ma.glasnost.orika.MapperFacade; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * @author LGH + */ +@RestController +@RequestMapping("/notice") +@Tag(name = "用户未登录状态的公告管理接口") +@AllArgsConstructor +public class NoticeController { + + private final NoticeService noticeService; + + private final MapperFacade mapperFacade; + + private final NoticeUserLogService noticeUserLogService; + + @GetMapping("/topNoticeList/{shopId}") + @Operation(summary = "置顶公告列表信息" , description = "获取所有置顶公告列表信息") + @Parameter(name = "shopId", description = "店铺id" , required = true) + public ServerResponseEntity<List<NoticeDto>> getTopNoticeListByShopId(@PathVariable("shopId") Long shopId) { + List<Notice> noticeList = noticeService.listTopNoticeByShopId(shopId); + //筛选出立即发送和到时间定时发送的公告 + List<Notice> list = noticeList.stream().filter(noticeVO -> Objects.equals(noticeVO.getImmediatelySend(), 1) || (Objects.equals(noticeVO.getImmediatelySend(), 0) && noticeVO.getSendTime().before(new Date()))).collect(Collectors.toList()); + List<NoticeDto> noticeDtoList = mapperFacade.mapAsList(list, NoticeDto.class); + UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get(); + List<NoticeDto> result = new ArrayList<>(); + String accountId = ""; + //登录状态显示 指定全部用户和指定用户可见的公告 + if(Objects.nonNull(userInfoInTokenBO)){ + accountId = userInfoInTokenBO.getUserId(); + String userId = String.valueOf(accountId); + result = noticeDtoList.stream().filter(notice ->Objects.isNull(notice.getUserIds()) || (Objects.nonNull(notice.getUserIds()) && notice.getUserIds().contains(userId) )).collect(Collectors.toList()); + }else{ + //未登录状态只显示 全部用户可见的公告 + result = noticeDtoList.stream().filter(notice -> Objects.isNull(notice.getUserIds())).collect(Collectors.toList()); + } + return ServerResponseEntity.success(result); + } + + @GetMapping("/info/{id}") + @Operation(summary = "公告详情" , description = "获取公告id公告详情") + @Parameter(name = "id", description = "公告id" , required = true) + public ServerResponseEntity<NoticeDto> getNoticeById(@PathVariable("id") Long id) { + Notice notice = noticeService.getNoticeById(id); + NoticeDto noticeDto = mapperFacade.map(notice, NoticeDto.class); + return ServerResponseEntity.success(noticeDto); + } + + /** + * 公告列表 + */ + @GetMapping("/noticeList/{shopId}") + @Operation(summary = "公告列表信息" , description = "获取所有公告列表信息") + @Parameter(name = "shopId", description = "店铺id" , required = true) + public ServerResponseEntity<IPage<NoticeDto>> pageNotice(@PathVariable("shopId") Long shopId, PageParam<NoticeDto> page) { + Notice notice = new Notice(); + notice.setShopId(shopId); + notice.setType(NoticeType.TO_USER.value()); + UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get(); + notice.setAccountId(Objects.nonNull(userInfoInTokenBO) ? userInfoInTokenBO.getUserId() : null); + notice.setSendTime(new Date()); + notice.setStatus(StatusEnum.ENABLE.value()); + return ServerResponseEntity.success(noticeService.pageNotice(page, notice)); + } + + @GetMapping("/upDateNotice") + @Operation(summary = "查询最新的公告", description = "查询最新的公告") + public ServerResponseEntity<NoticeDto> upDateNotice() { + NoticeDto newNotice = noticeService.getNewNotice(); + String content = newNotice.getContent(); + String newCount = removeRichTextTags(content); + newNotice.setNewContent(newNotice.getTitle()+" "+newCount); + return ServerResponseEntity.success(newNotice); + } + + + public String removeRichTextTags(String htmlStr) { + String script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; + String style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; + String html = "<[^>]+>"; + String space = "(\r?\n(\\s*\r?\n)+)"; + String white = " "; + Pattern pScript = Pattern.compile(script, 2); + Matcher mScript = pScript.matcher(htmlStr); + htmlStr = mScript.replaceAll(""); + Pattern pStyle = Pattern.compile(style, 2); + Matcher mStyle = pStyle.matcher(htmlStr); + htmlStr = mStyle.replaceAll(""); + Pattern pHtml = Pattern.compile(html, 2); + Matcher mHtml = pHtml.matcher(htmlStr); + htmlStr = mHtml.replaceAll(""); + Pattern pSpace = Pattern.compile(space, 2); + Matcher mSpace = pSpace.matcher(htmlStr); + htmlStr = mSpace.replaceAll(""); + htmlStr = htmlStr.replaceAll(white, ""); + htmlStr = htmlStr.replaceAll("<[^>]*>", ""); + htmlStr = htmlStr.replaceAll("</br>", ""); + + String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式 + String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式 + String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式 + String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符 + + Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); + Matcher m_script = p_script.matcher(htmlStr); + htmlStr = m_script.replaceAll(""); // 过滤script标签 + + Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); + Matcher m_style = p_style.matcher(htmlStr); + htmlStr = m_style.replaceAll(""); // 过滤style标签 + + Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); + Matcher m_html = p_html.matcher(htmlStr); + htmlStr = m_html.replaceAll(""); // 过滤html标签 + + Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE); + Matcher m_space = p_space.matcher(htmlStr); + htmlStr = m_space.replaceAll(""); // 过滤空格回车标签 + htmlStr = htmlStr.replaceAll(" ", "");//去空格 + + return htmlStr.trim(); + } +} -- Gitblit v1.9.3