diff --git a/src/main/java/com/budwk/app/sys/controller/SysHomeController.java b/src/main/java/com/budwk/app/sys/controller/SysHomeController.java index 6211427..986ff01 100644 --- a/src/main/java/com/budwk/app/sys/controller/SysHomeController.java +++ b/src/main/java/com/budwk/app/sys/controller/SysHomeController.java @@ -263,35 +263,65 @@ public class SysHomeController { // @CacheDefaults(cacheName = RedisConstant.PLATFORM_REDIS_WKCACHE_PREFIX + "web_news", isHash = true) // @CacheResult(cacheKey = "news", ignoreNull = true, cacheLiveTime = 60 * 60 * 24) public Result getNews() { - // 定义要爬取的URL String domain = "https://gh.hgu.edu.cn"; + JSONObject root = new JSONObject(); + root.set("grassroots", new ArrayList<>()); + root.set("notices", new ArrayList<>()); try { - // 使用Jsoup连接到URL并获取页面内容 - Document doc = Jsoup.connect(domain).get(); + // 工会官网首页栏目结构由外部站点维护,抓取时设置超时,避免首页接口长时间阻塞。 + Document doc = Jsoup.connect(domain).timeout(5000).get(); - JSONObject root = new JSONObject(); - List newsArray = new ArrayList<>(); - Elements newsItems = doc.select("table[cellspacing=3] tr"); - for (Element item : newsItems) { - Element aElement = item.select("a").first(); - if(aElement == null) { - continue; - } - String date = item.select(".timestyle125262").text().trim(); - String title = aElement.attr("title").trim(); - String url = aElement.attr("href").trim(); - - JSONObject node = new JSONObject(); - node.set("title", title); - node.set("date", date); - node.set("url", "https://gh.hgu.edu.cn/" + url); - newsArray.add(node); - } - root.set("grassroots", newsArray); + root.set("grassroots", parseWebsiteNews(doc.select(".sec1 .s1-right li a"), domain, false)); + root.set("notices", parseWebsiteNews(doc.select(".sec2 .dt-list2 li a"), domain, true)); return Result.success(root); } catch (Exception e) { log.error(e); - return Result.success(); + return Result.success(root); } } + + /** + * 解析工会官网首页新闻栏目。 + * + * @param elements 官网栏目链接节点,工会动态为.sec1 .s1-right li a,通知公告为.sec2 .dt-list2 li a + * @param domain 官网域名,用于把info/xxx.htm、/__local/xxx等相对地址补全为可直接打开的绝对地址 + * @param notice 是否为通知公告;通知公告日期由“年月”和“日”拆分展示,需要单独拼接 + * @return List,字段包括title标题、date发布日期、url详情地址,前端直接按数组轮播展示 + */ + private List parseWebsiteNews(Elements elements, String domain, boolean notice) { + List newsArray = new ArrayList<>(); + for (Element item : elements) { + String title = StrUtil.blankToDefault(item.attr("title").trim(), item.select("p,h3").first() == null ? "" : item.select("p,h3").first().text().trim()); + String url = item.attr("href").trim(); + if (StrUtil.isBlank(title) || StrUtil.isBlank(url)) { + continue; + } + + String date; + if (notice) { + String day = item.select(".date p").text().trim(); + String yearMonth = item.select(".date span").text().trim(); + date = StrUtil.isBlank(yearMonth) ? day : yearMonth + "-" + day; + } else { + date = item.select("span").text().trim(); + } + + JSONObject node = new JSONObject(); + node.set("title", title); + node.set("date", date); + node.set("url", buildWebsiteUrl(domain, url)); + newsArray.add(node); + } + return newsArray; + } + + private String buildWebsiteUrl(String domain, String url) { + if (StrUtil.startWithIgnoreCase(url, "http")) { + return url; + } + if (url.startsWith("/")) { + return domain + url; + } + return domain + "/" + url; + } } diff --git a/src/main/resources/views/layouts/v4/home/index.html b/src/main/resources/views/layouts/v4/home/index.html index 5575f97..860f255 100644 --- a/src/main/resources/views/layouts/v4/home/index.html +++ b/src/main/resources/views/layouts/v4/home/index.html @@ -86,7 +86,7 @@ layout("/layouts/v4/baseLayout.html"){ - + @@ -105,6 +105,7 @@ layout("/layouts/v4/baseLayout.html"){ return{ websiteNews:{ grassroots:[], + notices:[], } } }, @@ -122,7 +123,11 @@ layout("/layouts/v4/baseLayout.html"){ getWebSiteNews(){ this.$axios.post('/platform/home/getNews').then(res => { if (res.code === 0) { - this.websiteNews = res.data + const data = res.data || {} + this.websiteNews = { + grassroots: data.grassroots || [], + notices: data.notices || [], + } } }) } diff --git a/src/main/resources/views/layouts/v4/home/jcdt.js b/src/main/resources/views/layouts/v4/home/jcdt.js index 2a740ad..eb7da72 100644 --- a/src/main/resources/views/layouts/v4/home/jcdt.js +++ b/src/main/resources/views/layouts/v4/home/jcdt.js @@ -2,28 +2,27 @@ const jcdt = { template: /*language=HTML*/ `
-
- 工会动态 -
-
-
-
-
{{item.date}}
-
-
- -
-
{{item.title}}
-
- - - + + +
+ {{section.title}}
-
-
+
+
+
+
+ + {{item.date}} +
+
+
{{item.title}}
+
+
+
+
暂无{{section.title}}
+
+ +
`, @@ -31,9 +30,29 @@ const jcdt = { return {} }, props: { - list: { + grassroots: { type: Array, - default: [] + default: () => [] + }, + notices: { + type: Array, + default: () => [] + } + }, + computed: { + sections() { + return [ + { + key: "grassroots", + title: "工会要闻", + list: (this.grassroots || []).slice(0, 8) + }, + { + key: "notices", + title: "通知公告", + list: (this.notices || []).slice(0, 8) + } + ] } }, methods: { @@ -54,94 +73,112 @@ const jcdt = { width: 80%; max-width: 80%; margin: 0 auto; - background-color: #FFFFFF; border-radius: 12px; - padding: 20px; + padding: 42px 20px 40px; margin-bottom: 10px; - box-shadow: 0 6px 20px rgba(0, 0, 0, 0.06); + box-shadow: 0 10px 28px rgba(0, 0, 0, 0.08); } - .jcdt-title{ + .jcdt-carousel { + width: 100%; + } + + /deep/ .jcdt-carousel .el-carousel__container { + height: 340px; + } + + /deep/ .jcdt-carousel .el-carousel__item { + overflow: visible; + } + + /deep/ .jcdt-title{ font-size: 30px; color: #000000; display: block; font-weight: bold; position: relative; text-align: center; - padding: 20px 0; + padding: 0 0 24px; } /deep/ .jcdt-container .jcdt-title span::before { - content: ''; - width: 24px; - height: 11px; - background: url(https://www.ncu.edu.cn/images/titl.svg) no-repeat center; - background-size: 24px 11px; + content: '◇'; display: inline-block; - margin-right: 0; + margin-right: 8px; vertical-align: middle; + color: #e60012; + font-size: 16px; + font-weight: 400; } /deep/ .jcdt-container .jcdt-title span::after { - content: ''; - width: 24px; - height: 11px; - background: url(https://www.ncu.edu.cn/images/titr.svg) no-repeat center; - background-size: 24px 11px; + content: '◇'; display: inline-block; - margin-left: 0; + margin-left: 8px; vertical-align: middle; + color: #e60012; + font-size: 16px; + font-weight: 400; } - - .jcdt-list { + + /deep/ .jcdt-list { display: grid; - grid-template-columns: repeat(4, 1fr); + grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 20px; } - .jcdt-item { + /deep/ .jcdt-item { display: flex; flex-direction: column; padding: 16px; border: none; - border-bottom: 3px solid transparent; + border-bottom: 3px solid #c11623; transition: all 0.3s ease; cursor: pointer; - height: auto; - border-radius: 12px; - } - - .jcdt-item:hover{ + min-height: 84px; background: #ffffff; - border-bottom-color: var(--color-primary); - transform: translateY(-4px); - box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15); + box-shadow: 0 8px 18px rgba(0, 0, 0, 0.08); } - .jcdt-item .date { + /deep/ .jcdt-item-main { + display: flex; + align-items: center; + } + + /deep/ .jcdt-item:hover{ + background: #ffffff; + transform: translateY(-4px); + box-shadow: 0 10px 24px rgba(0, 0, 0, 0.14); + } + + /deep/ .jcdt-item .date { font-size: 20px; - /*font-weight: bold;*/ + font-weight: bold; white-space: nowrap; text-align: left; margin-bottom: 8px; transition: opacity 0.3s ease; } - .jcdt-item .title { + /deep/ .jcdt-item .date i { + color: #c11623; + font-size: 18px; + margin-right: 4px; + } + + /deep/ .jcdt-item .title { font-size: 16px; font-weight: 600; - /*margin-bottom: 12px;*/ line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; - /*height: 45px;*/ } - .jcdt-item .image { + /deep/ .jcdt-item .image { width: 100%; height: 180px; margin: 0 0 16px 0; @@ -155,14 +192,14 @@ const jcdt = { transition: transform 0.3s ease; } - .jcdt-item .image img { + /deep/ .jcdt-item .image img { width: 100%; height: 100%; object-fit: cover; transition: transform 0.3s ease; } - .jcdt-item .summary { + /deep/ .jcdt-item .summary { font-size: 14px; color: #666; line-height: 1.6; @@ -174,5 +211,14 @@ const jcdt = { margin-bottom: 12px; } + /deep/ .jcdt-empty { + height: 160px; + display: flex; + align-items: center; + justify-content: center; + color: #909399; + font-size: 16px; + } + ` };